Skip to content

Commit

Permalink
Avoid float precision issues with UUID timestamps
Browse files Browse the repository at this point in the history
Fixes pycassa#135 by using microseconds as an intermediate form
in place of nanoseconds, which were causing precision loss.
  • Loading branch information
thobbs committed Apr 24, 2012
1 parent 78d1a97 commit e00f2d5
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions pycassa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,18 @@ def convert_time_to_uuid(time_arg, lowest_val=True, randomize=False):
if isinstance(time_arg, uuid.UUID):
return time_arg

nanoseconds = 0
if hasattr(time_arg, 'timetuple'):
seconds = int(time.mktime(time_arg.timetuple()))
microseconds = (seconds * 1e6) + time_arg.time().microsecond
nanoseconds = microseconds * 1e3
elif type(time_arg) in _number_types:
nanoseconds = int(time_arg * 1e9)
microseconds = int(time_arg * 1e6)
else:
raise ValueError('Argument for a v1 UUID column name or value was ' +
'neither a UUID, a datetime, or a number')

# 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
timestamp = int(nanoseconds/100) + 0x01b21dd213814000L
timestamp = int(microseconds * 10) + 0x01b21dd213814000L

time_low = timestamp & 0xffffffffL
time_mid = (timestamp >> 32L) & 0xffffL
Expand Down

0 comments on commit e00f2d5

Please sign in to comment.