Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
local df_date={} --lol I really should put this in its own file
df_date.__eq=function(date1,date2)
return date1.year==date2.year and date1.year_tick==date2.year_tick
end
df_date.__lt=function(date1,date2)
if date1.year<date2.year then return true end
if date1.year>date2.year then return false end
if date1.year==date2.year then
return date1.year_tick<date2.year_tick
end
end
df_date.__le=function(date1,date2)
if date1.year<date2.year then return true end
if date1.year>date2.year then return false end
if date1.year==date2.year then
return date1.year_tick<=date2.year_tick
end
end
df_date.__sub=function(date1,date2)
local newDate={year=date1.year-date2.year,year_tick=date1.year_tick-date2.year_tick}
if newDate.year_tick<0 then
newDate.year=newDate.year-1
newDate.year_tick=newDate.year_tick%403200
end
return newDate
end
df_date.__add=function(date1,date2)
local newDate={year=date1.year+date2.year,year_tick=date1.year_tick+date2.year_tick}
if newDate.year_tick>=403200 then
newDate.year=newDate.year+1
newDate.year_tick=newDate.year_tick%403200
end
return newDate
end
function leading_zero(str,size)
str=tostring(str)
while str:len()<size do
str='0'..str
end
return str
end
df_date.__tostring=function(date)
local month=math.floor(date.year_tick/33600)+1
local day=math.floor((date.year_tick%33600)/1200)+1
return date.year..'-'..leading_zero(month,2)..'-'..leading_zero(day,2)
end
df_date.tostring_comparison=function(date)
local month=math.floor(date.year_tick/33600)
local day=math.floor((date.year_tick%33600)/1200)
local year_string=date.year==1 and ' year, ' or ' years, '
local month_string=month==1 and ' month, ' or ' months, and '
local day_string=day==1 and ' day' or ' days'
return date.year..year_string..month..month_string..day..day_string
end
local function getCurDate()
local cur_date={year=df.global.cur_year,year_tick=df.global.cur_year_tick}
setmetatable(cur_date,df_date)
return cur_date
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment