Skip to content

Instantly share code, notes, and snippets.

@Validark
Created May 17, 2016 20:53
Show Gist options
  • Save Validark/cb14334344374fadaa4f2dc3dc874762 to your computer and use it in GitHub Desktop.
Save Validark/cb14334344374fadaa4f2dc3dc874762 to your computer and use it in GitHub Desktop.
-- Set unix to be the number of seconds since January 1st, 1970
-- function floor rounds down to the nearest whole integer (5.9 becomes 5, 2.2 becomes 2, etc)
-- Get days, month and year
local days = floor(unix / 86400) + 719468
local wday = (days + 3) % 7
local year = floor((days >= 0 and days or days - 146096) / 146097) -- 400 Year bracket
days = (days - year * 146097) -- Days into 400 year bracket [0, 146096]
local years = floor((days - floor(days/1460) + floor(days/36524) - floor(days/146096))/365) -- Years into 400 Year bracket[0, 399]
days = days - (365*years + floor(years/4) - floor(years/100)) -- Days into year (March 1st is first day) [0, 365]
local month = floor((5*days + 2)/153) -- Month of year (March is month 0) [0, 11]
days = days - floor((153*month + 2)/5) + 1 -- Days into month [1, 31]
month = month + (month < 10 and 3 or -9) -- Real life month [1, 12]
year = years + year*400 + (month < 3 and 1 or 0) -- Actual year (Shift 1st month from March to January)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment