Jun-26-2023, 05:02 PM
(This post was last modified: Jun-26-2023, 05:02 PM by deanhystad.)
From the documentation for os
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.
Time is not easy. That is why there are Time Lords.
Quote:os.path.getmtime(path)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".
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.
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.