+ 1

Python time returning values 3 hours fast of UTC that convert to correct local time

When I call the time.gmtime() function or the time.time() function on Windows 11, it's returning a time three hours ahead of actual UTC according to this website. When called on Linux, they are accurate. time.localtime() return is correct for my local time (US Eastern). Time zone is set correctly on my system. On Windows 11, run the following code, and look at the hour of any online display of UTC at the same time: ``` import time print(time.gmtime().tm_hour) ``` This should be printing 18 right now according to https://www.timeanddate.com/worldclock/timezone/utc (it was 5 PM Eastern when I drafted this question), but instead it is printing 21. Again, on Windows 11, run the following code: ``` import time print(time.time()) ``` When I plug the value printed out here into https://www.epochconverter.com/, its output also shows my local time correctly like time.localtime(), but the UTC half of the conversion output is also 3 hours ahead.

10th Apr 2024, 7:15 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
4 ответов
0
I rebooted Windows a second time since correcting my timezone settings, and it started working correctly, not sure why.
10th Apr 2024, 9:03 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 3
It is because Windows and Linux store time in BIOS differently. Windows use local time, and Linux use UTC time. You can check the link below to learn more. https://www.howtogeek.com/323390/how-to-fix-windows-and-linux-showing-different-times-when-dual-booting/
11th Apr 2024, 3:28 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
This issue might be related to the way Windows handles time conversions and daylight saving time adjustments. To get the correct UTC time in your Python script on Windows 11, you can manually adjust the time difference. You can adjust for the 3-hour discrepancy by subtracting 3 hours from the time you get from `time.gmtime()` or `time.time()` functions on Windows 11. Here's an example that adjusts for the 3-hour difference in UTC time: import time # Get the current time in UTC (adjusted by 3 hours) current_utc_time = time.gmtime(time.time() - 3*3600) print(current_utc_time.tm_hour) This code snippet should print the correct current hour in UTC on Windows 11 by adjusting for the 3-hour discrepancy. If you want to convert this UTC time to your local time zone (US Eastern), you can then use `time.localtime()` on Windows 11 to get the correct local time.
10th Apr 2024, 8:01 PM
`нттየ⁴⁰⁶
`нттየ⁴⁰⁶ - avatar
0
Wong Hei Ming I suspected that, thank you.
11th Apr 2024, 11:48 AM
Wilbur Jaywright
Wilbur Jaywright - avatar