From 8ebc798dbf68f20a77b28066d73946fc0750a3ec Mon Sep 17 00:00:00 2001 From: "alberto.calderari" Date: Sat, 13 Jan 2018 21:33:36 +0000 Subject: [PATCH 1/6] Remove if dt.time() Remove if dt.time() to avoid convenrting timestampsat midnight to dates --- .gitignore | 1 + strconv.py | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5a7de29..cb6e168 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ __pycache__ .coverage dist MANIFEST +.idea* \ No newline at end of file diff --git a/strconv.py b/strconv.py index b9212d2..dc3cbe5 100644 --- a/strconv.py +++ b/strconv.py @@ -208,6 +208,7 @@ def infer_matrix(self, matrix, n=None, size=10): DATE_FORMATS = ( '%Y-%m-%d', '%m-%d-%Y', + '%Y/%m/%d', '%m/%d/%Y', '%m.%d.%Y', '%m-%d-%y', @@ -261,9 +262,7 @@ def convert_datetime(s, date_formats=DATE_FORMATS, time_formats=TIME_FORMATS): for sep in DATE_TIME_SEPS: f = '{0}{1}{2}'.format(df, sep, tf) try: - dt = datetime.strptime(s, f) - if dt.time(): - return dt + return datetime.strptime(s, f) except ValueError: pass raise ValueError From 363e0bcbf45735367ee6012cfeecec7c86d4f50f Mon Sep 17 00:00:00 2001 From: "alberto.calderari" Date: Sat, 13 Jan 2018 21:34:43 +0000 Subject: [PATCH 2/6] Added tests for midnight date time conversion and infer --- test_strconv.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test_strconv.py b/test_strconv.py index 3602093..40fc776 100644 --- a/test_strconv.py +++ b/test_strconv.py @@ -48,6 +48,8 @@ def test_convert(self): self.assertEqual(strconv.convert('5:40 PM'), time(17, 40)) self.assertEqual(strconv.convert('March 4, 2013 5:40 PM'), datetime(2013, 3, 4, 17, 40, 0)) + self.assertEqual(strconv.convert('March 4, 2013 12:00 AM'), + datetime(2013, 3, 4, 0, 0)) def test_convert_include_type(self): self.assertEqual(strconv.convert('-3', include_type=True), (-3, 'int')) @@ -67,6 +69,13 @@ def test_infer(self): self.assertEqual(strconv.infer('3/20/2013'), 'date') self.assertEqual(strconv.infer('5:40 PM'), 'time') self.assertEqual(strconv.infer('March 4, 2013 5:40 PM'), 'datetime') + self.assertEqual(strconv.infer('2018-12-01 00:00:00'), 'datetime') + self.assertEqual(strconv.infer('March 4, 2013 12:00 PM'), 'datetime') + # Midnight + self.assertEqual(strconv.convert_datetime('2013-03-01 00:00:00'), + datetime(2013, 3, 1, 0, 0, 0)) + self.assertEqual(strconv.convert_datetime('2018/03/01 00:00:00'), + datetime(2018, 3, 1, 0, 0, 0)) def test_infer_converted(self): self.assertEqual(strconv.infer('-3', converted=True), int) @@ -159,5 +168,6 @@ def test_convert_datetime(self): self.assertEqual(strconv.convert_datetime('2013-03-01 5:30:40 -0500'), datetime(2013, 3, 1, 5, 30, 40, tzinfo=tzoff)) + if __name__ == '__main__': unittest.main() From 27cd1c7c8bb82ba69eeab5bf8af93415e3f0f817 Mon Sep 17 00:00:00 2001 From: "alberto.calderari" Date: Sat, 13 Jan 2018 21:53:56 +0000 Subject: [PATCH 3/6] Removed deprecated option --use-mirrors which was causing all builds to fail --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 923e2d0..fdd4833 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ python: - "3.3" install: - - pip install -q coveralls --use-mirrors + - pip install -q coveralls - pip install -r requirements.txt - pip install flake8 From ca0a526736d609fd6a9e90afd299784fb5323a97 Mon Sep 17 00:00:00 2001 From: "alberto.calderari" Date: Sat, 13 Jan 2018 21:59:26 +0000 Subject: [PATCH 4/6] Fix import issue causing a flake8 error --- strconv.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/strconv.py b/strconv.py index dc3cbe5..2557e46 100644 --- a/strconv.py +++ b/strconv.py @@ -1,11 +1,13 @@ + +from collections import Counter +from datetime import datetime +import re + +__version__ = '0.4.1' # strconv.py # Copyright (c) 2013 Byron Ruth # BSD License -__version__ = '0.4.1' - -from collections import Counter - class TypeInfo(object): "Sampling and frequency of a type for a sample of values." @@ -190,10 +192,6 @@ def infer_matrix(self, matrix, n=None, size=10): # Built-in converters -import re - -from datetime import datetime - # Use dateutil for more robust parsing try: from dateutil.parser import parse as duparse From d41cae0a2a5a6be84402e1d82d81997c1b75e633 Mon Sep 17 00:00:00 2001 From: "alberto.calderari" Date: Sat, 13 Jan 2018 22:16:16 +0000 Subject: [PATCH 5/6] Try to specify patch version on python 3.2 to avoid odd flake8 error and added 3.5 check --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index fdd4833..3687990 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,9 @@ language: python python: - "2.7" - - "3.2" + - "3.2.5" - "3.3" + - "3.5" install: - pip install -q coveralls From a4c01add7d44a4125aac2bdb124bd5809c68b126 Mon Sep 17 00:00:00 2001 From: "alberto.calderari" Date: Sat, 13 Jan 2018 22:56:09 +0000 Subject: [PATCH 6/6] Fix bug on py 3.5 and 3.6 which would otherwise convert dates as datetimes Do not use dupers on py > 3.5 and add extra timezone conversions --- .travis.yml | 3 +-- strconv.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3687990..fdd4833 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,9 +2,8 @@ language: python python: - "2.7" - - "3.2.5" + - "3.2" - "3.3" - - "3.5" install: - pip install -q coveralls diff --git a/strconv.py b/strconv.py index 2557e46..a4fdbd7 100644 --- a/strconv.py +++ b/strconv.py @@ -3,6 +3,8 @@ from datetime import datetime import re +import sys + __version__ = '0.4.1' # strconv.py # Copyright (c) 2013 Byron Ruth @@ -220,7 +222,9 @@ def infer_matrix(self, matrix, n=None, size=10): '%H:%M:%S', '%H:%M', '%I:%M:%S %p', + '%I:%M:%S %z', '%I:%M %p', + '%I:%M %z', '%I:%M', ) @@ -247,13 +251,14 @@ def convert_bool(s): def convert_datetime(s, date_formats=DATE_FORMATS, time_formats=TIME_FORMATS): - if duparse: - try: - dt = duparse(s) - if dt.time(): - return duparse(s) - except TypeError: # parse may throw this in py3 - raise ValueError + if sys.version < '3.5': + if duparse: + try: + dt = duparse(s) + if dt.time(): + return duparse(s) + except TypeError: # parse may throw this in py3 + raise ValueError for df in date_formats: for tf in time_formats: