The Wayback Machine - https://web.archive.org/web/20210103200921/https://github.com/dateutil/dateutil/issues/666
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise: Parsing a local tzname #666

Open
pganssle opened this issue Apr 12, 2018 · 4 comments
Open

Exercise: Parsing a local tzname #666

pganssle opened this issue Apr 12, 2018 · 4 comments

Comments

@pganssle
Copy link
Member

@pganssle pganssle commented Apr 12, 2018

This is an example exercise to be solved by a new user to the library using only the dateutil documentation, per #663.

Exercise: Parsing a local tzname

Three-character time zone abbreviations are not unique in that they do not explicitly map to a time zone. A list of time zone abbreviations in use can be found here. This means that parsing datetime strings such as '2018-01-01 12:30:30 CST' is ambiguous without context.

Problem 1

Given the context that you will only be parsing dates coming from the continental United States, India and Japan, write a function that parses a datetime string and returns a timezone-aware datetime with an IANA-style timezone attached.
Note: For the purposes of the experiment, you may ignore the portions of the United States like Arizona and parts of Indiana that do not observe daylight saving time.

Test cases

from datetime import datetime
from dateutil import tz
from dateutil.parser import parse


PARSE_TZ_TEST_DATETIMES = [
    datetime(2018, 1, 1, 12, 0),
    datetime(2018, 3, 20, 2, 0),
    datetime(2018, 5, 12, 3, 30),
    datetime(2014, 9, 1, 23)
]

PARSE_TZ_TEST_ZONES = [
    tz.gettz('America/New_York'),
    tz.gettz('America/Chicago'),
    tz.gettz('America/Denver'),
    tz.gettz('America/Los_Angeles'),
    tz.gettz('Asia/Kolkata'),
    tz.gettz('Asia/Tokyo'),
]

def test_parse(parse_func):
    for tzi in PARSE_TZ_TEST_ZONES:
        for dt in PARSE_TZ_TEST_DATETIMES:
            dt_exp = dt.replace(tzinfo=tzi)
            dtstr = dt_exp.strftime('%Y-%m-%d %H:%M:%S %Z')

            dt_act = parse_func(dtstr)
            assert dt_act == dt_exp
            assert dt_act.tzinfo is dt_exp.tzinfo

    print('Success!')

Problem 2

Given the context that you will only be passed dates from India or Ireland, write a function that correctly parses all unambiguous time zone strings to aware datetimes localized to the correct IANA zone, and for ambiguous time zone strings default to India.

ISRAEL = tz.gettz('Asia/Jerusalem')
INDIA = tz.gettz('Asia/Kolkata')
PARSE_IXT_TEST_CASE = [
    ('2018-02-03 12:00 IST+02:00', datetime(2018, 2, 3, 12, tzinfo=ISRAEL)),
    ('2018-06-14 12:00 IDT+03:00', datetime(2018, 6, 14, 12, tzinfo=ISRAEL)),
    ('2018-06-14 12:00 IST', datetime(2018, 6, 14, 12, tzinfo=INDIA)),
    ('2018-06-14 12:00 IST+05:30', datetime(2018, 6, 14, 12, tzinfo=INDIA)),
    ('2018-02-03 12:00 IST', datetime(2018, 2, 3, 12, tzinfo=INDIA)),
]


def test_parse_ixt(parse_func):
    for dtstr, dt_exp in PARSE_IXT_TEST_CASE:
        dt_act = parse_func(dtstr)
        assert dt_act == dt_exp, (dt_act, dt_exp)
        assert dt_act.tzinfo is dt_exp.tzinfo, (dt_act, dt_exp)
    print('Success!')

To Do

  • Add exercise to documentation
  • Add solution to documentation
  • Improve documentation for parser based on this
@andymaheshw
Copy link

@andymaheshw andymaheshw commented Apr 12, 2018

Which section of the documentation does this go in?

@pganssle
Copy link
Member Author

@pganssle pganssle commented Apr 13, 2018

That is still undecided.

@dharmeshagase
Copy link

@dharmeshagase dharmeshagase commented Jun 2, 2019

I am new to Github and didn't really understand this....

@ffe4
Copy link
Member

@ffe4 ffe4 commented Apr 1, 2020

I was somewhat puzzled regarding how exactly I was supposed to implement problem 2 until I read the tests. Could it be that you made a mistake here?

Given the context that you will only be passed dates from India or Ireland

ISRAEL = tz.gettz('Asia/Jerusalem')

I have solved it assuming you meant to write Israel instead of Ireland in the problem. If you could verify whether these solutions are what you had in mind I would start converting my notes into changes to the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PyData NYC Sprint
  
Documentation General
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.