Search code examples
pythonpython-3.xpytzpython-datetime

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 always return new timestamps as aware datetimes using dt.timezone.utc (i.e. something like dt.datetime(..., tzinfo=dt.timezone.utc)).

Will these timestamps interact sensibly (e.g. datetime subtraction produces correct results) with pytz timestamps like those you get from pytz.localize(...), or do I need to use pytz.utc instead?


Solution

  • It is ok to use datetime.timezone.utc. You don't need to use pytz.utc, only to cooperate with pytz timezones.

    utc timezone has a fixed utc offset (zero, always). Such tzinfo objects should work with any tzinfo implementations.