Python Forum
Timestamp of file changes if a share mapped to a server….
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timestamp of file changes if a share mapped to a server….
#11
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?
Reply
#12
(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).
Reply
#13
You need to move the start time, not the end time
Reply
#14
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!
Reply
#15
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.
Reply
#16
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
Reply
#17
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.
tester_V likes this post
Reply
#18
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.
Reply
#19
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.
tester_V likes this post
Reply
#20
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 393 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 900 Feb-16-2023, 08:11 PM
Last Post: cubangt
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,301 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  access share attributed among several class methods drSlump 0 1,122 Nov-18-2021, 03:02 PM
Last Post: drSlump
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,626 Oct-21-2021, 03:29 AM
Last Post: buran
  How to take the tar backup files form remote server to local server sivareddy 0 2,001 Jul-14-2021, 01:32 PM
Last Post: sivareddy
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,569 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to share a numpy array between 2 processes on Windows? qstdy 0 2,217 Jan-29-2021, 04:24 AM
Last Post: qstdy
  Access Network SMB Share on a NAS deltapy 2 9,327 Oct-31-2020, 09:14 AM
Last Post: deltapy
  Multiprocessing share variable catosp 0 2,203 Jul-15-2020, 10:45 AM
Last Post: catosp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020