Broad Network


Number of Seconds in Perl Time

Date and Time in Perl - Part 5

Object Approach

Forward: In this part of the series I explain how to convert number of seconds to date and vice-versa.

By: Chrysanthus Date Published: 2 Mar 2013

Introduction

This is part 5 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. In this tutorial, date means 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.

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;

print "Content-Type: text/html\n\n";
print "<!DOCTYPE HTML>";
print "<html>";
print "<head><title>Perl program Code</title></head>";
print "<body>";


    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 . "<br>";
    print $min . "<br>";
    print $hour . "<br>";
    print $mday . "<br>";
    print $mon . "<br>";
    print $year . "<br>";


print "</body>";
print "</html>";

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;

print "Content-Type: text/html\n\n";
print "<!DOCTYPE HTML>";
print "<html>";
print "<head><title>Perl program Code</title></head>";
print "<body>";


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

    print $noSecs;


print "</body>";
print "</html>";

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.

That is it for this part of the series; we stop here and continue in the next part.

Chrys

Related Links

Perl Reference
Object Oriented Programming in Perl
Date and Time in Perl
Regular Expressions in Perl
Perl Course
Web Development Course
Major in Website Design

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message