Hey Stephen Wood

Web Name: Hey Stephen Wood

WebSite: http://www.heystephenwood.com

ID:246226

Keywords:

Hey,Stephen,Wood,

Description:

keywords:
description:
Hey Stephen Wood

Explore nerdy things with me.

Tuesday, October 27, 2020 The curious case of Python's datetime -0753 UTC offset

"Time, the devourer of all things." - Ovid's Metamorphoses

Confused why your python datetime object's UTC offset is showing up as-0753instead of-0700(PDT) or-0800(PST)? I certainly was.

The tl;dr is because Python'spytzdoes not do the right thing with daylight saving's time when simply adding a timezone to a naive datetime object, and you must localize it.

Here's an example of what I was encountering. I was parsing a timestamp which was in theAmerica/Los_Angelestimezone. I needed to change the printed format, so I parsed the date and did a quick strftime and...-0753??

from datetime import datetimeimport pytztz = pytz.timezone("America/Los_Angeles")# Super duper sure this is America/Los_Angeles tzniave_string = "2020-10-05 14:03:40"dateobj = datetime.strptime(niave_string, "%Y-%m-%d %H:%M:%S")# Make it a timezone-aware object (Or so you think)dateobj = dateobj.replace(tzinfo=tz)# Notice the -0753 UTC offset...print(dateobj.strftime("%Y-%m-%d %H:%M:%S%z"))'2020-10-05 14:03:40-0753'

The mismatch happens because you can't just slap a tz onto a naive datetime object and hope it will do the right thing. More info on what's happening here can be found on this greatstack overflow answer.

The fix for this is tolocalizeyour date objects, not just replace the TZ:

from datetime import datetimeimport pytztz = pytz.timezone("America/Los_Angeles")# Super duper sure this is America/Los_Angeles tzniave_string = "2020-10-05 14:03:40"dateobj = datetime.strptime(niave_string, "%Y-%m-%d %H:%M:%S")# Localize your date object to your timezonedateobj = tz.localize(dateobj)# 

TAGS:Hey Stephen Wood 

<<< Thank you for your visit >>>

Websites to related :
The New Zealand Whisky Collectio

  keywords:
description:
+64 3 434 8842 info@thenzwhisky.com Facebook Twitter Instagram

Earthsafe Home Page | Earth Safe

  keywords:
description:
Skip to main content

CM.com Status

  keywords:
description:Welcome to CM.com&#39;s home for real-time and historical data on system performance.
Current information on Service Availabilit

Theleanstartup.com - Website Inf

  keywords:
description:
ip-address.comHomeMy IPSpeedtestSitemapProxy CheckerProxy ListVerify Email AddressTrace Email AddressIP to Zip CodeIP Address D

The SU Bath

  keywords:
description:

Nobody Here 6 1.0 Free Download

  keywords:Nobody Here 6 publisher description, publisher description, Nobody Here 6 download, Nobody Here 6 free download, Nobody Here 6
description:No

Elena Kalis

  keywords:
description:Underwater fine art, editorial, and commercial images by Elena Kalis, the Bahamas underwater photographer.

Millioncolour.com : Display Syst

  keywords:
description:We give your business more Exposure!! | Millioncolour - Millioncolour.com traffic statistics
HOMERECENTLY ANALYZEDWORTHALE

Welcome to St Chris | Independen

  keywords:
description:What every parent wants is for their children to do well in life. And that means finding a school that will set them up for thei

People of Print

  keywords:
description:
HomePOP StoreMembersPinbadgesPosterzine®DEPT.STORE MarketplaceIn Perpetuum HomePOP StoreMembersPin

ads

Hot Websites