switched to Decimal based time. Much cleaner. Stacky Watch command working

This commit is contained in:
Sandy Walsh
2012-10-31 10:18:56 -03:00
parent cbc96b6b52
commit 71895341fe
7 changed files with 122 additions and 90 deletions

View File

@@ -0,0 +1,28 @@
import calendar
import datetime
import decimal
import time
def dt_to_decimal(utc):
decimal.getcontext().prec = 6
return decimal.Decimal(calendar.timegm(utc.utctimetuple()) +
utc.microsecond/float(1e6))
def dt_from_decimal(dec):
integer = int(dec)
micro = (dec - decimal.Decimal(integer)) * decimal.Decimal(1000000)
daittyme = datetime.datetime.utcfromtimestamp(integer)
return daittyme.replace(microsecond=micro)
if __name__ == '__main__':
now = datetime.datetime.utcnow()
d = dt_to_decimal(now)
daittyme = dt_from_decimal(d)
print repr(now)
print repr(d)
print repr(daittyme)
assert(now == daittyme)