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….
#1
Greetings!
This is a very strange problem. Wall
I’m copying files from Asia and we have like a 15 hours difference in time.
I’m trying to copy files with yesterday's timestamps. For example, in Kuala Lumpur, it is 06/23/2023 2PM, and in Seattle, it is 06/22/2023 11PM.
When I map a drive from the Kuala Lumpur host to my server, the newest files last modification was done on 06/22/2023 (I’m reading the time stamps) but, when I ask local guys to get the print screen of the files in the folder I clearly see the exact same files are having different time stamps, they were modified on 06/23/2023. I’m copying files with the 6/22/2023 timestamp but actually, they are timestamped as 06/23/2023.
Does anybody know how it can be fixed? Confused
Thank you!
Any help is appreciated...
Reply
#2
This can be done using pendulum (from command line: pip install pendulum)
see: https://pypi.org/project/pendulum/
and: https://www.geeksforgeeks.org/python-pendulum-module/
Reply
#3
Is it really an issue? Timestamps are displayed in local time, but they are saved im GMT. Change the time zone of your computer to Kuala Lampur, and you will see the same timestamps as the "local guys".
Reply
#4
(Jun-23-2023, 11:49 AM)deanhystad Wrote: Is it really an issue? Timestamps are displayed in local time, but they are saved im GMT. Change the time zone of your computer to Kuala Lampur, and you will see the same timestamps as the "local guys".

I cannot make any changes to the hosts I'm connecting to. I'm allowed to copy files only, but thank you for the idea.
Reply
#5
(Jun-23-2023, 11:14 AM)Larz60+ Wrote: This can be done using pendulum (from command line: pip install pendulum)
see: https://pypi.org/project/pendulum/
and: https://www.geeksforgeeks.org/python-pendulum-module/
I probably confusing everyone.... Sorry about that!
I do not want to manipulate the datetime. I want to see the real timestamp. When a share from the Kuala Lumpur host is mapped to my server, the time stamps I see are wrong, the newest file's timestamps are shown as of my current date 06/22, but in Kuala Lumpur, the files were last updated on 06/23 and the time stamps of the files are 06/23, knowing that, I asked the local team to take a print screen of the files in the share and I see exactly same files from a share I mapped to my server have a different time stamp. As a result, I'm copying files I'm supposed to copy tomorrow...
Sorry for the confusion again! But I'm really confused myself, I have never thought I can see something like that.
Thank you.
Reply
#6
As I said, the timestamps are UTC (I said GMT earlier, but they are the same) and when displayed will be corrected for the timezone you are in. A file changed in Kuala Lampur will have a timestamp 15 hours older when viewed from a computer in Kuala Lampur than the same file viewed from Seattle. You can verify this by changing the timezone for your computer. Instantly the timestamps will agree.

I think the problem may be that you are being naive. Read the section about Aware vs Naive.

https://docs.python.org/3/library/datetime.html

You might also find this interesting.

https://howchoo.com/g/ywi5m2vkodk/workin...-in-python

Here's an example of using "aware" datetime objects.
import pytz
import os

x = os.path.getctime("input.txt")
koala_lampur = datetime.fromtimestamp(x, tz=pytz.timezone('Asia/Kuala_Lumpur'))
seattle = datetime.fromtimestamp(x, tz=pytz.timezone('US/Pacific'))
here = datetime.fromtimestamp(x)

print("Koala Lampur", koala_lampur)
print("Seattle", seattle)
print("US Central", here)
print(koala_lampur == seattle, koala_lampur == here, seattle == here)
Output:
Koala Lampur 2023-02-14 10:59:15.117121+08:00 Seattle 2023-02-13 18:59:15.117121-08:00 US Central 2023-02-13 20:59:15.117121 True False False
Even though Seattle and Koala Lampur datetime strings are 15 hours apart, Python returns True when asked if they are equal. The naive "here" datetime object displays a correct US/Central time, but is not appropriate for comparing against datetime objects from different time zones.
tester_V likes this post
Reply
#7
deanhystad! I'm definitely confused...
And thank you for the explanation of the problem I have, I really appreciate it!
I see your point and I'll try it for sure...

Thank you again! You are DA MAN! Wink
Reply
#8
From the documentation for os
Quote:os.path.getmtime(path)
Return the time of last modification of path. The return value is a floating point number giving the number of seconds since the epoch (see the time module). Raise OSError if the file does not exist or is inaccessible.
getmtime(path) returns an "aware" timestamp. By definition, the timestamp is in UTC/GMT. When you display the timestamp as a time/date string, localization will adjust the string to be in the local time zone, but the file timestamp(s) are always UTC. If you want to determine if a file timestamp falls within a window of times, you need to make the start and end times for this window "aware".

The first thing you need to do is decide what timezone you want to use to define "today", because "today" is a defined by the current time, and the time zone where the time is measured. Do you want the start of day in Seattle, the start of day in Kuala Lampur, or maybe you want to use UTC time.

Once you select a timezone, create start and end timestamps for the start and end of the day int that timezone. Because the function used to get file creation/modification times return timestamps, I think using a timestamp for the comparison makes more sense than converting everything to datetime objects. Now that all times are UTC timestamps you can determine if a file was created "today" by comparing the file ctime against the start and end timestamps. In the following example I list files modified "today" in Seattle.
from datetime import datetime, date, time, timedelta
from pytz import timezone
from pathlib import Path

# Get start and end of day in Seattle
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
end_time = start_time + timedelta(hours=24)
print(start_time, end_time)

# Convert to timestamps.
start_time = start_time.timestamp()
end_time = end_time.timestamp()
print(start_time, end_time)

# Print all files that were modified today.
folder = Path(__file__).parent  # Scan folder that contains this file.
for file in folder.iterdir():
    mtime = file.stat().st_mtime
    if start_time <= mtime < end_time:
        print(file.name, mtime, datetime.fromtimestamp(mtime))
One more thing to consider. Do you want your program to be aware of daylight savings time? If so, you need to adjust the start time to take daylight savings time into account.

Time is not easy. That is why there are Time Lords.
tester_V likes this post
Reply
#9
Well, it is looking like snipped made by a time Lord Wink
Let's I'll try it.
deanhystad, I'm not on your level, probably on even on the "same" frore or even not in the same building... and that is the fact. Sad
I'll try it and might come back for more help.
About the daylight savings time, I was planning to run it 3 hours after midnight Mlaysia time, I thought it would take care of it. Thank you again! You guys have amazing skills and are also very nice to talk to... and that is also great!
Thank you!
Reply
#10
It seems the code you shared is working very well!
Quick question, If I need to download 30 days of data, should I multiply 24 hours by 30?
before:
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
end_time = start_time + timedelta(hours=24)
After:
start_time = datetime.combine(date.today(), time(), tzinfo=timezone("US/Pacific"))
end_time = start_time + timedelta(hours=720)
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 824 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,232 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  error in timestamp Led_Zeppelin 3 3,242 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,016 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  access share attributed among several class methods drSlump 0 1,071 Nov-18-2021, 03:02 PM
Last Post: drSlump
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,502 Oct-21-2021, 03:29 AM
Last Post: buran
  How to take the tar backup files form remote server to local server sivareddy 0 1,917 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,486 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to share a numpy array between 2 processes on Windows? qstdy 0 2,170 Jan-29-2021, 04:24 AM
Last Post: qstdy
  Access Network SMB Share on a NAS deltapy 2 9,071 Oct-31-2020, 09:14 AM
Last Post: deltapy

Forum Jump:

User Panel Messages

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