Skip to content

Commit

Permalink
add df time lua class
Browse files Browse the repository at this point in the history
  • Loading branch information
dhthwy committed Oct 20, 2023
1 parent ca16620 commit f61e91a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
73 changes: 73 additions & 0 deletions library/lua/time.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
local _ENV = mkmodule('time')

local TU_PER_DAY = 1200
--[[
if advmode then TU_PER_DAY = 86400 ? or only for cur_year_tick?
advmod_TU / 72 = ticks
--]]
local TU_PER_MONTH = TU_PER_DAY * 28
local TU_PER_YEAR = TU_PER_MONTH * 12

local MONTHS = {
'Granite',
'Slate',
'Felsite',
'Hematite',
'Malachite',
'Galena',
'Limestone',
'Sandstone',
'Timber',
'Moonstone',
'Opal',
'Obsidian',
}

Time = defclass(Time)
function Time:init(args)
self.year = args.year or 0
self.ticks = args.ticks or 0
end
function Time:getDays() -- >>float<< Days as age (including years)
return self.year * 336 + (self.ticks / TU_PER_DAY)
end
function Time:getDayInMonth()
return math.floor ( (self.ticks % TU_PER_MONTH) / TU_PER_DAY ) + 1
end
function Time:getMonths() -- >>int<< Months as age (not including years)
return math.floor (self.ticks / TU_PER_MONTH)
end
function Time:getYears() -- >>int<<
return self.year
end
function Time:getMonthStr() -- Month as date
return MONTHS[self:getMonths()+1] or 'error'
end
function Time:getDayStr() -- Day as date
local d = math.floor ( (self.ticks % TU_PER_MONTH) / TU_PER_DAY ) + 1
if d == 11 or d == 12 or d == 13 then
d = tostring(d)..'th'
elseif d % 10 == 1 then
d = tostring(d)..'st'
elseif d % 10 == 2 then
d = tostring(d)..'nd'
elseif d % 10 == 3 then
d = tostring(d)..'rd'
else
d = tostring(d)..'th'
end
return d
end
--function Time:__add()
--end
function Time:__sub(other)
if DEBUG then print(self.year,self.ticks) end
if DEBUG then print(other.year,other.ticks) end
if self.ticks < other.ticks then
return Time{ year = (self.year - other.year - 1) , ticks = (TU_PER_YEAR + self.ticks - other.ticks) }
else
return Time{ year = (self.year - other.year) , ticks = (self.ticks - other.ticks) }
end
end

return _ENV
2 changes: 1 addition & 1 deletion scripts

0 comments on commit f61e91a

Please sign in to comment.