[issue6823] time.strftime does unnecessary range check

2009-09-02 Thread Richard Shapiro

New submission from Richard Shapiro :

in Modules/timemodule.c, in the routine time_strftime, there is a range
check on the tm_isdst field:

if (buf.tm_isdst < -1 || buf.tm_isdst > 1) {
PyErr_SetString(PyExc_ValueError,
"daylight savings flag out of range");
return NULL;
}

This check is incorrect, as the tm_isdst field is interpreted as less
than 0, 0, or greater than zero (see the comment at the top of the
routine, and the documentation for the strftime function).
Unfortunately, there exists at least one platform for which tm_isdst is
set to something other than -1, 0, or 1 (on IBM zOS it's apparently set
to 2 for daylight savings). This means that you can't do the obvious 
strftime(...,localtime(...)) to format a time.

The fix is to either remove the range check entirely, or else to
normalize the +1, -1, or 0. The former is preferred unless there is some
platform on which strftime doesn't do the right thing with values
outside the range of [-1,1]. 

This bug exists in 2.5.1 and 2.6.2. I haven't checked other versions.

--
components: Library (Lib)
messages: 92170
nosy: rshapiro
severity: normal
status: open
title: time.strftime does unnecessary range check
type: behavior
versions: Python 2.5, Python 2.6

___
Python tracker 
<http://bugs.python.org/issue6823>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6823] time.strftime does unnecessary range check

2009-09-08 Thread Richard Shapiro

Richard Shapiro  added the comment:

Here's a patch to normalize the results of the various system calls
which return time information. This was against the source for Python 2.5.1.

*** timemodule.cTue Sep  8 10:28:31 2009
--- /home/rshapiro/216/redist/Python-2.5.1/Modules/timemodule.c.distMon
Jan 12 22:53:07 2009
***
*** 241,247 
  static PyObject *
  tmtotuple(struct tm *p)
  {
- long dstval;
PyObject *v = PyStructSequence_New(&StructTimeType);
if (v == NULL)
return NULL;
--- 241,246 
***
*** 256,271 
SET(5, p->tm_sec);
SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
SET(7, p->tm_yday + 1);/* Want January, 1 == 1 */
! /* Alas, on some platforms tm_isdst can be something other
than -1, 0, or 1 */
! if (p->tm_isdst < 0) {
!   dstval = -1;
! } else if (p->tm_isdst > 0) {
!   dstval = 1;
! } else {
!   dstval = 0;
! }
! 
!   SET(8, dstval);
  #undef SET
if (PyErr_Occurred()) {
Py_XDECREF(v);
--- 255,261 
SET(5, p->tm_sec);
SET(6, (p->tm_wday + 6) % 7); /* Want Monday == 0 */
SET(7, p->tm_yday + 1);/* Want January, 1 == 1 */
!   SET(8, p->tm_isdst);
  #undef SET
if (PyErr_Occurred()) {
Py_XDECREF(v);
(t)neo:/home/rshapiro/216/redist/patch/Python-2.5.1/Modules 140 %

--

___
Python tracker 
<http://bugs.python.org/issue6823>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com