62

I am using the datetime.datetime class from the Python standard library. I wish to construct an instance of this class with the UTC timezone. To do so, I gather that I need to pass as the tzinfo argument to the datetime constructor some instance of the tzinfo class.

The documentation for the tzinfo class says that:

tzinfo is an abstract base class, meaning that this class should not be instantiated directly. You need to derive a concrete subclass, and (at least) supply implementations of the standard tzinfo methods needed by the datetime methods you use. The datetime module does not supply any concrete subclasses of tzinfo.

Now I'm stumped. All I want to do is represent "UTC". I should be able to do that using approximately three characters, like this

import timezones
...
t = datetime(2015, 2, 1, 15, 16, 17, 345, timezones.UTC)

In short, I'm not going to do what the documentation tells me to do. So what's my alternative?

4
  • 2
    why do not use in pytz - pytz.sourceforge.net? Or you want to create your own objects? Commented Feb 13, 2015 at 11:16
  • @sKwa because I hadn't heard of it -- thanks! It looks like exactly what I want. Commented Feb 13, 2015 at 11:18
  • 1
    I'm glad I was able to help. But if you are interested how it works (tzinfo) you can read here - agiliq.com/blog/2009/02/… Commented Feb 13, 2015 at 11:26
  • Thanks. By the way, if you convert your comment to an answer, I'll accept it. Commented Feb 13, 2015 at 11:33

2 Answers 2

98

There are fixed-offset timezones in the stdlib since Python 3.2:

from datetime import datetime, timezone

t = datetime(2015, 2, 1, 15, 16, 17, 345, tzinfo=timezone.utc)

Constructor is :

datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

Docs link.

Though it is easy to implement utc timezone on earlier versions:

from datetime import tzinfo, timedelta, datetime

ZERO = timedelta(0)

class UTCtzinfo(tzinfo):
    def utcoffset(self, dt):
        return ZERO

    def tzname(self, dt):
        return "UTC"

    def dst(self, dt):
        return ZERO

utc = UTCtzinfo()
t = datetime(2015, 2, 1, 15, 16, 17, 345, tzinfo=utc)
Sign up to request clarification or add additional context in comments.

1 Comment

Aha, thanks! I'm using Python 3.4; my bad for reading the python 3 documentation ...
4

I used a lot in pytz and very satisfied from this module.

pytz

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference (datetime.tzinfo).

Also I would recommend for reading: Understanding DateTime, tzinfo, timedelta & TimeZone Conversions in python

3 Comments

the link provides incorrect info e.g., compare how it answers "How to get two days back/after datetime from now ?" with this answer to the related question
@J.F.Sebastian sure, anyone can do a mistake, but for understanding its good enough. I feel that SO not for me :(
pytz is overkill. It is very slow just to import (e.g. 1-2 seconds). It also adds a 638-file package to your dependencies—just to get a 7-line class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.