Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

timezone conversion

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
0
0
US
Hi,
does anyone understand the timezone conversions?
I found I cannot see the "real" timestamps of the files in a share in Asia when I mount it to my server in Pacific NW.
I thought I can do the following and it will give me the results I want but it did not.
Code:
time_today_KL = datetime.combine(date.today(), time(), tzinfo=timezone("Asia/Kuala_Lumpur")) # Time now in Kuala_Lumpur
t_start_KL = time_today_KL - timedelta(hours=24 # Start time 
t_end_KL = t_start_KL + timedelta(hours=24)     # End time

t_start_KL = t_start_KL.timestamp()
t_end_KL = t_end_KL.timestamp()

print(f" Start time={t_start_KL}, End time={t_end_KL}")

for file in folder.iterdir():
    mtime = file.stat().st_mtime
    if t_start_KL  <= mtime < t_end_KL :
        print(file.name, mtime, datetime.fromtimestamp(mtime))
thank you.
 
Hi Tester_V,

I tried getting times as in your example, but it does not work for me. I have Python version 3.8.10. What Python version you have?

So i have done it at different way
Code:
from datetime import datetime, timedelta
import pytz

time_now = datetime.now()

current_timezone = pytz.timezone("Europe/Amsterdam")
timezone_KL = pytz.timezone("Asia/Kuala_Lumpur")

time_now_KL = time_now.astimezone(timezone_KL)

t_start_KL = time_now_KL - timedelta(hours=24)
t_end_KL = t_start_KL + timedelta(hours=24)

print("Time Now Here:")
print(time_now)
print()

print("Time Now in Kuala Lumpur:")
print(time_now_KL)
print()

print("Start Time:")
print(t_start_KL)

print("End Time:")
print(t_end_KL)
print()

Code:
$ python3 timezone_example.py
Time Now Here:
2023-07-01 00:43:40.814256

Time Now in Kuala Lumpur:
2023-07-01 06:43:40.814256+08:00

Start Time:
2023-06-30 06:43:40.814256+08:00
End Time:
2023-07-01 06:43:40.814256+08:00

It seems to compute the times correctly.
Where is exactly your problem ?
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top