implement 'datetime_to_timestamp()'
and rename 'to_timestamp()' to the more descriptive 'datetime_to_timestamp_string()'
This commit is contained in:
@@ -183,7 +183,7 @@ class Extractor():
|
||||
elif until:
|
||||
if isinstance(until, datetime.datetime):
|
||||
# convert to UTC timestamp
|
||||
until = (until - util.EPOCH) / util.SECOND
|
||||
until = util.datetime_to_timestamp(until)
|
||||
else:
|
||||
until = float(until)
|
||||
seconds = until - now
|
||||
|
||||
@@ -254,7 +254,7 @@ def parse_format_spec(format_spec, conversion):
|
||||
"C": string.capwords,
|
||||
"j": json.dumps,
|
||||
"t": str.strip,
|
||||
"T": util.to_timestamp,
|
||||
"T": util.datetime_to_timestamp_string,
|
||||
"d": text.parse_timestamp,
|
||||
"U": text.unescape,
|
||||
"S": util.to_string,
|
||||
|
||||
@@ -173,8 +173,13 @@ def to_string(value):
|
||||
return str(value)
|
||||
|
||||
|
||||
def to_timestamp(dt):
|
||||
"""Convert naive datetime to UTC timestamp string"""
|
||||
def datetime_to_timestamp(dt):
|
||||
"""Convert naive UTC datetime to timestamp"""
|
||||
return (dt - EPOCH) / SECOND
|
||||
|
||||
|
||||
def datetime_to_timestamp_string(dt):
|
||||
"""Convert naive UTC datetime to timestamp string"""
|
||||
try:
|
||||
return str((dt - EPOCH) // SECOND)
|
||||
except Exception:
|
||||
|
||||
@@ -537,7 +537,16 @@ class TestOther(unittest.TestCase):
|
||||
self.assertEqual(f(["a", "b", "c"]), "a, b, c")
|
||||
self.assertEqual(f([1, 2, 3]), "1, 2, 3")
|
||||
|
||||
def test_to_timestamp(self, f=util.to_timestamp):
|
||||
def test_datetime_to_timestamp(self, f=util.datetime_to_timestamp):
|
||||
self.assertEqual(f(util.EPOCH), 0.0)
|
||||
self.assertEqual(f(datetime.datetime(2010, 1, 1)), 1262304000.0)
|
||||
self.assertEqual(f(datetime.datetime(2010, 1, 1, 0, 0, 0, 128000)),
|
||||
1262304000.128000)
|
||||
with self.assertRaises(TypeError):
|
||||
f(None)
|
||||
|
||||
def test_datetime_to_timestamp_string(
|
||||
self, f=util.datetime_to_timestamp_string):
|
||||
self.assertEqual(f(util.EPOCH), "0")
|
||||
self.assertEqual(f(datetime.datetime(2010, 1, 1)), "1262304000")
|
||||
self.assertEqual(f(None), "")
|
||||
|
||||
Reference in New Issue
Block a user