Converting PDT to UTC using pytz and dateutil

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import pytz
import dateutil.parser

TZINFOS = {
    'PDT': pytz.timezone('US/Pacific'),
    # ... add more to handle other timezones
    # (I wish pytz had a list of common abbreviations)
}

datestring = '11:45:00 Aug 13, 2008 PDT'

# Parse the string using dateutil
datetime_in_pdt = dateutil.parser.parse(datestring, tzinfos= TZINFOS)

# t is now a PDT datetime; convert it to UTC
datetime_in_utc = datetime_in_pdt.astimezone(pytz.utc)

# Let's convert it to a naive datetime object
datetime_naive = datetime_in_utc.replace(tzinfo = None)

More like this

  1. filter dates to user profile's timezone by Scanner 6 years, 1 month ago
  2. Querying datetime aware objects in your local timezone by jayliew 11 months, 3 weeks ago
  3. Template tags for localizing UTC times with pytz by wolever 1 year, 9 months ago
  4. astimezone template tag by whardier 2 years, 2 months ago
  5. "Dynamic" form with a hidden field by flaxter 4 years, 1 month ago

Comments

robhudson (on August 21, 2008):

Just thought I'd point out this project in case people find it useful: django-timezones

#

(Forgotten your password?)