Broad Network


Arbitrary Date in Perl

Date and Time in Perl – Part 4

Object Approach

Forward: In this part of the series I explain how to create and use your own time class and corresponding instantiated object.

By: Chrysanthus Date Published: 2 Mar 2013

Introduction

This is part 4 of my series, Date and Time in Perl. In the previous part of the series, I talked about localtime and GMT time all generated from the computer or server. Imagine that you are narrating a story and you have to quote a time. That is what I answer in this part of the series. You should have read the previous parts of the series before reaching here, because this is a continuation.

Remember, in Perl, date and/or time is referred to just as, Time. I use the complete time for illustration in this tutorial. In this part of the series I explain how to create and use your own time class and corresponding instantiated object.

A Time Class
I use the following properties, with the same meanings as they had in the previous part of the series: sec, min, hour, mday, mon, year, wday, yday, and isdst. The class is:


    {package Time;

        my $self = {};

        #constructor
        sub new
            {
                my $class = $_[0];

                $self->{'sec'} = $_[1];
                $self->{'min'} = $_[2];
                $self->{'hour'} = $_[3];
                $self->{'mday'} = $_[4];
                $self->{'mon'} = $_[5];
                $self->{'year'} = $_[6];
                $self->{'wday'} = $_[7];
                $self->{'yday'} = $_[8];
                $self->{'isdst'} = $_[9];

                bless $self, $class;
            }

        #methods
        sub sec
            {
                return $self->{'sec'};
            }

        sub min
            {
                return $self->{'min'};
            }

        sub hour
            {
                return $self->{'hour'};
            }

        sub  mday
            {
                return $self->{'mday'};
            }

        sub mon
            {
                return $self->{'mon'};
            }

        sub year
            {
                return $self->{'year'};
            }

        sub wday
            {
                return $self->{'wday'};
            }

        sub  yday
            {
                return $self->{'yday'};
            }

        sub isdst
            {
                return $self->{'isdst'};
            }

    }


A class is a package. It has a hash. The reference to the hash is held in the variable, $self. In Perl, you can create an empty hash and add in the key/value pairs later. The hash holds the properties of the class.

When the constructor method is called, the first argument is the name of the class, in this case, Time. It is copied to the variable, $class. After the first statement in the constructor method, you have the initialization of the properties of the class instance. Remember, the constructor is only called, when you want an instance (object) of the class. In this case, initialization, is giving values to the keys of the hash. Actually, the keys and values are created at the same time, in this case.

The names of the properties are the keys of the hash whose reference is held in $self. Note that the property name is not preceded by a $ symbol. Also, if you input year as the normal four digits, you would have the normal four digits returned. Each method returns the value that is inputted of the key/value pair in the hash.

The last statement of a Perl function (method or subroutine) returns its result. The last statement of the constructor method should be the bless statement. It is this statement that effectively instantiates the class, after the values of the hash keys have been assigned. It does so by returning a reference to the hash created. It also relates the hash created with the class description (definition). The class description in this case is the description of the package called, Time.

Instantiating the Class
A statement to instantiate the class can be:

    my $myTime = Time->new(10, 59, 17, 22, 1, 2013, 5, 52, 0);

The values of the time components are the arguments of the constructor method, new(). In the argument list, you can type the single digit values, preceded by zero, if you like. The reference of the hash created in the constructor method is now held in the variable, $myTime.

In my writings, I prefer to use the phrase “instantiated object” to “instantiated class”, in order to differentiate instantiated classes from fundamental objects, like integers.

Using the Class and Object
In the following code, the first segment reads the values of the Time class properties by calling the methods, and the second segment displays the values.

    my $sec = $myTime->sec();
    my $min = $myTime->min();
    my $hour = $myTime->hour();
    my $mday = $myTime->mday();
    my $mon = $myTime->mon();
    my $year = $myTime->year();
    my $wday = $myTime->wday();
    my $yday = $myTime->yday();
    my $isdst = $myTime->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>";

Remark
The procedure outlined above defines a class and instantiates it for an arbitrary time or time you imagine. It is not local time or GMT time. Local time is time read from the computer. GMT time is time read from the computer and adjusted by the time zone difference, relative to the time in Britain. This adjustment is done automatically by the computer.

Complete Code
The complete code for the above 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>";


    {package Time;

        my $self = {};

        #constructor
        sub new
            {
                my $class = $_[0];

                $self->{'sec'} = $_[1];
                $self->{'min'} = $_[2];
                $self->{'hour'} = $_[3];
                $self->{'mday'} = $_[4];
                $self->{'mon'} = $_[5];
                $self->{'year'} = $_[6];
                $self->{'wday'} = $_[7];
                $self->{'yday'} = $_[8];
                $self->{'isdst'} = $_[9];

                bless $self, $class;
            }

        #methods
        sub sec
            {
                return $self->{'sec'};
            }

        sub min
            {
                return $self->{'min'};
            }

        sub hour
            {
                return $self->{'hour'};
            }

        sub  mday
            {
                return $self->{'mday'};
            }

        sub mon
            {
                return $self->{'mon'};
            }

        sub year
            {
                return $self->{'year'};
            }

        sub wday
            {
                return $self->{'wday'};
            }

        sub  yday
            {
                return $self->{'yday'};
            }

        sub isdst
            {
                return $self->{'isdst'};
            }

    }

    my $myTime = Time->new(10, 59, 17, 22, 1, 2013, 5, 52, 0);

    my $sec = $myTime->sec();
    my $min = $myTime->min();
    my $hour = $myTime->hour();
    my $mday = $myTime->mday();
    my $mon = $myTime->mon();
    my $year = $myTime->year();
    my $wday = $myTime->wday();
    my $yday = $myTime->yday();
    my $isdst = $myTime->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>";


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