Skip to content

Commit 095b603

Browse files
author
Richard Jones
committed
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
1 parent 4b2231b commit 095b603

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Fixed:
2525
- HTTP authorization takes precedence over session cookie (sf bug 1396134)
2626
- fixed documentation of filter() in the case of multiple values in a
2727
String search (sf bug 1373396)
28+
- tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) (sf bug
29+
1290550)
2830

2931

3032
2005-10-07 0.9.0b1

roundup/date.py

Lines changed: 16 additions & 10 deletions
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.82 2005-06-08 03:47:09 anthonybaxter Exp $
18+
# $Id: date.py,v 1.83 2006-01-13 00:22:16 richard Exp $
1919

2020
"""Date, time and time interval handling.
2121
"""
@@ -39,6 +39,19 @@ def _add_granularity(src, order, value = 1):
3939
src[gran] = int(src[gran]) + value
4040
break
4141

42+
# no, I don't know why we must anchor the date RE when we only ever use it
43+
# in a match()
44+
date_re = re.compile(r'''^
45+
((?P<y>\d\d\d\d)([/-](?P<m>\d\d?)([/-](?P<d>\d\d?))?)? # yyyy[-mm[-dd]]
46+
|(?P<a>\d\d?)[/-](?P<b>\d\d?))? # or mm-dd
47+
(?P<n>\.)? # .
48+
(((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d(\.\d+)?))?)? # hh:mm:ss
49+
(?P<o>[\d\smyd\-+]+)? # offset
50+
$''', re.VERBOSE)
51+
serialised_date_re = re.compile(r'''
52+
(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d?(\.\d+)?)
53+
''', re.VERBOSE)
54+
4255
class Date:
4356
'''
4457
As strings, date-and-time stamps are specified with the date in
@@ -153,15 +166,8 @@ def __init__(self, spec='.', offset=0, add_granularity=0, translator=i18n):
153166
except:
154167
raise ValueError, 'Unknown spec %r' % (spec,)
155168

156-
def set(self, spec, offset=0, date_re=re.compile(r'''
157-
((?P<y>\d\d\d\d)([/-](?P<m>\d\d?)([/-](?P<d>\d\d?))?)? # yyyy[-mm[-dd]]
158-
|(?P<a>\d\d?)[/-](?P<b>\d\d?))? # or mm-dd
159-
(?P<n>\.)? # .
160-
(((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d(\.\d+)?))?)? # hh:mm:ss
161-
(?P<o>.+)? # offset
162-
''', re.VERBOSE), serialised_re=re.compile(r'''
163-
(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d?(\.\d+)?)
164-
''', re.VERBOSE), add_granularity=0):
169+
def set(self, spec, offset=0, date_re=date_re,
170+
serialised_re=serialised_date_re, add_granularity=0):
165171
''' set the date to the value in spec
166172
'''
167173

test/test_dates.py

Lines changed: 2 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.36 2005-02-25 17:18:46 a1s Exp $
18+
# $Id: test_dates.py,v 1.37 2006-01-13 00:22:17 richard Exp $
1919
from __future__ import nested_scopes
2020

2121
import unittest, time
@@ -64,6 +64,7 @@ def testDateError(self):
6464
self.assertRaises(ValueError, Date, "12")
6565
# Date cannot handle dates before UNIX epoch
6666
self.assertRaises(ValueError, Date, (1, 1, 1, 0, 0, 0.0, 0, 1, -1))
67+
self.assertRaises(ValueError, Date, "1/1/06")
6768

6869
def testOffset(self):
6970
ae = self.assertEqual

0 commit comments

Comments
 (0)