code_gs

Date to UNIX timestamp

Oct 20th, 2019
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.30 KB | None | 0 0
  1. local error = error
  2. local isnumber = isnumber
  3. local math_ceil = math.ceil
  4. local math_floor = math.floor
  5. local debug_getinfo = debug.getinfo
  6. local string_format = string.format
  7.  
  8. --[=[ Seconds constants:
  9.     Seconds per minute: 60
  10.     Seconds per hour: 3600
  11.     Seconds per day: 86400
  12.     Seconds per year: 31536000
  13.     Seconds per leap year: 31622400
  14.     Seconds per 4 years (3 years + 1 leap year): 126230400
  15.     Seconds per 100 years (24 4 year sequences + 4 years): 3155673600
  16.     Seconds per 400 years (3 100 year sequences + 25 4 year sequences): 12622780800
  17. ]=]
  18.  
  19. --[=[ Rules:
  20.     60 seconds per minute - seconds represented [0, 59]
  21.     60 minutes per hour - minutes represented [0, 59]
  22.     24 hours per day - hours represented [0, 23]
  23.     tMonths[month] days per month - days represented [1, tMonths[month]]
  24.     12 months per year - months represented [1, 12]
  25.     Year >= 1970
  26.  
  27.     Leap years happen every 4 years starting with 1972
  28.     Leap years are skipped every 100 years starting with 3000
  29.     Leap year skips are skipped every 400 years starting with 2000
  30. ]=]
  31.  
  32. local tMonths = {
  33.     31, -- Janurary
  34.     28, -- Feburary
  35.     31, -- March
  36.     30, -- April
  37.     31, -- May
  38.     30, -- June
  39.     31, -- July
  40.     31, -- August
  41.     30, -- September
  42.     31, -- October
  43.     30, -- November
  44.     31  -- December
  45. }
  46.  
  47. local tMonthsLeap = {
  48.     31, -- Janurary
  49.     29, -- Feburary
  50.     31, -- March
  51.     30, -- April
  52.     31, -- May
  53.     30, -- June
  54.     31, -- July
  55.     31, -- August
  56.     30, -- September
  57.     31, -- October
  58.     30, -- November
  59.     31  -- December
  60. }
  61.  
  62. local tSecondsInMonth = {[0] = 0}
  63. local tSecondsInMonthLeap = {[0] = 0}
  64.  
  65. for i = 1, 12 do
  66.     tSecondsInMonth[i] = tSecondsInMonth[i - 1] + tMonths[i] * 86400
  67.     tSecondsInMonthLeap[i] = tSecondsInMonthLeap[i - 1] + tMonthsLeap[i] * 86400
  68. end
  69.  
  70. -- Only made this a local function for benchmarking
  71. -- It would be faster to embed it inside of math.DateToTimestamp
  72. local function DateToTimestamp(uYear, uMonth, uDay, uHour, uMinute, uSecond)
  73.     -- Separating pre-2000 versus post-2000 is needed since 2000 skipped a leap year skip
  74.     if (uYear < 2000) then
  75.         uYear = uYear - 1970
  76.  
  77.         local uRemainder = uYear % 4
  78.  
  79.         if (uRemainder == 0) then
  80.             return
  81.                 math_floor(uYear / 4) * 126230400
  82.                 + tSecondsInMonth[uMonth - 1]
  83.                 + (uDay - 1) * 86400
  84.                 + uHour * 3600
  85.                 + uMinute * 60
  86.                 + uSecond
  87.         end
  88.  
  89.         if (uRemainder == 1) then
  90.             return 31536000
  91.                 + math_floor(uYear / 4) * 126230400
  92.                 + tSecondsInMonth[uMonth - 1]
  93.                 + (uDay - 1) * 86400
  94.                 + uHour * 3600
  95.                 + uMinute * 60
  96.                 + uSecond
  97.         end
  98.  
  99.         if (uRemainder == 2) then -- First leap year since 1970 was 1972, so this is a leap year
  100.             return 63072000 --[[31536000 * 2]]
  101.                 + math_floor(uYear / 4) * 126230400
  102.                 + tSecondsInMonthLeap[uMonth - 1]
  103.                 + (uDay - 1) * 86400
  104.                 + uHour * 3600
  105.                 + uMinute * 60
  106.                 + uSecond
  107.         end
  108.  
  109.         return 94694400 --[[31536000 * 2 + 31622400]]
  110.             + math_floor(uYear / 4) * 126230400
  111.             + tSecondsInMonth[uMonth - 1]
  112.             + (uDay - 1) * 86400
  113.             + uHour * 3600
  114.             + uMinute * 60
  115.             + uSecond
  116.     end
  117.  
  118.     uYear = uYear - 2000
  119.  
  120.     local u400s = math_floor(uYear / 400)
  121.     uYear = uYear - u400s
  122.  
  123.     local u100s = math_floor(uYear / 100)
  124.     uYear = uYear - u100s
  125.  
  126.     local uRemainder = uYear % 4
  127.  
  128.     -- Currently a leap year
  129.     if (uRemainder == 0 and (uYear % 100 ~= 0 or uYear % 400 == 0)) then
  130.         return 946684800
  131.             + u400s * 12622780800
  132.             + u100s * 3155673600
  133.             + math_floor(uYear / 4) * 126230400
  134.             + uRemainder * 31536000
  135.             + tSecondsInMonthLeap[uMonth - 1]
  136.             + (uDay - 1) * 86400
  137.             + uHour * 3600
  138.             + uMinute * 60
  139.             + uSecond
  140.     end
  141.  
  142.     return 946684800                        -- 1/1/2000 @ 00:00:00 UTC
  143.         + u400s * 12622780800               -- 400 years
  144.         + u100s * 3155673600                -- 100 years
  145.         + math_floor(uYear / 4) * 126230400 -- 4 years
  146.         + uRemainder * 31536000             -- Remainder years [0, 3]
  147.         + tSecondsInMonth[uMonth - 1]       -- Months
  148.         + (uDay - 1) * 86400                -- Days
  149.         + uHour * 3600                      -- Hours
  150.         + uMinute * 60                      -- Minutes
  151.         + uSecond                           -- Seconds
  152. end
  153.  
  154. local function GetFormat(sSubError)
  155.     local tDebug = debug_getinfo(3)
  156.  
  157.     if (tDebug == nil) then
  158.         return "bad argument #%u (" .. sSubError .. ")"
  159.     end
  160.  
  161.     local sName = tDebug.name
  162.  
  163.     if (tDebug.name == nil) then
  164.         return "bad argument #%u (" .. sSubError .. ")"
  165.     end
  166.  
  167.     return "bad argument #%u to '" .. sName .. "' (" .. sSubError .. ")"
  168. end
  169.  
  170. -- Unnecessary for math.DateToTimestamp specficially but can support negative years in the future
  171. local function Truncate(num)
  172.     if (num < 0) then
  173.         return math_ceil(num)
  174.     end
  175.  
  176.     return math_floor(num)
  177. end
  178.  
  179. local function AssertRange(num, uArg, iMin, iMax)
  180.     if (not isnumber(num)) then
  181.         error(string_format(GetFormat("number expected, got " .. type(num)), uArg), 3)
  182.     end
  183.  
  184.     if (iMin ~= nil and num < iMin or iMax ~= nil and num > iMax) then
  185.         error(string_format(GetFormat("number [%d, %d] expected, got '%d'"), uArg, iMin, iMax, num), 3)
  186.     end
  187.  
  188.     return Truncate(num)
  189. end
  190.  
  191. function math.DateToTimestamp(uYear, uMonth, uDay, uHour, uMinute, uSecond)
  192.     uYear = AssertRange(num, 1, 1970)
  193.     uMonth = AssertRange(num, 2, 1, 12)
  194.     uDay = AssertRange(num, 3, 1, (uYear % 4 == 0 and (uYear % 100 ~= 0 or uYear % 400 == 0)) and tMonthsLeap[uMonth] or tMonths[uMonth])
  195.     uHour = AssertRange(num, 4, 0, 23)
  196.     uMinute = AssertRange(num, 5, 0, 59)
  197.     uSecond = AssertRange(num, 6, 0, 59)
  198.  
  199.     return DateToTimestamp(uYear, uMonth, uDay, uHour, uMinute, uSecond)
  200. end
Advertisement
Add Comment
Please, Sign In to add comment