Broad Network


Perl Time Difference

Date and Time in Perl - Part 6

Object Approach

Forward: In this part of the series, I explain how to subtract a Perl time from another Perl time.

By: Chrysanthus Date Published: 2 Mar 2013

Introduction

This is part 6 of my series, Date and Time in Perl. In this part of the series, I explain how to subtract a Perl time from another Perl time. In Perl, date and/or time is simply referred to as time. It typically consists of seconds, minutes, hour, day of month, month, and year. I gave the ranges and specifications of these time components in the previous part of the series. You should have read the previous parts of the series, because this is a continuation. In the previous part of the series, I referred to the complete time as date, to make the analysis clearer. In this part of the series, I refer to the complete time like the Perl lover, simply as, time.

I use the USA time format in this tutorial.

Strategy
To subtract one date from another, covert both dates to number of seconds. Subtract one of the converted number of seconds from the other. Then convert the resulting number of seconds to Time. I use the localtime() and timelocal() methods. You can use the gmtime() and timegm() methods in the same way. The localtime() method is from the localtime class; the timelocal() method is from the Local class; the classes and methods, are all from the Time module.

To use the localtime() method, you have to precede the corresponding code segment with “use Time:: localtime;”. To use the timelocal()  method, you have to precede the corresponding code segment with “use Time:: Local;”.

The approach in this tutorial works with years after 1970. 1970 is the epoch year for windows. If the epoch year for your operating system is different, then you need to adjust the approach in this tutorial, accordingly.

Read and try the following program, which subtracts the USA time, 8-21-1995 18:36:44 from the USA time, 9-19-2013 22:41:33, where 1 has already been subtracted from the month-day-numbers, before inputting.

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 $noSecs1 = timelocal(44, 36, 18, 21, 8, 1995);
    my $noSecs2 = timelocal(33, 41, 22, 19, 9, 2013);
    my $secsDiff = $noSecs2 - $noSecs1;

    use Time::localtime;
    my $locTim = localtime($secsDiff);

    my $sec = $locTim->sec();
    my $min = $locTim->min();
    my $hour = $locTim->hour();
    my $mday = $locTim->mday();
    my $mon = $locTim->mon();
    my $year = $locTim->year();
       $year = $year - 70;

    print $mon . "-" . $mday . "-" . $year . " " . $hour . ":" . $min . ":" . $sec;

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

The timelocal() function converts the time to seconds and the localtime() function converts the difference is seconds to difference in time. Note that 70 is subtracted from year because the program determines time from 1970.

The result is:

    0-29-18 20:4:49

It is in the form: mm-dd-yyyy hh:mm:ss. The result needs interpretation. It means, between the two times you have 18 years plus 0 (+1) months plus 29 days plus 20 hours plus 4 minutes plus 49 seconds. That is how you should interpret the difference in times (dates) from this program

The principles of this tutorial applies to time after the epoch year, which is 1970 for my Windows operating system.

End of Tutorial, End of Series
This is the end of the tutorial. This is the end of the series. I hope you appreciate the series.

Thanks,
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

Comments

Become the Writer's Fan
Send the Writer a Message