Forked from ranchodeluxe/test_dateparser_tzstring_conversions.py
Created
February 9, 2016 04:52
-
-
Save akoumjian/3f44933ec02057bc5ac0 to your computer and use it in GitHub Desktop.
An interesting note about how dateparser parses date strings with tzinfo in them; then some expected interpretations of date strings with tzinfo
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
import re | |
import pytz | |
from datetime import datetime | |
import dateparser | |
import pytest | |
def expected_tz_conversion(datetime_obj, pytz_tzinfo_offset): | |
# keep the day and time, just give it tzinfo | |
return pytz_tzinfo_offset.localize(datetime_obj) | |
def another_expected_tz_conversion(datetime_obj, pytz_tzinfo_offset): | |
# use the subtraction operator to calc the tzinfo offset | |
datetime_obj_as_gmt = pytz.timezone('GMT').localize(datetime_obj) | |
return datetime_obj_as_gmt.astimezone(pytz_tzinfo_offset) | |
@pytest.mark.parametrize('date_string, dateparser_expected_datetime, expected_datetime, another_expected_datetime',[ | |
# assumes your /etc/localtime -> /usr/share/zoneinfo/America/Los_Angeles | |
[ | |
'13/03/2014 MST', | |
datetime(2014, 3, 12, 23, 0), | |
datetime(2014, 3, 13, 0, 0).replace(tzinfo=pytz.timezone('MST')), | |
datetime(2014, 3, 12, 17, 0).replace(tzinfo=pytz.timezone('MST')) | |
] | |
]) | |
def test_dateparser_and_two_alternatives(date_string, dateparser_expected_datetime, \ | |
expected_datetime, another_expected_datetime): | |
''' | |
parsing a date string such as "13/03/2014 MST" has many potential meanings | |
''' | |
# the default dateparser interpretation is do a tzinfo shift plus a local conversion | |
# https://github.com/scrapinghub/dateparser/blob/master/dateparser/timezone_parser.py#L18-L19 | |
datetime_obj = dateparser.parse(date_string) | |
assert datetime_obj == dateparser_expected_datetime | |
# one reasonable expectation would be to keep the day and time the same | |
# and just add the tzinfo to the datetime object | |
date_string_only, tzinfo_string = re.findall(r'[0-9\/]+',date_string)[0], re.findall(r'[a-zA-Z]+',date_string)[0] | |
datetime_obj = dateparser.parse(date_string_only) | |
assert expected_tz_conversion(datetime_obj, pytz.timezone(tzinfo_string)) == expected_datetime | |
# another reasonable expectation would be to calculate only the tzinfo offset and not try to convert to local tz | |
datetime_obj = dateparser.parse(date_string_only) | |
assert another_expected_tz_conversion(datetime_obj, pytz.timezone(tzinfo_string)) == another_expected_datetime | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment