Re: Seconds left in day + DST
On Fri, Jun 20, 2008 at 11:07:47AM +0200, Dr.Ruud wrote: > Rick Measham schreef: > > P DD wrote: > > >> my $tom = ($now + DateTime::Duration->new(days => 1))-> > >> truncate(to => 'day'); > > > > Can I suggest this for neater code: > > my $tom = $dt->clone->add( days => 1 )->truncate( to => 'day' ); > > I seem to remember that adding 24 hours is safer than adding 1 day. > (to never land on a non-existing DT value) Then my $tom = $dt->clone->truncate( to => 'day' )->add( days => 1 ); should be yet safer, in case you call it during a leap second ;-) Unless someone messes with the first second of a day, that is. Jost -- | Helft Spam ausrotten!HTML in Mail ist unhöflich. | | Postmaster, JAPH, manchmal Wahrsager am RZ der RUB | | Wahre Worte sind nicht gefällig, gefällige Worte sind nicht wahr.| | Lao Tse, Tao Te King 81 | smime.p7s Description: S/MIME cryptographic signature
Re: Seconds left in day + DST
Rick Measham schreef: > P DD wrote: >> my $tom = ($now + DateTime::Duration->new(days => 1))-> >> truncate(to => 'day'); > > Can I suggest this for neater code: > my $tom = $dt->clone->add( days => 1 )->truncate( to => 'day' ); I seem to remember that adding 24 hours is safer than adding 1 day. (to never land on a non-existing DT value) -- Affijn, Ruud "Gewoon is een tijger."
Re: Seconds left in day + DST
P DD wrote: my $tom = ($now + DateTime::Duration->new(days => 1))->truncate(to => 'day'); Can I suggest this for neater code: my $tom = $dt->clone->add( days => 1 )->truncate( to => 'day' ); Cheers! Rick Measham -- Message protected for iSite by MailGuard: e-mail anti-virus, anti-spam and content filtering. http://www.mailguard.com.au
Re: Seconds left in day + DST
For posterity... or as long as these mailing lists are archived. my $now = DateTime->now(time_zone => 'local'); my $tom = ($now + DateTime::Duration->new(days => 1))->truncate(to => 'day'); my $seconds_left = $tom->subtract_datetime_absolute($now)->seconds; On Thu, Jun 19, 2008 at 1:03 PM, Dave Rolsky <[EMAIL PROTECTED]> wrote: > On Thu, 19 Jun 2008, Jim Spath wrote: > > I'm a bit unclear on how to properly deal with DST when determining the >> number of seconds left in a day. >> >> Here's what I have now: >> >> my $now = DateTime->now(time_zone => 'local'); >> >> my $tom = >> ($dt + DateTime::Duration->new(days => 1))->truncate(to => 'day')); >> >> my $seconds_left = $tom_dt->epoch() - $dt->epoch(); >> >> It seems to work fine, but another developer here mentioned that it might >> not handle DST properly, although he wasn't sure how to do it himself. >> > > There is a method to do subtraction between two datetimes and return the > difference in seconds, subtract_datetime_absolute(). It handles leap seconds > and DST properly. > > Your method will also work for DST, but it's a little clunky, plus it won't > work outside the limited range of the epoch. > > Generally speaking, you should only use the epoch when you have to interact > with some non-DateTime module/system that requires epochs. Otherwise stick > with the DateTime.pm-provided methods for maximum correctness and > flexibility (and slowness, unfortunately ;) > > > -dave > > /*== > VegGuide.Org > Your guide to all that's veg > ==*/ >
Re: Seconds left in day + DST
On Thu, 19 Jun 2008, Jim Spath wrote: I'm a bit unclear on how to properly deal with DST when determining the number of seconds left in a day. Here's what I have now: my $now = DateTime->now(time_zone => 'local'); my $tom = ($dt + DateTime::Duration->new(days => 1))->truncate(to => 'day')); my $seconds_left = $tom_dt->epoch() - $dt->epoch(); It seems to work fine, but another developer here mentioned that it might not handle DST properly, although he wasn't sure how to do it himself. There is a method to do subtraction between two datetimes and return the difference in seconds, subtract_datetime_absolute(). It handles leap seconds and DST properly. Your method will also work for DST, but it's a little clunky, plus it won't work outside the limited range of the epoch. Generally speaking, you should only use the epoch when you have to interact with some non-DateTime module/system that requires epochs. Otherwise stick with the DateTime.pm-provided methods for maximum correctness and flexibility (and slowness, unfortunately ;) -dave /*== VegGuide.Org Your guide to all that's veg ==*/
Seconds left in day + DST
I'm a bit unclear on how to properly deal with DST when determining the number of seconds left in a day. Here's what I have now: my $now = DateTime->now(time_zone => 'local'); my $tom = ($dt + DateTime::Duration->new(days => 1))->truncate(to => 'day')); my $seconds_left = $tom_dt->epoch() - $dt->epoch(); It seems to work fine, but another developer here mentioned that it might not handle DST properly, although he wasn't sure how to do it himself. I'd like it to return the number of seconds until the next day, in my local timezone (EST). Thanks! - Jim