Module:Date/testcases

local date = require("Dev:Date") local ScribuntoUnit = require("Module:ScribuntoUnit") local suite = ScribuntoUnit:new

function suite:test_date_parsing self:assertTrue( date("Jul 27 2006 03:56:28 +2:00") == date(2006,07,27,1,56,28)) self:assertTrue(date("Jul 27 2006 -75 ") == date(2006,07,27,1,15,0)) self:assertTrue(date("Jul 27 2006 -115") == date(2006,07,27,1,15,0)) self:assertTrue(date("Jul 27 2006 +10 ") == date(2006,07,26,14,0,0)) self:assertTrue(date("Jul 27 2006 +2 ") == date(2006,07,26,22,0,0))

-- Standard timezone GMT, UTC, EST, EDT, CST, CDT, MST, MDT, PST, PDT are supported. self:assertTrue(date("Jul 27 2006 GMT") == date(2006,07,27,0,0,0)) self:assertTrue(date("Jul 27 2006 UTC") == date(2006,07,27,0,0,0)) self:assertTrue(date("Jul 27 2006 EST") == date(2006,07,27,5,0,0)) self:assertTrue(date("Jul 27 2006 EDT") == date(2006,07,27,4,0,0)) self:assertTrue(date("Jul 27 2006 CST") == date(2006,07,27,6,0,0)) self:assertTrue(date("Jul 27 2006 CDT") == date(2006,07,27,5,0,0)) self:assertTrue(date("Jul 27 2006 MST") == date(2006,07,27,7,0,0)) self:assertTrue(date("Jul 27 2006 MDT") == date(2006,07,27,6,0,0)) self:assertTrue(date("Jul 27 2006 PST") == date(2006,07,27,8,0,0)) self:assertTrue(date("Jul 27 2006 PDT") == date(2006,07,27,7,0,0)) -- Date Format. Short dates can use either a "/" or "-" date separator, -- but must follow the month/day/year format self:assertTrue(date("02-03-04")==date(1904,02,03)) self:assertTrue(date("12/25/98")==date(1998,12,25)) -- Long dates of the form "July 10 1995" can be given with the year, month, -- and day in any order, and the year in 2-digit or 4-digit form. If you use -- the 2-digit form, the year must be greater than or equal to 70. self:assertTrue(date("Feb-03-04")==date(1904,02,03)) self:assertTrue(date("December 25 1998")==date(1998,12,25)) -- Follow the year with BC or BCE to indicate that the year is before common era. self:assertTrue(date("Feb 3 0003 BC")==date(-2,02,03)) self:assertTrue(date("December 25 0001 BC")==date(0,12,25))

-- Supported ISO 8601 Formats. -- YYYY-MM-DDwhere YYYY is the year, MM is the month of the year, and DD is the -- day of the month. self:assertTrue(date("2000-12-31")==date(2000,12,31)) self:assertTrue(date(" 20001231 ")==date(2000,12,31)) -- Compact version -- YYYY-DDDwhere YYYY is the year, DDD is the day of the year. self:assertTrue(date("1995-035")==date(1995,02,04)) self:assertTrue(date("1995035 ")==date(1995,02,04)) -- Compact version -- YYYY-WDD-Dwhere YYYY is the year, DD is the week of the year, D is the day of    -- the week. self:assertTrue(date("1997-W01-1")==date(1996,12,30)) self:assertTrue(date(" 1997W017")==date(1997,01,05)) -- Compact version -- DATE HH:MM:SS.SSSWhere DATE is the date format discuss above, HH is the hour, -- MM is the miute, SS.SSS is the seconds (fraction is optional). self:assertTrue(date("1995-02-04 24:00:51.536")==date(1995,2,5,0,0,51.536)) self:assertTrue(date("1976-W01-1 12:12:12.123")==date(1975,12,29,12,12,12.123)) self:assertTrue(date("1995-035 23:59:59.99999")==date(1995,02,04,23,59,59.99999)) -- Compact version separated by latin capital letter T   self:assertTrue(date("  19950205T000051.536  ")==date(1995,2,5,0,0,51.536)) self:assertTrue(date(" 1976W011T121212.123  ")==date(1975,12,29,12,12,12.123)) self:assertTrue(date(" 1995035T235959.99999 ")==date(1995,02,04,23,59,59.99999)) -- DATE TIME +HH:MM, DATE TIME -HHMM, DATE TIME Z,Where DATE and TIME is the date -- and time format discuss above. First character is a sign "+" (east of UTC) or "-" -- (west of UTC). HH and MM is Hours and minutes offset. The Z stands for the zero offset. self:assertTrue(date("1976-W01-1 12:00Z    ")==date(1975,12,29,12)) self:assertTrue(date("1976-W01-1 13:00+01:00")==date(1975,12,29,12)) self:assertTrue(date("1976-W01-1 0700-0500 ")==date(1975,12,29,12))

local a = date(2006, 8, 13);  self:assertTrue(a == date("Sun Aug 13 2006")) local b = date("Jun 13 1999"); self:assertTrue(b == date(1999, 6, 13)) local c = date(1234483200);   self:assertTrue(c == date("Feb 13 2009")) local d = date({year=2009, month=11, day=13, min=6}) self:assertTrue(d == date("Nov 13 2009 00:06:00")) local e = date;             self:assertTrue(e) end

function suite:test_leap_year self:assertTrue(date.isleapyear(2012)) self:assertTrue(date.isleapyear(2000)) self:assertFalse(date.isleapyear(2011)) self:assertFalse(date.isleapyear(1900)) end

function suite:test_equality local a = date("20131230 00:57:04") self:assertTrue(a:getyear   == 2013) self:assertTrue(a:getmonth  == 12) self:assertTrue(a:getday    == 30) self:assertTrue(a:gethours  == 0) self:assertTrue(a:getminutes == 57) self:assertTrue(a:getseconds == 04) local b = date("20131230 01:00:00") local c = date("20131230 00:57:04") -- same as a    self:assertTrue(a < b)    self:assertTrue(a <= b)    self:assertTrue(not (a > b)) self:assertTrue(not (a >= b)) self:assertTrue(a == c)   self:assertTrue(a <= c)    self:assertTrue(a >= c) end

function suite:test_metamethods local a = date(1521,5,2) local b = a:copy:addseconds(0.001)

self:assertTrue((a - b):spanseconds == -0.001) self:assertTrue((a + b) == (b + a)) self:assertTrue(a == (b - date("00:00:00.001")) ) self:assertTrue(b == (a + date("00:00:00.001")) )

b:addseconds(-0.01)

self:assertTrue(a > b and b <  a)    self:assertTrue(a >= b and b <= a)    self:assertTrue(a ~= b and (not(a == b)))

a = b:copy

self:assertTrue(not (a > b and b <  a)) self:assertTrue(a >= b and b <= a)   self:assertTrue(a == b and (not(a ~= b)))

self:assertTrue((a .. 565369) == (b .. 565369))   self:assertTrue((a .. "????") == (b .. "????")) end --add more later function suite:test_fmt local d = date(1582,10,5) self:assertTrue(d:fmt('%D') == d:fmt("%m/%d/%y"))       -- month/day/year from 01/01/00 (12/02/79) self:assertTrue(d:fmt('%F') == d:fmt("%Y-%m-%d"))       -- year-month-day (1979-12-02) self:assertTrue(d:fmt('%h') == d:fmt("%b"))                     -- same as %b (Dec) self:assertTrue(d:fmt('%r') == d:fmt("%I:%M:%S %p"))    -- 12-hour time, from 01:00:00 AM (06:55:15 AM) self:assertTrue(d:fmt('%T') == d:fmt("%H:%M:%S"))       -- 24-hour time, from 00:00:00 (06:55:15) self:assertTrue(d:fmt('%a %A %b %B') == "Tue Tuesday Oct October") self:assertTrue(d:fmt('%C %d') == "15 05", d:fmt('%C %d')) end

return suite