Local Time and GMT Time in Perl
Date and Time in Perl – Part 2
Object Approach
Forward: In this part of the series I explain how to obtain local and GMT Times from a computer that has Perl installed.
By: Chrysanthus Date Published: 1 Mar 2013
Introduction
The brand of Perl I am using is ActivePerl. An ActivePerl program does not begin with “#!/usr/bin/perl” as traditional Perl programs begin. If you are using traditional Perl, then begin each of the programs in this series with something like, “#!/usr/bin/perl” without the quotes.
Also, the output of each program goes to a web page as feedback to the browser. So the first and last parts of each program of the series, enables the output to go to a page at the browser.
Note, date and/or time in Perl is simply, Time.
Prerequisite
This series is part of my volume called, Perl Course. Click the Perl Course link below to know where this series fits into the volume. There, you will know what you should have studied before reaching here.
The Time
Perl considers date and/or time simply as time. It has one structure in which it would place the second, minute, hour, day number of the month, month number, year, week day number, day number of year, and daylight saving indicator, of any particular moment in time. It is up to the programmer to separate conventional date from conventional time or use both as one unit. I explain all that below. The structure is not accesses directly; it is accessed indirectly – see below.
To use the localtime class of the Time module, you would begin the code segment of interest with the statement:
use Time::localtime;
Note the position and use of the double colon operator (::); it comes in between the module name (Time) and the class name (localtime). There is a constructor method called, localtime(). This function instantiates and returns an object of the localtime class. The object has all the component values (second, month number, etc.).
Note: there should be no pace between the double colon and the left or right operand (Time or localtime).
Time Components
Time components are second, minute, hour, day number of the month, month number, year, week day number, day number of year, and daylight saving indicator. The value of each of these is a number. These components are properties of the localtime class. After the object has be instantiated, each of the following methods of the localtime class, returns a number, which is the corresponding time component value:
sec()
Returns the number of seconds in the time, a number from 0 to 59.
min()
Returns the number of minutes in the time, a number from 0 to 59.
hour()
Returns the number of hours in the time, a number from 0 to 23.
mday()
Returns the day number of the month from 1 to 31.
mon()
Returns the month number of the year from 0 to 11, with 0 being January and 11 being December. You have to add 1 to this number “by hand”, to obtain the conventional month number.
year()
Returns a number, which is the difference between today’s year and the year, 1900. You have to add this number to 1900 “by hand”, to obtain today’s year in 4-digits.
wday()
The day number within the week from 0 to 6, where 0 is Sunday and 6 is Saturday. This component is not frequently used, but is available.
yday()
The day number of the year from 0 to 364 or 365 depending on whether you are dealing with a leap year or not; January first is day 0.
isdst()
Returns 1 if the time read is during daylight saving time or 0 if the time read is not during daylight saving time. All the time components are read from the computer time. You set the daylight saving scheme in the computer, using the operating system.
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();
my $sec = $locTim->sec();
my $min = $locTim->min();
my $hour = $locTim->hour();
my $mday = $locTim->mday();
my $mon = $locTim->mon();
my $year = $locTim->year();
my $wday = $locTim->wday();
my $yday = $locTim->yday();
my $isdst = $locTim->isdst();
print $sec . "<br>";
print $min . "<br>";
print $hour . "<br>";
print $mday . "<br>";
print $mon . "<br>";
print $year . "<br>";
print $wday . "<br>";
print $yday . "<br>";
print $isdst . "<br>";
print "</body>";
print "</html>";
Note that the number for year is the difference between today’s year and 1900. The dot in the program is the string concatenation operator.
There are many occasions when you need just the date (conventional date). In this case the useful methods of the localtime class are mday(), mon(), and year(). The rest of the methods are not needed.
Normal Time
There are many occasions when you need just the normal time (conventional time). In that case the useful methods are, sec(), min(), and hour(). The rest of the methods are not needed.
Date Formats with Figures Only
In some countries the date is written in the format, dd-mm-yyyy, where dd or mm is one or 2 digits, e.g. 20-5-2013. In other countries, it is written as yyyy-mm-dd e.g. 2013-5-22. In the U.S.A it is written as mm-dd-yyyy. Note: here mm means month.
Time Format with Figures Only
In most counties time is accepted as follows, hh:mm:ss, where hh is 24-hour clock, mm is minutes and ss is seconds. Each of these three components can be single or double digits. Do not confuse between mm for minutes in time and mm for months in Date. In the next part of the series, I will explain how to make mm (month), mm (minutes), dd, hh and ss all double digits (but as string).
Formatting Dates with Figures Only
The following program displays local date in USA format:
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();
my $mday = $locTim->mday();
my $mon = $locTim->mon();
my $year = $locTim->year();
my $localMonth = $mon + 1;
my $localYear = $year + 1900;
my $date = $localMonth . "-" . $mday . "-" . $localYear;
print $date;
print "</body>";
print "</html>";
Note that to obtain the exact year, 1900 has been added to the variable, $year. Here, the exact year is the local year. Also, 1 has been added to the variable $mon to have the exact (local) month. I explain how to double the single digits of mm and dd in the next part of the series.
So, to format a date with only figures, use the localtime class. Instantiate an object from the class. The values of the properties of the class are numbers, not strings. Create variables e.g. $mday of the values. In the variables, these values are still numbers. Add 1 to the month value to have a number in the range, 1 to 12 and not 0 to 11. Add 1900 to the year value to have the actual year. Use the string concatenation dot operator to form a string of the date in the country format you want. In the concatenation expression, the numbers automatically become characters (characters form a string), because of the string concatenation dot operators. Read the above code again.
In the concatenating string, the hyphens (-) are used so that the date should become something like, 20-5-2013.
So, after obtaining the component values from the instantiated object, you form a string by joining the component values with the concatenating string dot operator.
The following program displays local time in hh:mm:ss format:
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();
my $sec = $locTim->sec();
my $min = $locTim->min();
my $hour = $locTim->hour();
my $localTime = $hour . ":" . $min . ":" . $sec;
print $localTime;
print "</body>";
print "</html>";
So, to format a time with only figures, use the localtime class. Instantiate an object from the class. The values of the properties of the class are numbers, not strings. Create variables e.g. $sec of the values. In the variables these values are still numbers. Use the string concatenation dot operator to form a string of the time. In the concatenation expression, the numbers automatically become characters (characters form a string), because of the string concatenation dot operators. The character, “:” is used to separate the conventional time components. Read the above code again.
The GMT Time
Just as you have the localtime class in the Time module, you have the gmtime class in the Time module. To have a GMT time you instantiate an object from the gmtime class. It has the same properties as the localtime class. You use the gmtime object in the same way that you use the localtime object. The gmtime() constructor function reads the localtime from the computer and then automatically adds or subtracts the number of corresponding time zones. The result is the GMT time. The following program produces the GMT time object and their property values from the computer that has the Perl interpreter:
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::gmtime;
my $gm = gmtime();
my $sec = $gm->sec();
my $min = $gm->min();
my $hour = $gm->hour();
my $mday = $gm->mday();
my $mon = $gm->mon();
my $year = $gm->year();
my $wday = $gm->wday();
my $yday = $gm->yday();
my $isdst = $gm->isdst();
print $sec . "<br>";
print $min . "<br>";
print $hour . "<br>";
print $mday . "<br>";
print $mon . "<br>";
print $year . "<br>";
print $wday . "<br>";
print $yday . "<br>";
print $isdst . "<br>";
print "</body>";
print "</html>";
You use the gmtime object in the same way that you use the localtime object.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Perl ReferenceObject Oriented Programming in Perl
Date and Time in Perl
Regular Expressions in Perl
Perl Course
Web Development Course
Major in Website Design
BACK NEXT