Questions tagged [pytz]

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.3 or higher. It also solves the issue of ambiguous times at the end of daylight savings.

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.3 or higher. It also solves the issue of ambiguous times at the end of daylight savings.

717 questions
11
votes
2 answers

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

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…
fangsterr
  • 3,670
  • 4
  • 37
  • 54
11
votes
1 answer

Python datetime difference between .localize and tzinfo

Why do these two lines produce different results? >>> import pytz >>> from datetime import datetime >>> local_tz = pytz.timezone("America/Los_Angeles") >>> d1 = local_tz.localize(datetime(2015, 8, 1, 0, 0, 0, 0)) # line 1 >>> d2 = datetime(2015,…
Nick
  • 1,864
  • 5
  • 27
  • 49
11
votes
7 answers

Import pytz into AWS lambda function

I'm writing a lambda function that works with datetimes and trying to import pytz so I can have timezone be accounted for when comparing. import boto3 import pytz from datetime import timedelta, date, datetime from boto3.dynamodb.conditions import…
Scott Decker
  • 4,229
  • 7
  • 24
  • 39
11
votes
1 answer

when does `datetime.now(pytz_timezone)` fail?

delorean docs show this way to get the current time in a given timezone using datetime: from datetime import datetime from pytz import timezone EST = "US/Eastern" UTC = "UTC" d = datetime.utcnow() utc = timezone(UTC) est = timezone(EST) d =…
jfs
  • 399,953
  • 195
  • 994
  • 1,670
10
votes
3 answers

Why does creating a datetime with a tzinfo from pytz show a weird time offset?

Can someone explain me why I do not get the same result in those? import datetime,pytz var1 = datetime.datetime(2017,10,25,20,10,50,tzinfo=pytz.timezone("Europe/Athens"))) print(var1) The output of this code is: 2017-10-25 20:10:50+01:35 import…
Kwnstantinos Nikoloutsos
  • 1,832
  • 4
  • 18
  • 34
10
votes
3 answers

timezone conversion in Python

I'm probably missing something about timezones: >>> import datetime, pytz >>> date = datetime.datetime(2013,9,3,16,0, tzinfo=pytz.timezone("Europe/Paris")) >>> date.astimezone(pytz.UTC) datetime.datetime(2013, 9, 3, 15, 51, tzinfo=) I was…
jmague
  • 572
  • 1
  • 6
  • 13
10
votes
1 answer

pytz: _utcoffset has a wrong value for Iran

Correct value: >>> pytz.timezone('Asia/Tehran').utcoffset(datetime(2013, 1, 1)).total_seconds()/3600.0 3.5 >>> pytz.timezone('Asia/Tehran').utcoffset(datetime(2013, 1, 1)).total_seconds() 12600.0 Incorrect value: >>>…
saeedgnu
  • 4,110
  • 2
  • 31
  • 48
10
votes
2 answers

cx_freeze: How do I add package files into library.zip?

I've noticed that pytz misses zoneinfo folder when I try to roll a zip for Windows. Right now I have a workaround that I use after python setup.py build, namely 7z a -xr!*.py* build\exe.win32-2.7\library.zip C:\Python27\Lib\site-packages\pytz Is…
mlt
  • 1,595
  • 2
  • 21
  • 52
9
votes
1 answer

How can I check by type if an object is instance of pytz.timezone?

I want something like this: from datetime import datetime, timezone import pytz def convert_datetime_by_timezone(timestamp_dt, to_timezone): if isinstance(to_timezone, str): return timestamp_dt.astimezone(pytz.timezone(to_timezone)) …
elaspog
  • 1,635
  • 3
  • 21
  • 51
9
votes
2 answers

flask: convert utc time to user's local time

I'm trying to convert a UTC time to the appropriate local time in a flask application. I'm wondering if I can detect the user's timezone and dynamically set it. This is my code so far which works for everyone in the US/Pacific timezone only…
Johnny Metz
  • 5,977
  • 18
  • 82
  • 146
9
votes
1 answer

Pytz Timezone from UTC offset

I'm using Facebook's graph API with Python. For any user_id, it gives the timezone of the user as a float which represents the offset from UTC. Example: For someone in India, it gives 5.5 How would I convert this into a valid timezone like…
Pattu
  • 3,481
  • 8
  • 32
  • 41
9
votes
1 answer

Retrieve timezone aware DateTimeField in Django

In my models, I have a DateTimeField say class ReturnEvent(models.Model): book_title = models.CharField() return_time = models.DateTimeField() When I retrieve the return_time to be printed, for example: return_event =…
bryan.blackbee
  • 1,934
  • 4
  • 32
  • 46
9
votes
1 answer

What's the difference between pytz.utc and dt.timezone.utc?

I am writing a library and I don't want to require pytz since the library itself doesn't know or care about timezones (it's reading and writing data in the form of Unix timestamps, which don't have any timezone information associated with them). I…
Kevin
  • 28,963
  • 9
  • 62
  • 81
9
votes
2 answers
9
votes
2 answers

In pandas, why does tz_convert change the timezone used from EST to LMT?

In the script below, Why are tz and tz2 are different? import pandas import pytz tz = pytz.timezone('US/Eastern') t = pandas.Timestamp('2014-03-03 08:05:39.216809') tz2 = t.tz_localize(pytz.UTC).tz_convert(tz).tz In this case, tz displays…
D. A.
  • 3,369
  • 3
  • 31
  • 34