A function to get fractional age for my conky
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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