Is there a suitably Pythonic way of determining if a file was created between particular times on a particular day (or days) of the week - e.g. 09:00 to 17:00 Monday to Friday?
At the moment I have:
def IsEligible(filename, between = None, weekdays = None):
"""
Determines the elgibility of a file for processing based on
the date and time it was modified (mtime). 'weekdays' is a list
of day numbers (Monday = 0 .. Sunday = 6) or None to indicate ALL
days of the week. 'between' is a tuple which contains the
lower and upper limits (as datetime.time objects) of the periods
under consideration or None to indicate ALL times of day.
"""
modified = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
dow = modified.weekday()
mtime = modified.time()
if between is not None:
earliest = min(between)
latest = max(between)
if mtime < earliest or mtime >= latest:
return False
if weekdays is not None and dow not in weekdays:
return False
print(filename, modified)
return True
which works fine, but I didn't know if there was something smarter. (I know it's a bit wordy, but hopefully it's more readable that way).
One final thing, I originally used ctime
rather than mtime
but it didn't produce the results I was after, and seemed just to return the current time, even though the files hadn't been modified or anything since creation. Under what circumstances does the ctime
value get reset?