On Sat, Apr 23, 2016 at 8:55 AM, Eric Brine <ikeg...@adaelis.com> wrote:

> The page linked below has an implementation that finds the start of the
> day using the approach Zefram described. It should be trivial to change to
> to find the start of the month.
> http://stackoverflow.com/a/21000824/589924
> <http://stackoverflow.com/a/21000824/589924>
>
>
Thanks -- that looks good.

The problem I have (and perhaps the DateTime bug) is the ->truncate( to =>
'month' ), correct?

I need to find the current month in *the target time zone*, so does this
usage make sense?  Or is there an easier way?


my $now_in_amman = DateTime->now( time_zone => 'Asia/Amman' );

my $dt_first_day = DateTime->new(
    year    => $now_in_amman->year,
    month   => $now_in_amman->month,
    day     => 1,
    hour    => 12,  # Assume this exists
    minute  => 0,
    second  => 0,
);

my $dt = day_start( $dt_first_day->clone, 'Asia/Amman' );

Apr 1, 2016 1:00:00 AM +0300


And for the end time of the month (to the second):

$end_dt = day_start( $dt_first_day->clone->add( months => 1 ), 'Asia/Amman'
)->subtract( seconds => 1 );

Apr 30, 2016 11:59:59 PM +0300


I was thinking of an implementation that assumed DST change happened at an
hour boundary and simply try incrementing hours until no more exceptions.
 But, I suspect there's places where DST might change at a 1/2 our mark,
too -- or at any random time during the day.





-- 
Bill Moseley
mose...@hank.org

Reply via email to