Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Calculate Age, days to next and days from last for a given birthday

by ikegami (Patriarch)
on Apr 28, 2006 at 20:23 UTC ( [id://546389]=note: print w/replies, xml ) Need Help??


in reply to Calculate Age, days to next and days from last for a given birthday

For dates (i.e. times with h,m,s=0), it's better to use timegm+gmtime over timelocal+localtime. I think your program will return an incorrect answer for some time periods due to DST.

Update:

Solution which still uses core module Time::Local:

use strict; use warnings; use Time::Local qw( timegm_nocheck ); # birth_date_age(2006, 4, 28) for people born on April 28th, 2006. sub birth_date_age { my ($bday_y, $bday_m, $bday_d) = @_; # ---------------------------------------- # -- Get current date. my ($now_y, $now_m, $now_d) = (localtime)[5,4,3]; $now_y += 1900; $now_m++; my $date_now = timegm_nocheck(0,0,0, $now_d, $now_m-1, $now_y); # ---------------------------------------- # -- Get next birthday. my $date_next = timegm_nocheck(0,0,0, $bday_d, $bday_m-1, $now_y); $date_next = timegm_nocheck(0,0,0, $bday_d, $bday_m-1, $now_y+1) if $date_next <= $date_now; my ($next_y) = (gmtime($date_next))[5]; $next_y += 1900; # ---------------------------------------- # -- Get prev birthday. my $prev_y = $next_y - 1; my $date_prev = timegm_nocheck(0,0,0, $bday_d, $bday_m-1, $prev_y); # ---------------------------------------- # -- Calculate age and others my $age = $next_y - $bday_y - 1; my $days_to = ($date_next - $date_now) / (24*60*60); my $days_from = ($date_now - $date_prev) / (24*60*60); return ($age, $days_from, $days_to); }

Notes:

  • On years without a Feb 29th, March 1st will be considered the birthday anniversary of people born on Feb 29th.

  • I leave the date parsing to the caller. It makes for a more flexible solution.

  • For arguments, days, months and years are one-based.

  • Internally, days, months and years are one-based.

  • Comment on Re: Calculate Age, days to next and days from last for a given birthday
  • Download Code

Replies are listed 'Best First'.
Re^2: Calculate Age, days to next and days from last for a given birthday
by ruzam (Curate) on Apr 28, 2006 at 20:46 UTC
    The time period in this case would I guess be midnight as that's when the date would switch over. Wouldn't it be more appropriate to have that happen on local time? Otherwise your birthdate wouldn't flip until gmtime midnight, which wouldn't be correct (unless you happen to be living on the gm time line).
      As you can see in my (newly added) code, I work with *local* dates using timegm+gmtime. The number of days bewteen two dates is the same no matter which time zone you are in.
        OK, I see now how your code works works (took me a while, sorry). But I'm still confused as to the need to convert to seconds.

        my $days = (localtime)[7];
        $days will be correct for the current date regardless of the time or DST no? All other days are calculated as of 0:00:00 which should also be correct for any given date regardless of DST. Or am I missing something about the way localtime/timelocal work?
        (living in a time zone with no DST does have it's disadvantages here).

        I never thought of feb 29 birthdates failing as invalid dates in non-leap years!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-24 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found