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

get UTC times from parseDT - trouble with at 9:30 clock times being interpreted directly in UTC #103

Closed
MatthiasKauer opened this issue Jun 26, 2015 · 2 comments

Comments

@MatthiasKauer
Copy link

Hi,
I have trouble obtaining UTC based data from Calendar.parseDT. I tried making things work by adding sourceTime=datetime.utcnow() and it seemed fine for a while.
Parsing "at 9:30" however leads to 9:30 in UTC without adjusting for the time difference.

Consider the following.

from datetime import datetime
import parsedatetime as pdt
import sys
import pytz

def parse_args(argv=sys.argv):
    cal = pdt.Calendar()
    timestr = ' '.join(argv)
    ret = cal.parseDT(timestr, sourceTime=datetime.utcnow(), tzinfo=pytz.utc)[0]
    print(ret)

if __name__ == '__main__':
    parse_args()

Command line:

C:\dvl\tim>python pdt_test.py now
2015-06-26 04:57:06+00:00

C:\dvl\tim>python pdt_test.py in 10 min
2015-06-26 05:07:10+00:00

C:\dvl\tim>python pdt_test.py at 12:50
2015-06-26 12:50:00+00:00

I'd expect (or would like) the last output to be 4:50 UTC. Any advice?

Best regards,
Matthias

@MatthiasKauer
Copy link
Author

Ok, I put together a workaround now (or maybe this is how it's intended) with tzlocal and pytz that seems to work.

from datetime import datetime
import parsedatetime as pdt
import sys
import pytz

# http://stackoverflow.com/questions/13218506/how-to-get-system-timezone-setting-and-pass-it-to-pytz-timezone
from tzlocal import get_localzone # $ pip install tzlocal
local_tz = get_localzone()

def parse_args(argv=sys.argv):
    cal = pdt.Calendar()
    timestr = ' '.join(argv)
    # ret = cal.parseDT(timestr, sourceTime=datetime.utcnow(), tzinfo=pytz.utc)[0]
    ret = cal.parseDT(timestr, tzinfo=local_tz)[0]
    ret_utc = ret.astimezone(pytz.utc)
    print(ret)
    print(ret_utc)

    ret_loc = ret_utc.astimezone(local_tz)
    print(ret_loc)

if __name__ == '__main__':
    parse_args()

Command line:

C:\dvl\tim>python pdt_test.py now
2015-06-26 13:09:57+08:00
2015-06-26 05:09:57+00:00
2015-06-26 13:09:57+08:00

C:\dvl\tim>python pdt_test.py in 5 min
2015-06-26 13:15:05+08:00
2015-06-26 05:15:05+00:00
2015-06-26 13:15:05+08:00

C:\dvl\tim>python pdt_test.py at 9:30
2015-06-26 09:30:00+08:00
2015-06-26 01:30:00+00:00
2015-06-26 09:30:00+08:00

C:\dvl\tim>python pdt_test.py at 13:25
2015-06-26 13:25:00+08:00
2015-06-26 05:25:00+00:00
2015-06-26 13:25:00+08:00

@bear
Copy link
Owner

bear commented Jun 26, 2015

Yes, technically this is not-a-bug because parsedatetime doesn't look at or worry about timezones and leaves that to the caller

@bear bear closed this as completed Jun 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants