Editing Module:Date
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 897: | Line 897: | ||
end | end | ||
return date, options | return date, options | ||
end | end | ||
Line 1,130: | Line 1,102: | ||
-- Return true if dates identify same date/time where, for example, | -- Return true if dates identify same date/time where, for example, | ||
-- Date(-4712, 1, 1, 'Julian') == Date(-4713, 11, 24, 'Gregorian') is true. | -- Date(-4712, 1, 1, 'Julian') == Date(-4713, 11, 24, 'Gregorian') is true. | ||
-- This is called | -- This is only called if lhs and rhs have the same metatable. | ||
if lhs.partial or rhs.partial then | if lhs.partial or rhs.partial then | ||
-- One date is partial; the other is a partial or a full date. | -- One date is partial; the other is a partial or a full date. | ||
Line 1,142: | Line 1,114: | ||
-- Return true if lhs < rhs, for example, | -- Return true if lhs < rhs, for example, | ||
-- Date('1 Jan 2016') < Date('06:00 1 Jan 2016') is true. | -- Date('1 Jan 2016') < Date('06:00 1 Jan 2016') is true. | ||
-- This is called | -- This is only called if lhs and rhs have the same metatable. | ||
if lhs.partial or rhs.partial then | if lhs.partial or rhs.partial then | ||
-- One date is partial; the other is a partial or a full date. | -- One date is partial; the other is a partial or a full date. | ||
Line 1,188: | Line 1,160: | ||
options = {}, | options = {}, | ||
list = _date_list, | list = _date_list, | ||
text = _date_text, | text = _date_text, | ||
} | } | ||
Line 1,360: | Line 1,329: | ||
local function choose(v) | local function choose(v) | ||
if type(v) == 'table' then | if type(v) == 'table' then | ||
if not wantrange | if not wantrange then | ||
-- Example: Date('partial', 2005) - Date('partial', 2001) gives | -- Example: Date('partial', 2005) - Date('partial', 2001) gives | ||
-- diff.years = { 3, 4 } to show the range of possible results. | -- diff.years = { 3, 4 } to show the range of possible results. | ||
Line 1,381: | Line 1,350: | ||
if code == 'y' then | if code == 'y' then | ||
return choose(diff.partial.years) | return choose(diff.partial.years) | ||
end | end | ||
return nil | return nil | ||
Line 1,400: | Line 1,366: | ||
return floor(days/7 + offset) | return floor(days/7 + offset) | ||
end | end | ||
local H, M | local H, M = diff.hours, diff.minutes | ||
if code == 'dh' or code == 'dhm | if code == 'dh' or code == 'dhm' or code == 'h' or code == 'hm' then | ||
local days = floor(diff.age_days + extra_days) | local days = floor(diff.age_days + extra_days) | ||
local inc_hour | local inc_hour | ||
Line 1,409: | Line 1,375: | ||
inc_hour = true | inc_hour = true | ||
end | end | ||
elseif | elseif diff.seconds >= 30 then | ||
M = M + 1 | |||
if M >= 60 then | |||
M = 0 | |||
inc_hour = true | |||
end | end | ||
end | end | ||
if inc_hour then | if inc_hour then | ||
Line 1,432: | Line 1,390: | ||
end | end | ||
end | end | ||
if code == 'dh' | if code == 'dh' then | ||
return days, H | |||
elseif code == 'h' or code == 'hm' then | |||
local hours = days * 24 + H | |||
return | if code == 'h' then | ||
return hours | |||
end | end | ||
return hours, M | return hours, M | ||
end | end | ||
return | return days, H, M | ||
end | end | ||
if wantround then | if wantround then | ||
Line 1,462: | Line 1,408: | ||
end | end | ||
elseif code == 'ymdhm' or code == 'ymwdhm' then | elseif code == 'ymdhm' or code == 'ymwdhm' then | ||
if | if diff.seconds >= 30 then | ||
M = M + 1 | M = M + 1 | ||
if M >= 60 then | if M >= 60 then | ||
Line 1,579: | Line 1,525: | ||
} | } | ||
function DateDiff(date1, date2 | function DateDiff(date1, date2) -- for forward declaration above | ||
-- Return a table with the difference between two dates (date1 - date2). | -- Return a table with the difference between two dates (date1 - date2). | ||
-- The difference is negative if date1 is older than date2. | -- The difference is negative if date1 is older than date2. | ||
Line 1,594: | Line 1,540: | ||
if not (is_date(date1) and is_date(date2) and date1.calendar == date2.calendar) then | if not (is_date(date1) and is_date(date2) and date1.calendar == date2.calendar) then | ||
return | return | ||
end | end | ||
local isnegative = false | local isnegative = false | ||
Line 1,616: | Line 1,558: | ||
-------------A===B------------------------- A=2001-04-01 B=2001-04-30 | -------------A===B------------------------- A=2001-04-01 B=2001-04-30 | ||
--------C=====================D------------ C=2001-01-01 D=2001-12-31 | --------C=====================D------------ C=2001-01-01 D=2001-12-31 | ||
local function zdiff(date1, date2) | |||
local diff = date1 - date2 | |||
if diff.isnegative then | |||
return { years = 0, months = 0 } | |||
return | |||
end | end | ||
local function getdate(date, which) | return diff | ||
end | |||
local function getdate(date, which) | |||
return date.partial and date.partial[which] or date | |||
end | |||
local maxdiff = zdiff(getdate(date1, 'last'), getdate(date2, 'first')) | |||
local mindiff = zdiff(getdate(date1, 'first'), getdate(date2, 'last')) | |||
local years, months | |||
if maxdiff.years == mindiff.years then | |||
years = maxdiff.years | |||
if maxdiff.months == mindiff.months then | |||
months = maxdiff.months | |||
else | else | ||
months = { mindiff.months, maxdiff.months } | |||
end | end | ||
else | |||
years = { mindiff.years, maxdiff.years } | |||
end | end | ||
return setmetatable({ | |||
date1 = date1, | |||
date2 = date2, | |||
partial = { years = years, months = months }, | |||
isnegative = isnegative, | |||
iszero = iszero, | |||
age = _diff_age, | |||
duration = _diff_duration, | |||
}, diffmt) | |||
end | end | ||
local y1, m1 = date1.year, date1.month | local y1, m1 = date1.year, date1.month |