Python Forum
Timestamp of file changes if a share mapped to a server…. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Timestamp of file changes if a share mapped to a server…. (/thread-40221.html)

Pages: 1 2 3 4


RE: Timestamp of file changes if a share mapped to a server…. - deanhystad - Jun-27-2023

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?


RE: Timestamp of file changes if a share mapped to a server…. - tester_V - Jun-27-2023

(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).


RE: Timestamp of file changes if a share mapped to a server…. - deanhystad - Jun-27-2023

You need to move the start time, not the end time


RE: Timestamp of file changes if a share mapped to a server…. - tester_V - Jun-30-2023

Greetings!
Just to make sure I do (and understand) it correctly.
I need to collect all files with a yesterday timestamp from Asia Kuala Lumpur hosts.
I’m going to run my script every day at 11 AM (US/Pacific), in Kuala Lumpur it’ll be the next day 2 AM.
It means I need to find yesterday's start-end time in Kuala Lumpur.
Here is a snipped I come up with based on the code shared by 'deanhystad' :
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...
Please advise...

Thank you!


RE: Timestamp of file changes if a share mapped to a server…. - deanhystad - Jun-30-2023

To verify:
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.


RE: Timestamp of file changes if a share mapped to a server…. - tester_V - Jun-30-2023

Thank you for the reply!
I checked the https://docs.python.org/3/library/datetime.html you suggested. There are 47 “timestamp” instances in the document.
After reading it for 3 hours I become even more confused than before.
I thought the “tzinfo=timezone("Asia/Kuala_Lumpur"))” will supply the code with the current datetime in the KL.
And the following will make the boundaries for the “from-to” for the timestamps in KL:
t_start_KL = time_today_KL - timedelta(hours=24) # Start time 
t_end_KL = t_start_KL + timedelta(hours=24)     # End time
end 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. Wall
Thank you


RE: Timestamp of file changes if a share mapped to a server…. - deanhystad - Jun-30-2023

This does not print the timestamp. It prints a datetime string for a datetime object created from the timestamp. Since you did not specify a time zone, the local time zone will be used when making the timestamp object.
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.


RE: Timestamp of file changes if a share mapped to a server…. - tester_V - Jun-30-2023

Then how I can get the real file's time stamps if everything is converted to my current datetime?
Would it be a good idea to just add 15 hours to each timestamp to get the real one?
It sounds very good but then there are tons of Python datetime libraries for a reason...
Thank you.


RE: Timestamp of file changes if a share mapped to a server…. - deanhystad - Jun-30-2023

All file timestamps are the "real" timestamps. Timestamps are always UTC. They are always the number of seconds that have passed since the start of January 1, 1970 at Greenwich. If you print the file modification timestamp in Kuala Lumpur it will be the same float number you would see if you printed the file modification timestamp in Seattle.

Your confusion is caused by the operating system representing the timestamp as a string. Unless you are in London, it would be confusing to most operators if this date time string was for the UTC/GMT time zone. If you just created a file, you would like the file creation time to agree with the calendar and clock on your wall, so your computer displays this information as a date/time string in your local time zone. The actual timestamp is the same for everyone, everywhere, but the date/time string is different for each time zone.

The thing and a how a thing is represented being different (sometimes very different) is so common that you usually don't notice. A string is really an array of small integers. It is only when printing the string that the numbers become letters and symbols. Since you almost only ever see the display, this difference between thing and the representation is not something you are aware of. That is until you open a binary file or read a serial port or send a message over a socket connection. A timestamp being displayed as a date/time string is great until the timezone for the date/time becomes important. When your view of the thing no longer matches the representation that you expect, it can be very confusing, even disturbing.


RE: Timestamp of file changes if a share mapped to a server…. - Pedroski55 - Jun-30-2023

I haven't followed this thread carefully, but here is some basic datetime stuff.

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.