Module:Date: Difference between revisions
major refactor, mostly working now; add/subtract with a date; remove template handling which will be in Module:Age
(tweak formatted output; extract_date to parse an input date string) |
(major refactor, mostly working now; add/subtract with a date; remove template handling which will be in Module:Age) |
||
| Line 19: | Line 19: | ||
local function strip_to_nil(text) | local function strip_to_nil(text) | ||
-- If text is a string, return its trimmed content, or nil. | |||
-- If text is a | -- Otherwise return text (convenient when Date fields are provided from | ||
-- Otherwise return text (convenient when | -- another module which is able to pass, for example, a number). | ||
if type(text) == 'string' then | if type(text) == 'string' then | ||
text = text:match('(%S.-)%s*$') | |||
end | end | ||
return text | return text | ||
| Line 63: | Line 56: | ||
-- Return jd, jdz from a Julian or Gregorian calendar date where | -- Return jd, jdz from a Julian or Gregorian calendar date where | ||
-- jd = Julian date and its fractional part is zero at noon | -- jd = Julian date and its fractional part is zero at noon | ||
-- jdz = | -- jdz = same, but assume time is 00:00:00 if no time given | ||
-- http://www.tondering.dk/claus/cal/julperiod.php#formula | -- http://www.tondering.dk/claus/cal/julperiod.php#formula | ||
-- Testing shows this works for all dates from year -9999 to 9999! | -- Testing shows this works for all dates from year -9999 to 9999! | ||
| Line 69: | Line 62: | ||
-- 1 January 4713 BC = (-4712, 1, 1) Julian calendar | -- 1 January 4713 BC = (-4712, 1, 1) Julian calendar | ||
-- 24 November 4714 BC = (-4713, 11, 24) Gregorian calendar | -- 24 November 4714 BC = (-4713, 11, 24) Gregorian calendar | ||
local floor = math.floor | local floor = math.floor | ||
local offset | local offset | ||
| Line 82: | Line 72: | ||
end | end | ||
local m = date.month + 12*a - 3 | local m = date.month + 12*a - 3 | ||
local | local jd = date.day + floor((153*m + 2)/5) + 365*y + offset | ||
if date.hastime then | if date.hastime then | ||
jd = jd + (date.hour + (date.minute + date.second / 60) /60) / 24 - 0.5 | |||
return jd, jd | |||
end | end | ||
return jd, jd - 0.5 | |||
return jd, jd | |||
end | end | ||
local function set_date_from_jd(date) | local function set_date_from_jd(date) | ||
-- Set the fields of table date from its Julian date field. | -- Set the fields of table date from its Julian date field. | ||
-- Return true if date is valid. | |||
-- http://www.tondering.dk/claus/cal/julperiod.php#formula | -- http://www.tondering.dk/claus/cal/julperiod.php#formula | ||
-- This handles the proleptic Julian and Gregorian calendars. | -- This handles the proleptic Julian and Gregorian calendars. | ||
| Line 112: | Line 98: | ||
end | end | ||
if not (limits[1] <= jd and jd <= limits[2]) then | if not (limits[1] <= jd and jd <= limits[2]) then | ||
return | return | ||
end | end | ||
local jdn = floor(jd) | local jdn = floor(jd) | ||
if date.hastime then | if date.hastime then | ||
| Line 152: | Line 136: | ||
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) | ||
return true | |||
end | end | ||
| Line 189: | Line 174: | ||
date.minute = M -- 0 to 59 | date.minute = M -- 0 to 59 | ||
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' }) do | ||
| Line 200: | Line 184: | ||
end | end | ||
local function make_option_table( | local function make_option_table(options1, options2) | ||
-- If | -- If options1 is a string, return a table with its settings, or | ||
-- | -- if it is a table, use its settings. | ||
if type( | |||