Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Date::Manip and daylight savings

by ChrisDennis (Sexton)
on Nov 07, 2011 at 17:28 UTC ( [id://936546]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Date::Manip and daylight savings
in thread Date::Manip and daylight savings

Thanks for your reply, SBECK.

Yes, a full date/time needs to include the timezone. So why does Date::Manip just return a string that has no timezone in it? Is there a way to make it append the timezone?

And yes, the second question depends on what I mean by 'one day ago'. In this case, I mean 'the same time yesterday'. I don't think that's ambiguous: if I'm supposed to turn up to work at 8am on Friday, and do the same on Monday after the change to or from summer time, it's up to me to make the adjustment. In both cases, it's assumed that we're talking about 'local time'.

I hoped that Date::Manip would do that if I used 'approximate' mode, but apparently not.

Is there another option to get Date::Manip to do what I want?

My definition of '1 day' is: 24 hours, unless the period spans the change to/from daylight saving, in which case it's either 23 or 25 hours (in most countries).

If I want to go back 24 hours, I can use a delta such as '-0:0:-0:0:24:0:0'. But if I want to go back 1 day, then I feel that '-0:0:-0:1:0:0:0' should do the right thing.

cheers

Chris

Replies are listed 'Best First'.
Re^5: Date::Manip and daylight savings
by SBECK (Chaplain) on Nov 07, 2011 at 17:58 UTC

    If you'll switch to using the Object Oriented interface of Date::Manip, there won't be any problem. The functional interface is limited by being backward compatible with the old version of Date::Manip (which didn't work well with timezones). Look at the man page for Date::Manip for information on how to use it. Then, you would write:

    #!/usr/bin/perl use 5.10.1; use strict; use warnings; use Date::Manip::Date; my $date1 = new Date::Manip::Date; my @dates = ('2011103003:00:00', '2011103002:00:00', '2011103001:00:00', '2011103000:00:00'); my $delta1 = $date1->new_delta(); $delta1->parse('-0:0:-0:0:1:0:0'); foreach my $date (@dates) { $date1->parse($date); my $date2 = $date1->calc($delta1); print "date: ",$date1->printf('%g')," ", "date2: ",$date2->printf('%g'),"\n"; }

    If you want different information, use different printf formats.

    The 'one day ago' question cannot be defined to be 'the same time yesterday' because that's not guaranteed to exist. The definition that I'm leaning to (when/if I implement it) is:

    The same time yesterday with the same offest as today. If that doesn't exist, then the same time yesterday in the other offset. If that doesn't exist, then 24 hours in the past.

    A definition like that is the only way to handle all of the daylight saving time changes, and I'm strongly considering implementing it (in approximate mode only). It probably gives the results that people expect most of the time (though that one time when the third case kicks in is going to confuse people... but daylight saving time is by definition confusing :-).

    I won't use the 23/24/25 hour definition because that's even less well defined unless you're strictly talking about midnight to midnight calculations. If you do a 1:30 AM to 1:30 AM day, the rules would be non-intuitive (though you could certainly express the calculation in those terms).

    One way to get what you want is to do the calculation and then explicitly set the time... but be careful. You can't set the time if to an invalid time in your timezone.

      Thanks for that sample code -- I can see that I'll have to grapple with the OO interface, and code some special cases for when the target time doesn't exist.

      You're right about my definition of a day -- I'd forgotten about the missing hour in the spring.

      cheers

      Chris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://936546]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found