[PYTHON] Easy conversion from UTC to local time

import datetime
import calendar
import time

# utctime -> localtime
utcnow = datetime.datetime.utcnow()
local = datetime.datetime.fromtimestamp(calendar.timegm(utcnow.timetuple()))

# localtime -> utctime
localnow = datetime.datetime.now()
utc = datetime.datetime.urcfromtimestamp(time.mktime(localnow.timetuple()))

I have only tried it for a while and cannot guarantee it. If you handle the timezone properly, I think it is better to include python-dateutil.

Recommended Posts

Easy conversion from UTC to local time
vtkXMLUnstructuredGridReader Summary (updated from time to time)
vtkOpenFOAMReader Summary (Updated from time to time)
Engineer vocabulary (updated from time to time)
Conversion from pdf to txt 1 [pdfminer]
Tensorflow memo [updated from time to time]
Python (from first time to execution)
[Note] [PyTorch] From installation to easy usage
Private Python handbook (updated from time to time)
vtkClipPolyData / DataSet Summary (Updated from time to time)
[Updated from time to time] PostmarketOS related notes
Summary of vtkThreshold (updated from time to time)
Summary of gcc options (updated from time to time)
Easy script migration from CentOS to Oracle Linux
[Updated from time to time] LetCode algorithm and library
Notes on machine learning (updated from time to time)
Sum from 1 to 10
OpenFOAM post-processing cheat sheet (updated from time to time)
progate Python learning memo (updated from time to time)
From easy git installation to docker startup python
An easy way to call Java from Python
Useful help sites, etc. (updated from time to time)
Machine learning python code summary (updated from time to time)
From Kafka to KSQL --Easy environment construction with docker
Apache settings, log confirmation, etc. (* Updated from time to time)
[Updated from time to time] Review of Let Code NumPy
[Python] Conversion from WGS84 to plane orthogonal coordinate system
Consider a conversion from a Python recursive function to a non-recursive function
I read the Chainer reference (updated from time to time)
Automatic conversion from MySQL Workbench mwb file to sql file
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Transition from WSL1 to WSL2
Easy to use Flask
Easy to use SQLite3
From editing to execution
[Linux] Compress folders and transfer files from remote to local
[Updated from time to time] Summary of design patterns in Java
[Introduction to matplotlib] Read the end time from COVID-19 data ♬
Display the time in Django according to the user's region (UTC)