Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: datetime tzinfo time zone names documentation

I have a date that I build:

from datetime import datetime
from datetime import tzinfo
test = '2013-03-27 23:05'
test2 = datetime.strptime(test,'%Y-%m-%d %H:%M')
>>> test2
datetime.datetime(2013, 3, 27, 23, 5)

>>> test2.replace(tzinfo=EST)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'EST' is not defined

>> test2.replace(tzinfo=UTC)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'UTC' is not defined

I can't find documentation on the list of time zone names that I can assign to tzinfo in the replace.tzinfo= call.

I have read through the following and there is nothing:

http://docs.python.org/2/library/datetime.html#tzinfo-objects

I have also searched in google.

Edit: I followed the solution provided by unutbu but I get the following:

>>> test = '2013-03-27 00:05'
>>> test
'2013-03-27 00:05'

>>> test2 = dt.datetime.strp(test, '%Y-%m-%d %H:%M')
>>> test2
datetime.datetime(2013, 3, 27, 0, 5)

>>> est = pytz.timezone('US/Eastern')
>>> utc = pytz.utc
>>> print(est.localize(test2))
2013-03-27 00:05:00-04:00
>>> print(utc.localize(test2))
2013-03-27 00:05:00+00:00

>>> print(est.localize(test2,is_dst=False))
2013-03-27 00:05:00-04:00
>>> print(est.localize(test2,is_dst=True))
2013-03-27 00:05:00-04:00
>>>

As you can see even when I provide the is_dst= flag the offset is still '-04:00', which is EDT and not EST. I appreciate the help. Thank you.

The documentation shows the following:

If you insist on working with local times, this library provides a facility for constructing them unambiguously: http://pytz.sourceforge.net/#problems-with-localtime

>>> loc_dt = datetime(2002, 10, 27, 1, 30, 00)
>>> est_dt = eastern.localize(loc_dt, is_dst=True)
>>> edt_dt = eastern.localize(loc_dt, is_dst=False)
>>> print(est_dt.strftime(fmt) + ' / ' + edt_dt.strftime(fmt))
2002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500

eastern was defined earlier in the documentation as eastern = timezone('US/Eastern')

This seems to indicate that the is_dst= flag should further specify whether day light savings is specified or not. I would appreciate help on why this isn't working in my case.

like image 246
codingknob Avatar asked Mar 28 '13 21:03

codingknob


People also ask

What is Tzinfo datetime?

tzinfo is an abstract base class. It cannot be instantiated directly. A concrete subclass has to derive it and implement the methods provided by this abstract class. The instance of the tzinfo class can be passed to the constructors of the datetime and time objects.

How do I get a list of time zones in Python?

Use the pytz. all_timezones attribute to get the list of available timezone in the world. There is another attribute that returns a set of timezones instead of a list.

Does Python datetime include timezone?

One of the modules that helps you work with date and time in Python is datetime . With the datetime module, you can get the current date and time, or the current date and time in a particular time zone.

How do I print UTC time in Python?

datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC. Lastly, use the timestamp() to convert the datetime object, in UTC, to get the UTC timestamp.


3 Answers

The standard library does not define any timezones -- at least not well (the toy example given in the documentation does not handle subtle problems like the ones mentioned here). For predefined timezones, use the third-party pytz module.

import pytz
import datetime as DT

eastern = pytz.timezone('US/Eastern')
utc = pytz.utc
test = '2013-03-27 23:05'

This is a "naive" datetime:

test2 = DT.datetime.strptime(test, '%Y-%m-%d %H:%M')   
print(test2)
# 2013-03-27 23:05:00

This makes a timezone-aware datetime by interpreting test2 as if it were in the EST timezone:

print(eastern.localize(test2))
# 2013-03-27 23:05:00-04:00

This makes a timezone-aware datetime by interpreting test2 as if it were in the UTC timezone:

print(utc.localize(test2))
# 2013-03-27 23:05:00+00:00

Alternatively, you can convert one timezone-aware datetime to another timezone using the astimezone method:

test2_eastern = eastern.localize(test2)
print(test2_eastern.astimezone(utc))
# 2013-03-28 03:05:00+00:00
like image 144
unutbu Avatar answered Oct 20 '22 15:10

unutbu


since the release of Python 3.9, the standard lib does define time zones, and you can get them via

import zoneinfo
print(zoneinfo.available_timezones())

# {'America/Belem', 'Asia/Tel_Aviv', 'Australia/North', 'Asia/Omsk', 
#  'Europe/Isle_of_Man', 'America/New_York', 'Europe/Nicosia', 
#  'Pacific/Funafuti', 'America/Ensenada', 'Europe/Mariehamn', 
#  'America/Maceio', 'America/Guatemala', 'America/Guadeloupe', ...
  • docs - zoneinfo
  • see also List of tz database time zones
  • on Windows: make sure to have tzdata installed and up-to-date
like image 17
FObersteiner Avatar answered Oct 20 '22 17:10

FObersteiner


As mentioned by @MrFuppes, starting from python 3.9 you don't need to install 3rd party pytz but use natively zoneinfo.

from datetime import datetime
from zoneinfo import ZoneInfo

test = '2013-03-27 23:05'
test2 = datetime.strptime(test,'%Y-%m-%d %H:%M')
date_string = test2.replace(tzinfo=ZoneInfo('US/Eastern'))
print(datestring)
2013-03-27 23:05:00-04:00
like image 6
Mike K Avatar answered Oct 20 '22 16:10

Mike K