[Python-checkins] python/nondist/sandbox/datetime datetime.py,1.61,1.62
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Tue, 19 Nov 2002 09:41:43 -0800
Update of /cvsroot/python/python/nondist/sandbox/datetime
In directory sc8-pr-cvs1:/tmp/cvs-serv30549
Modified Files:
datetime.py
Log Message:
Bunch of changes related to timetuple() etc.:
- Rip out the basetime class.
- Classes date and datetime should not support utctimetuple(); only
datetimetz does.
- Get rid of _mktime() method.
- Implement date.timetuple() and datetime.timetuple() without using
the time module. One caveat: the tm_yday field is currently left
zero. Maybe Tim can show me how to implement it. This goes into
internal method _jday().
- Implement datetimetz. utctimetuple() without using the time module.
We construct a naive time from our data and a timedelta from the utc
offset, subtract them, and get the timetuple() of the result.
All this passes the test suite, but I've got a feeling some things
aren't properly tested.
Index: datetime.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/datetime/datetime.py,v
retrieving revision 1.61
retrieving revision 1.62
diff -C2 -d -r1.61 -r1.62
*** datetime.py 7 Nov 2002 16:20:36 -0000 1.61
--- datetime.py 19 Nov 2002 17:41:40 -0000 1.62
***************
*** 249,272 ****
- # XXX I don't think we should use this. Let's rip it out.
- class basetime(object):
- """Abstract date/time type.
-
- See http://effbot.org/ideas/time-type.htm
- """
-
- def timetuple(self):
- raise NotImplementedError
-
- def utctimetuple(self):
- raise NotImplementedError
-
- def __cmp__(self, other):
- raise NotImplementedError
-
- def __hash__(self):
- raise NotImplementedError
-
-
class timedelta(object):
"""Represent the difference between two datetime objects.
--- 249,252 ----
***************
*** 487,491 ****
! class date(basetime):
"""Concrete date type.
--- 467,471 ----
! class date(object):
"""Concrete date type.
***************
*** 505,509 ****
Methods:
! timetuple(), utctimetuple()
toordinal()
weekday(), isoweekday(), isocalendar()
--- 485,489 ----
Methods:
! timetuple()
toordinal()
weekday(), isoweekday(), isocalendar()
***************
*** 578,591 ****
# Standard conversions, __cmp__, __hash__ (and helpers)
! # XXX These should be done without reference to the time module
! def _mktime(self):
! # Helper to return a POSIX-ish timestamp
! t = tmxxx(self.__year, self.__month, self.__day)
! return t.time()
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
! return _time.localtime(self._mktime())
def toordinal(self):
--- 558,572 ----
# Standard conversions, __cmp__, __hash__ (and helpers)
! def _yday(self):
! """Return tm_yday: day within the current year, where Jan 1 == 1.
! XXX This is not correct for now. Who cares.
! """
! return 0
def timetuple(self):
"Return local time tuple compatible with time.localtime()."
! return (self.__year, self.__month, self.__day,
! 0, 0, 0, self.weekday(), self._yday(), -1)
def toordinal(self):
***************
*** 624,628 ****
def strftime(self, fmt):
"Format using strftime()."
! return _time.strftime(fmt, _time.localtime(self._mktime()))
# Computations
--- 605,609 ----
def strftime(self, fmt):
"Format using strftime()."
! return _time.strftime(fmt, self.timetuple())
# Computations
***************
*** 734,738 ****
Methods:
! timetuple(), utctimetuple()
ctime()
strftime()
--- 715,719 ----
Methods:
! timetuple()
ctime()
strftime()
***************
*** 824,839 ****
# Standard conversions, __cmp__, __hash__ (and helpers)
! # XXX These should be done without reference to the time module
!
! def _mktime(self):
! # Helper to return a POSIX-ish timestamp
! t = tmxxx(self.__year, self.__month, self.__day,
! self.__hour, self.__minute, self.__second,
! self.__microsecond)
! return t.time()
!
! def utctimetuple(self):
! "Return UTC time tuple compatible with time.gmtime()."
! return _time.gmtime(self._mktime())
def __cmp__(self, other):
--- 805,813 ----
# Standard conversions, __cmp__, __hash__ (and helpers)
! def timetuple(self):
! "Return local time tuple compatible with time.localtime()."
! return (self.__year, self.__month, self.__day,
! self.__hour, self.__minute, self.__second,
! self.weekday(), self._yday(), -1)
def __cmp__(self, other):
***************
*** 954,957 ****
--- 928,958 ----
tzinfo = property(lambda self: self.__tzinfo, doc="timezone info object")
+
+ def fromtimestamp(cls, t, tzinfo=None):
+ """Construct a datetimetz from a POSIX timestamp (like time.time()).
+
+ A timezone info object may be passed in as well.
+ """
+ y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
+ us = int((t % 1.0) * 1000000)
+ return cls(y, m, d, hh, mm, ss, us, tzinfo)
+ fromtimestamp = classmethod(fromtimestamp)
+
+ def now(cls, tzinfo=None):
+ "Construct a datetime from time.time() and optional time zone info."
+ t = _time.time()
+ return cls.fromtimestamp(t, tzinfo)
+ now = classmethod(now)
+
+ def utctimetuple(self):
+ "Return UTC time tuple compatible with time.gmtime()."
+ offset = self.utcoffset()
+ if not offset: # Either None or 0
+ return self.timetuple()
+ ts = datetime(self.year, self.month, self.day,
+ self.hour, self.minute, self.second,
+ self.microsecond)
+ dt = timedelta(minutes=offset)
+ return (ts - dt).timetuple()
def isoformat(self, sep=' '):