Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 726
Kevin Avatar asked Oct 21 '15 03:10

Kevin


People also ask

What is pytz UTC?

The pytz package encourages using UTC for internal timezone representation by including a special UTC implementation based on the standard Python reference implementation in the Python documentation. The UTC timezone unpickles to be the same instance, and pickles to a smaller size than other pytz tzinfo instances.

What does pytz timezone do?

The pytz module allows for date-time conversion and timezone calculations so that your Python applications can keep track of dates and times, while staying accurate to the timezone of a particular location.

Does pytz handle DST?

pytz will help you to tell if an date is under DST influence by checking dst() method.


1 Answers

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.

like image 118
jfs Avatar answered Oct 17 '22 04:10

jfs