Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7635fe1
TEMP: Add isoformatter test
pganssle Oct 21, 2021
b9b7a03
Add support for YYYYMMDD
pganssle Oct 21, 2021
c746b96
Expand support for ISO 8601 times
pganssle Oct 21, 2021
00978f9
Add support for ISO calendar-style strings
pganssle Oct 22, 2021
c36e306
Rework how string sanitization works
pganssle Oct 22, 2021
0234cae
WIP
pganssle Nov 16, 2021
ee1a7e3
Move Isoformatter into test helper, add date/time tests
pganssle Apr 27, 2022
7d2fd33
Final location for isoformatter and strategies
pganssle Apr 27, 2022
72266c4
Working version of date.isoformat
pganssle Apr 27, 2022
8067af1
Fix failure to set an error
pganssle May 1, 2022
7b9bca5
First version with time parsing allowed
pganssle May 1, 2022
328e781
Add support for leading T in time formatters
pganssle May 1, 2022
4d0e3a9
Fix pure python separator detection in YYYYWwwd
pganssle May 1, 2022
3e600f2
Version with all tests passing
pganssle May 1, 2022
e26f06f
Migrate fromisoformat tests to their own file
pganssle May 2, 2022
1ea0cd1
Fix bug in time parsing logic
pganssle May 2, 2022
1e3577f
s/ssize_t/size_t
pganssle May 2, 2022
6422799
Add fromisoformat example tests
pganssle May 2, 2022
3d24a15
Try to be consistent about use of double quotes in error messages
pganssle May 2, 2022
661b1b0
Update documentation
pganssle May 3, 2022
1defa1d
Remove isoformatter
pganssle May 3, 2022
75de7a4
Update out-of-date comment
pganssle May 3, 2022
07ee419
Only one space
pganssle May 3, 2022
3d0fb7a
Explicitly handle 0-length tzstr
pganssle May 3, 2022
cc8c737
Raise exceptions from None
pganssle May 3, 2022
31bf63e
Add test cases around week 53
pganssle May 3, 2022
5bfb3fc
Add examples around week 53
pganssle May 3, 2022
4879a47
Update docstrings
pganssle May 3, 2022
3cd657f
Add news entry
pganssle May 3, 2022
763d5bb
Add what's new entry
pganssle May 3, 2022
3a06505
Be consistent about ISO 8601
pganssle May 3, 2022
e643f02
Change name of isoformat separator detection function
pganssle May 5, 2022
5046809
Remove 'mode' logic and update comments
pganssle May 5, 2022
90093bf
Fix segfault case
pganssle May 5, 2022
6fc8157
Explicitly cast signed to unsigned
pganssle May 5, 2022
d9a766b
Document that ordinal dates are not supported
pganssle May 5, 2022
04ed787
Remove dead code
pganssle May 5, 2022
6da3e90
Various fixes
pganssle May 5, 2022
92cc0be
Fix example
pganssle May 5, 2022
bec0bee
Add example for time.fromisoformat
pganssle May 5, 2022
aad6011
Fix trailing colon
pganssle May 5, 2022
a33d776
Remove fromisoformat property test
pganssle May 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Various fixes
  • Loading branch information
pganssle committed May 5, 2022
commit 6da3e901321c2cf462ec38af47b4da9686456293
18 changes: 10 additions & 8 deletions Lib/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ def _wrap_strftime(object, format, timetuple):
return _time.strftime(newformat, timetuple)

# Helpers for parsing the result of isoformat()
def _is_ascii_digit(c):
return c in "0123456789"

def _find_isoformat_datetime_separator(dtstr):
# See the comment in _datetimemodule.c:_find_isoformat_datetime_separator
len_dtstr = len(dtstr)
Expand All @@ -279,7 +282,7 @@ def _find_isoformat_datetime_separator(dtstr):
if len_dtstr > 8 and dtstr[8] == date_separator:
if len_dtstr == 9:
raise ValueError("Invalid ISO string")
if len_dtstr > 10 and dtstr[10].isdigit():
if len_dtstr > 10 and _is_ascii_digit(dtstr[10]):
# This is as far as we need to resolve the ambiguity for
# the moment - if we have YYYY-Www-##, the separator is
# either a hyphen at 8 or a number at 10.
Expand All @@ -302,7 +305,7 @@ def _find_isoformat_datetime_separator(dtstr):
# YYYYWww (7) or YYYYWwwd (8)
idx = 7
while idx < len_dtstr:
if not dtstr[idx].isdigit():
if not _is_ascii_digit(dtstr[idx]):
break
idx += 1

Expand Down Expand Up @@ -342,7 +345,7 @@ def _parse_isoformat_date(dtstr):

dayno = int(dtstr[pos:pos + 1])

return _isoweek_to_gregorian(year, weekno, dayno)
return list(_isoweek_to_gregorian(year, weekno, dayno))
else:
month = int(dtstr[pos:pos + 2])
pos += 2
Expand Down Expand Up @@ -401,7 +404,7 @@ def _parse_hh_mm_ss_ff(tstr):
if to_parse < 6:
time_comps[3] *= _FRACTION_CORRECTION[to_parse-1]
if (len_remainder > to_parse
and not tstr[(pos+to_parse):].isdigit()):
and not all(map(_is_ascii_digit, tstr[(pos+to_parse):]))):
raise ValueError("Non-digit values in unparsed fraction")

return time_comps
Expand Down Expand Up @@ -482,7 +485,7 @@ def _isoweek_to_gregorian(year, week, day):
day_1 = _isoweek1monday(year)
ord_day = day_1 + day_offset

return list(_ord2ymd(ord_day))
return _ord2ymd(ord_day)


# Just raise TypeError if the arg isn't None or a string.
Expand Down Expand Up @@ -994,8 +997,6 @@ def fromisocalendar(cls, year, week, day):
This is the inverse of the date.isocalendar() function"""
return cls(*_isoweek_to_gregorian(year, week, day))

return cls(*_ord2ymd(ord_day))

# Conversions to string

def __repr__(self):
Expand Down Expand Up @@ -2625,7 +2626,8 @@ def _name_from_offset(delta):
_ord2ymd, _time, _time_class, _tzinfo_class, _wrap_strftime, _ymd2ord,
_divide_and_round, _parse_isoformat_date, _parse_isoformat_time,
_parse_hh_mm_ss_ff, _IsoCalendarDate, _isoweek_to_gregorian,
_find_isoformat_datetime_separator, _FRACTION_CORRECTION)
_find_isoformat_datetime_separator, _FRACTION_CORRECTION,
_is_ascii_digit)
# XXX Since import * above excludes names that start with _,
# docstring does not get overwritten. In the future, it may be
# appropriate to maintain a single module level docstring and
Expand Down
36 changes: 19 additions & 17 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,9 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month,
* 0: Success
* -1: Failed to parse date component
* -2: Inconsistent date separator usage
* -3: Failed to parse ISO week.
* -4: Failed to parse ISO day.
* -5, -6: Failure in iso_to_ymd
*/
const char *p = dtstr;
p = parse_digits(p, year, 4);
Expand Down Expand Up @@ -781,7 +784,7 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month,

int rv = iso_to_ymd(*year, iso_week, iso_day, year, month, day);
if (rv) {
return 3 - rv;
return -3 + rv;
} else {
return 0;
}
Expand All @@ -792,10 +795,8 @@ parse_isoformat_date(const char *dtstr, const size_t len, int *year, int *month,
return -1;
}

if (uses_separator) {
if (*(p++) != '-') {
return -2;
}
if (uses_separator && *(p++) != '-') {
return -2;
}
p = parse_digits(p, day, 2);
if (p == NULL) {
Expand All @@ -808,9 +809,11 @@ static int
parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour,
int *minute, int *second, int *microsecond)
{
*hour = *minute = *second = *microsecond = 0;
const char *p = tstr;
const char *p_end = tstr_end;
int *vals[3] = {hour, minute, second};
// This is initialized to satisfy an erroneous compiler warning.
unsigned char has_separator = 1;

// Parse [HH[:?MM[:?SS]]]
Expand Down Expand Up @@ -852,19 +855,16 @@ parse_hh_mm_ss_ff(const char *tstr, const char *tstr_end, int *hour,
return -3;
}

static int correction[5] = {
static int correction[] = {
100000, 10000, 1000, 100, 10
};

if (to_parse < 6) {
*microsecond *= correction[to_parse-1];
}

for (size_t i = 0; i < len_remains - 6; ++i) {
if (!is_digit(*p)) {
break;
}
p++;
while (is_digit(*p)){
++p; // skip truncated digits
}

// Return 1 if it's not the end of the string
Expand Down Expand Up @@ -918,7 +918,7 @@ parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute,
*tzmicrosecond = 0;

if (*(tzinfo_pos + 1) != '\0') {
return -6;
return -5;
} else {
return 1;
}
Expand All @@ -933,7 +933,7 @@ parse_isoformat_time(const char *dtstr, size_t dtlen, int *hour, int *minute,
*tzoffset = tzsign * ((tzhour * 3600) + (tzminute * 60) + tzsecond);
*tzmicrosecond *= tzsign;

return rv ? -7 : 1;
return rv ? -5 : 1;
}

/* ---------------------------------------------------------------------------
Expand Down Expand Up @@ -3120,7 +3120,7 @@ date_fromisocalendar(PyObject *cls, PyObject *args, PyObject *kw)
}

int month;
Py_ssize_t rv = iso_to_ymd(year, week, day, &year, &month, &day);
int rv = iso_to_ymd(year, week, day, &year, &month, &day);


if (rv == -2) {
Expand Down Expand Up @@ -4644,7 +4644,7 @@ time_fromisoformat(PyObject *cls, PyObject *tstr) {
// T, but the extended format allows this to be omitted as long as there
// is no ambiguity with date strings.
if (*p == 'T') {
p += 1;
++p;
len -= 1;
}

Expand Down Expand Up @@ -5286,10 +5286,12 @@ _sanitize_isoformat_str(PyObject *dtstr)
// in positions 7, 8 or 10. We'll check each of these for a surrogate and
// if we find one, replace it with `T`. If there is more than one surrogate,
// we don't have to bother sanitizing it, because the function will later
// fail when we try to convert the function into unicode characters.
// fail when we try to encode the string as ASCII.
static const size_t potential_separators[3] = {7, 8, 10};
size_t surrogate_separator = 0;
for(size_t idx = 0; idx < 3; ++idx) {
for(size_t idx = 0;
idx < sizeof(potential_separators) / sizeof(*potential_separators);
++idx) {
size_t pos = potential_separators[idx];
if (pos > (size_t)len) {
break;
Expand Down