Skip to content

Instantly share code, notes, and snippets.

@Akuba-
Last active July 27, 2016 08:46
Show Gist options
  • Save Akuba-/c0dcb649d97abe7c1055723a414e7543 to your computer and use it in GitHub Desktop.
Save Akuba-/c0dcb649d97abe7c1055723a414e7543 to your computer and use it in GitHub Desktop.
convertes a unix timestamp into a human readable string in python (ex: 2 years, 1 month and 7 seconds)
import time
def sectostr(t):
_time = ""
(t, _time) = sectostr_one(31536000, "year", "years", "one", t, _time)
(t, _time) = sectostr_one(604800, "week", "weeks", "one", t, _time)
(t, _time) = sectostr_one(86400, "day", "days", "one", t, _time)
(t, _time) = sectostr_one(3600, "hour", "hours", "one", t, _time)
(t, _time) = sectostr_one(60, "minute", "minutes", "one", t, _time)
(t, _time) = sectostr_one(1, "second", "seconds", "one", t, _time)
if not len(_time):
_time = "0 seconds"
return _time
def sectostr_one (x, cap, cappl, capone, t, _time):
if t >= x:
num = int(t / x)
t = t % x
if len(_time) != 0:
if t > 0:
_time = _time + ", "
else:
_time = _time + " and "
if num > 1:
_time = _time + str(num) + " " + cappl
else:
_time = _time + capone + " " + cap
return (t, _time)
print (sectostr(355345))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment