format_utcoffset(): The natural type of the buflen arg is size_t, so
used that. wrap_strftime(): Removed the most irritating uses of buf. TestDate.test_ordinal_conversions(): The C implementation is fast enough that we can afford to check the endpoints of every year. Also added tm_yday tests at the endpoints.
This commit is contained in:
parent
d0e2926d2a
commit
328fff7214
|
@ -469,20 +469,27 @@ class TestDate(unittest.TestCase):
|
||||||
self.assertEqual(fromord.second, 0)
|
self.assertEqual(fromord.second, 0)
|
||||||
self.assertEqual(fromord.microsecond, 0)
|
self.assertEqual(fromord.microsecond, 0)
|
||||||
|
|
||||||
# Check first and last days of year spottily across the whole
|
# Check first and last days of year across the whole range of years
|
||||||
# range of years supported.
|
# supported.
|
||||||
for year in xrange(MINYEAR, MAXYEAR+1, 7):
|
ordinal = 1
|
||||||
|
for year in xrange(MINYEAR, MAXYEAR+1):
|
||||||
# Verify (year, 1, 1) -> ordinal -> y, m, d is identity.
|
# Verify (year, 1, 1) -> ordinal -> y, m, d is identity.
|
||||||
d = self.theclass(year, 1, 1)
|
d = self.theclass(year, 1, 1)
|
||||||
n = d.toordinal()
|
n = d.toordinal()
|
||||||
|
self.assertEqual(ordinal, n)
|
||||||
d2 = self.theclass.fromordinal(n)
|
d2 = self.theclass.fromordinal(n)
|
||||||
self.assertEqual(d, d2)
|
self.assertEqual(d, d2)
|
||||||
# Verify that moving back a day gets to the end of year-1.
|
self.assertEqual(d.timetuple().tm_yday, 1)
|
||||||
if year > 1:
|
# Same for (year, 12, 31).
|
||||||
d = self.theclass.fromordinal(n-1)
|
isleap = year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
|
||||||
d2 = self.theclass(year-1, 12, 31)
|
days_in_year = 365 + isleap
|
||||||
self.assertEqual(d, d2)
|
d = self.theclass(year, 12, 31)
|
||||||
self.assertEqual(d2.toordinal(), n-1)
|
n = d.toordinal()
|
||||||
|
self.assertEqual(n, ordinal + days_in_year - 1)
|
||||||
|
self.assertEqual(d.timetuple().tm_yday, days_in_year)
|
||||||
|
d2 = self.theclass.fromordinal(n)
|
||||||
|
self.assertEqual(d, d2)
|
||||||
|
ordinal += days_in_year
|
||||||
|
|
||||||
# Test every day in a leap-year and a non-leap year.
|
# Test every day in a leap-year and a non-leap year.
|
||||||
dim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
dim = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||||
|
|
|
@ -812,7 +812,7 @@ format_ctime(PyDateTime_Date *date,
|
||||||
* bogus, an appropriate exception is set and -1 is returned.
|
* bogus, an appropriate exception is set and -1 is returned.
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
format_utcoffset(char *buf, int buflen, const char *sep,
|
format_utcoffset(char *buf, size_t buflen, const char *sep,
|
||||||
PyObject *tzinfo, PyObject *tzinfoarg)
|
PyObject *tzinfo, PyObject *tzinfoarg)
|
||||||
{
|
{
|
||||||
int offset;
|
int offset;
|
||||||
|
@ -863,13 +863,12 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple)
|
||||||
char *ptoappend; /* pointer to string to append to output buffer */
|
char *ptoappend; /* pointer to string to append to output buffer */
|
||||||
int ntoappend; /* # of bytes to append to output buffer */
|
int ntoappend; /* # of bytes to append to output buffer */
|
||||||
|
|
||||||
char buf[100]; /* scratch buffer */
|
|
||||||
|
|
||||||
assert(object && format && timetuple);
|
assert(object && format && timetuple);
|
||||||
assert(PyString_Check(format));
|
assert(PyString_Check(format));
|
||||||
|
|
||||||
/* Scan the input format, looking for %z and %Z escapes, building
|
/* Scan the input format, looking for %z and %Z escapes, building
|
||||||
* a new format.
|
* a new format. Since computing the replacements for those codes
|
||||||
|
* is expensive, don't unless they're actually used.
|
||||||
*/
|
*/
|
||||||
totalnew = PyString_Size(format); /* realistic if no %z/%Z */
|
totalnew = PyString_Size(format); /* realistic if no %z/%Z */
|
||||||
newfmt = PyString_FromStringAndSize(NULL, totalnew);
|
newfmt = PyString_FromStringAndSize(NULL, totalnew);
|
||||||
|
@ -880,8 +879,7 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple)
|
||||||
pin = PyString_AsString(format);
|
pin = PyString_AsString(format);
|
||||||
while ((ch = *pin++) != '\0') {
|
while ((ch = *pin++) != '\0') {
|
||||||
if (ch != '%') {
|
if (ch != '%') {
|
||||||
buf[0] = ch;
|
ptoappend = pin - 1;
|
||||||
ptoappend = buf;
|
|
||||||
ntoappend = 1;
|
ntoappend = 1;
|
||||||
}
|
}
|
||||||
else if ((ch = *pin++) == '\0') {
|
else if ((ch = *pin++) == '\0') {
|
||||||
|
@ -894,12 +892,13 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple)
|
||||||
else if (ch == 'z') {
|
else if (ch == 'z') {
|
||||||
if (zreplacement == NULL) {
|
if (zreplacement == NULL) {
|
||||||
/* format utcoffset */
|
/* format utcoffset */
|
||||||
|
char buf[100];
|
||||||
PyObject *tzinfo = get_tzinfo_member(object);
|
PyObject *tzinfo = get_tzinfo_member(object);
|
||||||
zreplacement = PyString_FromString("");
|
zreplacement = PyString_FromString("");
|
||||||
if (zreplacement == NULL) goto Done;
|
if (zreplacement == NULL) goto Done;
|
||||||
if (tzinfo != Py_None && tzinfo != NULL) {
|
if (tzinfo != Py_None && tzinfo != NULL) {
|
||||||
if (format_utcoffset(buf,
|
if (format_utcoffset(buf,
|
||||||
(int)sizeof(buf),
|
sizeof(buf),
|
||||||
"",
|
"",
|
||||||
tzinfo,
|
tzinfo,
|
||||||
object) < 0)
|
object) < 0)
|
||||||
|
@ -948,9 +947,8 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple)
|
||||||
ntoappend = PyString_Size(Zreplacement);
|
ntoappend = PyString_Size(Zreplacement);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
buf[0] = '%';
|
/* percent followed by neither z nor Z */
|
||||||
buf[1] = ch;
|
ptoappend = pin - 2;
|
||||||
ptoappend = buf;
|
|
||||||
ntoappend = 2;
|
ntoappend = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue