|
|
@@ -1,8 +1,6 @@ |
|
|
|
from nilmdb.utils import datetime_tz |
|
|
|
import re |
|
|
|
|
|
|
|
USE_FLOATS=0 |
|
|
|
|
|
|
|
# Range |
|
|
|
min_timestamp = (-2**63) |
|
|
|
max_timestamp = (2**62 - 1) |
|
|
@@ -10,30 +8,18 @@ max_timestamp = (2**62 - 1) |
|
|
|
# Smallest representable step |
|
|
|
epsilon = 1 |
|
|
|
|
|
|
|
if USE_FLOATS: |
|
|
|
min_timestamp = -1e12 |
|
|
|
max_timestamp = 1e12 |
|
|
|
epsilon = 1e-6 |
|
|
|
|
|
|
|
def string_to_timestamp(str): |
|
|
|
"""Convert a string that represents an integer number of microseconds |
|
|
|
since epoch.""" |
|
|
|
if USE_FLOATS: |
|
|
|
return float(str) |
|
|
|
try: |
|
|
|
# Parse a string like "1234567890123456" and return an integer |
|
|
|
return int(str) |
|
|
|
except ValueError: |
|
|
|
# For now, don't parse as float, to catch transition errors. |
|
|
|
raise ValueError("can't parse timestamp as integer") |
|
|
|
|
|
|
|
# Try parsing as a float, in case it's "1234567890123456.0" |
|
|
|
return int(round(float(str))) |
|
|
|
|
|
|
|
def timestamp_to_string(timestamp): |
|
|
|
"""Convert a timestamp (integer microseconds since epoch) to a string""" |
|
|
|
if USE_FLOATS: |
|
|
|
return "%.6f" % timestamp |
|
|
|
if isinstance(timestamp, float): |
|
|
|
return str(int(round(timestamp))) |
|
|
|
else: |
|
|
@@ -49,15 +35,11 @@ def timestamp_to_human(timestamp): |
|
|
|
def unix_to_timestamp(unix): |
|
|
|
"""Convert a Unix timestamp (floating point seconds since epoch) |
|
|
|
into a NILM timestamp (integer microseconds since epoch)""" |
|
|
|
if USE_FLOATS: |
|
|
|
return unix |
|
|
|
return int(round(unix * 1e6)) |
|
|
|
|
|
|
|
def timestamp_to_unix(timestamp): |
|
|
|
"""Convert a NILM timestamp (integer microseconds since epoch) |
|
|
|
into a Unix timestamp (floating point seconds since epoch)""" |
|
|
|
if USE_FLOATS: |
|
|
|
return timestamp |
|
|
|
return timestamp / 1e6 |
|
|
|
timestamp_to_seconds = timestamp_to_unix |
|
|
|
|
|
|
@@ -65,8 +47,6 @@ def rate_to_period(hz, cycles = 1): |
|
|
|
"""Convert a rate (in Hz) to a period (in timestamp units). |
|
|
|
Returns an integer.""" |
|
|
|
period = unix_to_timestamp(cycles) / float(hz) |
|
|
|
if USE_FLOATS: |
|
|
|
return period |
|
|
|
return int(round(period)) |
|
|
|
|
|
|
|
def parse_time(toparse): |
|
|
|