[Zope3-checkins] CVS: Zope3/src/datetime - _datetime.py:1.27

Tim Peters tim.one@comcast.net
Thu, 23 Jan 2003 16:38:38 -0500


Update of /cvs-repository/Zope3/src/datetime
In directory cvs.zope.org:/tmp/cvs-serv29349/src/datetime

Modified Files:
	_datetime.py 
Log Message:
SF bug 660872: datetimetz constructors behave counterintuitively (2.3a1)
datetime .now() and .fromtimestamp():  The undocumented optional tzinfo=
argument is documented in the LaTeX docs now, and its name was changed
to tz= ("tzinfo" had enough meanings without this one).  These
constructors previously returned local time, and passively attached the
tzinfo argument to the result; "passively" == no conversion of date or
time members was done.  This was less than useful.  Now they convert
platform-local date and time to local time in tz's time zone, if tz is
specified.  If tz is not specified, they continue to return a naive
datetime object with platform-local date and time (as returned by the
platform C localtime() function).


=== Zope3/src/datetime/_datetime.py 1.26 => 1.27 ===
--- Zope3/src/datetime/_datetime.py:1.26	Wed Jan 22 15:43:59 2003
+++ Zope3/src/datetime/_datetime.py	Thu Jan 23 16:38:05 2003
@@ -1196,15 +1196,24 @@
                            doc="microsecond (0-999999)")
     tzinfo = property(lambda self: self._tzinfo, doc="timezone info object")
 
-    def fromtimestamp(cls, t, tzinfo=None):
+    def fromtimestamp(cls, t, tz=None):
         """Construct a datetime 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)
+
+        _check_tzinfo_arg(tz)
+        if tz is None:
+            converter = _time.localtime
+        else:
+            converter = _time.gmtime
+        y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
         us = int((t % 1.0) * 1000000)
         ss = min(ss, 59)    # clamp out leap seconds if the platform has them
-        return cls(y, m, d, hh, mm, ss, us, tzinfo)
+        result = cls(y, m, d, hh, mm, ss, us, tz)
+        if tz is not None:
+            result = tz.fromutc(result)
+        return result
     fromtimestamp = classmethod(fromtimestamp)
 
     def utcfromtimestamp(cls, t):
@@ -1220,10 +1229,10 @@
     # XXX uses gettimeofday on platforms that have it, but that isn't
     # XXX available from Python.  So now() may return different results
     # XXX across the implementations.
-    def now(cls, tzinfo=None):
+    def now(cls, tz=None):
         "Construct a datetime from time.time() and optional time zone info."
         t = _time.time()
-        return cls.fromtimestamp(t, tzinfo)
+        return cls.fromtimestamp(t, tz)
     now = classmethod(now)
 
     def utcnow(cls):