Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lua time class for date math. #871

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 3 additions & 79 deletions gui/petitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ For best experience add following to your ``dfhack*.init``::

local gui = require 'gui'
local widgets = require 'gui.widgets'
local time = require 'time'
local utils = require 'utils'

-- local args = utils.invert({...})
Expand Down Expand Up @@ -40,83 +41,6 @@ local utils = require 'utils'

if not dfhack.world.isFortressMode() then return end

-- from gui/unit-info-viewer.lua
do -- for code folding
--------------------------------------------------
---------------------- 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
--------------------------------------------------
--------------------------------------------------
end

local we = df.global.plotinfo.group_id

local function getAgreementDetails(a)
Expand Down Expand Up @@ -162,9 +86,9 @@ local function getAgreementDetails(a)
sb[#sb+1] = NEWLINE
local expired = false
for _, d in ipairs (a.details) do
local petition_date = Time{year = d.year, ticks = d.year_tick}
local petition_date = time.Time{year = d.year, ticks = d.year_tick}
local petition_date_str = petition_date:getDayStr()..' of '..petition_date:getMonthStr()..' in the year '..tostring(petition_date.year)
local cur_date = Time{year = df.global.cur_year, ticks = df.global.cur_year_tick}
local cur_date = time.Time{year = df.global.cur_year, ticks = df.global.cur_year_tick}
sb[#sb+1] = ("On " .. petition_date_str)
sb[#sb+1] = NEWLINE
local diff = (cur_date - petition_date)
Expand Down
35 changes: 10 additions & 25 deletions list-agreements.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Options:
:help: shows this help screen

]====]

local time = require 'time'

local playerfortid = df.global.plotinfo.site_id -- Player fortress id
local templeagreements = {} -- Table of agreements for temples in player fort
local guildhallagreements = {} -- Table of agreements for guildhalls in player fort
Expand All @@ -54,32 +57,14 @@ function get_location_type(agr)
end

function get_petition_date(agr)
local agr_year = agr.details[0].year
local agr_year_tick = agr.details[0].year_tick
local julian_day = math.floor(agr_year_tick / 1200) + 1
local agr_month = math.floor(julian_day / 28) + 1
local agr_day = julian_day % 28
return string.format("%03d-%02d-%02d",agr_year, agr_month, agr_day)
local agr_time = time.Time{year = agr.details[0].year, ticks = agr.details[0].year_tick}
return string.format("%03d-%02d-%02d",agr_time:getYears(), agr_time:getMonths(), agr_time:getDayInMonth())
end

function get_petition_age(agr)
local agr_year_tick = agr.details[0].year_tick
local agr_year = agr.details[0].year
local cur_year_tick = df.global.cur_year_tick
local cur_year = df.global.cur_year
local del_year, del_year_tick
--delta, check to prevent off by 1 error, not validated
if cur_year_tick > agr_year_tick then
del_year = cur_year - agr_year
del_year_tick = cur_year_tick - agr_year_tick
else
del_year = cur_year - agr_year - 1
del_year_tick = agr_year_tick - cur_year_tick
end
local julian_day = math.floor(del_year_tick / 1200) + 1
local del_month = math.floor(julian_day / 28)
local del_day = julian_day % 28
return {del_year,del_month,del_day}
local agr_time = time.Time{year = agr.details[0].year, ticks = agr.details[0].year_tick}
local cur_time = time.Time{year = df.global.cur_year, ticks = df.global.cur_year_tick}
return cur_time - agr_time
end

function get_guildhall_profession(agr)
Expand Down Expand Up @@ -123,7 +108,7 @@ function is_resolved(agr)
elseif agr.flags.petition_not_accepted then
res = true
res_str = 'denied'
elseif get_petition_age(agr)[1] ~= 0 then
elseif get_petition_age(agr):getYears() >= 1 then
res = true
res_str = 'expired'
end
Expand All @@ -146,7 +131,7 @@ function generate_output(agr,loctype)
return
end

output_str = output_str..'\n\tas agreed on '..get_petition_date(agr)..'. \t'..agr_age[1]..'y, '..agr_age[2]..'m, '..agr_age[3]..'d ago'
output_str = output_str..'\n\tas agreed on '..get_petition_date(agr)..'. \t'..agr_age:getYears()..'y, '..agr_age:getMonths()..'m, '..agr_age:getDayInMonth()..'d ago'

-- can print '(outstanding)' status here, but imho none is cleaner
if not resolved then
Expand Down