Skip to Content
Modern Python Standard Library Cookbook
book

Modern Python Standard Library Cookbook

by Alessandro Molina
August 2018
Intermediate to advanced
366 pages
10h 14m
English
Packt Publishing
Content preview from Modern Python Standard Library Cookbook

How it works...

All Python datetimes, by default, come without any time zone specified, but by setting tzinfo, we can make them aware of the time zone they refer to.

If we just grab our current time (datetime.datetime.now()), there is no easy way for us to know from within our software which time zone we are grabbing the time from. The only time zone we can always rely on is UTC, for that reason. Whenever retrieving the current time, it's best always to rely on datetime.datetime.utcnow().

Once we have a date for UTC, as we know it's actually for the UTC time zone, we can easily attach the datetime.timezone.utc time zone (the only one that Python provides out of the box) and make it time-zone-aware.

The now function does that: it grabs the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Matthew Wilkes

Publisher Resources

ISBN: 9781788830829Supplemental Content