FAQ
Hi!

Given a date and time zone, is it possible to obtain the first time of that
date?

For example. in America/Sao_Paulo, 2013/10/20 started at 01:00 rather than
the usual midnight.

I've also asked this on StackOverflow, where I posted a potentially
incorrect solution.
http://stackoverflow.com/q/20805667/589924

- Eric

Search Discussions

  • Zefram at Dec 27, 2013 at 6:48 pm

    Eric Brine wrote:
    Given a date and time zone, is it possible to obtain the first time of that
    date?
    Given standard APIs for time conversion, I'd perform a binary search
    among UT times, converting them to the zone's local time and looking
    for the threshold at which the local date changes. The result of the
    conversion at the threshold time includes a local time-of-day that should
    be the one you're after.

    This search approach assumes that all the UT times associated with a
    particular local date are contiguous, which I think is the case throughout
    the database, even in extreme cases. (The most extreme cases are zones
    such as America/Anchorage that crossed the IDL from a positive UT offset
    to negative, yielding a period of 48 consecutive hours associated with a
    single calendar day in local time. Though in the specific case of Alaska
    the single calendar day is conventionally described using different
    calendars for the two halves of that 48-hour period.)

    Another type of edge case also might matter, depending on what you
    mean by "the first time of that date". If you actually want the local
    time-of-day at the beginning of the calendar day, that's fine. But if
    you actually want the lowest local time-of-day that occurs at any time
    during the calendar day, you could in principle be misled by a zone that
    jumps forward at midnight and then back slightly later (say, forward an
    hour at local 00:00->01:00, then back an hour at local 01:30->00:30).
    I think this also doesn't occur anywhere in the database.

    -zefram
  • Eric Brine at Dec 31, 2013 at 3:17 am

    On Fri, Dec 27, 2013 at 1:48 PM, Zefram wrote:

    Eric Brine wrote:
    Given a date and time zone, is it possible to obtain the first time of that
    date?
    Given standard APIs for time conversion, I'd perform a binary search
    among UT times, converting them to the zone's local time and looking
    for the threshold at which the local date changes. The result of the
    conversion at the threshold time includes a local time-of-day that should
    be the one you're after.
    I posted a solution here:
    http://stackoverflow.com/a/20850899/589924

    I perform a binary search of the tz's spans, looking at the *local* times.

    If a span is found (i.e. if midnight does exist locally), I convert the
    local time to a UTC time using the offset of the earliest matching span.
    (The latest span is usually used.)

    If a span isn't found (i.e. if midnight does not exist locally), I use the
    UTC start time of the following span.

    *I made one assumption*: There is no dt to which one can add time to obtain
    a dt with an earlier date.

    Test:

    sub new_date {
         my $y = shift;
         my $m = shift;
         my $d = shift;
         return DateTime->new(
             year => $y, month => $m, day => $d,
             @_,
             hour => 0, minute => 0, second => 0, nanosecond => 0,
             time_zone => 'floating'
         );}

    {
         # No midnight.
         my $tz = DateTime::TimeZone->new( name => 'America/Sao_Paulo' );
         my $dt = day_start($tz, new_date(2013, 10, 20));
         print($dt->iso8601(), "\n"); # 2013-10-20T01:00:00
         $dt->subtract( seconds => 1 );
         print($dt->iso8601(), "\n"); # 2013-10-19T23:59:59}
    {
         # Two midnights.
         my $tz = DateTime::TimeZone->new( name => 'America/Havana' );
         my $dt = day_start($tz, new_date(2013, 11, 3));
         print($dt->iso8601(), "\n"); # 2013-11-03T00:00:00
         $dt->subtract( seconds => 1 );
         print($dt->iso8601(), "\n"); # 2013-11-02T23:59:59}

    I intend to write a brute force tester.



    Another type of edge case also might matter, depending on what you
    mean by "the first time of that date". If you actually want the local
    time-of-day at the beginning of the calendar day, that's fine.
    The dt from which nothing can be subtracted to obtain a dt with the same
    date.

    But if iyou actually want the lowest local time-of-day that occurs at any
    time
    during the calendar day, you could in principle be misled by a zone that
    jumps forward at midnight and then back slightly later (say, forward an
    hour at local 00:00->01:00, then back an hour at local 01:30->00:30).
    I think this also doesn't occur anywhere in the database.
    I'd want the first 01:00, and my solution would get it.

    - Eric
  • Zefram at Dec 31, 2013 at 4:24 am

    Eric Brine wrote:
    I posted a solution here:
    http://stackoverflow.com/a/20850899/589924
    That message states
    On the DateTime mailing list, Zefram suggested I do a binary search on
    the time zone data. The following is an implementation of that solution:
    which is not correct. I did not suggest searching among observance
    spans, as the code there does. I suggested searching among UT times.
    Please post a correction.
    I perform a binary search of the tz's spans, looking at the *local* times.
    This is a valid approach *if* you have access to the span data itself.
    The code you posted on stackoverflow uses the internals of the current
    DT:TZ implementation. The public API for DT:TZ objects doesn't support
    this. That's (in part) why I suggested an algorithm that only accesses
    zone data in the form of UT->local conversions.
    *I made one assumption*: There is no dt to which one can add time to obtain
    a dt with an earlier date.
    That's the same assumption I made, which I reckon is justified by the
    Olson database.

    -zefram
  • Rick Measham at Dec 27, 2013 at 10:54 pm
    Back in 2003 I wrote the beginnings of DateTime::Event::DST which you can see here: http://marc.info/?l=perl-datetime&m=106716694406817&w=2

    It didn't go far beyond that. After Flavio's comment it descended into chatter about which way the toilet flushes, and I've no idea if the code there would help. YMMV.

    Cheers!
    Rick Measham
    📱
    On 28 Dec 2013, at 5:29, Eric Brine wrote:

    Hi!

    Given a date and time zone, is it possible to obtain the first time of that date?

    For example. in America/Sao_Paulo, 2013/10/20 started at 01:00 rather than the usual midnight.

    I've also asked this on StackOverflow, where I posted a potentially incorrect solution.
    http://stackoverflow.com/q/20805667/589924

    - Eric


    Message protected for iSite by MailGuard: e-mail anti-virus, anti-spam and content filtering.
    http://www.mailguard.com.au


    Report this message as spam
    --
    Message protected for iSite by MailGuard: e-mail anti-virus, anti-spam and content filtering.http://www.mailguard.com.au

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdatetime @
categoriesperl
postedDec 27, '13 at 6:29p
activeDec 31, '13 at 4:24a
posts5
users3
websitemetacpan.org...

People

Translate

site design / logo © 2019 Grokbase