Jump to content

Module:Date: Difference between revisions

237 bytes added ,  8 years ago
when parse a date, replace nbsp with space and record input format (dmy/mdy/ymd) in date.format
(use error(...) to report caller's errors)
(when parse a date, replace nbsp with space and record input format (dmy/mdy/ymd) in date.format)
Line 187: Line 187:
date.second = S  -- 0 to 59
date.second = S  -- 0 to 59
if type(options) == 'table' then
if type(options) == 'table' then
for _, k in ipairs({ 'am', 'era' }) do
for _, k in ipairs({ 'am', 'era', 'format' }) do
if options[k] then
if options[k] then
date.options[k] = options[k]
date.options[k] = options[k]
Line 380: Line 380:
error('date:text: need a date (use "date:text()" with a colon)', 2)
error('date:text: need a date (use "date:text()" with a colon)', 2)
end
end
if type(fmt) ~= 'string' then
if type(fmt) == 'string' and fmt:match('%S') then
if fmt:find('%', 1, true) then
return strftime(date, fmt, options)
end
else
fmt = 'dmy'
fmt = 'dmy'
if date.hastime then
if date.hastime then
fmt = (date.second > 0 and 'hms ' or 'hm ') .. fmt
fmt = (date.second > 0 and 'hms ' or 'hm ') .. fmt
end
end
elseif fmt:find('%', 1, true) then
return strftime(date, fmt, options)
end
end
local function hm_fmt()
local function hm_fmt()
Line 608: Line 610:
-- Parse the date/time in text and return n, o where
-- Parse the date/time in text and return n, o where
--  n = table of numbers with date/time fields
--  n = table of numbers with date/time fields
--  o = table of options for AM/PM or AD/BC, if any
--  o = table of options for AM/PM or AD/BC or format, if any
-- or return nothing if date is known to be invalid.
-- or return nothing if date is known to be invalid.
-- Caller determines if the values in n are valid.
-- Caller determines if the values in n are valid.
Line 615: Line 617:
-- ('0001' to '9999'). The only way to enter year <= 0 is by specifying
-- ('0001' to '9999'). The only way to enter year <= 0 is by specifying
-- the date as three numeric parameters like ymd Date(-1, 1, 1).
-- the date as three numeric parameters like ymd Date(-1, 1, 1).
-- Dates of form d/m/y, m/d/y, y/m/d are rejected as ambiguous.
-- Dates of form d/m/y, m/d/y, y/m/d are rejected as potentially ambiguous.
local date, options = {}, {}
local date, options = {}, {}
local function extract_ymd(item)
local function extract_ymd(item)
Line 627: Line 629:
end
end
if m then
if m then
options.format = 'ymd'
date.year = tonumber(ystr)
date.year = tonumber(ystr)
date.month = m
date.month = m
Line 635: Line 638:
end
end
local function extract_month(item)
local function extract_month(item)
-- A month must be given as a name or abbreviation; a number would be ambiguous.
-- A month must be given as a name or abbreviation; a number could be ambiguous.
local m = month_number(item)
local m = month_number(item)
if m then
if m then
Line 682: Line 685:
end
end
end
end
for item in text:gsub(',', ' '):gmatch('%S+') do
for item in text:gsub(',', ' '):gsub('&nbsp;', ' '):gmatch('%S+') do
item_count = item_count + 1
item_count = item_count + 1
if era_text[item] then
if era_text[item] then
Line 719: Line 722:
if item:match('^(%d%d?)$') then
if item:match('^(%d%d?)$') then
date.day = tonumber(item)
date.day = tonumber(item)
elseif not extract_month(item) then
options.format = 'dmy'
elseif extract_month(item) then
options.format = 'mdy'
else
return
return
end
end
Line 839: Line 845:
-- Era text (never a negative sign) from year and options.
-- Era text (never a negative sign) from year and options.
value = get_era_for_year(self.options.era, self.year)
value = get_era_for_year(self.options.era, self.year)
elseif key == 'format' then
value = self.options.format or 'dmy'
elseif key == 'gsd' then
elseif key == 'gsd' then
-- GSD = 1 from 00:00:00 to 23:59:59 on 1 January 1 AD Gregorian calendar,
-- GSD = 1 from 00:00:00 to 23:59:59 on 1 January 1 AD Gregorian calendar,
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.