[Python-checkins] CVS: python/nondist/sandbox/datetime datetime.py,1.26,1.27 test_datetime.py,1.18,1.19

Guido van Rossum gvanrossum@users.sourceforge.net
2002年3月03日 13:24:35 -0800


Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory usw-pr-cvs1:/tmp/cvs-serv20056
Modified Files:
	datetime.py test_datetime.py 
Log Message:
- Add isoformat() and utcisoformat(), returning ISO style timestamps.
- Use isoformat(' ') in __str__.
Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.26
retrieving revision 1.27
diff -C2 -d -r1.26 -r1.27
*** datetime.py	3 Mar 2002 20:52:29 -0000	1.26
--- datetime.py	3 Mar 2002 21:24:33 -0000	1.27
***************
*** 324,327 ****
--- 324,329 ----
 strftime(), utcstrftime()
 toordinal()
+ weekday(), isoweekday(), isocalendar()
+ isoformat(), utcisoformat()
 
 Properties (readonly):
***************
*** 438,450 ****
 def __str__(self):
 "Convert to pretty string, for str()."
! if self.__tzoffset == 0:
! return self.ctime() + " UTC"
! if self.__tzoffset > 0:
! h, m = divmod(self.__tzoffset, 60)
! else:
! h, m = divmod(-self.__tzoffset, 60)
! h = -h
! # XXX This is broken! ctime() renormalizes to local time. :-(
! return self.ctime() + " %+03d:%02d" % (h, m)
 
 # Read-only field accessors
--- 440,444 ----
 def __str__(self):
 "Convert to pretty string, for str()."
! return self.isoformat(sep=' ')
 
 # Read-only field accessors
***************
*** 669,672 ****
--- 663,701 ----
 week = 0
 return year, week+1, day+1
+ 
+ def isoformat(self, sep='T'):
+ """Return the time formatted according to ISO.
+ 
+ This is 'YYYY-MM-DD HH:MM:SS.mmmmmm-xx:yy'
+ where -xx:yy is the timezone offset.
+ 
+ Optional argument sep specifies the separator between date and
+ time, default 'T'.
+ 
+ This method does not convert to local time.
+ """
+ if self.__tzoffset >= 0:
+ h, m = divmod(self.__tzoffset, 60)
+ else:
+ h, m = divmod(-self.__tzoffset, 60)
+ h = -h
+ return "%04d-%02d-%02d%c%02d:%02d:%02d.%06d%+03d:%02d" % (
+ self.__year, self.__month, self.__day,
+ sep,
+ self.__hour, self.__minute, self.__second,
+ self.__microsecond,
+ h, m)
+ 
+ def utcisoformat(self, sep='T'):
+ """Return the time as UTC formatted according to ISO.
+ 
+ This is 'YYYY-MM-DD HH:MM:SS.mmmmmmZ'.
+ 
+ Optional argument sep specifies the separator between date and
+ time, default 'T'.
+ """
+ y, m, d, hh, mm = self._utc_ymdHM()
+ return "%04d-%02d-%02d%c%02d:%02d:%02d.%06dZ" % (
+ y, m, d, sep, hh, mm, self.__second, self.__microsecond)
 
 
Index: test_datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/test_datetime.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** test_datetime.py	3 Mar 2002 20:52:29 -0000	1.18
--- test_datetime.py	3 Mar 2002 21:24:33 -0000	1.19
***************
*** 295,298 ****
--- 295,305 ----
 self.assertEqual(L, iso_long_years)
 
+ def test_isoformat(self):
+ t = datetime(2, 3, 2, 4, 5, 1, 123, tzoffset=-120)
+ self.assertEqual(t.isoformat(), "0002年03月02日T04:05:01.000123-02:00")
+ self.assertEqual(t.isoformat(' '), "0002年03月02日 04:05:01.000123-02:00")
+ self.assertEqual(t.utcisoformat(), "0002年03月02日T06:05:01.000123Z")
+ self.assertEqual(t.utcisoformat(' '), "0002年03月02日 06:05:01.000123Z")
+ 
 def test_tmxxx(self):
 from datetime import tmxxx

AltStyle によって変換されたページ (->オリジナル) /