Skis has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to calculate the numbers of days since a calendar day each year. Say August 1st.

Today, it would calculate 347.
On August 2nd, it would calculate 1.
Any advice?

Replies are listed 'Best First'.
Re: Number of days since
by hippo (Archbishop) on Jul 14, 2016 at 13:51 UTC
Re: Number of days since
by ww (Archbishop) on Jul 14, 2016 at 13:53 UTC
Re: Number of days since
by FreeBeerReekingMonk (Deacon) on Jul 14, 2016 at 15:35 UTC
    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?

Re: Number of days since
by Skis (Initiate) on Jul 14, 2016 at 20:54 UTC
    Here is my first solution.
    use Time::Piece; my $now = Time::Piece->new; my $nowlastyear = $now->add_years(-1); my $thisyear = $now->strftime("%Y"); my $lastyear = $nowlastyear->strftime("%Y"); my $afy = "08/01/$thisyear"; my $afyt = Time::Piece->strptime($afy, '%m/%d/%Y'); my $bfy = "08/01/$lastyear"; my $bfyt = Time::Piece->strptime($bfy, '%m/%d/%Y'); my $daysdiff; if ($now > $afyt) { $daysdiff = $now - $afyt; } else { $daysdiff = $now - $bfyt; } $daysdiff = int($daysdiff->days); print $daysfiff;