# Perl Time Seconds and DateTime Difference

## Date and Time in Perl � Part 4

### Perl Course

Foreword: In this part of the series I explain how to convert number of seconds to date and vice-versa; I also explain how to determine the difference in Time.

By: Chrysanthus Date Published: 5 Nov 2015

### Introduction

This is part 4 of my series, Date and Time in Perl. In this part of the series I explain how to convert number of seconds to date and vice-versa; I also explain how to determine the difference in Time. In this tutorial, date, time and datetime are synonymous; an instant of time, e.g. 8-15-2013 21:65:45. The instant of time is identified by the year, month, day in month, hour, minute and second. In this tutorial, date will be written in the order: seconds, minutes, hour, day in month, month and year. The abbreviation and order for these six date components are: sec, min, hours, mday, mon, year. In this tutorial, they will be identified by variables in the same order as: \$sec, \$min, \$hours, \$mday, \$mon, \$year.

You should have read the previous parts of the series before reaching here, as this is a continuation.
Note: The output of the code samples in this tutorial are sent to the browser (HTML is involved).

Local and GMT Time Methods
To obtain the local and GMT times from the computer, you use the constructor methods, localtime() and gmtime() respectively. If each of these methods has no argument, it gets the time from the computer. Each of the methods can have an argument of seconds. In this case, the methods convert the number of seconds to a date beginning from 1970 for my Windows operating system; your own operating system might have a different reference (epoch) date.

The localtime() method gives the date as the number of seconds would have been counted by �somebody� stationed in your country since 1970. The gmtime() method gives the date as the number of seconds would have been counted by �somebody� stationed in Britain since 1970.

Converting Seconds to Date
The following program converts the number of seconds to a date starting from 1970:

use strict;

use Time::localtime;
my \$locTim = localtime(1348223530);

my \$sec = \$locTim->sec();
my \$min = \$locTim->min();
my \$hour = \$locTim->hour();
my \$mday = \$locTim->mday();
my \$mon = \$locTim->mon();
my \$year = \$locTim->year();

print \$sec . "\n";
print \$min . "\n";
print \$hour . "\n";
print \$mday . "\n";
print \$mon . "\n";
print \$year . "\n";
The different date components are gotten from the return object of the localtime() method. The number of seconds is 1348223530 and it is the argument of the localtime() method. The year value returned is the number of years from 1970 and not the four-digit year. You can format the output into a more presentable date. I leave that as an exercise for you.  The gmtime() method is used in a similar way.

So, to convert a number of seconds to a date, use the localtime() or gmtime() method. The argument of each of the methods is the number of seconds counting from the year 1970. The returned date is the equivalent date counting still from the year 1970. gmtime() is used in the same way as localtime().

Note: the year returned is the difference between the year of interest and 1900, however the number of seconds inputted is from 1970, a nuance there. However, do understand that we are dealing with time from 1970, in this situation.

Ranges of Date Components
The date (or time) component returned is of a certain range.

Year: the year returned is the difference from 1970 to the actual year.
Month: from 0 to 11 instead of from 1 to 12.
Day of Month: 1 to 31, as expected.
Seconds: 0 to 59 as determined by the modulus nature of the clock.
Minutes: 0 to 59 as determined by the modulus nature of the clock.
Hour: 0 to 23 as determined by the modulus nature of the 24-hour clock.

These unexpected ranges are not a problem; they are simply a matter of representation: In the case of year, just add 1970 to the returned difference. In the case of month, just add 1 to the returned value. In the case of seconds and minutes, know that that the 60th position on the clock is considered as position zero, for the repeating clock cycle. In the case of hour, a similar reasoning applies, that is, the 24th position of the 24-hour clock is considered as position zero for the repeating clock cycle. The reason for these is because the computer counts internally from zero and not from 1.
Converting Date to Number of Seconds
The same date that has been returned above, can be converted back to seconds. However, this time, you use a different class called, Local and its own methods called, timelocal() and timegm().

Do not confuse between the class, localtime and the class, Local. Also remember that Perl coding is case sensitive. Do not confuse between the method, localtime() and the method here, timelocal(). localtime() is the constructor method for the localtime class while timelocal() is a method to convert date to number of seconds for the Local class. Do not confuse between the gmtime() method and the timegm() method here. gmtime() is the constructor method for the gmtime class, while timegm() is another method to convert date to number of seconds.

The localtime, gmtime and Local classes are all of the Time module. localtime() is the constructor method of the localtime class and gmtime() is the constructor method of the gmtime class. timelocal() and timegm() are methods of the Local class.

The timelocal() and timegm() Methods
timelocal() and timegm() are methods of the Local class. timelocal() converts a date counting from 1970 to number of seconds counting still from 1970 as would be counted by someone stationed in your country. timegm() converts a date counting from 1970 to number of seconds counting still from 1970 as would be counted by someone stationed in Britain. I use timelocal() for illustration in this tutorial.

The syntax for timelocal() is:

\$DateReturned = timelocal(\$sec,\$min,\$hours,\$mday,\$mon,\$year);

The syntax for timegm() is similar, but change timelocal to timegm. The arguments shown in the syntax are variables, but you can have numbers in place of the variables. The ranges of the argument numbers are the same as those mentioned above, except for the year, which is the four-digit number. The following program converts a date counting from 1970 to number of seconds counting from 1970:

use strict;

use Time::Local;
my \$noSecs = timelocal(10, 32, 3, 21, 8, 2012);

print \$noSecs;

Even though the year is the four-figure year, even though the date may be the current year, the number of seconds returned is from 1970.

This program is exactly the opposite of the previous one, returning 1348223530 inputted above, as the number of seconds.

Note: The methods in this tutorial are actually dealing with an interval of time from the year, 1970 to the time instant (date) typed in the program.
Time Difference

Below, I explain how to subtract a Perl time from another Perl time. In Perl, date and/or time is simply referred to as time. It typically consists of seconds, minutes, hour, day of month, month, and year. I gave the ranges and specifications of these time components before. Before, I referred to the complete time as date, to make the analysis clearer. In this section, I refer to the complete time like the Perl lover, simply as, time.

I use the USA time format in this section.

Strategy
To subtract one date from another, covert both dates to number of seconds. Subtract one of the converted number of seconds from the other. Then convert the resulting number of seconds to Time. I use the localtime() and timelocal() methods. You can use the gmtime() and timegm() methods in the same way. The localtime() method is from the localtime class; the timelocal() method is from the Local class; the classes and methods, are all from the Time module.

To use the localtime() method, you have to precede the corresponding code segment with �use Time:: localtime;�. To use the timelocal()  method, you have to precede the corresponding code segment with �use Time:: Local;�.

The approach in this tutorial works with years after 1970. 1970 is the epoch year for windows. If the epoch year for your operating system is different, then you need to adjust the approach in this tutorial, accordingly.

Read and try the following program, which subtracts the USA time, 8-21-1995 18:36:44 from the USA time, 9-19-2013 22:41:33, where 1 has already been subtracted from the month-day-numbers, before inputting.

use strict;

use Time::Local;
my \$noSecs1 = timelocal(44, 36, 18, 21, 8, 1995);
my \$noSecs2 = timelocal(33, 41, 22, 19, 9, 2013);
my \$secsDiff = \$noSecs2 - \$noSecs1;

use Time::localtime;
my \$locTim = localtime(\$secsDiff);

my \$sec = \$locTim->sec();
my \$min = \$locTim->min();
my \$hour = \$locTim->hour();
my \$mday = \$locTim->mday();
my \$mon = \$locTim->mon();
my \$year = \$locTim->year();
\$year = \$year - 70;

print \$mon . "-" . \$mday . "-" . \$year . " " . \$hour . ":" . \$min . ":" . \$sec;

The timelocal() function converts the time to seconds and the localtime() function converts the difference is seconds to difference in time. Note that 70 is subtracted from year because the program determines time from 1970.

The result is:

0-29-18 20:4:49

It is in the form: mm-dd-yyyy hh:mm:ss. The result needs interpretation. It means, between the two times you have 18 years plus 0 (+1) months plus 29 days plus 20 hours plus 4 minutes plus 49 seconds. That is how you should interpret the difference in times (dates) from this program

The principles of this tutorial applies to time after the epoch year, which is 1970 for my Windows operating system.

End of Tutorial, End of Series
This is the end of the tutorial. This is the end of the series. I hope you appreciate the series.

Thanks,
Chrys

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Miscellaneous Features in Perl
Perl Two-Dimensional Structures