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/.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 diff --git a/strconv.py b/strconv.py index b9212d2..a4fdbd7 100644 --- a/strconv.py +++ b/strconv.py @@ -1,11 +1,15 @@ + +from collections import Counter +from datetime import datetime +import re + +import sys + +__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 +194,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 @@ -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', @@ -221,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', ) @@ -248,22 +251,21 @@ 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: 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 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()