Module:Date: Difference between revisions

Created page with "-- Date functions for use by other modules. -- I18N and time zones are not supported. local MINUS = '−' -- Unicode U+2212 MINUS SIGN local floor = math.floor local Date, DateDiff, diffmt -- forward declarations local uniq = { 'unique identifier' } local function is_date(t) -- The system used to make a date read-only means there is no unique -- metatable that is conveniently accessible to check. return type(t) == 'table' and t._id == uniq end local function is_..."
 
No edit summary
 
Line 568: Line 568:


local function month_number(text)
local function month_number(text)
return name_to_number(text, {
return name_to_number(text:gsub('%.', ''), {
jan = 1, january = 1,
jan = 1, january = 1,
feb = 2, february = 2,
feb = 2, february = 2,
Line 695: Line 695:
end
end


-- A table to get the current date/time (UTC), but only if needed.
-- Cache the current date/time (UTC).
local current = setmetatable({}, {
--
__index = function (self, key)
-- Note that os.date() returns an empty object with optimized lazy getters for each field.
local d = os.date('!*t')
-- Avoid current.min or current.sec in articles! https://phabricator.wikimedia.org/T416616
self.year = d.year
--
self.month = d.month
-- Beware that while os.date() uses 'min' and 'sec', our Date() uses 'minute' and 'second'.
self.day = d.day
local current = os.date('!*t')
self.hour = d.hour
self.minute = d.min
self.second = d.sec
return rawget(self, key)
end })


local function extract_date(newdate, text)
local function extract_date(newdate, text)
Line 1,295: Line 1,290:
if argtype == 'currentdatetime' then
if argtype == 'currentdatetime' then
newdate.hour = current.hour
newdate.hour = current.hour
newdate.minute = current.minute
newdate.minute = current.min
newdate.second = current.second
newdate.second = current.sec
newdate.hastime = true
newdate.hastime = true
end
end