Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How to find Date and Time difference?

by gube (Parson)
on Jul 08, 2005 at 05:01 UTC ( #473325=perlquestion: print w/ replies, xml ) Need Help??
gube has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I used the below code using Date::Calc. It's working good. But, my requirement is something exactly with date and minute correct also.

use Date::Calc qw(Delta_YMDHMS); ($day1,$month1,$year1,$hour1,$min1,$sec1) = &getMeanTime; ($day2,$month2,$year2,$hour2,$min2,$sec2) = &getMeanTime; ($D_y,$D_m,$D_d, $Dh,$Dm,$Ds) = Delta_YMDHMS($year1,$month1,$day1, $ho +ur1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2); print sprintf("%02d", $D_d).'/'.sprintf("%02d", $D_m).'/'.sprintf("%04 +d", $D_y).' '.sprintf("%02d", $Dh).':'.sprintf("%02d", $Dm).':'.sprin +tf("%02d", $Ds); sub getMeanTime { my $time = time; my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $D +ayOfYear, $IsDST) = gmtime($time); $Year+=1900; $Month+=1; return $Day,$Month,$Year,$Hour,$Minute,$Second; }
If the difference between the date and time zero means i want the same date and time with seconds. But, It showing all date time to zero.

Thanks in advance

Comment on How to find Date and Time difference?
Download Code
Re: How to find Date and Time difference?
by Zaxo (Archbishop) on Jul 08, 2005 at 05:24 UTC

    I think it would be simpler to take time differences from epoch time. POSIX::strftime() is the best way I know to get odd time strings.

    It is unclear to me what your code is failing to do for you. Can you show an example of what you would prefer, and how your code fails to provide it?

    After Compline,
    Zaxo

Re: How to find Date and Time difference?
by gube (Parson) on Jul 08, 2005 at 05:29 UTC
    Thanks zaxo, i am clear with the process it has been done.
Re: How to find Date and Time difference?
by Enlil (Parson) on Jul 08, 2005 at 05:32 UTC
    I think your code is working fine. In between the two calls to getMeanTime() add a sleep(2); in there and you will find that you will get a difference in seconds instead of no difference at all.That is:
    #!/usr/bin/perl use strict; use warnings; use Date::Calc qw(Delta_YMDHMS); my ($day1,$month1,$year1,$hour1,$min1,$sec1) = &getMeanTime; sleep(2); my ($day2,$month2,$year2,$hour2,$min2,$sec2) = &getMeanTime; my ($D_y,$D_m,$D_d, $Dh,$Dm,$Ds) = Delta_YMDHMS( $year1,$month1,$day1, $hour1,$min1,$sec1, $year2,$month2,$day2, $hour2,$min2,$sec2); print sprintf("%02d", $D_d).'/' . sprintf("%02d", $D_m).'/' . sprintf("%04d", $D_y).' ' . sprintf("%02d", $Dh) . ':' . sprintf("%02d", $Dm) . ':' . sprintf("%02d", $Ds); sub getMeanTime { my $time = time; my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOfYe +ar, $IsDST) = gmtime($time); $Year+=1900; $Month+=1; return $Day,$Month,$Year,$Hour,$Minute,$Second; } #getMeanTime __END__ 00/00/0000 00:00:02

    -enlil

Re: How to find Date and Time difference?
by Joost (Canon) on Jul 08, 2005 at 07:50 UTC
Re: How to find Date and Time difference?
by davidrw (Prior) on Jul 08, 2005 at 09:44 UTC
    As mentioned laready, your code looks fine.. just wanted to toss in a couple comments...
    • When working with Date::Calc, I find it much easier and clearer to use arrays for the year/month/day or year/month/day/hour/min/sec sets.
      * To facilitate this, note the change in return value for getMeanTime() in the code below.
    • Those multiple sprintf's should really just be a single one and just take advantage of sprintf's template nature, and the print sprintf is redundant (should just be a printf).
    • note that gmtime() is the same as gmtime(time).
    • note from the Date::Calc docs for Delta_YMDHMS: "Arguments are expected to be in chronological order to yield a (usually) positive result."
    so i would write it something like:
    use Date::Calc qw(Delta_YMDHMS); # @d arrays are (y,mon,d,h,min,s) my @dt1 = getMeanTime(); sleep(2); my @dt2 = getMeanTime(); my @dt = Delta_YMDHMS( @dt1, @dt2 ); printf "%02d/%02d/%04d %02d:$02d:$02d", @dt[2,1,0, 3..5]; sub getMeanTime { my @t = gmtime; return ( $t[5]+1900, $t[4]+1, $t[3], @t[2..0] ); # (y, mon, day, h, + min, s) }
    Also note that instead of gmtime, you can (just for ease of the array usage and for Date::Clac consistency) use Today_and_Now():
    use Date::Calc qw(Delta_YMDHMS Today_and_Now); # @d arrays are (y,mon,d,h,min,s) my @dt1 = Today_and_Now(); sleep(2); my @dt2 = Today_and_Now(); my @dt = Delta_YMDHMS( @dt1, @dt2 ); printf "%02d/%02d/%04d %02d:$02d:$02d", @dt[2,1,0, 3..5];
Re: How to find Date and Time difference?
by leedo (Novice) on Jul 08, 2005 at 19:22 UTC
    I have had great success finding time/date differences using the DateTime module. Here is an example where I get the difference in time between now and the creation date on a file.
    use DateTime; my $current = DateTime->now; my $created = DateTime->from_epoch( epoch => (stat $_ )[9] ); my $duration = $current - $created;
    Then to get the actual duration values from $duration take a look at the DateTime::Duration module.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://473325]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-10-20 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls