develooper Front page | perl.datetime | Postings from July 2017

Re: How to tell (in advance) if a date-time is ambiguous?

Thread Previous | Thread Next
July 10, 2017 23:09
Re: How to tell (in advance) if a date-time is ambiguous?
Message ID:
Binarus wrote:
>Using DateTime, is it possible to tell in advance if a certain date-time
>which is given in a certain locale will be ambiguous due to switching
>from DST to standard time?

That is tricky.  I don't think our APIs provide any way to do it.
Thinking about the facilities available a bit lower down, the way I'd
probably approach it is to look at the list of all the offsets ever used
in that timezone (not available through any API, but extractable from
the tzfile).  I'd compute for each the UT time that would be represented
by the specified local time with that offset, and then use the regular
API to convert that UT time to the local time in the specified timezone
(or equivalently just to look up the zone's offset for that UT time).
Look at which local times come out matching the specified local
time (which offsets match the candidate offset that we were trying).
If there's more than one match, that's an ambiguous local time.  If there
are no matches, it's a non-existent local time.

Something close to this can actually be done in the C time API.  You
don't get to ask what all the zone's offsets are, but in the local->UT
conversion you can specify whether DST is in effect.  Giving both states
of that flag gives you two UT times, which you can then convert back
to local to check whether they come out with the same DST flag state.
This will work for regular DST changes, but not for offset changes that
are unrelated to DST.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About