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
58
votes
3 answers

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

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 =…
Tampa
  • 75,446
  • 119
  • 278
  • 425
56
votes
5 answers

unexpected results converting timezones in python

I'm trying to understand why I'm getting these results when converting time zones to UTC: In [74]: d1 = datetime(2007, 12, 5, 6, 30,tzinfo=pytz.timezone('US/Pacific')) In [75]: d1 Out[75]: datetime.datetime(2007, 12, 5, 6, 30, tzinfo=
jxstanford
  • 3,339
  • 3
  • 27
  • 39
50
votes
4 answers

pytz utc conversion

What is the right way to convert a naive time and a tzinfo into an UTC time? Say I have: d = datetime(2009, 8, 31, 22, 30, 30) tz = timezone('US/Pacific') First way, pytz inspired: d_tz = tz.normalize(tz.localize(d)) utc =…
Art
  • 23,747
  • 29
  • 89
  • 101
48
votes
4 answers

How to get the common name for a pytz timezone eg. EST/EDT for America/New_York

Given a pytz timezone for a particular user(calculated from his offset), i want to display the common name for that timezone. I'm assuming people are more accustomed to seeing EST or PST instead of spelled out like America/NewYork. Does pytz give me…
Sidmitra
  • 1,152
  • 2
  • 9
  • 17
44
votes
2 answers

How to convert datetime.date.today() to UTC time?

How to make sure the datetime.date.today() is converted to UTC time? This is my code so far: #today : 2014-12-21 today = datetime.date.today() #1900-01-01 16:00:00+00:00 timeformat = datetime.datetime.strptime('16:00',…
user2492364
  • 6,543
  • 22
  • 77
  • 147
36
votes
8 answers

how to get tz_info object corresponding to current timezone?

Is there a cross-platform function in python (or pytz) that returns a tzinfo object corresponding to the timezone currently set on the computer? environment variables cannot be counted on as they are not cross-platform
random guy
  • 2,225
  • 6
  • 24
  • 24
35
votes
1 answer

datetime and timezone conversion with pytz - mind blowing behaviour

I'm trying to convert timezone aware datetime object to UTC and then back to it's original timezone. I have a following snippet t = datetime( 2013, 11, 22, hour=11, minute=0, tzinfo=pytz.timezone('Europe/Warsaw') ) now in ipython: In [18]:…
yakxxx
  • 2,841
  • 2
  • 21
  • 22
33
votes
2 answers

Python - Setting a datetime in a specific timezone (without UTC conversions)

Just to be clear, this is python 2.6, I am using pytz. This is for an application that only deals with US timezones, I need to be able to anchor a date (today), and get a unix timestamp (epoch time) for 8pm and 11pm in PST only. This is driving me…
liam
  • 3,830
  • 6
  • 32
  • 31
33
votes
3 answers

Python string to Django timezone (aware datetime)

TL;DR; How to convert 2016-01-01 to Django timezone? Full version: I receive a query string parameter from a form and I wanna get that string and use it as a datetime filter in Django. The problem is that when I convert the string to a datetime,…
jarussi
  • 1,491
  • 1
  • 13
  • 25
33
votes
2 answers

'ImportError: No module named pytz' when trying to import pylab?

As far as I can tell, I don't even need pytz for what I'm doing. I'm re-learning python for image processing using the O'Reilly book 'Programming Computer Vision with Python' for work (and I'm also new to mac, so on both counts I apologise if this…
nale
  • 451
  • 1
  • 4
  • 4
29
votes
4 answers

Getting the correct timezone offset in Python using local timezone

Ok let me first start by saying my timezone is CET/CEST. The exact moment it changes from CEST to CET (back from DST, which is GMT+2, to normal, which GMT+1, thus) is always the last Sunday of October at 3AM. In 2010 this was 31 October 3AM. Now…
Joren Van Severen
  • 2,269
  • 2
  • 24
  • 30
28
votes
2 answers

Use Python to find out if a timezone currently in daylight savings time

We have a server that runs on GMT time. I need to write a Python script that determines if it's currently (at this very second) Daylight Savings Time (DST) in Los Angeles, CA. How can I accomplish this? I took a look at pytz and time, but I can't…
Joey Mason
  • 707
  • 1
  • 8
  • 15
26
votes
2 answers

pytz: Why is normalize needed when converting between timezones?

I'm reading the not so complete pytz documentation and I'm stuck on understand one part of it. Converting between timezones also needs special attention. This also needs to use the normalize method to ensure the conversion is correct. >>> utc_dt =…
Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162
24
votes
3 answers

When to use datetime.utcnow() or datetime.now(tz=pytz.utc).replace(tzinfo=None)

I would like to understand when I should be using datetime.now(tz=pytz.utc).replace(tzinfo=None) as opposed to simply datetime.utcnow() Will the latter not take into account e.g. daylight savings?
Aert
  • 1,989
  • 2
  • 15
  • 17
24
votes
3 answers

Unix timestamp to datetime in django with timezone

I have a javascript calendar that is sending me a unixtimestamp. I am in Singapore. I want this timestamp to be interpreted as a Singapore timestamp and then converted to utc for comparisons with the db. I cant, for the life of myself, figure out…
nknj
  • 2,436
  • 5
  • 31
  • 45
1
2
3
47 48