pytz

Daylight savings time in Python

*爱你&永不变心* 提交于 2019-11-27 03:20:38
问题 I am writing a program which deals a lot with timezones and crossing them. The two things I deal with most are creating a datetime object from "now" and then localizing a naive datetime object. To create a datetime object from now in the pacific timezone, I am currently doing this (python 2.7.2+) from datetime import datetime import pytz la = pytz.timezone("America/Los_Angeles") now = datetime.now(la) Is this correct with regards to DST? If not, I suppose I should be doing: now2 = la.localize

How to check if a datetime object is localized with pytz?

孤街浪徒 提交于 2019-11-27 01:43:46
问题 I want to store a datetime object with a localized UTC timezone. The method that stores the datetime object can be given a non-localized datetime (naive) object or an object that already has been localized. How do I determine if localization is needed? Code with missing if condition: class MyClass: def set_date(self, d): # what do i check here? # if(d.tzinfo): self.date = d.astimezone(pytz.utc) # else: self.date = pytz.utc.localize(d) 回答1: How do I determine if localization is needed? From

pytz localize vs datetime replace

会有一股神秘感。 提交于 2019-11-27 01:40:15
I'm having some weird issues with pytz's .localize() function. Sometimes it wouldn't make adjustments to the localized datetime: .localize behaviour: >>> tz <DstTzInfo 'Africa/Abidjan' LMT-1 day, 23:44:00 STD> >>> d datetime.datetime(2009, 9, 2, 14, 45, 42, 91421) >>> tz.localize(d) datetime.datetime(2009, 9, 2, 14, 45, 42, 91421, tzinfo=<DstTzInfo 'Africa/Abidjan' GMT0:00:00 STD>) >>> tz.normalize(tz.localize(d)) datetime.datetime(2009, 9, 2, 14, 45, 42, 91421, tzinfo=<DstTzInfo 'Africa/Abidjan' GMT0:00:00 STD>) As you can see, time has not been changed as a result of localize/normalize

Python: How do you convert datetime/timestamp from one timezone to another timezone?

喜夏-厌秋 提交于 2019-11-26 23:02:20
问题 Specifically, given the timezone of my server (system time perspective) and a timezone input, how do I calculate the system time as if it were in that new timezone (regardless of daylight savings, etc)? import datetime current_time = datetime.datetime.now() #system time server_timezone = "US/Eastern" new_timezone = "US/Pacific" current_time_in_new_timezone = ??? 回答1: If you know your origin timezone and the new timezone you want to convert it to, first use pytz.localize to handle any

pytz.timezone shows weird results for Asia/Calcutta? [duplicate]

荒凉一梦 提交于 2019-11-26 21:48:32
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Python datetime object show wrong timezone offset import pytz, datetime pytz.timezone("Asia/Calcutta") prints the following: < DstTzInfo 'Asia/Calcutta' HMT+5:53:00 STD > Why it is not 05:30 hrs? I am in timezone America/Los_Angeles. 回答1: Time zones change over the years. According to http://www.prokerala.com/travel/timezones/Asia/Kolkata?mode=history the original offset for that zone was 5.88888888889 hours,

Is a specific timezone using DST right now?

匆匆过客 提交于 2019-11-26 21:47:30
问题 How would I get my python script to check whether or not a specific timezone that is stored in a variable using DST right now? My server is set to UTC. So I have say for instance zonename = Pacific/Wallis I want to run the query about if it is using DST right now and have the reply come back as either true of false. 回答1: from pytz import timezone from datetime import datetime zonename = "Pacific/Wallis" now = datetime.now(tz=timezone(zonename)) dst_timedelta = now.dst() ### dst_timedelta is

pytz and astimezone() cannot be applied to a naive datetime

痴心易碎 提交于 2019-11-26 19:47:45
问题 I have a date and I need to make it time zone aware. local_tz = timezone('Asia/Tokyo') start_date = '2012-09-27' start_date = datetime.strptime(start_date, "%Y-%m-%d") start_date = start_date.astimezone(local_tz) now_utc = datetime.now(timezone('UTC')) local_now = now_utc.astimezone(local_tz) I need to find if this is true: print start_date>local_now But I get this error. start_date = start_date.astimezone(local_tz) ValueError: astimezone() cannot be applied to a naive datetime I convert utc

Datetime Timezone conversion using pytz

北城以北 提交于 2019-11-26 15:27:56
This is just another post on pytz . There are two functions to convert datetime objects between two timezones. The second functions works for all cases. The first function fails in two cases, (3) and (4). Similar SO post did not have an issue like this. Any explanation based on the difference between localize(datetime.datetime) and replace(tzinfo) would be a great help. >>> from dateutil.parser import parse >>> import pytz First function (buggy) The function below uses datetime.datetime.replace(tzinfo) . def buggy_timezone_converter(input_dt, current_tz='UTC', target_tz='US/Eastern'): '''input

Python datetime object show wrong timezone offset

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 15:26:58
I am try creating a datetime object in python using datetime and pytz, the offset shown is wrong. import datetime from pytz import timezone start = datetime.datetime(2011, 6, 20, 0, 0, 0, 0, timezone('Asia/Kolkata')) print start The output shown is datetime.datetime(2011, 6, 20, 0, 0, tzinfo=<DstTzInfo 'Asia/Kolkata' HMT+5:53:00 STD>) Note that 'Asia/Kolkata' is IST which is GMT+5:30 and not HMT+5:53. This is a standard linux timezone, why do I get this wrong, and how do I solve it? See: http://bytes.com/topic/python/answers/676275-pytz-giving-incorrect-offset-timezone In the comments, someone

Weird timezone issue with pytz

こ雲淡風輕ζ 提交于 2019-11-26 14:41:12
>>> import pytz >>> pytz.timezone('Asia/Hong_Kong') <DstTzInfo 'Asia/Hong_Kong' LMT+7:37:00 STD> A seven hour and 37 minute offset? This is a little strange, does anyone experience the same issue? In fact I'm getting different behavior between import pytz from datetime import datetime hk = pytz.timezone('Asia/Hong_Kong') dt1 = datetime(2012,1,1,tzinfo=hk) dt2 = hk.localize(datetime(2012,1,1)) if dt1 > dt2: print "Why?" Time zones and offsets change over the years. The default zone name and offset delivered when pytz creates a timezone object are the earliest ones available for that zone, and