Skip to content

Commit 0b015d5

Browse files
author
Richard Jones
committed
handle Py2.3+ datetime objects as Date specs [SF#971300]
1 parent 1dfe079 commit 0b015d5

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Fixed:
3838
- less specific messages for login failures (thanks Chris Withers)
3939
- Reject raised against email messages should result in email rejection, not
4040
discarding of the message
41+
- handle Py2.3+ datetime objects as Date specs (sf bug 971300)
4142

4243

4344
2004-07-21 0.7.6

doc/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Roy Rapoport,
119119
John P. Rouillard,
120120
Ollie Rutherfurd,
121121
Toby Sargeant,
122+
Gregor Schmid,
122123
Florian Schulze,
123124
Klamer Schutte,
124125
Dougal Scott,

roundup/date.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: date.py,v 1.74 2004-07-04 05:07:19 richard Exp $
18+
# $Id: date.py,v 1.75 2004-09-29 07:27:08 richard Exp $
1919

2020
"""Date, time and time interval handling.
2121
"""
@@ -25,6 +25,12 @@
2525
from types import *
2626
import i18n
2727

28+
try:
29+
import datetime
30+
have_datetime = 1
31+
except:
32+
have_datetime = 0
33+
2834
def _add_granularity(src, order, value = 1):
2935
'''Increment first non-None value in src dictionary ordered by 'order'
3036
parameter
@@ -126,6 +132,11 @@ def __init__(self, spec='.', offset=0, add_granularity=0, translator=i18n):
126132
if type(spec) == type(''):
127133
self.set(spec, offset=offset, add_granularity=add_granularity)
128134
return
135+
elif have_datetime and isinstance(spec, datetime.datetime):
136+
# Python 2.3+ datetime object
137+
y,m,d,H,M,S,x,x,x = spec.timetuple()
138+
S += spec.microsecond/1000000.
139+
spec = (y,m,d,H,M,S,x,x,x)
129140
elif hasattr(spec, 'tuple'):
130141
spec = spec.tuple()
131142
elif isinstance(spec, Date):

test/test_dates.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717
#
18-
# $Id: test_dates.py,v 1.32 2004-04-18 05:31:03 richard Exp $
18+
# $Id: test_dates.py,v 1.33 2004-09-29 07:27:09 richard Exp $
1919
from __future__ import nested_scopes
2020

2121
import unittest, time
@@ -372,6 +372,14 @@ def ae(spec, pretty):
372372
ae('-1y', '1 year ago')
373373
ae('-2y', '2 years ago')
374374

375+
def testPyDatetime(self):
376+
try:
377+
import datetime
378+
except:
379+
return
380+
d = datetime.datetime.now()
381+
Date(d)
382+
375383
def test_suite():
376384
suite = unittest.TestSuite()
377385
suite.addTest(unittest.makeSuite(DateTestCase))

0 commit comments

Comments
 (0)