Skip to content

Commit

Permalink
Merge d41cae0 into a112c4f
Browse files Browse the repository at this point in the history
  • Loading branch information
albertocalderari committed Jan 13, 2018
2 parents a112c4f + d41cae0 commit 33e431e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ __pycache__
.coverage
dist
MANIFEST
.idea*
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ language: python

python:
- "2.7"
- "3.2"
- "3.2.5"
- "3.3"
- "3.5"

install:
- pip install -q coveralls --use-mirrors
- pip install -q coveralls
- pip install -r requirements.txt
- pip install flake8

Expand Down
19 changes: 8 additions & 11 deletions strconv.py
Original file line number Diff line number Diff line change
@@ -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."
Expand Down Expand Up @@ -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
Expand All @@ -208,6 +206,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',
Expand Down Expand Up @@ -261,9 +260,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
Expand Down
10 changes: 10 additions & 0 deletions test_strconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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)
Expand Down Expand Up @@ -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()

0 comments on commit 33e431e

Please sign in to comment.