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

$daysfrom = ($yyyy % 4 ? 365 : 366) isn't sufficient for leap years .. there's another part of the rule -- e.g. Feb 1900 only had 28 days but Feb 2000 had 29. google results

I see you were trying to avoid other modules, but just for an example (and cause it's a module i use alot), here's a partial Date::Calc solution:
my $birthday = '2000-01-02'; # yyyy-mm-dd use Date::Calc qw/Delta_Days Today Add_Delta_YMD/; my @date = split /-/, $birthday; $date[0] = (Today())[0]; # set the year to current year print Delta_Days( Add_Delta_YMD(@date,-1,0,0), @date ); # days since +last one print Delta_Days( @date, Add_Delta_YMD(@date,1,0,0) ); # days until +next one
Update: added in the Add_Delta_YMD to account for birthdays on 2/29

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:49 UTC
    But when calculating days to/from birthdate, we don't deal with 1900. We only ever work with the current year (plus or minus one). I don't recall what the rules are regarding the 'leap year skip', but I'm pretty sure it won't affect this code for a very long time.
      We only ever work with the current year (plus or minus one).
      Which by definition includes the Feb/Mar boundry, so leap year can affect it by a day...
      I don't recall what the rules are regarding the 'leap year skip',
      See the google link above (searched for 'leap year calculation'); Also another reason to use Date::Calc, Date::Time, etc modules.
      but I'm pretty sure it won't affect this code for a very long time.
      Yeah.. i'm pretty sure that's what people said about the year 2000, which came and went, as will 2038 .. as will 2100 (which has only 28 days in Feb) .. Sure, that's a little way off, but on prinicpal you should care about the accuracy of your code, also, what about this realistic (at least plausible) scenario: Someone sees your snippet here and adapts it to their code, which isn't limited to the current year (e.g. days to/from a certain future date) and 2100 might be a valid input ..
        I thought the category 'fun stuff' was obvious....

        I take great pride in my code (even if it's sometimes wrong).

        I also spent the better part of 1997 thru 1999 fixing broken mainframe date code for a living. If you want to compare a 30 year old 2 digit year problem to a 100 year old leap year problem I think there's a baby/bath-water comparison that could be made. For all you know, by 2100 the leap year rule may be changed into another exception just to prevent computer leap year problems from popping up! (if we're even using the same calendar system by then)

        The code I've written here specifically uses current date +/- 1 year. Without rewriting portions of it, it will never see 1900. You, me and everything we know and believe about computers will be long since dead and turned to dust before this code ever sees 2100. So the scenario of someone using this snippet untouched for either of those years is not even a probability. That's the beauty of the 2000 'exception'.

        The code as written (pending some revisions) will happily work with any birthdate of any year. You can use any number that Perl will accept for the birthdate year, it isn't even passed to a date function of any kind. There is no provision to provide any date other than the current date. To make such a modification would require more careful attention to leap years as you've suggested. It would also not be the code I wrote.

        The point of my writing it was to avoid using Date::Calc. If Date::Calc were a core module I might have considered it first but it's not. There are times when you can't just pick the module of your dreams and toss it into the script.