Skip to content

Commit

Permalink
further py3k work
Browse files Browse the repository at this point in the history
  • Loading branch information
hannosch committed Feb 23, 2013
1 parent 93ed01e commit b9ddd8b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 29 deletions.
21 changes: 20 additions & 1 deletion src/DateTime/DateTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import copyreg as copy_reg
basestring = str
long = int
explicit_unicode_type = type(None)
else:
import copy_reg
explicit_unicode_type = unicode

default_datefmt = None

Expand Down Expand Up @@ -1242,6 +1244,8 @@ def greaterThan(self, t):
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros > long(t * 1000000)
try:
Expand All @@ -1263,6 +1267,8 @@ def greaterThanEqualTo(self, t):
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros >= long(t * 1000000)
try:
Expand All @@ -1283,6 +1289,8 @@ def equalTo(self, t):
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros == long(t * 1000000)
try:
Expand Down Expand Up @@ -1328,6 +1336,8 @@ def lessThan(self, t):
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros < long(t * 1000000)
try:
Expand All @@ -1348,6 +1358,8 @@ def lessThanEqualTo(self, t):
Revised to give more correct results through comparison of
long integer microseconds.
"""
if t is None:
t = 0
if isinstance(t, float):
return self._micros <= long(t * 1000000)
try:
Expand Down Expand Up @@ -1537,9 +1549,16 @@ def strftime(self, format):
tzdiff = _tzoffset(ltz, self._t) - _tzoffset(self._tz, self._t)
zself = self + tzdiff / 86400.0
microseconds = int((zself._second - zself._nearsec) * 1000000)
return datetime(zself._year, zself._month, zself._day, zself._hour,
unicode_format = False
if isinstance(format, explicit_unicode_type):
format = format.encode('utf-8')
unicode_format = True
ds = datetime(zself._year, zself._month, zself._day, zself._hour,
zself._minute, int(zself._nearsec),
microseconds).strftime(format)
if unicode_format:
return ds.decode('utf-8')
return ds

# General formats from previous DateTime
def Date(self):
Expand Down
33 changes: 17 additions & 16 deletions src/DateTime/DateTime.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ with from zero to seven arguments:

>>> x = DateTime('1997/3/9 1:45pm')
>>> x.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, 0.0, ...)
(1997, 3, 9, 13, 45, ...)

o Specified time in local machine zone, verbose format:

>>> y = DateTime('Mar 9, 1997 13:45:00')
>>> y.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, 0.0, ...)
(1997, 3, 9, 13, 45, ...)
>>> y == x
True

Expand Down Expand Up @@ -330,8 +330,8 @@ Component access
* ``parts()`` returns a tuple containing the calendar year, month,
day, hour, minute second and timezone of the object

>>> dt.parts()
(1997, 3, 9, 13, 45, 0.0, 'US/Eastern')
>>> y.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, ... 'US/Eastern')

* ``timezone()`` returns the timezone in which the object is represented:

Expand Down Expand Up @@ -432,13 +432,13 @@ Component access

* ``second()`` returns the second:

>>> dt.second()
0.0
>>> dt.second() == 0
True

* ``millis()`` returns the milliseconds since the epoch in GMT.

>>> dt.millis()
857933100000L
>>> dt.millis() == 857933100000
True

strftime()
~~~~~~~~~~
Expand Down Expand Up @@ -603,7 +603,7 @@ General Services Provided by DateTime
DateTimes can be repr()'ed; the result will be a string indicating how
to make a DateTime object like this:

>>> `dt`
>>> repr(dt)
"DateTime('1997/03/09 13:45:00 US/Eastern')"

When we convert them into a string, we get a nicer string that could
Expand Down Expand Up @@ -743,10 +743,13 @@ DateTime:

Two DateTimes cannot be added:

>>> dt + dt
Traceback (most recent call last):
...
DateTimeError: Cannot add two DateTimes
>>> from DateTime.interfaces import DateTimeError
>>> try:
... dt + dt
... print('fail')
... except DateTimeError:
... print('ok')
ok

Either a DateTime or a number may be subtracted from a DateTime,
however, a DateTime may not be subtracted from a number:
Expand All @@ -761,11 +764,9 @@ however, a DateTime may not be subtracted from a number:
TypeError: unsupported operand type(s) for -: 'int' and 'DateTime'

DateTimes can also be converted to integers (number of seconds since
the epoch), longs (not too long ;)) and floats:
the epoch) and floats:

>>> int(dt)
857933100
>>> long(dt)
857933100L
>>> float(dt)
857933100.0
13 changes: 8 additions & 5 deletions src/DateTime/pytz.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ examples:

Of course pytz doesn't know about everything.

>>> d = DateTime('July 21, 1969 Moon/Eastern')
Traceback (most recent call last):
...
SyntaxError: July 21, 1969 Moon/Eastern
>>> from DateTime.interfaces import SyntaxError
>>> try:
... d = DateTime('July 21, 1969 Moon/Eastern')
... print('fail')
... except SyntaxError:
... print('ok')
ok

You can still use zone names that DateTime defines that aren't part of
the pytz database.
Expand Down Expand Up @@ -112,7 +115,7 @@ The DateTime class uses a new time zone cache.

>>> from DateTime.DateTime import _TZINFO
>>> _TZINFO #doctest: +ELLIPSIS
<DateTime.pytz_support.PytzCache instance at ...>
<DateTime.pytz_support.PytzCache ...>

The cache maps time zone names to time zone instances.

Expand Down
15 changes: 8 additions & 7 deletions src/DateTime/tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

if sys.version_info > (3, ):
import pickle
unicode = str
else:
import cPickle as pickle

Expand Down Expand Up @@ -238,10 +239,10 @@ def test_pickle_with_micros(self):

def test_pickle_old(self):
dt = DateTime('2002/5/2 8:00am GMT+0')
data = ('(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05_amonq'
'\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq\x08h'
'\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU\x04T'
'hu.q\x0cU\x05_fdayq\rU\x08Thursdayq\x0eU\x03_pmq\x0fU\x02amq'
data = ('(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05'
'_amonq\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq'
'\x08h\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU'
'\x04Thu.q\x0cU\x05_fdayq\rU\x08Thursdayq\x0eU\x03_pmq\x0fU\x02amq'
'\x10U\x02_tq\x11GA\xcehy\x00\x00\x00\x00U\x07_minuteq\x12K\x00U'
'\x07_microsq\x13L1020326400000000L\nU\x02_dq\x14G@\xe2\x12j\xaa'
'\xaa\xaa\xabU\x07_secondq\x15G\x00\x00\x00\x00\x00\x00\x00\x00U'
Expand All @@ -256,9 +257,9 @@ def test_pickle_old(self):

def test_pickle_old_without_micros(self):
dt = DateTime('2002/5/2 8:00am GMT+0')
data = ('(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05_amonq'
'\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq\x08h'
'\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU'
data = ('(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05'
'_amonq\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq'
'\x08h\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU'
'\x04Thu.q\x0cU\x05_fdayq\rU\x08Thursdayq\x0eU\x03_pmq\x0fU'
'\x02amq\x10U\x02_tq\x11GA\xcehy\x00\x00\x00\x00U\x07_minuteq'
'\x12K\x00U\x02_dq\x13G@\xe2\x12j\xaa\xaa\xaa\xabU\x07_secondq'
Expand Down

0 comments on commit b9ddd8b

Please sign in to comment.