--[[ Date and DateDiff objects for the proleptic Gregorian calendar. mw.date.getCurrentDate() returns today's date (UTC). Comparing it with, or subtracting it from, another date sets the parser cache expiry to the time when the result can next change, instead of expiring at every midnight. A date made by adding or subtracting whole days from it moves with it. Partly copied from https://en.wikipedia.org/w/index.php?title=Module:Date&oldid=1338943873 by Johnuniq, with permission (https://phabricator.wikimedia.org/T416616#12590750). @license GNU GPL v2+ ]] local date = {} local php local util = require 'libraryUtil' local checkType = util.checkType local floor = math.floor -- Julian day number of 1970-01-01, and limits for -9999-01-01 .. 9999-12-31 local UNIX_EPOCH_JDN = 2440588 local MIN_JDN = -1930999 local MAX_JDN = 5373484 local DAYS_IN_MONTH = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } local PUBLIC_DATE_FIELDS = { year = true, month = true, day = true } local dateMt, diffMt local dateData = setmetatable( {}, { __mode = 'k' } ) local diffData = setmetatable( {}, { __mode = 'k' } ) local function updateExpiry( jdn ) php.setExpiry( ( jdn - UNIX_EPOCH_JDN ) * 86400 ) end local function is_leap_year( year ) return ( year % 4 == 0 and year % 100 ~= 0 ) or year % 400 == 0 end local function days_in_month( year, month ) if month == 2 and is_leap_year( year ) then return 29 end return DAYS_IN_MONTH[month] end -- Gregorian calendar <-> Julian day number (Claus Tondering's formula) -- Adapted from en:Module:Date, revision 1338943873. local function julian_day( year, month, day ) local a = floor( ( 14 - month ) / 12 ) local y = year + 4800 - a local m = month + 12 * a - 3 return day + floor( ( 153 * m + 2 ) / 5 ) + 365 * y + floor( y / 4 ) - floor( y / 100 ) + floor( y / 400 ) - 32045 end local function date_from_jdn( jdn ) if jdn < MIN_JDN or jdn > MAX_JDN then return nil end local a = jdn + 32044 local b = floor( ( 4 * a + 3 ) / 146097 ) local c = a - floor( 146097 * b / 4 ) local d = floor( ( 4 * c + 3 ) / 1461 ) local e = c - floor( 1461 * d / 4 ) local m = floor( ( 5 * e + 2 ) / 153 ) local day = e - floor( ( 153 * m + 2 ) / 5 ) + 1 local month = m + 3 - 12 * floor( m / 10 ) local year = 100 * b + d - 4800 + floor( m / 10 ) return year, month, day end local function isFiniteInt( v ) return type( v ) == 'number' and v == floor( v ) and v ~= math.huge and v ~= -math.huge end local function isIntInRange( v, min, max ) return isFiniteInt( v ) and v >= min and v <= max end local function checkOptions( name, argIdx, options ) if options == nil then return end if type( options ) ~= 'table' then error( string.format( "bad argument #%d to '%s' (table expected, got %s)", argIdx, name, type( options ) ), 3 ) end local k = next( options ) if k ~= nil then error( string.format( "bad argument #%d to '%s' (unknown option '%s')", argIdx, name, tostring( k ) ), 3 ) end end local function makeDate( d ) d.jdn = d.jdn or julian_day( d.year, d.month, d.day ) local obj = setmetatable( {}, dateMt ) dateData[obj] = d return obj end --- Create a date. Returns nil if the date is invalid. function date.new( year, month, day, options ) checkType( 'new', 1, year, 'number' ) checkType( 'new', 2, month, 'number' ) checkType( 'new', 3, day, 'number' ) checkOptions( 'new', 4, options ) if not ( isIntInRange( year, -9999, 9999 ) and isIntInRange( month, 1, 12 ) and isIntInRange( day, 1, days_in_month( year, month ) ) ) then return nil end return makeDate( { year = year + 0, month = month, day = day } ) end -- A date derived from mw.date.getCurrentDate() has a numeric offset (0 for -- today, n for today + n); fixed dates have offset == nil. Its jdn advances -- with real time, so it reaches jdn on real day jdn - offset. --- JDN of the first day after today local function tomorrow( today ) return today.jdn - today.offset + 1 end --- Set the expiry for a comparison between a moving date and a fixed date local function compareExpiry( moving, fixed ) if fixed.jdn >= moving.jdn then updateExpiry( math.max( tomorrow( moving ), fixed.jdn - moving.offset ) ) end end local function compare( lhs, rhs ) local a, b = dateData[lhs], dateData[rhs] if ( a.offset ~= nil ) ~= ( b.offset ~= nil ) then if a.offset ~= nil then compareExpiry( a, b ) else compareExpiry( b, a ) end end return a.jdn, b.jdn end --- JDN of day `day` in `(year, month)`, rolling to the 1st of the next month --- when that month is shorter; matches makeDiff(). local function monthBoundary( year, month, day ) return julian_day( year, month, 1 ) + math.min( day, days_in_month( year, month ) + 1 ) - 1 end local function nextMonthChange( d, day ) local jdn = monthBoundary( d.year, d.month, day ) if jdn > d.jdn then return jdn end if d.month == 12 then return monthBoundary( d.year + 1, 1, day ) end return monthBoundary( d.year, d.month + 1, day ) end local function nextYearChange( d, month, day ) local jdn = monthBoundary( d.year, month, day ) if jdn > d.jdn then return jdn end return monthBoundary( d.year + 1, month, day ) end local function makeDiff( lhs, rhs ) local isNegative = lhs.jdn < rhs.jdn local earlier, later = rhs, lhs if isNegative then earlier, later = lhs, rhs end -- Year/month/day difference algorithm from en:Module:Date, revision 1338943873 local y1, m1, d1 = later.year, later.month, later.day local y2, m2, d2 = earlier.year, earlier.month, earlier.day local years = y1 - y2 local months = m1 - m2 local days if d1 >= d2 then days = d1 - d2 else months = months - 1 local dpm = m1 > 1 and days_in_month( y1, m1 - 1 ) or 31 if d2 >= dpm then days = d1 else days = dpm - d2 + d1 end end if months < 0 then years = years - 1 months = months + 12 end local obj = setmetatable( {}, diffMt ) diffData[obj] = { lhs = lhs, rhs = rhs, fields = { isNegative = isNegative, isZero = later.jdn == earlier.jdn, totalDays = later.jdn - earlier.jdn, years = years, months = months, days = days, }, } return obj end local function addDays( d, n ) local jdn = d.jdn + n local year, month, day = date_from_jdn( jdn ) if not year then return nil end return makeDate( { year = year, month = month, day = day, jdn = jdn, offset = d.offset and d.offset + n, } ) end --- Compute the DateDiff between self and other local function diffMethod( self, other, options ) local d1 = dateData[self] if not d1 then error( "mw.date: invalid Date object. Did you call diff with a dot instead of a colon, " .. "i.e. date.diff() instead of date:diff()?", 2 ) end checkType( 'diff', 1, other, 'table' ) checkOptions( 'diff', 2, options ) local d2 = dateData[other] if not d2 then error( "bad argument #1 to 'diff' (Date expected)", 2 ) end return makeDiff( d1, d2 ) end dateMt = { __metatable = false, __newindex = function () error( 'Date objects are read-only', 2 ) end, __index = function ( self, key ) if key == 'diff' then return diffMethod end if not PUBLIC_DATE_FIELDS[key] then return nil end local d = dateData[self] if d.offset ~= nil then if key == 'year' then updateExpiry( julian_day( d.year + 1, 1, 1 ) - d.offset ) elseif key == 'month' then updateExpiry( nextMonthChange( d, 1 ) - d.offset ) else updateExpiry( tomorrow( d ) ) end end return d[key] end, __tostring = function ( self ) local d = dateData[self] if d.offset ~= nil then updateExpiry( tomorrow( d ) ) end if d.year < 0 then return string.format( '-%04d-%02d-%02d', -d.year, d.month, d.day ) end return string.format( '%04d-%02d-%02d', d.year, d.month, d.day ) end, __eq = function ( lhs, rhs ) local a, b = compare( lhs, rhs ) return a == b end, __lt = function ( lhs, rhs ) local a, b = compare( lhs, rhs ) return a < b end, __le = function ( lhs, rhs ) local a, b = compare( lhs, rhs ) return a <= b end, __add = function ( lhs, rhs ) local d = dateData[lhs] if not ( d and isFiniteInt( rhs ) ) then error( 'only a whole number of days can be added to a date', 2 ) end return addDays( d, rhs ) end, __sub = function ( lhs, rhs ) local d1 = dateData[lhs] if d1 then local d2 = dateData[rhs] if d2 then return makeDiff( d1, d2 ) end if isFiniteInt( rhs ) then return addDays( d1, -rhs ) end end error( 'only a date or whole number of days can be subtracted from a date', 2 ) end, } diffMt = { __metatable = false, __newindex = function () error( 'DateDiff objects are read-only', 2 ) end, __index = function ( self, key ) local private = diffData[self] local value = private.fields[key] if value == nil then return nil end local lhs, rhs = private.lhs, private.rhs if lhs.offset ~= nil and rhs.offset ~= nil then if lhs.offset ~= rhs.offset and ( key == 'years' or key == 'months' or key == 'days' ) then updateExpiry( tomorrow( lhs ) ) end elseif lhs.offset ~= nil or rhs.offset ~= nil then local moving, fixed = lhs, rhs if rhs.offset ~= nil then moving, fixed = rhs, lhs end if key == 'isNegative' or key == 'isZero' then compareExpiry( moving, fixed ) elseif key == 'years' and fixed.jdn <= moving.jdn then updateExpiry( nextYearChange( moving, fixed.month, fixed.day ) - moving.offset ) elseif key == 'months' and fixed.jdn <= moving.jdn then updateExpiry( nextMonthChange( moving, fixed.day ) - moving.offset ) else updateExpiry( tomorrow( moving ) ) end end return value end, } function date.setupInterface( options ) -- Boilerplate date.setupInterface = nil php = mw_interface mw_interface = nil local currentYear, currentMonth, currentDay = options.year, options.month, options.day function date.getCurrentDate( opts ) checkOptions( 'getCurrentDate', 1, opts ) return makeDate( { year = currentYear, month = currentMonth, day = currentDay, offset = 0, } ) end -- Register this library in the "mw" global mw = mw or {} mw.date = date mw.registerCloneHandler( function ( val ) local d = dateData[val] if d then return makeDate( d ) end local diff = diffData[val] if diff then local obj = setmetatable( {}, diffMt ) diffData[obj] = diff return obj end end ) package.loaded['mw.date'] = date end return date