[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[minion-cvs] Add function to parse dates the way we format them for ...



Update of /home/minion/cvsroot/src/minion/lib/mixminion
In directory moria.mit.edu:/tmp/cvs-serv16685

Modified Files:
	Common.py 
Log Message:
Add function to parse dates the way we format them for filenames

Index: Common.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/Common.py,v
retrieving revision 1.140
retrieving revision 1.141
diff -u -d -r1.140 -r1.141
--- Common.py	17 May 2004 22:39:17 -0000	1.140
+++ Common.py	27 Jul 2004 03:05:47 -0000	1.141
@@ -13,6 +13,7 @@
             'encodeBase64', 'englishSequence', 'floorDiv', 'formatBase64',
             'formatDate', 'formatFnameDate', 'formatFnameTime', 'formatTime',
             'installSIGCHLDHandler', 'isSMTPMailbox', 'openUnique',
+            'parseFnameDate',
             'previousMidnight', 'readFile', 'readPickled',
             'readPossiblyGzippedFile', 'secureDelete', 'stringContains',
             'succeedingMidnight', 'tryUnlink', 'unarmorText',
@@ -1206,7 +1207,8 @@
 
 def formatFnameDate(when=None):
     """Given a time in seconds since the epoch, returns a date value suitable
-       for use as part of a filename.  Defaults to the current time."""
+       for use as part of a filename, in YYYYMMDD format.  Defaults to the
+       current time."""
     if when is None:
         when = time.time()
     return time.strftime("%Y%m%d", time.localtime(when))
@@ -1219,6 +1221,20 @@
         when = time.time()
     return time.strftime("%Y%m%d%H%M%S", time.localtime(when))
 
+def parseFnameDate(s):
+    """Given a date as generated by formatFnameTime, return a (long) time
+       value for midnight on that date."""
+    if len(s) != 8:
+        raise ValueError("Invalid filename date %r (wrong length)"%s)
+    try:
+        yyyy = int(s[:4])
+        MM = int(s[4:6])
+        dd = int(s[6:])
+    except ValueError:
+        raise ValueError("Invalid filename date %r (contains noninteger)"%s)
+
+    return calendar.timegm((yyyy,MM,dd,0,0,0,0,0,0))
+
 #----------------------------------------------------------------------
 class Duration:
     """A Duration is a number of time units, such as '1.5 seconds' or