Module:Date/testcases
Appearance
| This is the test cases page for the module Module:Date. Results of the test cases. |
local date = require('Module:Date')
local ScribuntoUnit = require('Module:ScribuntoUnit')
local suite = ScribuntoUnit.new()
-- Unit tests for [[Module:Date]]. Click talk page to run tests.
local Date = date._Date
local NBSP = ' '
local MINUS = '−' -- U+2212
-- Return tostring(Date(...)), or nil if the date is invalid.
local function text(...)
local d = Date(...)
return d and tostring(d) or nil
end
-- Check a table of { {arguments for Date}, expected text or nil } cases.
local function check_text(self, cases)
for _, case in ipairs(cases) do
local input, expected = case[1], case[2]
local ok, result = pcall(text, unpack(input))
if not ok then
result = 'ERROR: ' .. tostring(result)
end
local args = {}
for i, v in ipairs(input) do
args[i] = type(v) == 'table' and '{...}' or tostring(v)
end
self:assertEquals(expected, result, 'Date(' .. table.concat(args, ', ') .. ')')
end
end
-- Return the main fields of a diff as a string such as '1y 2m 3d 4:05:06 -'
-- where '-' means isnegative and '0' means iszero.
local function diff_text(diff)
return string.format('%dy %dm %dd %d:%02d:%02d%s',
diff.years, diff.months, diff.days, diff.hours, diff.minutes, diff.seconds,
diff.isnegative and ' -' or (diff.iszero and ' 0' or ''))
end
--------------------------------------------------------------------------------
-- Construction
--------------------------------------------------------------------------------
function suite:test_construction_numbers()
check_text(self, {
{ { 1990, 5, 10 }, '10 May 1990' },
{ { 1990, 5, 10, 'julian' }, '10 May 1990' },
{ { 1990, 5, 10, 'Gregorian' }, '10 May 1990' },
{ { 2020, 4, 10, 12, 20, 22 }, '12:20:22 10 April 2020' },
{ { 2020, 4, 10, 12, 20 }, '12:20 10 April 2020' },
{ { 2020, 4, 10, 0, 0, 0 }, '00:00 10 April 2020' },
{ { '2020', '4', '10' }, '10 April 2020' }, -- template parameters are strings
{ { ' 2020 ', ' 4 ', ' 10 ' }, '10 April 2020' }, -- and may have spaces
{ { 2020, 'Apr', 10 }, '10 April 2020' }, -- month name accepted as second number
{ { 2020, 'april', 10 }, '10 April 2020' },
{ { 2020, '', 4, 10 }, '10 April 2020' }, -- empty arguments are ignored
{ { 2000, 2, 29 }, '29 February 2000' }, -- 400-year leap year
{ { 1, 1, 1 }, '1 January 1' },
{ { 9999, 12, 31 }, '31 December 9999' },
{ { 0, 1, 1 }, '1 January 1' .. NBSP .. 'BC' }, -- year 0 is 1 BC
{ { -43, 3, 15 }, '15 March 44' .. NBSP .. 'BC' },
{ { -9999, 1, 1 }, '1 January 10000' .. NBSP .. 'BC' },
})
end
function suite:test_construction_invalid()
check_text(self, {
{ { 2021, 2, 29 }, nil }, -- not a leap year
{ { 1900, 2, 29 }, nil }, -- not a Gregorian leap year
{ { 2100, 2, 29 }, nil }, -- century non-leap
{ { 2021, 4, 31 }, nil },
{ { 2021, 13, 1 }, nil },
{ { 2021, 0, 1 }, nil },
{ { 2021, 1, 0 }, nil },
{ { 2021, 1, 32 }, nil },
{ { 2021, 1, 1, 24, 0, 0 }, nil },
{ { 2021, 1, 1, 12, 60, 0 }, nil },
{ { 2021, 1, 1, 12, 0, 60 }, nil },
{ { 10000, 1, 1 }, nil },
{ { -10000, 12, 31 }, nil },
{ { 2021, 1, 1.5 }, '1 January 2021' }, -- FIXME? a fractional day is accepted
{ { { year = 2026, month = 2, day = 30 } }, nil },
{ { { year = 2026, month = 2 } }, nil }, -- missing day without 'partial'
{ { 2021, 'Foo', 1 }, nil },
{ { 'bogus' }, nil },
{ {}, nil },
})
end
function suite:test_construction_julian()
check_text(self, {
{ { 1900, 2, 29, 'julian' }, '29 February 1900' }, -- Julian leap year
{ { 1700, 2, 29, 'julian' }, '29 February 1700' },
{ { 1700, 2, 29 }, nil },
})
-- The same day in both calendars.
self:assertTrue(Date(1582, 10, 5, 'julian') == Date(1582, 10, 15), 'Gregorian reform')
self:assertTrue(Date(1582, 10, 4, 'julian') < Date(1582, 10, 15), 'day before Gregorian reform')
self:assertTrue(Date(1752, 9, 2, 'julian') + 1 == Date(1752, 9, 14), 'British reform')
self:assertTrue(Date(-4712, 1, 1, 'julian') == Date(-4713, 11, 24, 'gregorian'), 'JD 0')
self:assertEquals('Julian', Date(1900, 1, 1, 'JULIAN').calendar)
self:assertEquals('Gregorian', Date(1900, 1, 1).calendar)
end
function suite:test_construction_juliandate()
check_text(self, {
{ { 'juliandate', 2451545 }, '1 January 2000' },
{ { 'juliandate', 2451544.5 }, '00:00 1 January 2000' }, -- fraction gives a time
{ { 'juliandate', '2451545.0' }, '12:00 1 January 2000' }, -- "." in text gives a time
{ { 'juliandate', 2451545.25 }, '18:00 1 January 2000' },
{ { 'juliandate', 0 }, '24 November 4714' .. NBSP .. 'BC' },
{ { 'juliandate', 0, 'julian' }, '1 January 4713' .. NBSP .. 'BC' },
{ { 'juliandate', 1e9 }, nil },
})
end
function suite:test_construction_copy()
local d = Date(2020, 4, 10, 12, 20, 22)
check_text(self, {
{ { d }, '12:20:22 10 April 2020' },
-- FIXME? Overriding a field from a table resets the hour, but not the minute or second.
{ { d, { year = 2021 } }, '00:20:22 10 April 2021' },
{ { d, { day = 31 } }, nil }, -- 31 April
{ { { year = 2020, month = 2, day = 29 } }, '29 February 2020' },
{ { { year = 2020, month = 2, day = 29, hour = 6 } }, '06:00 29 February 2020' },
})
self:assertFalse(Date(d) == nil and true or false, 'copy is valid')
end
function suite:test_construction_fix()
-- 'fix' normalizes out of range values like PHP mktime.
check_text(self, {
{ { 'fix', 2021, 2, 29 }, '1 March 2021' },
{ { 'fix', 2021, 1, 0 }, '31 December 2020' },
{ { 'fix', 2021, 1, -1 }, '30 December 2020' },
{ { 'fix', 2021, 13, 1 }, '1 January 2022' },
{ { 'fix', 2021, 0, 1 }, '1 December 2020' },
{ { 'fix', 2021, 1, 32 }, '1 February 2021' },
{ { 'fix', 2021, 1, 1, 24, 0, 0 }, '00:00 2 January 2021' },
{ { 'fix', 2021, 1, 1, 12, 0, 0 }, '12:00 1 January 2021' },
})
end
function suite:test_construction_partial()
check_text(self, {
{ { 'partial', 2001 }, '2001' },
{ { 'partial', 2001, 4 }, 'April 2001' },
{ { 'partial', 'April 2001' }, 'April 2001' },
{ { 'partial', '2001' }, '2001' },
{ { 'partial', '44' .. NBSP .. 'BC' }, '44' .. NBSP .. 'BC' },
{ { 'partial', 2001, 4, 1 }, '1 April 2001' }, -- a full date
{ { 'April 2001' }, nil }, -- partial must be requested
{ { '2001' }, nil },
})
local d = Date('partial', 2001, 4)
self:assertEquals(1, d.partial.first.day, 'partial.first')
self:assertEquals(30, d.partial.last.day, 'partial.last')
self:assertEquals('April', d.monthname, 'partial month has monthname')
self:assertEquals(30, d.monthdays, 'partial month has monthdays')
self:assertEquals(false, d.isleapyear, 'partial has isleapyear')
self:assertEquals(nil, d.day, 'partial has no day')
self:assertEquals(nil, d.dayname, 'partial has no dayname')
self:assertEquals(nil, d.jd, 'partial has no jd')
local leap_feb = Date('partial', 2020, 2)
self:assertEquals(29, leap_feb.partial.last.day, 'partial leap Feb last')
local nonleap_feb = Date('partial', 2021, 2)
self:assertEquals(28, nonleap_feb.partial.last.day, 'partial non-leap Feb last')
local yr = Date('partial', 2020)
self:assertEquals('1 January 2020', tostring(yr.partial.first), 'partial year first')
self:assertEquals('31 December 2020', tostring(yr.partial.last), 'partial year last')
self:assertEquals(nil, yr.monthname, 'partial year has no monthname')
self:assertEquals(nil, yr.monthdays, 'partial year has no monthdays')
self:assertEquals(true, yr.isleapyear, 'partial leap year')
end
--------------------------------------------------------------------------------
-- Parsing text
--------------------------------------------------------------------------------
function suite:test_parse_formats()
check_text(self, {
{ { '1 April 1995' }, '1 April 1995' },
{ { 'April 1, 1995' }, '1 April 1995' },
{ { 'April 1 1995' }, '1 April 1995' },
{ { '1995-04-01' }, '1 April 1995' },
{ { '1995-4-1' }, '1 April 1995' },
{ { '1995-Apr-01' }, '1 April 1995' },
{ { '2000-02-29' }, '29 February 2000' },
{ { '1st April 1995' }, '1 April 1995' },
{ { 'April 2nd, 1995' }, '2 April 1995' },
{ { '23rd April 1995' }, '23 April 1995' },
{ { 'Apr. 1, 1995' }, '1 April 1995' },
{ { 'Sept 1 1995' }, '1 September 1995' },
{ { 'sep 1 1995' }, '1 September 1995' },
{ { '1 April 1995' }, '1 April 1995' },
{ { '1 April 1995', 'julian' }, '1 April 1995' },
{ { '5 April 2012 AD', 'julian' }, '5 April 2012' .. NBSP .. 'AD' },
{ { '15 March 44' .. NBSP .. 'BC' }, '15 March 44' .. NBSP .. 'BC' },
{ { '15 March 44 BCE' }, '15 March 44' .. NBSP .. 'BCE' },
{ { '15 March 44 B.C.' }, '15 March 44' .. NBSP .. 'B.C.' },
{ { 'AD 5 April 2012' }, '5 April 2012' .. NBSP .. 'AD' }, -- era in a peculiar place
})
end
function suite:test_parse_time()
check_text(self, {
{ { '04:30:59 1 April 1995', 'julian' }, '04:30:59 1 April 1995' },
{ { '04:30 1 April 1995' }, '04:30 1 April 1995' },
{ { '1 April 1995 04:30' }, '04:30 1 April 1995' },
{ { '4:30 pm 1 April 1995' }, '4:30' .. NBSP .. 'pm 1 April 1995' },
{ { '4:30 PM 1 April 1995' }, '4:30' .. NBSP .. 'PM 1 April 1995' },
{ { '4:30 p.m. 1 April 1995' }, '4:30' .. NBSP .. 'p.m. 1 April 1995' },
{ { '12:00 am 1 April 1995' }, '12:00' .. NBSP .. 'am 1 April 1995' },
{ { '12:00 pm 1 April 1995' }, '12:00' .. NBSP .. 'pm 1 April 1995' },
{ { '13:00 pm 1 April 1995' }, '1:00' .. NBSP .. 'pm 1 April 1995' },
{ { '13:00 am 1 April 1995' }, nil },
{ { '0:00 am 1 April 1995' }, nil },
{ { '24:00 1 April 1995' }, nil },
{ { '04:60 1 April 1995' }, nil },
{ { '04:30:5 1 April 1995' }, nil },
{ { '04:30 05:30 1 April 1995' }, nil },
})
self:assertEquals(16, Date('4:30 pm 1 April 1995').hour, 'pm hour')
self:assertEquals(0, Date('12:00 am 1 April 1995').hour, '12 am hour')
self:assertEquals(12, Date('12:00 pm 1 April 1995').hour, '12 pm hour')
end
function suite:test_parse_invalid()
-- Module:Date only accepts unambiguous dates.
check_text(self, {
{ { '1/2/2026' }, nil },
{ { '01/02/2026' }, nil },
{ { '2026/01/02' }, nil },
{ { '1.2.2026' }, nil },
{ { '2026-1-2' }, '2 January 2026' }, -- unambiguous because the year has four digits
{ { '26-01-02' }, nil },
{ { '0000-06-15' }, nil }, -- year 0 rejected in ISO text
{ { '2026-02-30' }, nil },
{ { '2100-02-29' }, nil }, -- century non-leap
{ { '31 April 2020' }, nil },
{ { '29 February 2021' }, nil },
{ { '1 April 1995 1996' }, nil },
{ { '1 April' }, nil },
{ { 'April 1995' }, nil },
{ { '32 April 1995' }, nil },
{ { '1 Foo 1995' }, nil },
{ { 'foo' }, nil },
{ { '0 April 1995' }, nil },
{ { '1 April 0' }, nil }, -- no year 0 in text
{ { '1 April 10000' }, nil },
{ { '1 April 1995 AD BC' }, nil },
})
end
function suite:test_parse_wikidata()
check_text(self, {
{ { '+2016-06-21T00:00:00Z' }, '21 June 2016' },
{ { '+2016-06-21T14:30:00Z' }, '14:30 21 June 2016' },
{ { 'partial', '+1952-00-00T00:00:00Z' }, '1952' },
{ { 'partial', '+1952-06-00T00:00:00Z' }, 'June 1952' },
-- FIXME? Wikidata -0044 means 44 BCE, but it is read as astronomical year -44 (45 BCE).
{ { '-0044-03-15T00:00:00Z' }, '15 March 45' .. NBSP .. 'BCE' },
{ { '+2016-06-21Z' }, nil },
})
end
function suite:test_parse_format_detection()
local cases = {
{ '1 April 1995', 'dmy' },
{ 'April 1, 1995', 'mdy' },
{ '1995-04-01', 'ymd' },
}
for _, case in ipairs(cases) do
self:assertEquals(case[2], Date(case[1]).format, case[1])
end
self:assertEquals('dmy', Date(1995, 4, 1).format, 'numbers')
end
--------------------------------------------------------------------------------
-- Properties
--------------------------------------------------------------------------------
function suite:test_properties()
local d = Date(2016, 2, 29, 18, 30, 15)
local expected = {
year = 2016, month = 2, day = 29, hour = 18, minute = 30, second = 15,
calendar = 'Gregorian', hastime = true, partial = false,
dow = 1, dayofweek = 1, dowiso = 1, dayofweekiso = 1,
dayabbr = 'Mon', dayname = 'Monday',
monthabbr = 'Feb', monthname = 'February', monthdays = 29,
dayofyear = 60, isleapyear = true, gsd = 736023,
jdnoon = 2457448,
}
for k, v in pairs(expected) do
self:assertEquals(v, d[k], k)
end
self:assertWithinDelta(2457448.2710069, d.jd, 1e-6, 'jd')
self:assertWithinDelta(2457448.2710069, d.juliandate, 1e-6, 'juliandate')
self:assertWithinDelta(2457448.2710069, d.jdz, 1e-6, 'jdz with time')
self:assertEquals(2457448, Date(2016, 2, 29).jd, 'jd without time is noon')
self:assertEquals(2457447.5, Date(2016, 2, 29).jdz, 'jdz without time is midnight')
self:assertEquals(0, Date(2016, 2, 28).dowiso % 7, 'Sunday is 7 in ISO')
self:assertEquals(7, Date(2016, 2, 28).dowiso, 'Sunday ISO')
self:assertEquals(0, Date(2016, 2, 28).dow, 'Sunday')
self:assertEquals(366, Date(2016, 12, 31).dayofyear, 'last day of leap year')
self:assertEquals(1, Date(1, 1, 1).gsd, 'gsd of 1 January 1')
end
function suite:test_leap_years()
local cases = {
{ 2000, true }, { 1900, false }, { 2004, true }, { 2001, false },
{ 2100, false }, { 2400, true }, { 0, true }, { -4, true }, { -1, false },
}
for _, case in ipairs(cases) do
self:assertEquals(case[2], Date(case[1], 1, 1).isleapyear, 'Gregorian ' .. case[1])
end
self:assertTrue(Date(1900, 1, 1, 'julian').isleapyear, 'Julian 1900')
self:assertEquals(29, date._days_in_month(2000, 2), '_days_in_month 2000')
self:assertEquals(28, date._days_in_month(1900, 2), '_days_in_month 1900')
self:assertEquals(29, date._days_in_month(1900, 2, 'Julian'), '_days_in_month Julian 1900')
self:assertEquals(31, date._days_in_month(2001, 12), '_days_in_month December')
end
function suite:test_read_only()
local d = Date(2020, 1, 1)
self:assertThrows(function () d.year = 2021 end, 'date.year is read-only')
end
--------------------------------------------------------------------------------
-- Output
--------------------------------------------------------------------------------
function suite:test_text_standard_formats()
local d = Date(2016, 4, 1, 14, 30, 5)
local cases = {
{ nil, '14:30:05 1 April 2016' },
{ 'dmy', '14:30 1 April 2016' },
{ 'mdy', '14:30 April 1, 2016' },
{ 'ymd', '14:30 2016-04-01' },
{ 'hms dmy', '14:30:05 1 April 2016' },
{ 'hm mdy', '14:30 April 1, 2016' },
{ 'dmy hm', '1 April 2016 14:30' },
{ 'ymd hms', '2016-04-01 14:30:05' },
{ 'xyz', 'xyz' }, -- bad format is returned unchanged
}
for _, case in ipairs(cases) do
self:assertEquals(case[2], d:text(case[1]), tostring(case[1]))
end
self:assertEquals('1 April 2016', Date(2016, 4, 1):text(), 'no time')
self:assertEquals('April 1, 2016', Date(2016, 4, 1):text('mdy'), 'mdy no time')
self:assertEquals('2:30' .. NBSP .. 'pm 1 April 2016', d:text('dmy', 'am:pm'), 'am option')
self:assertEquals('2:30' .. NBSP .. 'P.M. 1 April 2016', d:text('dmy', 'am=P.M.'), 'P.M. option')
end
function suite:test_text_strftime()
local d = Date(2016, 4, 3, 9, 5, 7)
local cases = {
{ '%Y-%m-%d', '2016-04-03' },
{ '%-Y-%-m-%-d', '2016-4-3' },
{ '%A %a', 'Sunday Sun' },
{ '%B %b', 'April Apr' },
{ '%H:%M:%S', '09:05:07' },
{ '%-H:%-M:%-S', '9:5:7' },
{ '%I %-I %p', '09 9' .. NBSP .. 'am' },
{ '%j %-j', '094 94' },
{ '%u %w', '7 0' },
{ '%%Y', '%Y' },
{ '%{dayname} %{monthdays} %{isleapyear}', 'Sunday 30 1' },
{ '%{bogus}x', '%{bogus}x' },
{ '%x', '3 April 2016' },
{ '%X', '9:05' .. NBSP .. 'am' },
{ '%c', '9:05' .. NBSP .. 'am 3 April 2016' },
}
for _, case in ipairs(cases) do
self:assertEquals(case[2], d:text(case[1]), case[1])
end
local pm = Date(2016, 4, 3, 0, 0, 0)
self:assertEquals('12' .. NBSP .. 'am', pm:text('%-I %p'), 'midnight')
self:assertEquals('12' .. NBSP .. 'pm', Date(2016, 4, 3, 12, 0, 0):text('%-I %p'), 'noon')
end
function suite:test_text_era()
local bc = Date(-43, 3, 15)
local ad = Date(2016, 4, 1)
local cases = {
{ bc, nil, '15 March 44' .. NBSP .. 'BC' },
{ bc, 'era:BCE', '15 March 44' .. NBSP .. 'BCE' },
{ bc, 'era:B.C.E.', '15 March 44' .. NBSP .. 'B.C.E.' },
{ bc, 'era:AD', '15 March 44' .. NBSP .. 'BC' },
{ bc, 'era:CE', '15 March 44' .. NBSP .. 'BCE' },
{ bc, 'era:BCMINUS', '15 March 44' .. NBSP .. 'BC' },
{ ad, 'era:AD', '1 April 2016' .. NBSP .. 'AD' },
{ ad, 'era:A.D.', '1 April 2016' .. NBSP .. 'A.D.' },
{ ad, 'era:CE', '1 April 2016' .. NBSP .. 'CE' },
{ ad, 'era:BC', '1 April 2016' },
}
for i, case in ipairs(cases) do
self:assertEquals(case[3], case[1]:text('dmy', case[2]), 'era ' .. i)
end
self:assertEquals(MINUS .. '0043-03-15', bc:text('%Y-%m-%d', 'era:BCMINUS'), 'BCMINUS ymd')
self:assertEquals('-0043-03-15', bc:text('%Y-%m-%d', 'era:BCNEGATIVE'), 'BCNEGATIVE ymd')
self:assertEquals('0044-03-15', bc:text('%Y-%m-%d'), 'default ymd')
self:assertEquals('0001-01-01', Date(0, 1, 1):text('%Y-%m-%d'), 'year 0 is 1' .. NBSP .. 'BC')
end
function suite:test_text_partial()
local y, ym = Date('partial', 2001), Date('partial', 2001, 4)
self:assertEquals('2001', y:text(), 'y default')
self:assertEquals('April 2001', ym:text(), 'ym default')
self:assertEquals('2001-04', ym:text('ymd'), 'ym ymd')
self:assertEquals('April 2001', ym:text('mdy'), 'ym mdy')
self:assertEquals('2001', ym:text('y'), 'ym y')
self:assertEquals('hm', ym:text('hm'), 'ym bad format')
end
function suite:test_concat_and_tostring()
local d = Date(2016, 4, 1)
self:assertEquals('1 April 2016', tostring(d))
self:assertEquals('x 1 April 2016', 'x ' .. d)
self:assertEquals('1 April 2016 x', d .. ' x')
end
--------------------------------------------------------------------------------
-- Comparison
--------------------------------------------------------------------------------
function suite:test_comparison()
local a, b = Date(2016, 1, 1), Date(2016, 1, 2)
self:assertTrue(a < b, 'a < b')
self:assertTrue(a <= b, 'a <= b')
self:assertFalse(a > b, 'a > b')
self:assertFalse(a == b, 'a == b')
self:assertTrue(a == Date(2016, 1, 1), 'equal dates')
self:assertTrue(a ~= b, 'a ~= b')
self:assertTrue(Date('1 Jan 2016') < Date('06:00 1 Jan 2016'), 'no time is midnight')
self:assertTrue(Date(2016, 1, 1) == Date(2016, 1, 1, 0, 0, 0), 'no time equals 00:00')
self:assertTrue(Date(-1, 1, 1) < Date(1, 1, 1), 'BC')
end
function suite:test_comparison_partial()
self:assertTrue(Date('partial', 2001) == Date('partial', 2001), 'same year')
self:assertFalse(Date('partial', 2001) == Date('partial', 2001, 4), 'year vs month')
self:assertTrue(Date('partial', 2001) < Date('partial', 2001, 4), 'year starts first')
self:assertTrue(Date('partial', 2001, 4) < Date(2001, 4, 2), 'partial vs full')
self:assertFalse(Date('partial', 2001, 4) < Date(2001, 4, 1), 'partial vs first day')
self:assertFalse(Date('partial', 2001) < Date(2001, 1, 1), 'partial year vs Jan 1')
self:assertTrue(Date('partial', 2001) < Date(2001, 1, 2), 'partial year vs Jan 2')
-- FIXME? Equality on partial ignores day, so partial == full and partial < full are both true.
self:assertTrue(Date('partial', 2001, 4) == Date(2001, 4, 1), 'partial month == first day')
self:assertTrue(Date('partial', 2001, 4) == Date(2001, 4, 15), 'partial month == mid-month')
self:assertTrue(Date('partial', 2001, 4) < Date(2001, 4, 15), 'partial month < mid-month')
self:assertFalse(Date('partial', 2001) == Date(2001, 4, 1), 'partial year ~= full')
-- FIXME? Cross-calendar ordering with a partial always puts Julian before Gregorian regardless of year.
self:assertTrue(Date('partial', 2001, 'julian') < Date(1500, 1, 1), 'partial Julian < Gregorian')
self:assertFalse(Date('partial', 1500) < Date(2001, 1, 1, 'julian'), 'partial Gregorian not < Julian')
self:assertFalse(Date('partial', 2001, 'julian') == Date('partial', 2001), 'calendars differ')
end
--------------------------------------------------------------------------------
-- Arithmetic
--------------------------------------------------------------------------------
function suite:test_add_subtract()
local d = Date(2015, 1, 31)
local cases = {
{ d + 0, '31 January 2015' },
{ d - 0, '31 January 2015' },
{ d + 1, '1 February 2015' },
{ 1 + d, '1 February 2015' },
{ d - 31, '31 December 2014' },
{ d + 365, '31 January 2016' },
{ d + '0d', '31 January 2015' },
{ d + '0m', '31 January 2015' },
{ d + '0y', '31 January 2015' },
{ d + '+2d', '2 February 2015' },
{ d + '1m', '28 February 2015' }, -- clamped to end of month
{ Date(2016, 1, 31) + '1m', '29 February 2016' },
{ Date(1900, 1, 31, 'julian') + '1m', '29 February 1900' }, -- Julian leap year
{ Date(1900, 1, 31) + '1m', '28 February 1900' }, -- Gregorian non-leap
{ d + '2m', '31 March 2015' },
{ d + '13 months', '29 February 2016' },
{ d - '1m', '31 December 2014' },
{ d + '-1m', '31 December 2014' },
{ d - '-1m', '28 February 2015' }, -- double negation
{ d - '-1d', '1 February 2015' },
{ Date(2016, 2, 29) + '1y', '28 February 2017' },
{ Date(2016, 2, 29) + '4y', '29 February 2020' },
{ Date(2016, 2, 29) - '1 year', '28 February 2015' },
{ d + '2w', '14 February 2015' },
{ d + '3d', '3 February 2015' },
{ d + '1 days', '1 February 2015' },
{ d + '36h', '12:00 1 February 2015' }, -- fractional days give a time
{ d + 0.5, '12:00 31 January 2015' },
{ d + '1min', '00:01 31 January 2015' },
{ d + '90 minutes', '01:30 31 January 2015' },
{ d + '30s', '00:00:30 31 January 2015' },
{ Date(2015, 1, 31, 23, 0, 0) + '1h', '00:00 1 February 2015' },
}
for i, case in ipairs(cases) do
self:assertEquals(case[2], case[1] and tostring(case[1]) or nil, 'case ' .. i)
end
self:assertEquals(nil, d + '1.5m', 'fractional months')
self:assertEquals(nil, d + '1mi', 'minutes requires at least 3 letters')
self:assertEquals(nil, d + '1x', 'bad unit')
self:assertEquals(nil, d + {}, 'bad operand')
self:assertEquals(nil, 1 - d, 'number minus date')
self:assertEquals(nil, Date(9999, 12, 31) + 1, 'beyond 9999')
self:assertEquals(nil, Date(9999, 12, 31) + '1m', 'month beyond 9999')
self:assertEquals(nil, Date(-9999, 1, 1) - 1, 'before -9999')
self:assertEquals(nil, Date(-9999, 1, 1) - '1y', 'year before -9999')
self:assertEquals(nil, Date('partial', 2001) + 1, 'partial')
end
function suite:test_add_diff()
local diff = Date(2015, 3, 3) - Date(2015, 1, 31)
self:assertEquals('3 March 2015', tostring(Date(2015, 1, 31) + diff), 'date + diff')
self:assertEquals('31 January 2015', tostring(Date(2015, 3, 3) - diff), 'date - diff')
local neg = Date(2015, 1, 31) - Date(2015, 3, 3)
self:assertEquals('31 January 2015', tostring(Date(2015, 3, 3) + neg), 'date + negative diff')
self:assertEquals('3 March 2015', tostring(Date(2015, 1, 31) - neg), 'date - negative diff')
end
--------------------------------------------------------------------------------
-- Differences
--------------------------------------------------------------------------------
function suite:test_diff_fields()
local cases = {
-- Age templates on other wikis count some of these differently (T416616).
{ { 2020, 3, 5 }, { 2020, 1, 30 }, '0y 1m 5d 0:00:00' },
{ { 2015, 3, 3 }, { 2015, 1, 31 }, '0y 1m 3d 0:00:00' },
{ { 2015, 3, 3 }, { 2015, 1, 28 }, '0y 1m 3d 0:00:00' },
{ { 2015, 3, 1 }, { 2015, 1, 31 }, '0y 1m 1d 0:00:00' },
{ { 2015, 3, 1 }, { 2015, 1, 28 }, '0y 1m 1d 0:00:00' }, -- d2 == dpm (28)
{ { 2015, 3, 1 }, { 2015, 1, 27 }, '0y 1m 2d 0:00:00' }, -- d2 < dpm (28)
{ { 2015, 2, 28 }, { 2015, 1, 31 }, '0y 0m 28d 0:00:00' },
{ { 2016, 3, 1 }, { 2016, 1, 31 }, '0y 1m 1d 0:00:00' },
{ { 2016, 3, 1 }, { 2016, 1, 29 }, '0y 1m 1d 0:00:00' }, -- d2 == dpm (29)
{ { 2016, 3, 1 }, { 2016, 1, 28 }, '0y 1m 2d 0:00:00' }, -- d2 < dpm (29)
{ { 2015, 5, 1 }, { 2015, 3, 31 }, '0y 1m 1d 0:00:00' },
{ { 2015, 5, 1 }, { 2015, 3, 30 }, '0y 1m 1d 0:00:00' }, -- d2 == dpm (30)
{ { 2015, 5, 1 }, { 2015, 3, 29 }, '0y 1m 2d 0:00:00' }, -- d2 < dpm (30)
{ { 2026, 5, 31 }, { 2026, 4, 30 }, '0y 1m 1d 0:00:00' },
{ { 2026, 5, 31 }, { 2026, 5, 1 }, '0y 0m 30d 0:00:00' },
-- Birthday and month boundaries around 1 February.
{ { 2026, 2, 1 }, { 2025, 11, 1 }, '0y 3m 0d 0:00:00' },
{ { 2026, 2, 1 }, { 2025, 12, 15 }, '0y 1m 17d 0:00:00' },
{ { 2026, 2, 1 }, { 2024, 2, 2 }, '1y 11m 30d 0:00:00' },
{ { 2026, 2, 1 }, { 2024, 2, 1 }, '2y 0m 0d 0:00:00' },
{ { 2026, 2, 1 }, { 2024, 1, 31 }, '2y 0m 1d 0:00:00' },
-- Leap-day birthdays across leap, non-leap, and century years.
-- Note: 2026-02-01 - 2024-02-29 gives 1y 11m 3d (PHP DateTime::diff gives 1y 11m 4d).
{ { 2026, 2, 1 }, { 2024, 2, 29 }, '1y 11m 3d 0:00:00' },
{ { 2021, 3, 1 }, { 2020, 2, 29 }, '1y 0m 1d 0:00:00' },
{ { 2021, 2, 28 }, { 2020, 2, 29 }, '0y 11m 30d 0:00:00' },
{ { 2024, 2, 28 }, { 2020, 2, 29 }, '3y 11m 30d 0:00:00' },
{ { 2024, 2, 29 }, { 2020, 2, 29 }, '4y 0m 0d 0:00:00' },
{ { 2024, 3, 1 }, { 2020, 2, 29 }, '4y 0m 1d 0:00:00' },
{ { 2028, 2, 29 }, { 2027, 2, 28 }, '1y 0m 1d 0:00:00' },
{ { 2028, 2, 29 }, { 2027, 3, 1 }, '0y 11m 28d 0:00:00' },
{ { 1900, 2, 28 }, { 1896, 2, 29 }, '3y 11m 30d 0:00:00' }, -- century non-leap
{ { 1900, 3, 1 }, { 1896, 2, 29 }, '4y 0m 1d 0:00:00' },
{ { 2000, 2, 29 }, { 1996, 2, 29 }, '4y 0m 0d 0:00:00' }, -- 400-year leap
-- Julian calendar (1900 is a leap year).
{ { 1900, 2, 29, 'julian' }, { 1896, 2, 29, 'julian' }, '4y 0m 0d 0:00:00' },
{ { 1900, 3, 1, 'julian' }, { 1900, 1, 28, 'julian' }, '0y 1m 2d 0:00:00' },
-- Year-end and December-to-January borrows (dpm = 31).
{ { 2026, 11, 25 }, { 1990, 11, 26 }, '35y 11m 30d 0:00:00' },
{ { 2026, 11, 26 }, { 1990, 11, 26 }, '36y 0m 0d 0:00:00' },
{ { 2027, 1, 1 }, { 2026, 12, 31 }, '0y 0m 1d 0:00:00' },
{ { 2026, 1, 1 }, { 2025, 12, 31 }, '0y 0m 1d 0:00:00' },
{ { 2021, 1, 15 }, { 2020, 12, 31 }, '0y 0m 15d 0:00:00' },
{ { 2021, 1, 15 }, { 2020, 12, 20 }, '0y 0m 26d 0:00:00' },
{ { 2021, 1, 1 }, { 2020, 1, 31 }, '0y 11m 1d 0:00:00' },
-- Sign, zero, time-of-day, BC, and range extremes.
{ { 2020, 1, 30 }, { 2020, 3, 5 }, '0y 1m 5d 0:00:00 -' },
{ { 2020, 1, 30 }, { 2020, 1, 30 }, '0y 0m 0d 0:00:00 0' },
{ { 2020, 1, 2, 3, 4, 5 }, { 2020, 1, 1 }, '0y 0m 1d 3:04:05' },
{ { 2020, 1, 2 }, { 2020, 1, 1, 18, 0, 0 }, '0y 0m 0d 6:00:00' },
{ { 2020, 3, 1, 1, 0, 0 }, { 2020, 1, 31, 23, 0, 0 }, '0y 1m 0d 2:00:00' },
{ { 2020, 3, 1, 0, 0, 0 }, { 2020, 1, 31, 6, 0, 0 }, '0y 1m 0d 18:00:00' },
{ { 2020, 3, 15, 6, 0, 0 }, { 2020, 1, 15, 18, 0, 0 }, '0y 1m 28d 12:00:00' },
-- FIXME? Floating-point rounding in (day + hms) yields 24:00:00 instead of 1d 0:00:00.
{ { 2020, 3, 2, 0, 3, 40 }, { 2020, 3, 1, 0, 3, 40 }, '0y 0m 0d 24:00:00' },
{ { 1, 1, 1 }, { -1, 1, 1 }, '2y 0m 0d 0:00:00' },
{ { 9999, 12, 31 }, { -9999, 1, 1 }, '19998y 11m 30d 0:00:00' },
}
for _, case in ipairs(cases) do
local d1, d2 = Date(unpack(case[1])), Date(unpack(case[2]))
local diff = d1 - d2
self:assertEquals(case[3], diff_text(diff),
tostring(d1) .. ' - ' .. tostring(d2))
-- Negative diffs swap date1 and date2 so date1 >= date2 always holds.
if d1 < d2 then
self:assertTrue(diff.date1 == d2 and diff.date2 == d1, 'negative diff swaps date1/date2')
else
self:assertTrue(diff.date1 == d1 and diff.date2 == d2, 'non-negative diff preserves date1/date2')
end
-- Round-trip invariant holds when years and months are added together (adding
-- years then months separately can clamp Feb 29 -> Feb 28 before adding months).
if d1 >= d2 and not d1.hastime and not d2.hastime then
self:assertTrue(
d2 + ((diff.years * 12 + diff.months) .. 'm') + diff.days == d1,
'round-trip ' .. tostring(d1) .. ' - ' .. tostring(d2)
)
end
end
-- Note: adding years and months separately clamps 2024-02-29 + 1y to 2025-02-28.
local leap_diff = Date(2026, 2, 1) - Date(2024, 2, 29)
self:assertEquals('31 January 2026', tostring(Date(2024, 2, 29) + (leap_diff.years .. 'y') + (leap_diff.months .. 'm') + leap_diff.days), 'separate y+m clamps leap day')
end
function suite:test_diff_age_days()
self:assertEquals(35, (Date(2020, 3, 5) - Date(2020, 1, 30)).age_days, 'age_days')
self:assertEquals(35, (Date(2020, 1, 30) - Date(2020, 3, 5)).age_days, 'not negative; see isnegative')
self:assertEquals(366, (Date(2021, 1, 1) - Date(2020, 1, 1)).age_days, 'leap year')
self:assertEquals(0.25, (Date(2020, 1, 1, 6, 0, 0) - Date(2020, 1, 1)).age_days, 'with time')
self:assertEquals('35', tostring(Date(2020, 3, 5) - Date(2020, 1, 30)), 'tostring')
self:assertEquals('35 days', (Date(2020, 3, 5) - Date(2020, 1, 30)) .. ' days', 'concat')
self:assertEquals(nil, Date(2020, 1, 1) - Date(2020, 1, 1, 'julian'), 'calendars differ')
self:assertEquals(nil, Date(2020, 1, 1):subtract(Date(2020, 1, 1, 'julian')), 'subtract method')
end
function suite:test_diff_age_codes()
local diff = Date(2026, 11, 25) - Date(1990, 3, 10)
local cases = {
{ 'ymd', { 36, 8, 15 } },
{ 'ym', { 36, 8 } },
{ 'y', { 36 } },
{ 'm', { 440 } },
{ 'md', { 440, 15 } },
{ 'yd', { 36, 260 } },
{ 'd', { 13409 } },
{ 'w', { 1915 } },
{ 'wd', { 1915, 4 } },
{ 'ymw', { 36, 8, 2 } },
{ 'ymwd', { 36, 8, 2, 1 } },
{ 'dh', { 13409, 0 } },
{ 'h', { 321816 } },
{ 'bogus', {} },
}
for _, case in ipairs(cases) do
self:assertDeepEquals(case[2], { diff:age(case[1]) }, case[1])
end
local timed = Date(2020, 1, 12, 3, 4, 5) - Date(2020, 1, 1)
local timed_cases = {
{ 'dhm', { 11, 3, 4 } },
{ 'dhms', { 11, 3, 4, 5 } },
{ 'hm', { 267, 4 } },
{ 'hms', { 267, 4, 5 } },
{ 'M', { 16024 } },
{ 's', { 961445 } },
{ 'ymdh', { 0, 0, 11, 3 } },
{ 'ymdhm', { 0, 0, 11, 3, 4 } },
{ 'ymwdh', { 0, 0, 1, 4, 3 } },
{ 'ymwdhm', { 0, 0, 1, 4, 3, 4 } },
}
for _, case in ipairs(timed_cases) do
self:assertDeepEquals(case[2], { timed:age(case[1]) }, 'timed ' .. case[1])
end
-- yd with leap-day birth date clamps date2 + '1y' to Feb 28.
self:assertDeepEquals({ 1, 338 }, { (Date(2026, 2, 1) - Date(2024, 2, 29)):age('yd') }, 'yd leap-day')
self:assertDeepEquals({ 1, 10 }, { (Date(2026, 2, 1) - Date(2025, 1, 22)):age('yd') }, 'yd with intermediate days')
end
function suite:test_diff_age_round_duration()
local diff = Date(2026, 11, 25) - Date(1990, 3, 10)
self:assertDeepEquals({ 37 }, { diff:age('y', true) }, 'y round up at 6 months')
self:assertDeepEquals({ 36, 8 }, { diff:age('ym', { round = true }) }, 'ym round down below 16 days')
self:assertDeepEquals({ 36, 9 }, { (diff.date1 + 1 - diff.date2):age('ym', true) }, 'ym round up at 16 days')
self:assertDeepEquals({ 36, 8, 16 }, { diff:duration('ymd') }, 'duration adds a day')
self:assertDeepEquals({ 13410 }, { diff:duration('d') }, 'duration d')
local short = Date(2026, 3, 9) - Date(2025, 9, 10)
self:assertDeepEquals({ 0 }, { short:age('y', true) }, 'y round down below 6 months')
self:assertDeepEquals({ 1 }, { (Date(2020, 7, 1) - Date(2020, 1, 1)):age('y', true) }, 'y round up at exact 6 months')
-- Carry from 11 months 16 days into the next year.
self:assertDeepEquals({ 0, 11 }, { (Date(2021, 1, 16) - Date(2020, 2, 1)):age('ym', true) }, 'ym 11m 15d no carry')
self:assertDeepEquals({ 1, 0 }, { (Date(2021, 1, 17) - Date(2020, 2, 1)):age('ym', true) }, 'ym 11m 16d carries to year')
self:assertDeepEquals({ 12 }, { (Date(2021, 1, 17) - Date(2020, 2, 1)):age('m', true) }, 'm round up at 16 days')
-- Duration month/year rollover when adding the inclusive day.
self:assertDeepEquals({ 0, 1, 0 }, { (Date(2020, 1, 31) - Date(2020, 1, 1)):duration('ymd') }, 'duration month rollover')
self:assertDeepEquals({ 1 }, { (Date(2020, 12, 31) - Date(2020, 1, 1)):duration('y') }, 'duration year rollover')
self:assertDeepEquals({ 1, 0 }, { (Date(2020, 12, 31) - Date(2020, 1, 1)):duration('yd') }, 'duration yd rollover')
-- Week and sub-day rounding boundaries and carry cascades.
self:assertDeepEquals({ 0 }, { (Date(2020, 1, 4) - Date(2020, 1, 1)):age('w', true) }, 'w round down at 3 days')
self:assertDeepEquals({ 1 }, { (Date(2020, 1, 5) - Date(2020, 1, 1)):age('w', true) }, 'w round up at 4 days')
self:assertDeepEquals({ 0, 0, 0 }, { (Date(2020, 1, 4, 11, 59, 59) - Date(2020, 1, 1)):age('ymw', true) }, 'ymw round down below 3d 12h')
self:assertDeepEquals({ 0, 0, 1 }, { (Date(2020, 1, 4, 12, 0, 0) - Date(2020, 1, 1)):age('ymw', true) }, 'ymw round up at 3d 12h')
local timed = Date(2020, 1, 2, 13, 0, 0) - Date(2020, 1, 1)
self:assertDeepEquals({ 0, 0, 2 }, { timed:age('ymd', true) }, 'ymd round up at 12 hours')
self:assertDeepEquals({ 1, 13 }, { timed:age('dh') }, 'dh')
local carry = Date(2020, 1, 1, 23, 59, 30) - Date(2020, 1, 1)
self:assertDeepEquals({ 1, 0, 0 }, { carry:age('dhm', true) }, 'dhm carry to next day')
self:assertDeepEquals({ 0, 0, 1, 0, 0 }, { carry:age('ymdhm', true) }, 'ymdhm carry to next day')
self:assertDeepEquals({ 1, 0 }, { carry:age('dh', true) }, 'dh carry to next day')
self:assertDeepEquals({ 1440 }, { carry:age('M', true) }, 'M round up at 30s')
self:assertThrows(function () diff.age(nil, 'y') end, 'age: need a date difference (use "diff:age()" with a colon)')
self:assertThrows(function () diff.duration(nil, 'y') end, 'duration: need a date difference (use "diff:duration()" with a colon)')
end
function suite:test_diff_partial()
local diff = Date('partial', 2005) - Date('partial', 2001)
self:assertDeepEquals({ 3, 4 }, diff.partial.years, 'years range')
self:assertDeepEquals({ 4 }, { diff:age('y') }, 'y picks the larger')
self:assertDeepEquals({ { 3, 4 } }, { diff:age('y', { range = true }) }, 'y range')
self:assertEquals(1461, diff.age_days, 'partial age_days uses partial.first')
self:assertDeepEquals({ { 36, 59 } }, { diff:age('m', { range = true }) }, 'partial m range')
self:assertDeepEquals({ 59 }, { diff:age('m') }, 'partial m picks larger')
self:assertDeepEquals({ nil }, { diff:age('yd') }, 'partial unsupported code')
local same = Date('partial', 2001) - Date('partial', 2001)
self:assertDeepEquals({ 0, 0 }, { same:age('ym') }, 'same partial year')
self:assertDeepEquals({ 0, { 0, 11 } }, { same:age('ym', { range = true }) }, 'same partial year range')
-- Overlapping partials clamp mindiff to zero.
local overlap = Date('partial', 2001, 4) - Date('partial', 2001)
self:assertFalse(overlap.isnegative, 'overlap not negative')
self:assertEquals(0, overlap.partial.years, 'overlap years')
self:assertDeepEquals({ 0, 3 }, overlap.partial.months, 'overlap months')
-- Negative partial diff.
local neg = Date('partial', 2001) - Date('partial', 2005)
self:assertTrue(neg.isnegative, 'negative partial diff')
self:assertDeepEquals({ 3, 4 }, neg.partial.years, 'negative partial years')
local month = Date('partial', 2005, 4) - Date('partial', 2001, 4)
self:assertDeepEquals({ 3, 4 }, month.partial.years, 'days of the month unknown')
-- FIXME? No month is returned when the years are a range.
self:assertDeepEquals({ 4 }, { month:age('ym') }, 'same month ym')
local full = Date(2005, 4, 15) - Date('partial', 2001)
self:assertDeepEquals({ 3, 4 }, full.partial.years, 'full minus partial')
local full_m = Date(2005, 5, 15) - Date('partial', 2001, 4)
self:assertEquals(4, full_m.partial.years, 'full minus earlier partial month')
self:assertDeepEquals({ 0, 1 }, full_m.partial.months, 'full minus partial month range')
-- autofill (fill = true)
local filled = Date('partial', 2005):subtract(Date(2001, 4, 15), { fill = true })
self:assertEquals('4y 0m 0d 0:00:00', diff_text(filled), 'fill uses the month and day of the full date')
local filled_two = Date('partial', 2005):subtract(Date('partial', 2001), { fill = true })
self:assertEquals('4y 0m 0d 0:00:00', diff_text(filled_two), 'fill two year partials with Jan 1')
local filled_clamp = Date('partial', 2021, 2):subtract(Date(2020, 1, 31), { fill = true })
self:assertEquals('1y 0m 28d 0:00:00', diff_text(filled_clamp), 'fill clamps day 31 to Feb 28')
local filled_leap = Date('partial', 2021):subtract(Date(2020, 2, 29), { fill = true })
self:assertEquals('0y 11m 30d 0:00:00', diff_text(filled_leap), 'fill clamps Feb 29 to Feb 28 in non-leap year')
end
--------------------------------------------------------------------------------
-- Lists
--------------------------------------------------------------------------------
function suite:test_list()
local d = Date(2016, 4, 13) -- a Wednesday
local cases = {
{ '3 Mon', '18 April 2016, 25 April 2016, 2 May 2016' },
{ '2 Wed', '20 April 2016, 27 April 2016' },
{ '2 Wed >=', '13 April 2016, 20 April 2016' },
{ '2 Mon <', '4 April 2016, 11 April 2016' },
{ '1 Wed <', '6 April 2016' },
{ 'Fridays', '15 April 2016, 22 April 2016, 29 April 2016' },
{ 'Fri <=', '1 April 2016, 8 April 2016' },
{ 2, '20 April 2016, 27 April 2016' },
{ -2, '30 March 2016, 6 April 2016' },
{ 'bogus', '' },
}
for _, case in ipairs(cases) do
self:assertEquals(case[2], d:list(case[1]):text():join(', '), tostring(case[1]))
end
self:assertEquals('2016-04-18', d:list('1 Mon'):text('ymd'):join(), 'list text format')
self:assertEquals(0, #Date('partial', 2016):list('2 Mon'), 'partial')
end
--------------------------------------------------------------------------------
-- Current date
--------------------------------------------------------------------------------
function suite:test_current()
-- Module:Date uses UTC.
local now = os.date('!*t')
local today = Date(now.year, now.month, now.day)
local curr = Date('currentdate')
self:assertTrue(curr == today, 'currentdate')
self:assertFalse(curr.hastime, 'currentdate has no time')
self:assertTrue(Date('currentdate', 'julian') == today, 'calendar ignored')
-- Properties, copy, and override must match a fixed date for today.
self:assertEquals(today.year, curr.year, 'currentdate.year')
self:assertEquals(today.month, curr.month, 'currentdate.month')
self:assertEquals(today.day, curr.day, 'currentdate.day')
self:assertEquals(today.isleapyear, curr.isleapyear, 'currentdate.isleapyear')
self:assertEquals(today.monthdays, curr.monthdays, 'currentdate.monthdays')
self:assertEquals(today.dow, curr.dow, 'currentdate.dow')
self:assertEquals(today.jd, curr.jd, 'currentdate.jd')
self:assertEquals(today:text('ymd'), curr:text('ymd'), 'currentdate:text')
self:assertTrue(Date(curr) == today, 'copy currentdate')
self:assertTrue(Date(curr, { day = 1 }) == Date(now.year, now.month, 1), 'override currentdate')
-- Arithmetic and comparisons against fixed, partial, and Julian dates.
self:assertTrue(curr + 1 == today + 1, 'currentdate + 1')
self:assertTrue(curr - 1 == today - 1, 'currentdate - 1')
self:assertTrue(curr + '1d' == today + '1d', 'currentdate + 1d')
self:assertTrue(curr - '1d' == today - '1d', 'currentdate - 1d')
self:assertTrue(curr + '1w' == today + '1w', 'currentdate + 1w')
self:assertTrue(curr + '1m' == today + '1m', 'currentdate + 1m')
self:assertTrue(curr + '1y' == today + '1y', 'currentdate + 1y')
self:assertTrue(curr > Date(2000, 1, 1), 'currentdate > past')
self:assertTrue(curr < Date(2099, 1, 1), 'currentdate < future')
self:assertTrue(curr > Date('partial', 2000), 'currentdate > partial past')
self:assertTrue(curr > Date(1500, 1, 1, 'julian'), 'currentdate > Julian past')
-- Diffs and age calculations must match a fixed date for today.
local birth = Date(1990, 3, 10)
local cdiff, tdiff = curr - birth, today - birth
self:assertEquals(diff_text(tdiff), diff_text(cdiff), 'currentdate diff')
self:assertDeepEquals({ tdiff:age('y') }, { cdiff:age('y') }, 'currentdate age y')
self:assertDeepEquals({ tdiff:age('ym') }, { cdiff:age('ym') }, 'currentdate age ym')
self:assertDeepEquals({ tdiff:age('m') }, { cdiff:age('m') }, 'currentdate age m')
self:assertDeepEquals({ tdiff:age('y', true) }, { cdiff:age('y', true) }, 'currentdate age y round')
self:assertDeepEquals({ tdiff:duration('y') }, { cdiff:duration('y') }, 'currentdate duration y')
local pdiff = curr - Date('partial', 1990)
self:assertDeepEquals((today - Date('partial', 1990)).partial.years, pdiff.partial.years, 'currentdate minus partial')
self:assertTrue((curr - Date(2099, 1, 1)).isnegative, 'currentdate minus future is negative')
self:assertEquals(diff_text(Date(2099, 1, 1) - today), diff_text(Date(2099, 1, 1) - curr), 'future minus currentdate')
local dt = Date('currentdatetime')
self:assertTrue(dt.hastime, 'currentdatetime has a time')
self:assertTrue(dt >= today and dt < today + 1, 'currentdatetime is today')
self:assertEquals(now.year, date._current.year, '_current')
end
return suite