Skip to content

Instantly share code, notes, and snippets.

@chuckwagoncomputing
Created December 27, 2016 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckwagoncomputing/108825eeb50ba5452dfd572b42c56432 to your computer and use it in GitHub Desktop.
Save chuckwagoncomputing/108825eeb50ba5452dfd572b42c56432 to your computer and use it in GitHub Desktop.
A function to get fractional age for my conky
function conky_get_age(year, month, day)
if conky_window == nil then
return
end
daystoday = os.date("%j")
daysatbirth = os.date("%j", os.time{year=year, month=month, day=day})
yearsold = os.date("%Y") - os.date("%Y", os.time{year=year, month=month, day=day})
if daysatbirth > daystoday then
yearsold = math.floor(yearsold - 1)
dayssince = (365 - daysatbirth) + daystoday
else
dayssince = daystoday - daysatbirth
end
if dayssince == 0 then
return string.format("%d", yearsold).." years old."
end
function isin(...)
one, two, three, four, five, six = ...
return isle(one, two, three, four) and islt(three, four, five, six)
end
function islt(...)
one, two, three, four = ...
return (one * four) < (two * three)
end
function isle(...)
one, two, three, four = ...
return (one * four) <= (two * three)
end
lown = 0
lowd = 1
highn = 1
highd = 0
while 1 do
num = lown + highn
den = lowd + highd
if isin(dayssince, 365, num, den, (dayssince + 1), 365) then
return string.format("%d", yearsold).." "..num.."/"..den.." years old."
elseif islt(num, den, dayssince, 365) then
lown = num
lowd = den
elseif isle((dayssince + 1), 365, num, den) then
highn = num
highd = den
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment