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?
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.
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.
pytz will help you to tell if an date is under DST influence by checking dst() method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With