Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

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


in reply to Re^2: Calculate Age, days to next and days from last for a given birthday
in thread Calculate Age, days to next and days from last for a given birthday

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.
  • Comment on Re^3: Calculate Age, days to next and days from last for a given birthday

Replies are listed 'Best First'.
Re^4: Calculate Age, days to next and days from last for a given birthday
by ruzam (Curate) on Apr 29, 2006 at 02:48 UTC
    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!
      But I'm still confused as to the need to convert to seconds.

      You can't do arithmetic on years+months+days. Just like you can't do arithmetic on degrees+minutes+seconds. You need to convert it into a number.

      I could have converted every date into the number of days since an arbitrary day, but there's no existing function to do that (or the inverse operation). Doing it manually would force me to handle leap seconds, leap days, oddly numbered months, etc.

      On the other hand, there's already a function (timegm) to convert a date into the number of seconds since an arbitrary second (Jan 1st, 1970, 0:00:00), and there's already a function which does the inverse operation (gmtime).

        Isn't converting years+months+days into a number exactly what (localtime)[7] does? It's returning the day number (on a scale of 1 to 365 or 366) of the current date in the current year. The conversion has been done, leap seconds, DST, odd months, leap years and all.

        I'm not trying to calculate the number of days since birth, or even the number of days between two dates for that matter, just the day number of the birth date in an arbitrary year (either last or next). While it may look like the code is trying to calculate based on the number of days from a common reference, it's actually calculating based on the day number of given year (with an adjustment for an extra leap day where neccessary).

        For example Jan 1 this year is day 1. Today is day 117. If the birthdate in question was Jan 1, the days from birthday is simply 117 - 1 = 116. Jan 1 next year is also day 1. This year has 365 days, so days to next birthday is 365 - 117 + 1 = 249 (days in the year - days from today to the end of this year + days to date in next year). At no time am I actually trying to calculate the number of days between two dates, only the number of days since the beginning of a year, which (localtime)[7] does nicely. It even works through leap months, as long as you're careful about the what you consider is the number of days in the year.

        Or am I still missing it?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-16 06:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found