Would you consider letting the new() method optionally take a DateTime object?The awkward workaround is this: my $here = DateTime->new( time_zone => 'Australia/Melbourne', map { $_ => $rth->$_() } qw( year month day hour minute second ) );That should do what you want, which is create two datetime objects with the same _local_ time, and different time zones.
Then the above could be:
my $here = DateTime->new(datetime=>$rth, time_zone => 'Australia/Melbourne');
Which would take the localtime from $rth but make the timezone Aust/Melb.
This would mean:
my $here = DateTime->new(datetime=>$rth);
is the equivalent of
my $here = $rth->clone;
but allows us to move the timezone.
Extending further we could move other stuff too:
my $here = DateTime->new(datetime=>$rth, hour => 19);
which would set $here to a clone of $rth, but with the hour set to 19.
You ask me for a use for this? How about something that expires today at midnight? (23:59:60 or Tomorrow at 00:00:00 depending on your perspective, see Note 1)
Let's imagine our application has already set
$NOW = DateTime->now;
so all we need to do to get midnight tonight is:
$midnight = DateTime->new(datetime=>$NOW, hour=>23, minute=>59, second=>60);
rather than
$midnight = DateTime->new(year=>$NOW->year, month=>$NOW->month, day=>$NOW->day, hour=>23, minute=>59, second=>60);
Basically if there's a datetime parameter then we set all the values in the new object to be the same. Then we parse the rest of the parameters setting those we get.
(I'm not sure of current behavior is parameters are missing .. do we assume them to be now() or do we error? If we assume now() then consider the above example to be for 'tomorrow night at midnight')
NOTE 1:
Tonight at midnight is the equivalent of 23:59:60 which is MUCH easier that calculating tomorrow at 00:00:00 :) I know that leap-seconds will confuse this issue, but those that care *that* much can do it by:
$midnight = DateTime->new(datetime=>$NOW, day=>$NOW->day+1, hour=>0, minute=>0, second=>0);
NOTE 2:
The additions in note 1 assume we're allowing overflows to flow up: 13:59:75 becomes 14:00:15
Hope all this makes sense.
Cheers!
Rick Measham
--
--------------------------------------------------------
There are 10 kinds of people:
those that understand binary, and those that don't.
--------------------------------------------------------
The day Microsoft makes something that doesn't suck
is the day they start selling vacuum cleaners
--------------------------------------------------------
