Broad Network


Formatting Time in Perl

Date and Time in Perl – Part 3

Object Approach

Forward: In this part of the series, I explain how to precede a single digit time component with zero and how to have words or abbreviations in place of month numbers and day numbers of the week, in a Perl time string.

By: Chrysanthus Date Published: 1 Mar 2013

Introduction

This is part 3 of my series, Date and Time in Perl. Remember, date and/or time in Perl is simply, Time. In this part of the series, I explain how to precede a single digit time component with zero and how to have words or abbreviations in place of month numbers and day numbers of the week, in a Perl time string. The number for the day of the month and the month of the year and the hour , minute or second is normally one digit if it is less than 10. Under this condition, it is fashionable to present the number as two digits, the actual digit preceded by zero. The second, minute, hour, day of the week, day of the month, month and year are each a time component. You should have read the previous parts of this series as this is a continuation.

Strategy
In the previous part of the series, I explained how to produce a Perl time string consisting of only figures (in character forms). To achieve the aim of this tutorial, you would have to use the same code. However, you will have to use the if-construct to precede the single digits with zero and you will have to have arrays with names or abbreviations of months and days of the week. The words in an array correspond to numbers of a particular time component.

So, as soon as the gmtime or localtime object reads the time components, for the components of interest, an if-construct will check if the value is less than 10. If it is, the construct precedes the value with zero, resulting in a string of two characters. To replace a number with a word in a time string, you need arrays with words corresponding to the possible values of the components; one array for one component.

The gmtime and localtime objects are handled in the same way, so I will use only the localtime object for illustration in this tutorial.

Preceding Single Digits
Time components are the object properties. The properties are: sec, min, hour, mday, mon, year, wday, yday, and isdst. The meanings of theses properties where given in the previous part of the series. The value of isdst is 1 or 0 and indicates if the daylight saving scheme is in effect. The value of this property is not used in this tutorial.

The properties of interest, whose values can have single digits during display are: sec, min, hour, mday and mon. So you need five if-constructs. Read and try the following program, which displays the complete time in the 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 $sec = $locTim->sec();
    if ($sec < 10)
        {
            $sec = "0" . $sec;
        }
    my $min = $locTim->min();
    if ($min < 10)
        {
            $min = "0" . $min;
        }
    my $hour = $locTim->hour();
    if ($hour < 10)
        {
            $hour = "0" . $hour;
        }
    my $mday = $locTim->mday();
    if ($mday < 10)
        {
            $mday = "0" . $mday;
        }
    my $mon = $locTim->mon();
    my $localMonth = $mon + 1;
    if ($localMonth < 10)
        {
            $localMonth = "0" . $localMonth;
        }
    my $year = $locTim->year();
    my $localYear = $year + 1900;

    my $localTime = $localMonth . "-" . $mday . "-" . $localYear . " " . $hour . ":" . $min . ":" . $sec;

    print $localTime;


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


Note: with the string dot concatenation operator, if one operand is a number and the other is a string (or character) the result is a string, not a number. The statement that forms the overall string is:

    my $localTime = $localMonth . "-" . $mday . "-" . $localYear . " " . $hour . ":" . $min . ":" . $sec;

Note how the characters ‘–‘ and ‘:’ have been inserted. A space has also been inserted between the actual (conventional) date and the actual (conventional) time with " ".

Replacing Week Day and Month with Abbreviation
In the above code the day of the week e.g. Monday was not considered. There are times when you need to include it in a date and it is normally included as its full word or abbreviation. There are times when you want to include the month in full word or abbreviation. Whether you are including a date property value in full word or abbreviation, the technique is the same. In this tutorial I illustrate the technique only with abbreviations.

In the case of days of the week, the following array will do (suffices):

    my @week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

Array element counting begins from zero. In Perl the numbers are: 0, 1, 2, 3, 4, 5 and 6. Sunday is 0, Monday is 1, Tuesday is 2, etc. So the indices of the array just correspond to the Perl numbers of the days of the week. There is no need to add 1 to obtain the ordinary man’s day number of the week in the code.

In the case of months of the year, the following array suffices:

    my @mYear = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

Again, array element counting begins from zero. In Perl the numbers are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 and 11. January is 0, February is 1, March is 2, etc. So the indices of the array just correspond to the Perl numbers of the months of the year. There is no need to add 1 to obtain the ordinary man’s month number of the year in the code.

An example of a complete local time format that the following program will produce is:

    Fri, 21 Nov 2013 9:55:13

The program is:

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 $wday = $locTim->wday();
    my @week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    my $sec = $locTim->sec();
    if ($sec < 10)
        {
            $sec = "0" . $sec;
        }
    my $min = $locTim->min();
    if ($min < 10)
        {
            $min = "0" . $min;
        }
    my $hour = $locTim->hour();
    if ($hour < 10)
        {
            $hour = "0" . $hour;
        }
    my $mday = $locTim->mday();
    if ($mday < 10)
        {
            $mday = "0" . $mday;
        }
    my $mon = $locTim->mon();
    my @mYear = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    my $year = $locTim->year();
    my $localYear = $year + 1900;

    my $localTime = $week[$wday] . ", " . $mday . " " . $mYear[$mon] . " " . $localYear . " " . $hour . ":" . $min . ":" . $sec;

    print $localTime;


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

Read and try the code if you have not already done so. The program produces the same format for GMT.

The statement that produces the string for the complete time is:

    my $localTime = $week[$wday] . ", " . $mday . " " . $mYear[$mon] . " " . $localYear . " " . $hour . ":" . $min . ":" . $sec;

In this statement, $week[$wday] is effectively something like, $week[2] where 2 is from $wday. Also, $mYear[$mon] is effectively something like, $mYear[3] where 3 is from $mon. The single space has been included by, " ". Comma is included by ", " and the colon is included by ":"

So, after obtaining the components from the instantiated object, you form a string by joining the bits (modified components) with the concatenating string dot operator.

Use of Forward Slash in Dates
You can have an American date like:

    08/25/2013

instead of

    08-25-2013

To have the forward slashes is simple, just replace ‘–‘ in the time string formation with ‘/’.

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