NAME

Date::Tiny::Math - A date object with maths, with as little code as possible

SYNOPSIS

use Date::Tiny::Math;

# How many days have elapsed since 1 January 2000?
$date = Date::Tiny::Math->new(year => 2000, month => 1, day => 1);
$date2 = Date::Tiny::Math->new; # today
$elapsed_days = $date2 - $date;

$date3 = $date2 + 100;      # 100 days after today
$date2 -= 7;                # shift back by 7 days
++$date2;                   # increment by 1 day

print "earlier\n" if $date2 < $date3;

DESCRIPTION

Date::Tiny::Math is a subclass of Date::Tiny and adds the ability to do simple arithmetic with the dates, assuming those dates to be specified in the common, Gregorian, calendar -- even for dates preceding the introduction of the Gregorian calendar in the year 1582.

METHODS

The below list of methods is in addition to the methods provided by Date::Tiny.

new

$date = Date::Tiny::Math->new(year => 2000, month => 1, day => 1);
$date = Date::Tiny::Math->new(cjdn => 2451545);

Creates a new instance based on the specified calendar date (year, month, day) or Chronological Julian Day Number (CJDN). The CJDN is a count of days since 24 November −4713 (Gregorian). It can be positive, zero, or negative.

The Chronological Julian Day Number is distinct from the better known integer Julian Day Number (JDN) and fractional Julian Day (JD) in that the days don't begin at noon UTC but correspond to calendar days at the location of interest.

cjdn

$cjdn = $date->cjdn;  # returns 2451545 for 2000-01-01

Returns the Chronological Julian Day Number (CJDN) for the specified date object. The CJDN is the integer day count since 24 November −4713 (Gregorian). It can be positive, zero, or negative.

+, ++, +=

$date2 = $date1 + 17;         # get the date 17 days later
$date2 = 17 + $date1;         # same as previous
$date1 += 17;                 # modifies $date1
++$date1;                     # adds 1 day to $date1

Adds a number to a date. The number is rounded down to the nearest integer if needed.

Croaks if the non-date argument is not numerical.

-, --, -+

$date2 = $date1 - 17;         # get the date 17 days earlier
$date1 -= 17;                 # modifies $date1
$offset_in_days = $date2 - $date1; # difference between two dates
--$date1;                     # subtracts 1 day from $date1

# these croak:

$date2 = 17 - $date1;         # cannot subtract a date from a number
$date2 -= $date1;             # cannot modify LHS from date to scalar

Subtracts a number or a date from a date. The number is rounded down to the nearest integer as needed.

If both arguments are dates then their difference measured in days is returned.

Croaks if the non-date argument is not numerical, or if the left-hand side isn't a Date::Tiny::Math.

numerical comparison

$date2 == $date3;             # also < <= != <=> => >

Instances of Date::Tiny::Math have the same order as their CJDN values. Later dates compare as greater than earlier dates.

Croaks if not both arguments are instances of Date::Tiny::Math.

NOTE: Date::Tiny, the parent of the current module, does not provide numerical comparison. It provides stringwise equality (eq) and not-equality (ne) operators instead, which compare the stringified versions of the dates. The current module inherits those stringwise operators. It does not expand stringwise comparison by implementing the inequality operators (lt, le, ge, gt) because stringwise comparison does not define the same order as numerical comparison when negative year numbers are involved.

AUTHOR

Louis Strous, <lstrous at cpan.org>

BUGS

Please report any bugs or feature requests to bug-date-tiny-math at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Date-Tiny-Math. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Date::Tiny::Math

You can also look for information at:

LICENSE AND COPYRIGHT

This software is Copyright (c) 2025 by Louis Strous.

This is free software, licensed under:

The Artistic License 2.0 (GPL Compatible)