Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Number of days since

by FreeBeerReekingMonk (Deacon)
on Jul 14, 2016 at 15:35 UTC ( [id://1167788]=note: print w/replies, xml ) Need Help??


in reply to Number of days since [solved]

Well, there are many libraries that do that, so read up on those from fellow monks. The reason for that is that there are extra seconds, so some days exist that have 23:59:62 as a real, existing time.

Then, you have a difference between localtime and UTC time.

If you do not care much about that precision, and assume localtime, then you can use timelocal to transform the start and end dates into an epoch (seconds sinds 1970-01-01), substract these two numbers, and divide this number /60/60/24 to get aproximate days, then maybe round the value. Here is an example:

#!/usr/bin/perl use strict; use warnings; use Time::Local; my $seconds = 0; my $minutes = 0; my $hours = 0; my $day = 14; my $month = 7; my $year = 2016; my $epoch_from = timelocal($seconds||0, $minutes||0, $hours||0, $day, +$month-1, $year-1900); my $epoch_target = timelocal(0, 0, 0, 1, 8-1, 2016-1900); print secToTimeString0( $epoch_target - $epoch_from ); # This will give you an estimate of time. Used with -'"%;"' sub secToTimeString0 { my($t) = @_; my $s= ($t<0 && ($t*=-1))? '-':''; # return sprintf("$s%2d seconds", $t) if ($t < 60); $t /=60; #return sprintf("%s%2.2f minutes", $s, $t) if ($t < 60); $t /=60; #return sprintf("%s%2.2f hours", $s, $t) if ($t < 24); $t /=24; return sprintf("%s%2.2f days", $s, $t) ;#if ($t < 30); # $t /=30; return sprintf("%s%2.2f months", $s, $t) if ($t < 12); # $t /=12; return sprintf("%s%2.2f years", $s, $t); }

Instead of the secToTimeString0() function, you can also use round to get an integer number of days, like so:

my $days = int(0.5 + ( $epoch_target - $epoch_from ) /60/60/24 );

How do I round a number?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1167788]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-18 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found