mjdominus / datetime-moonpig Goto Github PK
View Code? Open in Web Editor NEWA sanity-preserving wrapper around DateTime, taken from the Moonpig project
A sanity-preserving wrapper around DateTime, taken from the Moonpig project
There are multiple ways to do this with Dist::Zilla so rather than sent a pull request I'm just creating a ticket.
See here for more info:
http://perlmaven.com/how-to-add-link-to-version-control-system-of-a-cpan-distributions
Suppose you have a DateTime::Moonpig
and you want another one that represents 3PM on the same day. With DateTime
you could do:
$dt->clone->set(hour => 3, minute => 0, second => 0)
but in DateTime::Moonpig
we have removed set
. If there isn't some other way to do this, we may need something like
$dt->clone(hour => 3, minute => 0, second => 0)
to fill this gap.
Using the ->today
constructor fails, with a message saying not to use mutators.
This is surprising, since ->today
is a constructor, not a mutator. That it happens to use a mutator internally is an implementation detail.
$ perl -MDateTime::Moonpig -E 'say DateTime::Moonpig->today'
Do not mutate DateTime objects! (http://rjbs.manxome.org/rubric/entry/1929) at /usr/local/share/perl/5.18.2/DateTime/Moonpig.pm line 85.
DateTime::Moonpig::__ANON__('DateTime::Moonpig=HASH(0x2f535d8)', 'to', 'day') called at /usr/lib/perl5/DateTime.pm line 545
DateTime::today('DateTime::Moonpig') called at -e line 1
When only working with dates, not times, I couldn't find a convenient way to get a DateTime::MoonPig
object which represents the start of today. The suggested extension to ->clone
in Issue #1 would make this straightforward.
For now the best I've come up with is:
$ perl -MDateTime::Moonpig -E 'say DateTime::Moonpig->new_datetime(DateTime->today)'
2016-02-10T00:00:00
But it's unfortunate to have to use the DateTime
class directly.
See this CPAN tester report. However, I have confirmed that the tests pass on ortolan with DateTime
0.74, 0.75, 0.76, 0.77, and 0.78, so it isn't merely that DateTime::Moonpig
doesn't work with old DateTime
.
The following program demonstrates the issue.
#!/usr/bin/env perl
use 5.24.0;
use warnings;
use DateTime;
use DateTime::Moonpig;
# roughly 2017-02-23T10:28:10
my $time = 1487845690.37924;
my $now = DateTime->from_epoch( epoch => $time, time_zone => 'UTC' );
my $moonpig = DateTime::Moonpig->new_datetime($now);
if ( $now != $moonpig ) { # sanity checking
die "Datetime $now is not equal to moonpig $moonpig";
}
foreach my $seconds ( 1, 1.2, 1.5, 1.75, 5 ) {
my $datetime = $now->clone->add( seconds => $seconds );
my $moonpig2 = $moonpig + $seconds;
# sanity checking
unless ( $datetime == $datetime ) {
die "Datetime $datetime is not equal to itself";
}
unless ( $moonpig2 == $moonpig2 ) {
die "Moonpig $moonpig2 is not equal to itself";
}
say $datetime > $moonpig2 ? "datetime is greater (added $seconds seconds)"
: $moonpig2 > $datetime ? "moonpig is greater (added $seconds seconds)"
: "equal (added $seconds seconds)";
printf "DateTime nanoseconds: %d versus Moonpig nanosecond: %d\n" =>
$datetime->nanosecond, $moonpig2->nanosecond;
}
For the above, all of those dates in the loop should be equal. Instead, the output is:
datetime is greater (added 1 seconds)
DateTime nanoseconds: 379240000 versus Moonpig nanosecond: 0
datetime is greater (added 1.2 seconds)
DateTime nanoseconds: 379240000 versus Moonpig nanosecond: 200000000
moonpig is greater (added 1.5 seconds)
DateTime nanoseconds: 379240000 versus Moonpig nanosecond: 500000000
moonpig is greater (added 1.75 seconds)
DateTime nanoseconds: 379240000 versus Moonpig nanosecond: 750000000
datetime is greater (added 5 seconds)
DateTime nanoseconds: 379240000 versus Moonpig nanosecond: 0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.