Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using timezone with openpyxl

I need to add a date in an excel file.
I use timezones on these dates. It works very well with django rest framework. (My GET requests return the date with this format: 2022-07-23T13:19:59+02:00) Same in Django admin, the Europe/Paris timezone is well taken into account (+02:00).

However, when I use openpyxl, the indicated time is 2h late (it takes the UTC timezone).

sheet.append([date_recorded.strftime("%d/%m/%Y %H:%M:%S")])

Same thing if I print this date in console. (2022-07-11 15:19:50+00:00)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How to correct this?

>Solution :

You can simply convert it to your desired zone like this

from dateutil import tz
utctime = "YOUR CURRENT VALUE OF DATETIME"
from_zone = tz.gettz("UTC")
to_zone = tz.gettz("yourzone")
utctime = utctime.replace(tzinfo=from_zone)
new_time = utctime.astimezone(to_zone)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading