Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

DateTime and hour 0

by tcf03 (Deacon)
on Apr 18, 2005 at 13:13 UTC ( [id://448837]=perlquestion: print w/replies, xml ) Need Help??

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

Im using DateTime in a script and I keep getting the following error when entering 0 or 00 for my hour.
The 'hour' parameter ("24") to DateTime::new did not pass the 'is betw +een 0 and 23' callback at /usr/lib/perl5/site_perl/5.8.5/DateTime/Format/Epoch.pm line 179
and here is how DateTime is using $hour:
$datetime=DateTime->new( year => 2005, month => $MONTHNUM, day => $FDAY, hour => $hour, minute => $minute, second => 00 );
any Ideas about the error?
Ted

UPDATE another oddity is that 00 does work (in the hour field) as long as $minute >= 10. if I set minute to anything less that 10. Im looking through Epoch.pm from DateTime::Format::Epoch, but its a bit above my skill level.
UPDATE2
fglock nailed it when he suggested changing the algorithm. Here is the new code that works. Thanks for all you're suggestions.
sub minutes_before { my $minutes_b4=shift; my $month=shift; my $day=shift; my $hour=shift; my $minute=shift; # Lets find n minutes before! my $datetime=DateTime->new( year => 2005, month => month_to_num($month), day => $day, hour => $hour, minute => $minute ); $datetime->subtract( minutes => $minutes_b4 ); # This is clumsy, ill use it now # and fix it later if ( $datetime->day < 10 ) { $day="0".$datetime->day; } else { $day=$datetime->day; } if ( $datetime->hour < 10 ) { $hour="0".$datetime->hour; } else { $hour=$datetime->hour; } if ( $datetime->minute < 10 ) { $minute="0".$datetime->minute; } else { $minute=$datetime->minute; } # End of genereal clumsiness... $ready=1; return @MINBEFORE = ( num_to_month($datetime->month), $day, $hour, $minute ); }
There is still the messiness of zero padding, but ill figure that out in time - if anyone has suggestions that would be cool.

Replies are listed 'Best First'.
Re: DateTime and hour 0
by RazorbladeBidet (Friar) on Apr 18, 2005 at 13:19 UTC
    You entered hour "24" when there is only an hour up to "23"... try $hour % 24 (that is, hour modulus 24)
    --------------
    "But what of all those sweet words you spoke in private?"
    "Oh that's just what we call pillow talk, baby, that's all."
      I didn't set it to 24 I tried 0 and 00, thats why the error seems odd to me. here is the beginning of the sub where this is located
      sub minutes_before { my $minutes_b4=shift; my $month=shift; my $day=shift; my $hour=shift; my $minute=shift; my $MONTHNUM=&month_to_num($month); ( $day < 10 ) ? $FDAY="0$day" : $FDAY=$day; # Lets find n minutes before! $datetime=DateTime->new( year => 2005, month => $MONTHNUM, day => $FDAY, hour => $hour, minute => $minute, second => 00 ); my $formatter=DateTime::Format::Epoch->new( epoch => $datetime, unit => 'seconds', type => 'int', # or 'float', or 'b +igint' skip_leap_seconds => 1, start_at => 0, local_epoch => undef ); my $seconds=$formatter->format_datetime($datetime); my $conv_seconds=$minutes_b4 * 60; $seconds=$seconds-$conv_seconds; # And we are left with n seconds before our search time my $MinBefore=$formatter->parse_datetime( $seconds ); # Now we'll reformat $MinBefore to fit the log file my ($DATE2, $TIME2)=split(/T/, $MinBefore); ($YEAR2, $MONTH2, $DAY2)=split(/-/, $DATE2); $MONTHNAME2=&num_to_month($MONTH2); ($HOUR2, $MINUTE2, $SECOND2)=split(/:/, $TIME2); # End of reformatting $MinBefore $DAY2=~s/^0+//; $ready=1; return @MINBEFORE=($MONTHNAME2, $DAY2, $HOUR2, $MINUTE2); }
      and here is where $hour gets set:
      if ( defined (param('MONTH')) and defined (param('DAY')) and defined (param('HOUR')) and defined (param('MINUTE')) ) { $MONTH=param('MONTH'); $DAY=param('DAY'); $HOUR=param('HOUR'); $MINUTE=param('MINUTE');
      snip...
      @M1=&minutes_before(1, $MONTH, $DAY, $HOUR, $MINUTE);
      The code works as its supposed to execept when I enter 0 or 00.
      Thanks
      Ted
      update
      $hour % 24 didn't work either.
Re: DateTime and hour 0
by Zaxo (Archbishop) on Apr 18, 2005 at 13:21 UTC

    Yes, $hour is set to 24, an out-of-range value. You should show where $hour gets its value for better advice.

    After Compline,
    Zaxo

Re: DateTime and hour 0
by fglock (Vicar) on Apr 18, 2005 at 15:04 UTC

    I can't replicate the problem here. How about simplifying the algorithm instead?

    sub minutes_before { my $minutes_b4=shift; my $month=shift; my $day=shift; my $hour=shift; my $minute=shift; # Lets find n minutes before! my $datetime=DateTime->new( year => 2005, month => month_to_num($month), day => $day, hour => $hour, minute => $minute, second => 00 ); $datetime->subtract( minutes => $minutes_b4 ); $ready=1; return @MINBEFORE = ( num_to_month( $datetime->month ), $datetime->day, $datetime->hour, $datetime->minute ); }
      Thanks That seems to work better, Im just trying to zero-pad the day and num_to_month( $datetime->month ) isnt returning anything. But I get the idea. Thanks again.
      Ted
        Zero-padding is best done with printf or sprintf. I don't know where num_to_month is coming from (something you wrote?) but why not just use $datetime->month_name?
Re: DateTime and hour 0
by shemp (Deacon) on Apr 20, 2005 at 22:10 UTC
    Just an observation. Dont know how this will help or hurt you situation. Anytime you have an unquoted numeric that begins with a zero, it is interpreted as an octal:
    { my $x = 10; my $y = 010; print "x = $x\n"; print "y = $y\n"; }
    Outputs:
    x =10
    y =8

    But since you're using 00, that should interpret as numeric zero anyway, so im not sure whats happening.

    Thought it might help someone else figure out the whole deal.

Re: DateTime and hour 0
by Mabooka-Mabooka (Sexton) on Apr 21, 2005 at 23:37 UTC
    I'm afraid I am too late, the discussion seems to be finished... But if somebody looks at it again,
    -- what was the question exactly?
    1) why doesn't this long code snippet work, or
    2) smth. wrong with passing 0 or 00 to this class?
    I mean, can somebody narrow this down to smth. like this:
    use DateTime; $datetime=Time->new( year => 2005, month => 5, day => 5, hour => 0, minute => 13, second => 00 );
    ,or am I missing the point (as ususal)?
    PS: I'd love to comment, have a couple of ideas, but cannot play with / reproduce this, -- apparently I don't have this module.....
      I was having some issues, with some of the formatting, in one of my posts I posted the fix, basically it was to chang the algorithm as fglock had suggested.

      ted

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://448837]
Approved by RazorbladeBidet
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found