Skip to content

time.strptime does not properly convert out-of-bounds values #70467

Closed

Description

@iaslan
mannequin
BPO 26279
Nosy @ned-deily, @Vgr255
Superseder
  • bpo-23718: strptime() can produce invalid date with negative year day
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2016-02-12.05:19:15.613>
    created_at = <Date 2016-02-03.16:02:51.498>
    labels = ['type-bug', 'library']
    title = 'time.strptime does not properly convert out-of-bounds values'
    updated_at = <Date 2016-02-12.05:21:10.901>
    user = 'https://bugs.python.org/iaslan'

    bugs.python.org fields:

    activity = <Date 2016-02-12.05:21:10.901>
    actor = 'ned.deily'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-02-12.05:19:15.613>
    closer = 'ned.deily'
    components = ['Library (Lib)']
    creation = <Date 2016-02-03.16:02:51.498>
    creator = 'iaslan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 26279
    keywords = []
    message_count = 4.0
    messages = ['259488', '259957', '260165', '260166']
    nosy_count = 4.0
    nosy_names = ['ned.deily', 'abarry', 'beng94', 'iaslan']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '23718'
    type = 'behavior'
    url = 'https://bugs.python.org/issue26279'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6']

    Activity

    1. iaslan commented on Feb 3, 2016

      iaslanmannequin
      MannequinAuthor

      There seems to be a bug in the time library when performing the following conversion:

      """
      In [8]: mytime=time.strptime(str([2015, 53, 0]), '[%Y, %U, %w]')

      In [9]: mytime
      Out[9]: time.struct_time(tm_year=2016, tm_mon=1, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=368, tm_isdst=-1)

      In [10]: time.strftime('%Y %U', mytime)
      ---------------------------------------------------------------------------

      ValueError Traceback (most recent call last)
      <ipython-input-10-c13538c08ea5> in <module>()
      ----> 1 time.strftime('%Y %U', mytime)

      ValueError: day of year out of range
      """

      As you can observe, tm_yday got a value of 368 instead of 3. It seems that the C function is not properly subtracting 365 or 366 days when converting from week 53 of a particular year which, according to ISO 8601 [1] and per python documentation [2]. Documentation explicitly says that: "The first week of the year is the week with the year's first Thursday in it".

      [1] https://en.wikipedia.org/wiki/ISO_8601#Week_dates
      [2] https://docs.python.org/2/library/time.html

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Feb 3, 2016
    3. changed the title [-]Possible bug in time library[/-] [+]time.strptime does not properly convert out-of-bounds values[/+] on Feb 3, 2016
    4. beng94 commented on Feb 9, 2016

      beng94mannequin
      Mannequin

      This is connected to http://bugs.python.org/issue23718

      I submitted a patch that hopefully solves this problem.

    5. ned-deily commented on Feb 12, 2016

      @ned-deily
      Member

      Thanks for the report. Closing this issue as a duplicate of bpo-23719.

    6. ned-deily commented on Feb 12, 2016

      @ned-deily
      Member

      Make that bpo-23718

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions