Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local error = error
- local isnumber = isnumber
- local math_ceil = math.ceil
- local math_floor = math.floor
- local debug_getinfo = debug.getinfo
- local string_format = string.format
- --[=[ Seconds constants:
- Seconds per minute: 60
- Seconds per hour: 3600
- Seconds per day: 86400
- Seconds per year: 31536000
- Seconds per leap year: 31622400
- Seconds per 4 years (3 years + 1 leap year): 126230400
- Seconds per 100 years (24 4 year sequences + 4 years): 3155673600
- Seconds per 400 years (3 100 year sequences + 25 4 year sequences): 12622780800
- ]=]
- --[=[ Rules:
- 60 seconds per minute - seconds represented [0, 59]
- 60 minutes per hour - minutes represented [0, 59]
- 24 hours per day - hours represented [0, 23]
- tMonths[month] days per month - days represented [1, tMonths[month]]
- 12 months per year - months represented [1, 12]
- Year >= 1970
- Leap years happen every 4 years starting with 1972
- Leap years are skipped every 100 years starting with 3000
- Leap year skips are skipped every 400 years starting with 2000
- ]=]
- local tMonths = {
- 31, -- Janurary
- 28, -- Feburary
- 31, -- March
- 30, -- April
- 31, -- May
- 30, -- June
- 31, -- July
- 31, -- August
- 30, -- September
- 31, -- October
- 30, -- November
- 31 -- December
- }
- local tMonthsLeap = {
- 31, -- Janurary
- 29, -- Feburary
- 31, -- March
- 30, -- April
- 31, -- May
- 30, -- June
- 31, -- July
- 31, -- August
- 30, -- September
- 31, -- October
- 30, -- November
- 31 -- December
- }
- local tSecondsInMonth = {[0] = 0}
- local tSecondsInMonthLeap = {[0] = 0}
- for i = 1, 12 do
- tSecondsInMonth[i] = tSecondsInMonth[i - 1] + tMonths[i] * 86400
- tSecondsInMonthLeap[i] = tSecondsInMonthLeap[i - 1] + tMonthsLeap[i] * 86400
- end
- -- Only made this a local function for benchmarking
- -- It would be faster to embed it inside of math.DateToTimestamp
- local function DateToTimestamp(uYear, uMonth, uDay, uHour, uMinute, uSecond)
- -- Separating pre-2000 versus post-2000 is needed since 2000 skipped a leap year skip
- if (uYear < 2000) then
- uYear = uYear - 1970
- local uRemainder = uYear % 4
- if (uRemainder == 0) then
- return
- math_floor(uYear / 4) * 126230400
- + tSecondsInMonth[uMonth - 1]
- + (uDay - 1) * 86400
- + uHour * 3600
- + uMinute * 60
- + uSecond
- end
- if (uRemainder == 1) then
- return 31536000
- + math_floor(uYear / 4) * 126230400
- + tSecondsInMonth[uMonth - 1]
- + (uDay - 1) * 86400
- + uHour * 3600
- + uMinute * 60
- + uSecond
- end
- if (uRemainder == 2) then -- First leap year since 1970 was 1972, so this is a leap year
- return 63072000 --[[31536000 * 2]]
- + math_floor(uYear / 4) * 126230400
- + tSecondsInMonthLeap[uMonth - 1]
- + (uDay - 1) * 86400
- + uHour * 3600
- + uMinute * 60
- + uSecond
- end
- return 94694400 --[[31536000 * 2 + 31622400]]
- + math_floor(uYear / 4) * 126230400
- + tSecondsInMonth[uMonth - 1]
- + (uDay - 1) * 86400
- + uHour * 3600
- + uMinute * 60
- + uSecond
- end
- uYear = uYear - 2000
- local u400s = math_floor(uYear / 400)
- uYear = uYear - u400s
- local u100s = math_floor(uYear / 100)
- uYear = uYear - u100s
- local uRemainder = uYear % 4
- -- Currently a leap year
- if (uRemainder == 0 and (uYear % 100 ~= 0 or uYear % 400 == 0)) then
- return 946684800
- + u400s * 12622780800
- + u100s * 3155673600
- + math_floor(uYear / 4) * 126230400
- + uRemainder * 31536000
- + tSecondsInMonthLeap[uMonth - 1]
- + (uDay - 1) * 86400
- + uHour * 3600
- + uMinute * 60
- + uSecond
- end
- return 946684800 -- 1/1/2000 @ 00:00:00 UTC
- + u400s * 12622780800 -- 400 years
- + u100s * 3155673600 -- 100 years
- + math_floor(uYear / 4) * 126230400 -- 4 years
- + uRemainder * 31536000 -- Remainder years [0, 3]
- + tSecondsInMonth[uMonth - 1] -- Months
- + (uDay - 1) * 86400 -- Days
- + uHour * 3600 -- Hours
- + uMinute * 60 -- Minutes
- + uSecond -- Seconds
- end
- local function GetFormat(sSubError)
- local tDebug = debug_getinfo(3)
- if (tDebug == nil) then
- return "bad argument #%u (" .. sSubError .. ")"
- end
- local sName = tDebug.name
- if (tDebug.name == nil) then
- return "bad argument #%u (" .. sSubError .. ")"
- end
- return "bad argument #%u to '" .. sName .. "' (" .. sSubError .. ")"
- end
- -- Unnecessary for math.DateToTimestamp specficially but can support negative years in the future
- local function Truncate(num)
- if (num < 0) then
- return math_ceil(num)
- end
- return math_floor(num)
- end
- local function AssertRange(num, uArg, iMin, iMax)
- if (not isnumber(num)) then
- error(string_format(GetFormat("number expected, got " .. type(num)), uArg), 3)
- end
- if (iMin ~= nil and num < iMin or iMax ~= nil and num > iMax) then
- error(string_format(GetFormat("number [%d, %d] expected, got '%d'"), uArg, iMin, iMax, num), 3)
- end
- return Truncate(num)
- end
- function math.DateToTimestamp(uYear, uMonth, uDay, uHour, uMinute, uSecond)
- uYear = AssertRange(num, 1, 1970)
- uMonth = AssertRange(num, 2, 1, 12)
- uDay = AssertRange(num, 3, 1, (uYear % 4 == 0 and (uYear % 100 ~= 0 or uYear % 400 == 0)) and tMonthsLeap[uMonth] or tMonths[uMonth])
- uHour = AssertRange(num, 4, 0, 23)
- uMinute = AssertRange(num, 5, 0, 59)
- uSecond = AssertRange(num, 6, 0, 59)
- return DateToTimestamp(uYear, uMonth, uDay, uHour, uMinute, uSecond)
- end
Advertisement
Add Comment
Please, Sign In to add comment