DateTime::Math -- provides common math operations on DateTime objects
Branch: master
Clone or download
Timothy Totten
Timothy Totten broke stuff
Latest commit 722b445 Apr 21, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/DateTime - multi for DateTime is now in Rakudo, so remove Apr 20, 2017
t Fix errors resulting from rakudo's internal implementation of DateTim… Mar 30, 2016
.gitignore
.travis.yml Add Travis-CI configuration Oct 28, 2016
META6.json broke stuff Apr 21, 2017
README.md

README.md

DateTime::Math

Description

Provides to-seconds(), from-seconds(), duration-from-to() and the ability to perform certain math operations on DateTime objects.

  use DateTime::Math;

  ## Given $dt1 and $dt2 are both DateTime objects.

  my $days = from-seconds($dt2 - $dt1, 'd');
  say "The events where $days days apart";

  my $target = $dt1 + to-seconds(1, 'M');
  say "One month after $dt1 would be $target";

  my $hours = duration-from-to(3, 'y', 'h');
  say "3 years contains $hours hours.";

The math functions allowed are:

  • Seconds = DateTime + DateTime
  • Seconds = DateTime - DateTime
  • DateTime = DateTime + Seconds
  • DateTime = DateTime - Seconds

And comparisons:

  • DateTime cmp DateTime
  • DateTime <=> DateTime
  • DateTime == DateTime
  • DateTime != DateTime
  • DateTime <= DateTime
  • DateTime < DateTime
  • DateTime >= DateTime
  • DateTime > DateTime

Note: Rakudo has since implemented internal DateTime comparisons, therefore DateTime::Math's comparisons have been removed.

Note that all of the math operations on DateTime objects are using the POSIX time, which is stored as seconds, so it does not support sub-second math at this time.

Author

Contributions by

License

Artistic License 2.0