On my system, I have set the current time zone for my clock to “Kuala Lumpur, Singapore (UTC+08:00)”, which puts my clock forward by eight hours (of course).
In python, I’m using the code to get the current time/date:
from datetime import datetime
dateTimeTuple = datetime.now()
I’m using this method as I need the individual parts (including the micro-second).
However, it will always returns me a regular time (not eight hours in front based on the selected time zone).
Is there another method?
datetime.now()returns a naive local datetime. To convert that to Kuala Lumpur time, you first need to make it a timezone-aware datetime, and then convert it to the Kuala Lumpur timezone:The easiest way to do timezone conversions is to use pytz.
Here is the naive datetime.
Use
localizeto make the datetime timezone aware.Use
astimezoneto convert to another timezone:Due to Daylight Savings Time in the UK and no DST in Kuala Lumpur, the time difference is currently 7 hours.