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

I knocked up the following code to figure out if the day was odd or even numbering using UNIX epoch. Thing is it seems to always tell me the days is always even, but I can't figure out why
$var=time/24*60*60; print $var%2 ? "odd\n" : "even\n";
Any pointers would be great as I'm a bit lost
Thanks

Replies are listed 'Best First'.
Re: odd or even day?
by saintmike (Vicar) on Jan 07, 2006 at 17:45 UTC
    Dividing an integer by 24 and then muliplying it by 60*60 is equivalent to multiplying it by 150 -- which will always result in an even number.

    But there's also a problem with your date math: Assuming that you want to know if the day of the month is odd or even, use Perl's localtime function or, for advanced functionality, DateTime. Unless you want to deal with leap years, months with 28, 30, 31 days etc yourself.

Re: odd or even day?
by Not_a_Number (Prior) on Jan 07, 2006 at 19:52 UTC

    Not quite sure what you mean by 'odd' and 'even'.

    January 7 2006 is the 13,155th day since the start of the epoch. It is therefore 'odd' by your calculation (update: I should have written something like "by your calculation adjusted as per replies above"). However, January 7 2007 will be the 13,520th day, and therefore 'even'.

    If that is what you want, fine, just ignore this post.

    However, for a more normal definition of 'odd/even' - ie Jan 7 is odd, Jan 8 is even, Feb 1 is odd, Feb 2 is even, etc - you could do:

    print +( localtime )[3] % 2 ? "odd\n" : "even\n";

    Or else, counting from the beginning of the year - ie March 1 is the 60th day (even) in a normal year, but the 61st day (odd) in a leap year:

    print +( localtime )[7] % 2 ? "even\n" : "odd\n";
Re: odd or even day?
by McDarren (Abbot) on Jan 07, 2006 at 17:47 UTC
    The following works:
    #!/usr/bin/perl -w use strict; my $var=time/(24*60*60); print $var%2 ? "odd\n" : "even\n";

    The only difference is the parentheses. I'm not sure why they are required. Perhaps somebody else can explain that...

      * and / have the same precedence and evaluate left to right so time/24*60*60 is equivelent to (time/24)*60*60, which is not what OP intended.

      Update: See perlop Operator Precedence and Associativity.


      DWIM is Perl's answer to Gödel

      The only difference is the parentheses. I'm not sure why they are required. Perhaps somebody else can explain that...

      I think its because perl follows standard mathematical operator precedence rules: B.E.D.M.A.S (Brackets, exponents, division, multiplication, addition, subtraction.)

      update: Grandfather's explanation is better.

      ---
      $world=~s/war/peace/g

        Thanks for the replies. I'd completely forgotten about precedence rules, seems to have sorted it out.
        Thanks alot
        Jon
Re: odd or even day?
by trammell (Priest) on Jan 07, 2006 at 17:39 UTC
    Check your math. You can use paper and pencil to estimate the current day number according to your scheme, then print $var and see if it's in the right ballpark.

    Update: quick sanity check: assume there are 365 days in each year. This is 2006, so there have been about 36 * 365 days:

    36 * 365 ~ 10 * 36 * 36
             = 10 * 12 * 12 * 9
             ~ 14400
    
      This was the reason why I'm using UNIX time to avoid issues such as this. I'm assuming that 'time' provides this, is that correct, I believe so
Re: odd or even day?
by Anonymous Monk on Jan 07, 2006 at 17:47 UTC
    How about...
    $var=time/24/60/60; print $var%2 ? "odd\n" : "even\n";
    ...or...
    $var=time/(24*60*60); print $var%2 ? "odd\n" : "even\n";
Re: odd or even day?
by ysth (Canon) on Jan 08, 2006 at 03:16 UTC
    Your problems seem to have been straightened out, so I'm going to suggest looking at the odd or even test on ambrus's homenode.
Re: odd or even day?
by ambrus (Abbot) on Jan 08, 2006 at 15:21 UTC

    Take care with this because time/(24*60*60)%2 needn't switch at midnight, but some other time of the day.