[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.14,1.15

Guido van Rossum gvanrossum@users.sourceforge.net
Sat, 02 Mar 2002 15:33:40 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory usw-pr-cvs1:/tmp/cvs-serv27887

Modified Files:
	datetime.py 
Log Message:
Add __str__ to timedelta that returns something like

    "7 days, 1 hour, 1 minute, 18 seconds, 321623 microseconds"


Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** datetime.py	2 Mar 2002 22:57:54 -0000	1.14
--- datetime.py	2 Mar 2002 23:33:38 -0000	1.15
***************
*** 146,149 ****
--- 146,168 ----
          return "timedelta(%d)" % self.__days
  
+     def __str__(self):
+         def plural(n):
+             return n, n != 1 and "s" or ""
+         L = []
+         if self.__days:
+             L.append("%d day%s" % plural(self.__days))
+         if self.__seconds:
+             mm, ss = divmod(self.__seconds, 60)
+             hh, mm = divmod(mm, 60)
+             if hh:
+                 L.append("%d hour%s" % plural(hh))
+             if mm:
+                 L.append("%d minute%s" % plural(mm))
+             if ss:
+                 L.append("%d second%s" % plural(ss))
+         if self.__microseconds:
+             L.append("%d microsecond%s" % plural(self.__microseconds))
+         return ", ".join(L)
+ 
      days = property(lambda self: self.__days, doc="days")
      seconds = property(lambda self: self.__seconds, doc="seconds")