Module:Date: Difference between revisions

3,539 bytes added ,  9 years ago
tweak formatted output; extract_date to parse an input date string
(formatted output and strftime)
(tweak formatted output; extract_date to parse an input date string)
Line 152: Line 152:
date.month = m + 3 - 12*floor(m/10)
date.month = m + 3 - 12*floor(m/10)
date.year = 100*b + d - 4800 + floor(m/10)
date.year = 100*b + d - 4800 + floor(m/10)
end
local function set_date_from_numbers(date, numbers, options)
-- Set the fields of table date from numeric values.
-- Return true if date is valid.
if type(numbers) ~= 'table' then
return
end
local y = numbers.y or numbers[1]
local m = numbers.m or numbers[2]
local d = numbers.d or numbers[3]
local H = numbers.H or numbers[4]
local M = numbers.M or numbers[5] or 0
local S = numbers.S or numbers[6] or 0
if not (y and m and d) then
return
end
if not (-9999 <= y and y <= 9999 and 1 <= m and m <= 12 and
1 <= d and d <= days_in_month(y, m, date.calname)) then
return
end
if H then
date.hastime = true
else
H = 0
end
if not (0 <= H and H <= 23 and
0 <= M and M <= 59 and
0 <= S and S <= 59) then
return
end
date.year = y    -- -9999 to 9999 ('n BC' → year = 1 - n)
date.month = m  -- 1 to 12
date.day = d    -- 1 to 31
date.hour = H    -- 0 to 59
date.minute = M  -- 0 to 59
date.second = S  -- 0 to 59
date.isvalid = true
if type(options) == 'table' then
for _, k in ipairs({ 'am', 'era' }) do
if options[k] then
date.options[k] = options[k]
end
end
end
return true
end
end


Line 177: Line 223:
return '(invalid)'
return '(invalid)'
end
end
local shortcuts = {
['%c'] = '%-I:%M %p %-d %B %Y%{era}',  -- date and time: 2:30 pm 1 April 2016
['%x'] = '%-d %B %Y%{era}',            -- date:          1 April 2016
['%X'] = '%-I:%M %p',                  -- time:          2:30 pm
}
local codes = {
local codes = {
a = { field = 'dayabbr' },
a = { field = 'dayabbr' },
Line 184: Line 235:
u = { fmt = '%d'  , field = 'dowiso' },
u = { fmt = '%d'  , field = 'dowiso' },
w = { fmt = '%d'  , field = 'dow' },
w = { fmt = '%d'  , field = 'dow' },
d = { fmt = '%02d', field = 'day' },
d = { fmt = '%02d', fmt2 = '%d', field = 'day' },
m = { fmt = '%02d', field = 'month' },
m = { fmt = '%02d', fmt2 = '%d', field = 'month' },
Y = { fmt = '%04d', field = 'year' },
Y = { fmt = '%04d', fmt2 = '%d', field = 'year' },
H = { fmt = '%02d', field = 'hour' },
H = { fmt = '%02d', fmt2 = '%d', field = 'hour' },
M = { fmt = '%02d', field = 'minute' },
M = { fmt = '%02d', fmt2 = '%d', field = 'minute' },
S = { fmt = '%02d', field = 'second' },
S = { fmt = '%02d', fmt2 = '%d', field = 'second' },
j = { fmt = '%03d', field = 'doy' },
j = { fmt = '%03d', fmt2 = '%d', field = 'doy' },
I = { fmt = '%02d', field = 'hour', special = 'hour12' },
I = { fmt = '%02d', fmt2 = '%d', field = 'hour', special = 'hour12' },
p = { field = 'hour', special = 'am' },
p = { field = 'hour', special = 'am' },
X = { fmt = '%02d:%02d:%02d', field = 'hour', special = 'hms' },
['-d'] = { fmt = '%d', field = 'day' },
['-m'] = { fmt = '%d', field = 'month' },
['-Y'] = { fmt = '%d', field = 'year' },
['-j'] = { fmt = '%d', field = 'doy' },
}
}
options = make_option_table(options or date.options)
options = make_option_table(options or date.options)
local amopt = options.am
local amopt = options.am
local eraopt = options.era
local eraopt = options.era
local function replace_code(id)
local function replace_code(modifier, id)
local code = codes[id]
local code = codes[id]
if code then
if code then
local fmt = code.fmt
local fmt = code.fmt
if modifier == '-' and code.fmt2 then
fmt = code.fmt2