Broad Network


Perl Local and GMT Time

Date and Time in Perl – Part 2

Perl Course

Foreword: 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: 5 Nov 2015

Introduction

This is part 2 of my series, Date and Time in Perl. In this part of the series I explain how to obtain local and GMT Times from a computer that has Perl installed. Each computer has a clock. The time and date read from the computer is local time. It can always be converted to GMT time automatically by the computer as indicated in the previous part of the series. You need to have read the previous part of the series before coming here, as this is a continuation.

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.

Note: date and/or time in Perl is simply, Time.

Pre-Knowledge
This series is part of the volume called, Perl Course. At the bottom of this page you will find links to the different series, you should have read before coming 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 accessed directly; it is accessed indirectly – see below.

Perl has a module (a kind of library) called, Time. This module has a class called, localtime. This class has properties (data members) and methods (data functions). You access the time values (second, month number, etc.) using the properties of the class.

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.).

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.

Read and try the following program that displays the Perl local time property values:

use strict;

    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 . "\n";
    print $min . "\n";
    print $hour . "\n";
    print $mday . "\n";
    print $mon . "\n";
    print $year . "\n";
    print $wday . "\n";
    print $yday . "\n";
    print $isdst . "\n";

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.

Date
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 (try it):

use strict;

    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;

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.

Formatting Time with Figures Only
The following program displays local time in hh:mm:ss format:

use strict;

    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;

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;

    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 . "\n";
    print $min . "\n";
    print $hour . "\n";
    print $mday . "\n";
    print $mon . "\n";
    print $year . "\n";
    print $wday . "\n";
    print $yday . "\n";
    print $isdst . "\n";

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 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
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message