pytz

Python datetime + pytz issue

给你一囗甜甜゛ 提交于 2019-12-23 15:35:25
问题 I'm creating a datetime object via strptime, set to "2016-01-02 03:04:05" in the "Europe/Madrid" timezone via pytz. Then I'm converting it to UTC. Why does it add 15 minutes instead of subtract 1 hour? >>> import datetime >>> import pytz >>> d = datetime.datetime.strptime('2016-01-02 03:04:05', '%Y-%m-%d %H:%M:%S') >>> d datetime.datetime(2016, 1, 2, 3, 4, 5) >>> d = d.replace(tzinfo=pytz.timezone('Europe/Madrid')) >>> d datetime.datetime(2016, 1, 2, 3, 4, 5, tzinfo=<DstTzInfo 'Europe/Madrid'

How to find next day's Unix timestamp for same hour, including DST, in Python?

て烟熏妆下的殇ゞ 提交于 2019-12-23 09:18:05
问题 In Python, I can find the Unix time stamp of a local time, knowing the time zone, like this (using pytz): >>> import datetime as DT >>> import pytz >>> mtl = pytz.timezone('America/Montreal') >>> naive_time3 = DT.datetime.strptime('2013/11/03', '%Y/%m/%d') >>> naive_time3 datetime.datetime(2013, 11, 3, 0, 0) >>> localized_time3 = mtl.localize(naive_time3) >>> localized_time3 datetime.datetime(2013, 11, 3, 0, 0, tzinfo=<DstTzInfo 'America/Montreal' EDT-1 day, 20:00:00 DST>) >>> localized_time3

UnknownTimeZoneError when using RequestContext

女生的网名这么多〃 提交于 2019-12-23 03:16:35
问题 When using Django 1.4, I am getting an UnknownTimeZoneError for America/Chicago when using RequestContext, but it works without it...any ideas? Error Message UnknownTimeZoneError at /my_proj/bad_view/ 'America/Chicago' Source # This works def good_view(request): data = {} return render_to_response('mytemplate.html', data) # This doesn't def bad_view(request): data = {} return render_to_response('mytemplate.html', data, context_instance=RequestContext(request)) 回答1: If you don't care about

How to convert a datetime from one arbitrary timezone to another arbitrary timezone

孤街醉人 提交于 2019-12-22 09:51:46
问题 Let's say I receive an arbitrary datetime object in a request, like this, which could be coming from any possible timezone - I don't know which one. For the sake of example, pretend it comes from the East Coast import pytz from colander import iso8601 ... ests1 = iso8601.parse_date('2016-04-01T00:00:00.0000-04:00') pretend ests1 is the object that comes in Using pytz, I can find out a bit about it's timezone etz = ests1.timetz() # 00:00:00-04:00 etzi = ests1.tzinfo # <FixedOffset '-04:00'

PYTZ 'America/Edmonton' offset wrong [duplicate]

限于喜欢 提交于 2019-12-22 05:23:30
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Weird timezone issue with pytz This seems wrong: >>> import pytz >>> z1 = timezone('America/Edmonton') >>> z2 = timezone('US/Mountain') >>> z1 <DstTzInfo 'America/Edmonton' LMT-1 day, 16:26:00 STD> >>> z2 <DstTzInfo 'US/Mountain' MST-1 day, 17:00:00 STD> >>> pytz.VERSION '2012f' >>> 'America/Edmonton' and 'US/Eastern' should be the same time zone (17:00:00 STD). Not to mention 16:26:00 doesn't make any sense.

First call to pytz.timezone is slow in virtualenv

一笑奈何 提交于 2019-12-22 04:26:11
问题 I have installed pytz (v2013.8, but it happens in 2013.b, 2011k) in a virtualenv. The first call to pytz.timezone("US/Eastern") takes about 4 seconds. In a regular environment this is essentially instantaneous. Does anyone have a trick to get this to run faster? 回答1: I actually came across the answer by playing around and looking at the source code. Since it gets its timezone settings from within the egg and the first call to timezone has to check that all the timezone files exist, the first

How to store a naive datetime in Django 1.4

北城以北 提交于 2019-12-21 09:37:27
问题 I have a naive date and time in the format '2012-05-19 19:13:00' and need to store it using Django 1.4 and its timezone-aware abilities. Although there is no way of knowing what timezone the date is originally in, it seems to make sense to store it as if it were UTC. However, using pytz etc, I'm not sure how to convert a date that has no timezone into a UTC datetime. 回答1: If it has no tzinfo then of course there can be no conversion to UTC. Instead you could just make the datetime object into

Parse timezone abbreviation to UTC [duplicate]

半城伤御伤魂 提交于 2019-12-21 05:25:08
问题 This question already has answers here : Parsing date/time string with timezone abbreviated name in Python? (5 answers) Closed 4 years ago . How can I convert a date time string of the form Feb 25 2010, 16:19:20 CET to the unix epoch? Currently my best approach is to use time.strptime() is this: def to_unixepoch(s): # ignore the time zone in strptime a = s.split() b = time.strptime(" ".join(a[:-1]) + " UTC", "%b %d %Y, %H:%M:%S %Z") # this puts the time_tuple(UTC+TZ) to unixepoch(UTC+TZ

Django Tutorial - ImproperlyConfigured exception (pytz isn't installed)

随声附和 提交于 2019-12-21 03:59:05
问题 I'm currently working through the official Django tutorial using version 1.6.1 and version 2.7.5 of python on OSX. I'm working on part 2, which is the admin interface. When I attempt to go to /admin/polls/poll/, I get the following error report: ImproperlyConfigured at /admin/polls/poll/ This query requires pytz, but it isn't installed. Request Method: GET Request URL: http://127.0.0.1:8000/admin/polls/poll/ Django Version: 1.6.1 Exception Type: ImproperlyConfigured Exception Value: This

How to get current time in Pacific Timezone when import pytz fails?

老子叫甜甜 提交于 2019-12-20 04:55:19
问题 I'm working in an environment (AWS Lambda) where import pytz doesn't work. The environment is set to UTC. How can I get the current time in the U.S. Pacific Timezone in this environment? I need something simple, and low-maintenance. Somehow forcing import pytz to work would be ideal, and I hope to avoid having to copy the entire pytz library into my own script. Details What have I tried so far? I tried using import pytz , and it failed with module not found. Example code? I tried this,