|
| 1 | +local _ENV = mkmodule('time') |
| 2 | + |
| 3 | +local TU_PER_DAY = 1200 |
| 4 | +--[[ |
| 5 | +if advmode then TU_PER_DAY = 86400 ? or only for cur_year_tick? |
| 6 | +advmod_TU / 72 = ticks |
| 7 | +--]] |
| 8 | +local TU_PER_MONTH = TU_PER_DAY * 28 |
| 9 | +local TU_PER_YEAR = TU_PER_MONTH * 12 |
| 10 | + |
| 11 | +local MONTHS = { |
| 12 | + 'Granite', |
| 13 | + 'Slate', |
| 14 | + 'Felsite', |
| 15 | + 'Hematite', |
| 16 | + 'Malachite', |
| 17 | + 'Galena', |
| 18 | + 'Limestone', |
| 19 | + 'Sandstone', |
| 20 | + 'Timber', |
| 21 | + 'Moonstone', |
| 22 | + 'Opal', |
| 23 | + 'Obsidian', |
| 24 | +} |
| 25 | + |
| 26 | +Time = defclass(Time) |
| 27 | +function Time:init(args) |
| 28 | + self.year = args.year or 0 |
| 29 | + self.ticks = args.ticks or 0 |
| 30 | +end |
| 31 | +function Time:getDays() -- >>float<< Days as age (including years) |
| 32 | + return self.year * 336 + (self.ticks / TU_PER_DAY) |
| 33 | +end |
| 34 | +function Time:getDayInMonth() |
| 35 | + return math.floor ( (self.ticks % TU_PER_MONTH) / TU_PER_DAY ) + 1 |
| 36 | +end |
| 37 | +function Time:getMonths() -- >>int<< Months as age (not including years) |
| 38 | + return math.floor (self.ticks / TU_PER_MONTH) |
| 39 | +end |
| 40 | +function Time:getYears() -- >>int<< |
| 41 | + return self.year |
| 42 | +end |
| 43 | +function Time:getMonthStr() -- Month as date |
| 44 | + return MONTHS[self:getMonths()+1] or 'error' |
| 45 | +end |
| 46 | +function Time:getDayStr() -- Day as date |
| 47 | + local d = math.floor ( (self.ticks % TU_PER_MONTH) / TU_PER_DAY ) + 1 |
| 48 | + if d == 11 or d == 12 or d == 13 then |
| 49 | + d = tostring(d)..'th' |
| 50 | + elseif d % 10 == 1 then |
| 51 | + d = tostring(d)..'st' |
| 52 | + elseif d % 10 == 2 then |
| 53 | + d = tostring(d)..'nd' |
| 54 | + elseif d % 10 == 3 then |
| 55 | + d = tostring(d)..'rd' |
| 56 | + else |
| 57 | + d = tostring(d)..'th' |
| 58 | + end |
| 59 | + return d |
| 60 | +end |
| 61 | +--function Time:__add() |
| 62 | +--end |
| 63 | +function Time:__sub(other) |
| 64 | + if DEBUG then print(self.year,self.ticks) end |
| 65 | + if DEBUG then print(other.year,other.ticks) end |
| 66 | + if self.ticks < other.ticks then |
| 67 | + return Time{ year = (self.year - other.year - 1) , ticks = (TU_PER_YEAR + self.ticks - other.ticks) } |
| 68 | + else |
| 69 | + return Time{ year = (self.year - other.year) , ticks = (self.ticks - other.ticks) } |
| 70 | + end |
| 71 | +end |
| 72 | + |
| 73 | +return _ENV |
0 commit comments