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….
#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


Messages In This Thread
RE: Timestamp of file changes if a share mapped to a server…. - by deanhystad - Jun-26-2023, 05:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 376 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 889 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,288 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  error in timestamp Led_Zeppelin 3 3,384 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,070 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  access share attributed among several class methods drSlump 0 1,119 Nov-18-2021, 03:02 PM
Last Post: drSlump
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,614 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,988 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,564 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  How to share a numpy array between 2 processes on Windows? qstdy 0 2,214 Jan-29-2021, 04:24 AM
Last Post: qstdy

Forum Jump:

User Panel Messages

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