@@ -359,8 +359,41 @@ def timestamp_pattern(self, value):
359359 set_property (self , 'timestamp_pattern' , pattern )
360360
361361
362- class RotateBackups (PropertyManager ):
362+ class FilestatMatch (Match ):
363+ """A match based on a filename statistics."""
364+
365+ entry : str = None
366+ field : str = None
367+
368+ def __init__ (self , entry : str , timestamp = 'mtime' ):
369+ """Make a Match from a field's stat attribute."""
370+ self .entry = entry
371+ self .field = f'st_{ timestamp } '
372+
373+ def match_to_datetime (self ) -> datetime .datetime :
374+ """Return a datetime from the file's stat field."""
375+ return datetime .datetime .fromtimestamp (
376+ getattr (os .stat (self .entry ), self .field )
377+ )
378+
379+
380+ class FilestatMatcher (Matcher ):
381+ """A date-matching scheme based on file statistics."""
363382
383+ timestamp : str = None
384+
385+ def __init__ (self , timestamp : str = 'mtime' ):
386+ """Make a Matcher based on a file's stat attribute."""
387+ self .timestamp = timestamp
388+
389+ def search (self , location : str , entry : str ) -> FilestatMatch :
390+ """Return the file's stat attribute."""
391+ return FilestatMatch (
392+ os .path .join (location .directory , entry ),
393+ self .timestamp )
394+
395+
396+ class RotateBackups (PropertyManager ):
364397 """Python API for the ``rotate-backups`` program."""
365398
366399 _matcher : Matcher = FilenameMatcher (TIMESTAMP_PATTERN )
@@ -379,6 +412,9 @@ def __init__(self, rotation_scheme, **options):
379412 """
380413 options .update (rotation_scheme = rotation_scheme )
381414 super (RotateBackups , self ).__init__ (** options )
415+ if self .stat_timestamp :
416+ logger .info ("Using file mtime to determine file date" )
417+ self .matcher = FilestatMatcher ()
382418
383419 @mutable_property
384420 def config_file (self ):
@@ -519,6 +555,10 @@ def rotation_scheme(self):
519555 in the dictionary.
520556 """
521557
558+ @mutable_property
559+ def stat_timestamp (self ):
560+ """Whether to use the files' mtime instead of parsing their name."""
561+
522562 @mutable_property
523563 def strict (self ):
524564 """
0 commit comments