Created
October 19, 2012 08:50
-
-
Save dittore/3917027 to your computer and use it in GitHub Desktop.
Parse input into YYYY-MM-DD HH:MM:SS (PYTHON)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_datetime(d): | |
""" | |
Datetime parser | |
Creates datetimes from input into format yyyy-mm-dd hh:mm:ss | |
Needs proper validation! | |
Should accept following: | |
yyyy | |
yyyy-mm | |
yyyy-mm-dd | |
yyyy hh:mm | |
yyyy hh:mm:ss | |
yyyy-mm hh:mm | |
yyyy-mm hh:mm:ss | |
yyyy-mm-dd hh:mm | |
yyyy-mm-dd hh:mm:ss | |
""" | |
year = None | |
month = None | |
day = None | |
hours = None | |
minutes = None | |
seconds = None | |
# Check for size limit | |
if not d or len(d) > 19: | |
return '%s-%s-%s %s:%s:%s' %(year,month,day,hours,minutes,seconds) | |
# Check for invalid characters | |
reg = re.match("^[\s0-9:-]+$", d) | |
if not reg: | |
return '%s-%s-%s %s:%s:%s' %(year,month,day,hours,minutes,seconds) | |
date_set = False | |
time_set = False | |
d = d.split(' ') | |
for item in d: | |
# Check if we have time or date | |
if len(item) > 3 and item[2] == ':': | |
item = item.split(':') | |
try: | |
hours = item[0] | |
except Exception, e: | |
hours = "00" | |
try: | |
minutes = item[1] | |
except Exception, e: | |
minutes = "00" | |
try: | |
seconds = item[2] | |
except Exception, e: | |
seconds = "00" | |
time_set = True | |
else: | |
item = item.split('-') | |
try: | |
year = item[0] | |
except Exception, e: | |
year = "1970" | |
try: | |
month = item[1] | |
except Exception, e: | |
month = "01" | |
try: | |
day = item[2] | |
except Exception, e: | |
day = "01" | |
date_set = True | |
if not date_set: | |
year = "1970" | |
month = "01" | |
day = "01" | |
if not time_set: | |
hours = "00" | |
minutes = "00" | |
seconds = "00" | |
return '%s-%s-%s %s:%s:%s' %(year,month,day,hours,minutes,seconds) | |
def query_tests(query,match): | |
result = parse_datetime(query) | |
if 'None' in result and match is False: | |
print "Test for %s: FAIL" %(query) | |
elif result == match: | |
print "Test for %s: OK" %(query) | |
else: | |
print "Test for %s: FAIL" %(query) | |
# PASS | |
query_tests("2012","2012-01-01 00:00:00") | |
query_tests("2012-10","2012-10-01 00:00:00") | |
query_tests("2012-10-15","2012-10-15 00:00:00") | |
query_tests("2012-10-15 10:00","2012-10-15 10:00:00") | |
query_tests("2012-10-15 10:33","2012-10-15 10:33:00") | |
query_tests("2012-10-15 10:00:00","2012-10-15 10:00:00") | |
query_tests("2012-10-15 10:33:33","2012-10-15 10:33:33") | |
query_tests("2012 10:00","2012-01-01 10:00:00") | |
query_tests("2012 10:00:00","2012-01-01 10:00:00") | |
query_tests("2012 10:33","2012-01-01 10:33:00") | |
query_tests("2012 10:33:33","2012-01-01 10:33:33") | |
query_tests("2012-10 10:00","2012-10-01 10:00:00") | |
query_tests("2012-10 10:00:00","2012-10-01 10:00:00") | |
query_tests("2012-10 10:33","2012-10-01 10:33:00") | |
query_tests("2012-10 10:33:33","2012-10-01 10:33:33") | |
query_tests("10:00","1970-01-01 10:00:00") | |
query_tests("10:00:00","1970-01-01 10:00:00") | |
# FAIL | |
query_tests("2012/\ frfr ",False) | |
query_tests("2012 10",False) | |
query_tests("2012-10-15 10",False) | |
query_tests("2012-10 10",False) | |
query_tests("2012 10 10",False) | |
query_tests("2012-10 10-10",False) | |
query_tests("2012:10 10:33:33",False) | |
query_tests("2012 10-10 10:33:33",False) | |
query_tests("2012-10 10:33-10",False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And that bug is fixed now.