Code Monkey home page Code Monkey logo

datetime-moonpig's People

Contributors

mjdominus avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

datetime-moonpig's Issues

Removing the mutators may leave a functionality gap

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.

->today constructor fails

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.

DateTime::Moonpig overloaded +/- ignores nanoseconds

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.