Jun-27-2023, 03:06 AM
I suggest you read up on the datetime module and then think about your proposal. Does it make any sense to for the next thirty days of files, starting today?
(Jun-27-2023, 03:06 AM)deanhystad Wrote: [ -> ]I suggest you read up on the datetime module and then think about your proposal. Does it make any sense to for the next thirty days of files, starting today?I wanted to download all 30 or 90 days old files starting from today (Kuala-Lumpur). That is what I mean "...next thirty days of files..." I think I can use your code to do that. And again, thank you for sharing and coaching. I checked the datetime module documents, but it is not that clear how to do this (my case).
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}")It looks ok to me but it is not the first time when I'm making mistakes...
from datetime import datetime, date, time, timedelta from pytz import timezone 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(datetime.fromtimestamp(t_start_KL), datetime.fromtimestamp(t_end_KL))This will print the start and stop times in Seattle (or wherever you are when you run this script). Check the printout against your expectations.
t_start_KL = time_today_KL - timedelta(hours=24) # Start time t_end_KL = t_start_KL + timedelta(hours=24) # End timeend when I use the following it will print the real timestamp, not the one that my server converts it too:
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))I hope I’m not overstaying my welcome here! Sorry about that but the subject is very confusing to me.
print(datetime.fromtimestamp(mtime))All timestamps are UTC. It is part of the definition of a timestamp. DateTime objects will be in the local timezone unless otherwise specified.
myfile = '/home/pedro/temp/atext_1.txt' print('This file is', os.path.getsize(myfile), 'bytes big!') print('This file was modified on', os.path.getmtime(myfile)) print('This file was created on,', os.path.getctime(myfile)) print('This file was last accessed on,', os.path.getatime(myfile)) print(os.stat(myfile)) file_data = os.stat(myfile) for f in file_data: print(f) # get the creation time in seconds and as a date creation_time = file_data[-1] ct = datetime.fromtimestamp(creation_time) cd = ct.strftime("%B %d %Y %H:%M:%S") print('This file was created on', cd) # get the modified time in seconds and as a date modified_time = file_data[-2] mt = datetime.fromtimestamp(modified_time) md = mt.strftime("%B %d %Y %H:%M:%S") print('This file was modified on', md) # get the last accessed time in seconds and as a date lat = file_data[-3] latt = datetime.fromtimestamp(lat) latd = mt.strftime("%B %d %Y %H:%M:%S") print('This file was last accessed on', latd)I'm not sure where your files may be modified, in Malaysia or in America. But if you stick with UTC, that is irrelevant.