in reply to Re^5: 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

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

Replies are listed 'Best First'.
Re^7: Calculate Age, days to next and days from last for a given birthday
by ikegami (Patriarch) on Apr 29, 2006 at 05:58 UTC
    Isn't converting years+months+days into a number exactly what (localtime)[7] does?

    No, it only combines month and day, not year, month and day. Without taking the year into account, you need to work around leap years. I didn't have to. You have to, and you did, but incorrectly. (Years divisible by 4 are leap years, except those divisible by 100, except those divisible by 400.)

    Actually, it just occured to me that both of our solutions have the following problems: They can't handle dates < Jan 1st 1970 or >= (some point in) 2038. Not very useful.

      Again with the leap years...

      As I explained to davidrw, the script as written (both mine and your version) uses current year +/- 1 year, not some arbitrary year in the past or future. At no time is any other year passed into a date function.

      Which means it breaks if you can go back in time to run it before 1970 (which, if you've found a way to do it, please let me know cause there are some investments I'd like to make). It also breaks if you're still using it by 2038, but that's a Perl internal date problem and well beyond the control of this script (and every other Perl script written that uses current date).

      So neither of our scripts will ever have to deal with dates < Jan 1st 1970 or >= (some point in) 2038. Not as written anyway
        Not again, still. You asked what was wrong with (localtime)[7], and I told you. You need to handle leap years (contrary to what you said). You attempted to do so, but it's buggy. It's wrong for 1900 and 2100, for starters. You might say that's not a problem because the system doesn't accept those dates, but it's a bug that it can't accept those dates. Once you fix your solution to accepts those dates, you'll have to fix the problem with the year 1900 and 2100. So that's two bugs. You're also off by a day (I think) when you run the program with certain combinations of current time, current date and current timezones for a total of three bugs.