[Python-checkins] r82143 - sandbox/branches/py3k-datetime/datetime.py
alexander.belopolsky
python-checkins at python.org
Mon Jun 21 19:37:20 CEST 2010
Author: alexander.belopolsky
Date: Mon Jun 21 19:37:19 2010
New Revision: 82143
Log:
updated date comparisons to match 3.x
Modified:
sandbox/branches/py3k-datetime/datetime.py
Modified: sandbox/branches/py3k-datetime/datetime.py
==============================================================================
--- sandbox/branches/py3k-datetime/datetime.py (original)
+++ sandbox/branches/py3k-datetime/datetime.py Mon Jun 21 19:37:19 2010
@@ -19,7 +19,7 @@
import time as _time
import math as _math
-def cmp(x, y):
+def _cmp(x, y):
return 0 if x == y else 1 if x > y else -1
MINYEAR = 1
@@ -298,35 +298,11 @@
if tz is not None and not isinstance(tz, tzinfo):
raise TypeError("tzinfo argument must be None or of a tzinfo subclass")
-
-# Notes on comparison: In general, datetime module comparison operators raise
-# TypeError when they don't know how to do a comparison themself. If they
-# returned NotImplemented instead, comparison could (silently) fall back to
-# the default compare-objects-by-comparing-their-memory-addresses strategy,
-# and that's not helpful. There are two exceptions:
-#
-# 1. For date and datetime, if the other object has a "timetuple" attr,
-# NotImplemented is returned. This is a hook to allow other kinds of
-# datetime-like objects a chance to intercept the comparison.
-#
-# 2. Else __eq__ and __ne__ return False and True, respectively. This is
-# so opertaions like
-#
-# x == y
-# x != y
-# x in sequence
-# x not in sequence
-# dict[x] = y
-#
-# don't raise annoying TypeErrors just because a datetime object
-# is part of a heterogeneous collection. If there's no known way to
-# compare X to a datetime, saying they're not equal is reasonable.
-
def _cmperror(x, y):
raise TypeError("can't compare '%s' to '%s'" % (
type(x).__name__, type(y).__name__))
-class timedelta(object):
+class timedelta:
"""Represent the difference between two datetime objects.
Supported operators:
@@ -579,7 +555,7 @@
return q, timedelta(0, 0, r)
return NotImplemented
- # Comparisons.
+ # Comparisons of timedelta objects with other.
def __eq__(self, other):
if isinstance(other, timedelta):
@@ -619,7 +595,7 @@
def __cmp(self, other):
assert isinstance(other, timedelta)
- return cmp(self.__getstate(), other.__getstate())
+ return _cmp(self.__getstate(), other.__getstate())
def __hash__(self):
return hash(self.__getstate())
@@ -631,8 +607,6 @@
# Pickle support.
- __safe_for_unpickling__ = True # For Python 2.2
-
def __getstate(self):
return (self.__days, self.__seconds, self.__microseconds)
@@ -792,31 +766,22 @@
_check_date_fields(year, month, day)
return date(year, month, day)
- # Comparisons.
+ # Comparisons of date objects with other.
def __eq__(self, other):
if isinstance(other, date):
return self.__cmp(other) == 0
- elif hasattr(other, "timetuple"):
- return NotImplemented
- else:
- return False
+ return NotImplemented
def __ne__(self, other):
if isinstance(other, date):
return self.__cmp(other) != 0
- elif hasattr(other, "timetuple"):
- return NotImplemented
- else:
- return True
+ return NotImplemented
def __le__(self, other):
if isinstance(other, date):
return self.__cmp(other) <= 0
- elif hasattr(other, "timetuple"):
- return NotImplemented
- else:
- _cmperror(self, other)
+ return NotImplemented
def __lt__(self, other):
if isinstance(other, date):
@@ -837,7 +802,7 @@
assert isinstance(other, date)
y, m, d = self.__year, self.__month, self.__day
y2, m2, d2 = other.__year, other.__month, other.__day
- return cmp((y, m, d), (y2, m2, d2))
+ return _cmp((y, m, d), (y2, m2, d2))
def __hash__(self):
"Hash."
@@ -912,8 +877,6 @@
# Pickle support.
- __safe_for_unpickling__ = True # For Python 2.2
-
def __getstate(self):
yhi, ylo = divmod(self.__year, 256)
return bytes([yhi, ylo, self.__month, self.__day]),
@@ -933,7 +896,7 @@
date.max = date(9999, 12, 31)
date.resolution = timedelta(days=1)
-class tzinfo(object):
+class tzinfo:
"""Abstract base class for time zone info classes.
Subclasses must override the name(), utcoffset() and dst() methods.
@@ -987,8 +950,6 @@
# Pickle support.
- __safe_for_unpickling__ = True # For Python 2.2
-
def __reduce__(self):
getinitargs = getattr(self, "__getinitargs__", None)
if getinitargs:
@@ -1005,9 +966,9 @@
else:
return (self.__class__, args, state)
-_tzinfo_class = tzinfo # so functions w/ args named "tinfo" can get at it
+_tzinfo_class = tzinfo # so functions w/ args named "tzinfo" can get at it
-class time(object):
+class time:
"""Time with time zone.
Constructors:
@@ -1064,7 +1025,7 @@
# Standard conversions, __hash__ (and helpers)
- # Comparisons.
+ # Comparisons of time objects with other.
def __eq__(self, other):
if isinstance(other, time):
@@ -1116,8 +1077,8 @@
base_compare = myoff == otoff
if base_compare:
- return cmp((self.__hour, self.__minute, self.__second,
- self.__microsecond),
+ return _cmp((self.__hour, self.__minute, self.__second,
+ self.__microsecond),
(other.__hour, other.__minute, other.__second,
other.__microsecond))
if myoff is None or otoff is None:
@@ -1125,8 +1086,8 @@
raise TypeError("cannot compare naive and aware times")
myhhmm = self.__hour * 60 + self.__minute - myoff
othhmm = other.__hour * 60 + other.__minute - otoff
- return cmp((myhhmm, self.__second, self.__microsecond),
- (othhmm, other.__second, other.__microsecond))
+ return _cmp((myhhmm, self.__second, self.__microsecond),
+ (othhmm, other.__second, other.__microsecond))
def __hash__(self):
"""Hash."""
@@ -1274,8 +1235,6 @@
# Pickle support.
- __safe_for_unpickling__ = True # For Python 2.2
-
def __getstate(self):
us2, us3 = divmod(self.__microsecond, 256)
us1, us2 = divmod(us2, 256)
@@ -1590,7 +1549,7 @@
offset = _check_utc_offset("dst", offset)
return offset
- # Comparisons.
+ # Comparisons of datetime objects with other.
def __eq__(self, other):
if isinstance(other, datetime):
@@ -1656,9 +1615,9 @@
base_compare = myoff == otoff
if base_compare:
- return cmp((self.__year, self.__month, self.__day,
- self.__hour, self.__minute, self.__second,
- self.__microsecond),
+ return _cmp((self.__year, self.__month, self.__day,
+ self.__hour, self.__minute, self.__second,
+ self.__microsecond),
(other.__year, other.__month, other.__day,
other.__hour, other.__minute, other.__second,
other.__microsecond))
@@ -1726,8 +1685,6 @@
# Pickle support.
- __safe_for_unpickling__ = True # For Python 2.2
-
def __getstate(self):
yhi, ylo = divmod(self.__year, 256)
us2, us3 = divmod(self.__microsecond, 256)
More information about the Python-checkins
mailing list