Created
July 9, 2017 11:29
-
-
Save prafulliu/dde360f2b7027dbb6d509d23a156b61a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JAN = 31 | |
daysFeb = {28, 29} | |
MAR = 31 | |
APR = 30 | |
MAY = 31 | |
JUN = 30 | |
JUL = 31 | |
AUG = 31 | |
SEPT = 30 | |
OCT = 31 | |
NOV = 30 | |
DEC = 31 | |
MONTH = {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEPT, OCT, NOV, DEC} | |
SUN = 1 | |
MON = 2 | |
TUE = 3 | |
WED = 4 | |
THU = 5 | |
FRI = 6 | |
SAT = 7 | |
WEEK = {SUN, MON, TUE, WED, THU, FRI, SAT} | |
EVEN = 1 | |
LEAP = 2 | |
EVENYEAR = 365 | |
LEAPYEAR = 366 | |
DAYSPERYEAR = {EVENYEAR, LEAPYEAR} | |
DAYSPERYEAR.isLeapYear = function(year) | |
if ((((year % 4 ==0) and (year % 100~=0)) or (year % 400==0)))then | |
daysOfYear = DAYSPERYEAR[LEAP] | |
MONTH[2] = daysFeb[LEAP] | |
else | |
daysOfYear = DAYSPERYEAR[EVEN] | |
MONTH[2] = daysFeb[EVEN] | |
end | |
return daysOfYear | |
end | |
function getDaysLeft(y,m,d) | |
daysOfYear = DAYSPERYEAR.isLeapYear(y) | |
monEnd = m-1 | |
monStart = 1 | |
local daysLeft = 0 | |
for i = monStart, monEnd do | |
daysLeft = daysLeft + MONTH[i] | |
end | |
daysLeft = daysLeft + d | |
daysLeft = daysOfYear - daysLeft | |
return daysLeft, daysOfYear | |
end | |
function processInMonthLevel(day, y, m, d) | |
local retDate = {} | |
--print("day", day, "MONTH[", m,"]", MONTH[m], "d", d, "m - d",MONTH[m]-d) | |
if ( day <= MONTH[m]-d) then | |
--还在本月之内 | |
d = d + day | |
retDate["year"] = y | |
retDate["month"] = m | |
retDate["day"] = d | |
else | |
--在本月之外 | |
day = day - MONTH[m] + d | |
m = m + 1 | |
--print(MONTH[2]) | |
while(day > MONTH[m]) do | |
day = day - MONTH[m] | |
m = m + 1 | |
end | |
d = day | |
retDate["year"] = y | |
retDate["month"] = m | |
retDate["day"] = d | |
end | |
return retDate | |
end | |
function getDate(day, startDate) | |
retDate = {} | |
if (startDate == nil) then | |
myDate = os.date("%Y/%m/%d") | |
else | |
myDate = startDate | |
end | |
m = tonumber(string.sub(myDate, 6,7)) | |
d = tonumber(string.sub(myDate, 9,10)) | |
y = tonumber(string.sub(myDate, 1,4)) | |
daysLeft, daysOfYear = getDaysLeft(y,m,d) | |
if(day <= daysLeft) then | |
-- 还在y年之内 | |
retDate = processInMonthLevel(day,y, m, d) | |
else | |
-- 已经出了y年之外 | |
day = day - daysLeft | |
y = y + 1 | |
daysOfYear = DAYSPERYEAR.isLeapYear(y) | |
while( day > daysOfYear ) do | |
day = day - daysOfYear | |
y = y + 1 | |
daysOfYear = DAYSPERYEAR.isLeapYear(y) | |
end | |
m = 1 | |
d = 0 | |
retDate = processInMonthLevel(day, y, m, d) | |
end | |
if(retDate.month < 10) then | |
retDate.month = "0" .. tostring(retDate.month) | |
end | |
if(retDate.day < 10) then | |
retDate.day = "0" .. tostring(retDate.day) | |
end | |
retdate = retDate.year .. "-" .. retDate.month .. "-" .. retDate.day | |
return retdate | |
end | |
function date2time(luaDate) | |
if (luaDate == nil or type(luaDate) ~= "string") then | |
return; | |
else | |
local year = string.match(luaDate, "%d+") | |
year = tonumber(year) | |
local month = string.match(luaDate, "-%d+") | |
month = string.gsub(month, "-", "") | |
month = tonumber(month) | |
local day = string.match(luaDate, "%d+%s") | |
day = string.gsub(day, "%s", "") | |
day = tonumber(day) | |
local hour = string.match(luaDate, "%d+:") | |
hour = string.gsub(hour, ":", "") | |
hour = tonumber(hour) | |
local minite = string.match(luaDate, ":%d+:") | |
minite = string.gsub(minite, ":", "") | |
minite = tonumber(minite) | |
local second = string.match(luaDate, ":%d+:%d+") | |
second = string.gsub(second, minite, "") | |
second = string.gsub(second, ":", "") | |
second = tonumber(second) | |
print(year, month, day, hour, minite, second) | |
print(type(year),type(month), type(day), type(hour), type(minite), type(second)) | |
if ( (type(month) == "number" and month>=1 and month<= 12) | |
and (type(day) == "number" and day>=1 and day <=31) | |
and (type(hour) == "number" and hour>=0 and hour <=23) | |
and(type(minite) == "number" and minite>=0 and minite<=59) | |
and (type(second) == "number" and second >=0 and second <= 59)) then | |
return os.time{year = year, month = month, day = day, hour = hour, min = minite, sec = second} | |
else | |
return; | |
end | |
end | |
end | |
function time2date(luaTime) | |
if (luaTime == nil or type(luaTime) ~= "number") then | |
return; | |
else | |
return os.date("%Y-%m-%d %H:%M:%S", luaTime) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment