Issue with python/pytz Converting from local timezone to UTC then back

2024/10/2 16:29:31

I have a requirement to convert a date from a local time stamp to UTC then back to the local time stamp.

Strangely, when converting back to the local from UTC python decides it is PDT instead of the original PST so the post converted date has gained an hour. Can someone explain to me what is going on or what I am doing wrong?

from datetime import datetime
from pytz import timezone
import pytzDATE_FORMAT = '%Y-%m-%d %H:%M:%S %Z%z'def print_formatted(dt):formatted_date = dt.strftime(DATE_FORMAT)print "%s :: %s" % (dt.tzinfo, formatted_date)#convert the strings to date/time
date = datetime.now()
print_formatted(date)#get the user's timezone from the pofile table
users_timezone = timezone("US/Pacific")#set the parsed date's timezone
date = date.replace(tzinfo=users_timezone)
date = date.astimezone(users_timezone)
print_formatted(date)#Create a UTC timezone
utc_timezone = timezone('UTC')
date = date.astimezone(utc_timezone)
print_formatted(date)#Convert it back to the user's local timezone
date = date.astimezone(users_timezone)
print_formatted(date)

And here is the output:

None :: 2011-09-18 18:24:23 
US/Pacific :: 2011-09-18 18:24:23 PST-0800
UTC :: 2011-09-19 02:24:23 UTC+0000
US/Pacific :: 2011-09-18 19:24:23 PDT-0700
Answer

Change

date = date.replace(tzinfo=users_timezone)

to

date = users_timezone.localize(date)

localize adjusts for Daylight Savings Time, replace does not. See the docs for more info.

https://en.xdnf.cn/q/70829.html

Related Q&A

Regex to replace %variables%

Ive been yanking clumps of hair out for 30 minutes doing this one...I have a dictionary, like so:{search: replace,foo: bar}And a string like this:Foo bar %foo% % search %.Id like to replace each var…

Python kivy - how to reduce height of TextInput

I am using kivy to make a very simple gui for an application. Nothing complex, very simple layout.Nevertheless I am having a hard time with TextInputs...They always display with full height and I cant …

Python-Matplotlib boxplot. How to show percentiles 0,10,25,50,75,90 and 100?

I would like to plot an EPSgram (see below) using Python and Matplotlib. The boxplot function only plots quartiles (0, 25, 50, 75, 100). So, how can I add two more boxes?

Python Pandas reads_csv skip first x and last y rows

I think I may be missing something obvious here, but I am new to python and pandas. I am reading a large text file and only want to use rows in range(61,75496). I can skip the first 60 rows withkeyword…

Combine two arrays data using inner join

Ive two data sets in array: arr1 = [[2011-10-10, 1, 1],[2007-08-09, 5, 3],... ]arr2 = [[2011-10-10, 3, 4],[2007-09-05, 1, 1],... ]I want to combine them into one array like this: arr3 = [[2011-10-10, 1…

How to change fontsize of individual legend entries in pyplot?

What Im trying to do is control the fontsize of individual entries in a legend in pyplot. That is, I want the first entry to be one size, and the second entry to be another. This was my attempt at a so…

Split array into equal sized windows [duplicate]

This question already has answers here:Sliding window of M-by-N shape numpy.ndarray(8 answers)Closed 10 months ago.I am trying to split an numpy.array of length 40 into smaller, equal-sized numpy.array…

Is there a way to send a click event to a window in the background in python?

So Im trying to build a bot to automate some actions in a mobile game that Im running on my pc through Bluestacks.My program takes a screenshot of the window, looks for certain button templates in the …

how to store an image into redis using python / PIL

Im using python and the Image module(PIL) to process images.I want to store the raw bits stream of the image object to redis so that others can directly read the images from redis using nginx & htt…

Delete OCR word from Image (OpenCV,Python)

So, from what I can begin..I am working with OCR. The script works pretty well for what I need. It detects the words with an accuracy which for me is ok.This is the result: 100% accuracy with attached …