Skip to content

Commit ed7f1fc

Browse files
author
Paul Santa Clara
committed
preserve fractional seconds in datetimes
1 parent a7bfef9 commit ed7f1fc

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

singer/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
from singer.catalog import Catalog
99

10-
DATETIME_FMT = "%Y-%m-%dT%H:%M:%SZ"
10+
DATETIME_PARSE = "%Y-%m-%dT%H:%M:%SZ"
11+
DATETIME_FMT = "%Y-%m-%dT%H:%M:%S.%fZ"
1112

1213

1314
def strptime(dtime):
14-
return datetime.datetime.strptime(dtime, DATETIME_FMT)
15-
15+
try:
16+
return datetime.datetime.strptime(dtime, DATETIME_FMT)
17+
except Exception:
18+
return datetime.datetime.strptime(dtime, DATETIME_PARSE)
1619

1720
def strftime(dtime):
1821
return dtime.strftime(DATETIME_FMT)

tests/test_transform.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ def test_null_transform(self):
3232
def test_datetime_transform(self):
3333
schema = {"type": "string", "format": "date-time"}
3434
string_datetime = "2017-01-01T00:00:00Z"
35-
self.assertEqual(string_datetime, transform(string_datetime, schema, NO_INTEGER_DATETIME_PARSING))
36-
self.assertEqual('1970-01-02T00:00:00Z', transform(86400, schema, UNIX_SECONDS_INTEGER_DATETIME_PARSING))
37-
self.assertEqual(string_datetime, transform(string_datetime, schema, UNIX_SECONDS_INTEGER_DATETIME_PARSING))
38-
self.assertEqual('1970-01-01T00:01:26Z', transform(86400, schema, UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING))
39-
self.assertEqual(string_datetime, transform(string_datetime, schema, UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING))
35+
transformed_string_datetime = "2017-01-01T00:00:00.000000Z"
36+
self.assertEqual(transformed_string_datetime, transform(string_datetime, schema, NO_INTEGER_DATETIME_PARSING))
37+
self.assertEqual('1970-01-02T00:00:00.000000Z', transform(86400, schema, UNIX_SECONDS_INTEGER_DATETIME_PARSING))
38+
self.assertEqual(transformed_string_datetime, transform(string_datetime, schema, UNIX_SECONDS_INTEGER_DATETIME_PARSING))
39+
self.assertEqual('1970-01-01T00:01:26.400000Z', transform(86400, schema, UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING))
40+
self.assertEqual(transformed_string_datetime, transform(string_datetime, schema, UNIX_MILLISECONDS_INTEGER_DATETIME_PARSING))
4041

4142
trans = Transformer(NO_INTEGER_DATETIME_PARSING)
4243
self.assertIsNone(trans._transform_datetime('cat'))
@@ -45,10 +46,16 @@ def test_datetime_transform(self):
4546
trans.integer_datetime_fmt = UNIX_SECONDS_INTEGER_DATETIME_PARSING
4647
self.assertIsNone(trans._transform_datetime('cat'))
4748

49+
def test_datetime_fractional_seconds_transform(self):
50+
schema = {"type": "string", "format": "date-time"}
51+
string_datetime = "2017-01-01T00:00:00.123000Z"
52+
self.assertEqual(string_datetime, transform(string_datetime, schema, NO_INTEGER_DATETIME_PARSING))
53+
4854
def test_anyof_datetime(self):
4955
schema = {'anyOf': [{'type': 'null'}, {'format': 'date-time', 'type': 'string'}]}
5056
string_datetime = '2016-03-10T18:47:20Z'
51-
self.assertEqual(string_datetime, transform(string_datetime, schema))
57+
transformed_string_datetime = '2016-03-10T18:47:20.000000Z'
58+
self.assertEqual(transformed_string_datetime, transform(string_datetime, schema))
5259
self.assertIsNone(transform(None, schema))
5360

5461
def test_error_path(self):

0 commit comments

Comments
 (0)