Determining the time at which a date starts

2019-03-23 02:11发布

Say I want to create a daily planner, and I want to divide the day into 15 minute chunks.

Easy, right? Just start at midnight, and... Wrong! In America/Sao_Paulo, one day each year starts at 01:00 because of Daylight Saving Time changes.

Given a time zone and a date, how does one find the epoch time at which the day starts?

My first thought was to use the following, but it assumes each day has a 23:59. That's probably no better of an assumption than assuming each day has a midnight.

perl -MDateTime -E'
   say
      DateTime->new( year => 2013, month => 10, day => 20 )
      ->subtract( days => 1 )
      ->set( hour => 23, minute => 59 )
      ->set_time_zone("America/Sao_Paulo")
      ->add( minutes => 1 )
      ->strftime("%H:%M");
'
01:00

Is there a more robust or more direct alternative?

7条回答
Viruses.
2楼-- · 2019-03-23 03:12

You'd think this is something that needs to be done commonly! I suspect there's a lot of buggy code out there...

Here's a solution that's coded with the intention of trying to get it incorporated into DateTime.

use strict;
use warnings;


use DateTime           qw( );
use DateTime::TimeZone qw( );


# Assumption:
#    There is no dt to which one can add time
#    to obtain a dt with an earlier date.

sub day_start {
    my $tz = shift;
    my $dt = shift;

    my $local_rd_days = ( $dt->local_rd_values() )[0];
    my $seconds = $local_rd_days * 24*60*60;

    my $min_idx;
    if ( $seconds < $tz->max_span->[DateTime::TimeZone::LOCAL_END] ) {
        $min_idx = 0;
    } else {
        $min_idx = @{ $tz->{spans} };
        $tz->_generate_spans_until_match( $dt->utc_year()+1, $seconds, 'local' );
    }

    my $max_idx = $#{ $tz->{spans} };

    my $utc_rd_days;
    my $utc_rd_secs;
    while (1) {
        my $current_idx = int( ( $min_idx + $max_idx )/2 );
        my $current = $tz->{spans}[$current_idx];

        if ( $seconds < $current->[DateTime::TimeZone::LOCAL_START] ) {
            $max_idx = $current_idx - 1;
        }
        elsif ( $seconds >= $current->[DateTime::TimeZone::LOCAL_END] ) {
            $min_idx = $current_idx + 1;
        }
        else {
            my $offset = $current->[DateTime::TimeZone::OFFSET];

            # In case of overlaps, always prefer earlier span.
            if ($current->[DateTime::TimeZone::IS_DST] && $current_idx) {
                my $prev = $tz->{spans}[$current_idx-1];
                $offset = $prev->[DateTime::TimeZone::OFFSET]
                    if $seconds >= $prev->[DateTime::TimeZone::LOCAL_START]
                    && $seconds < $prev->[DateTime::TimeZone::LOCAL_END];
            }

            $utc_rd_days = $local_rd_days;
            $utc_rd_secs = -$offset;
            DateTime->_normalize_tai_seconds($utc_rd_days, $utc_rd_secs);
            last;
        }

        if ($min_idx > $max_idx) {
            $current_idx = $min_idx;
            $current = $tz->{spans}[$current_idx];

            if (int( $current->[DateTime::TimeZone::LOCAL_START] / (24*60*60) ) != $local_rd_days) {
                my $err = 'Invalid local time for date';
                $err .= " in time zone: " . $tz->name;
                $err .= "\n";
                die $err;
            }

            $utc_rd_secs = $current->[DateTime::TimeZone::UTC_START] % (24*60*60);
            $utc_rd_days = int( $current->[DateTime::TimeZone::UTC_START] / (24*60*60) );
            last;
        }
    }

    my ($year, $month, $day) = DateTime->_rd2ymd($utc_rd_days);
    my ($hour, $minute, $second) = DateTime->_seconds_as_components($utc_rd_secs);

    return
       $dt
         ->_new_from_self(
             year      => $year,
             month     => $month,
             day       => $day,
             hour      => $hour,
             minute    => $minute,
             second    => $second,
             time_zone => 'UTC',
         )
         ->set_time_zone($tz);
}

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
}

A practical example,

sub today_as_floating {
    return
        DateTime
            ->now( @_ )
            ->set_time_zone('floating')
            ->truncate( to => 'day' );
}

{
    my $tz = DateTime::TimeZone->new( name => 'local' );
    my $dt = today_as_floating( time_zone => $tz );
    $dt = day_start($tz, $dt);
    print($dt->iso8601(), "\n");
}
查看更多
登录 后发表回答