pytz

How to get pytz timezone from common abbreviation (PST, EST, etc.)?

本秂侑毒 提交于 2019-12-04 03:22:24
This is a similar question as How to get the common name for a pytz timezone eg. EST/EDT for America/New_York , except I want to be able to just get a timezone from "PST" from pytz. such as tz = timezone("PST") Is this something like this possible with pytz? I ended up just manually making a dictionary that mapped abbreviations to timezone names. For example, 'PST' : 'America/Los_Angeles' would be an entry (as would PDT for the daylight savings abbreviation). Have you tried using strptime with the %Z format code? I've never done it myself, but at least in theory you should be able to put in

Weird .astimezone behavior

余生颓废 提交于 2019-12-04 03:03:36
问题 I am doing some timezone conversions, and I get really weird results. Basically converting between timezones that differ only by whole hours, I still get non-whole results. For example: from datetime import datetime from pytz import timezone datetime(2013, 12, 27, 20, 0, 0, tzinfo=timezone('Europe/Bucharest'))\ .astimezone(timezone('Europe/Berlin')).replace(tzinfo=None) gives me datetime.datetime(2013, 12, 27, 19, 16) (time difference between Bucharest and Berlin is 1 hour, so I should get 19

converting from local to utc timezone

江枫思渺然 提交于 2019-12-03 09:08:34
I'm attempting to craft a function that takes a time object and converts it to UTC time. The code below appears to be off by one hour. When i run noon through the converter, i get back 18:00:00. But when i run the same data through online converters, i get 17:00:00. What am i doing wrong here? Any help would be greatly appreciated. import pytz, datetime def convert_to_utc(time, tz): now_dt = datetime.datetime.utcnow() #get a date object date_dt = now_dt.date() #combine the current date object with our given time object dt = datetime.datetime.combine(date_dt, time) #get an timezone object for

Django IPython sqlite complains about naive datetime

回眸只為那壹抹淺笑 提交于 2019-12-02 22:52:20
I have a new project in Django 1.4, using sqlite db. Also using django_extenstions' shell_plus with no problems. When I installed IPython, both shell and shell_plus started to complain about: /path/to/my/virtualenv/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:50: RuntimeWarning: SQLite received a naive datetime (2012-07-29 13:15:45.229464) while time zone support is active. It seems IPython itself uses unaware datetimes. How can this be fixed? EDIT: I don't want to disable Django's timezone support. powlo I put this in my local_settings.py: #ignore the following error when

Why does time still show with UTC time zone instead of settings.TIME_ZONE?

北慕城南 提交于 2019-12-02 12:42:23
I have a model that shows a short string in __str__() method of the model def __str__(self): return "Scheduled at %s" % (self.date_time.strftime("%B %d %Y %I:%M %p")) #Output: <Task: Scheduled at September 30 2018 12:30 AM> # it should have been: <Task: Scheduled at September 29 2018 08:30 PM> When I go to the Django admin , I see in the title Task: Scheduled at September 30 2018 12:30 AM and in the input it is filled with the correct TIME_ZONE : 20:30:00 settings.py TIME_ZONE = 'Etc/GMT+4' USE_I18N = False USE_L10N = False USE_TZ = True He keeps showing time with UTC time zone, however I set

Flag Daylight Saving Time (DST) Hours in Pandas Date-Time Column

帅比萌擦擦* 提交于 2019-12-02 09:19:09
问题 I created an hourly dates dataframe, and now I would like to create a column that flags whether each row (hour) is in Daylight Saving Time or not. For example, in summer hours, the flag should == 1, and in winter hours, the flag should == 0. # Localized dates dataframe dates = pd.DataFrame(data=pd.date_range('2018-1-1', '2019-1-1', freq='h', tz='America/Denver'), columns=['date_time']) # My failed attempt to create the flag column dates['dst_flag'] = np.where(dates['date_time'].dt.daylight

pytz difference of 2 datetimes in seconds? (Different time zones)

試著忘記壹切 提交于 2019-12-02 07:43:22
I have 2 datetime objects with 2 different time zones: datetime1 = 18:26:23, with tzinfo = UTC datetime2 = 14:30:00, with tzinfo = US/Eastern Both dates are on the same day. There should be exactly 1 hour, 3 minutes and 37 seconds difference between the 2 datetimes, which is: 3817 seconds total difference. However, when I use the following code to compare: time_diff = (datetime2 - datetime1).total_seconds() time_diff gives me a value of: 3576. Am I doing the difference in seconds wrong? Or am I not utilizing pytz for time zones correctly? Many thanks. There are two likely scenarios here.

Flag Daylight Saving Time (DST) Hours in Pandas Date-Time Column

与世无争的帅哥 提交于 2019-12-02 06:46:30
I created an hourly dates dataframe, and now I would like to create a column that flags whether each row (hour) is in Daylight Saving Time or not. For example, in summer hours, the flag should == 1, and in winter hours, the flag should == 0. # Localized dates dataframe dates = pd.DataFrame(data=pd.date_range('2018-1-1', '2019-1-1', freq='h', tz='America/Denver'), columns=['date_time']) # My failed attempt to create the flag column dates['dst_flag'] = np.where(dates['date_time'].dt.daylight_saving_time == True, 1, 0) There's a nice link in the comments that at least let you do this manually.

Weird .astimezone behavior

强颜欢笑 提交于 2019-12-01 16:52:02
I am doing some timezone conversions, and I get really weird results. Basically converting between timezones that differ only by whole hours, I still get non-whole results. For example: from datetime import datetime from pytz import timezone datetime(2013, 12, 27, 20, 0, 0, tzinfo=timezone('Europe/Bucharest'))\ .astimezone(timezone('Europe/Berlin')).replace(tzinfo=None) gives me datetime.datetime(2013, 12, 27, 19, 16) (time difference between Bucharest and Berlin is 1 hour, so I should get 19:00 - instead I get 19:16) I'm probably missing something really obvious, but I can't figure it out.

How to get time zone by passing city and country?

瘦欲@ 提交于 2019-11-30 22:41:18
In my django project I have a small hotel database which has fields city and country . Can I get the timezone for that place by passing the city and country as parameters in someway. The reason I want to do this is because I have some commands that work based on the difference in check-in time of hotels and the time at that hote now . Since datetime.now() gives naive datetime I cannot use it to compare with the hotel time. How do I look forward to the solution. Thanks :) To resolve a time zone by city and country, you will first need to obtain latitude/longitude coordinates. There are multiple