[issue29081] time.strptime() return wrong result

Karthikeyan Singaravelan report at bugs.python.org
Wed Dec 19 13:31:11 EST 2018


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

This causes the round trip to be a ValueError. 

./python.exe
Python 3.8.0a0 (heads/master:1dd035954b, Dec 18 2018, 10:12:34)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> value = '2016 51 0'
>>> format = '%Y %W %w'
>>> time.strftime(format, time.strptime('2016 51 0', format)) == value
True
>>> time.strptime('2016 52 0', format)
time.struct_time(tm_year=2017, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=367, tm_isdst=-1)
>>> time.strftime(format, time.strptime('2016 52 0', format)) == value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day of year out of range

On Ruby this causes runtime error

irb(main):005:0> DateTime::strptime("2016 52 0", "%Y %W %w")
Traceback (most recent call last):
        3: from /usr/local/bin/irb:11:in `<main>'
        2: from (irb):5
        1: from (irb):5:in `strptime'
ArgumentError (invalid date)

With C it returns 2016 00 5 on Mac OS 10.10.4 and 2017 00 0 on Ubuntu

#include <stdio.h>
#include <time.h>

int main() {
  struct tm ltm = {0};
  char buf[] = "2016 52 0";
  strptime(buf, "%Y %W %w", &ltm);
  time_t ptm = mktime(&ltm);

  printf("tm year %d\n", ltm.tm_year);
  printf("tm yday %d\n", ltm.tm_yday);
  printf("tm wday %d\n", ltm.tm_wday);

  char str[50];
  struct tm *tm_info = localtime(&ptm);
  strftime(str, 50, "%Y %W %w", tm_info);
  printf("%s\n", str);
}

Output : 

Mac

tm year 116
tm yday 0
tm wday 5
2016 00 5

Ubuntu 

tm year 117
tm yday 0
tm wday 0
2017 00 0

----------
nosy: +belopolsky, p-ganssle
versions: +Python 3.7, Python 3.8 -Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue29081>
_______________________________________


More information about the Python-bugs-list mailing list