Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datetime.strptime at the turn of the year #67323

Closed
torm mannequin opened this issue Dec 30, 2014 · 4 comments
Closed

datetime.strptime at the turn of the year #67323

torm mannequin opened this issue Dec 30, 2014 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@torm
Copy link
Mannequin

torm mannequin commented Dec 30, 2014

BPO 23134
Nosy @abalkin, @vadmium, @serhiy-storchaka
Superseder
  • bpo-23136: BUG in how _strptime() handles week 0
  • Files
  • strptimetest.c
  • 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 2014-12-31.16:30:33.370>
    created_at = <Date 2014-12-30.18:04:02.231>
    labels = ['type-bug']
    title = 'datetime.strptime at the turn of the year'
    updated_at = <Date 2014-12-31.16:30:33.367>
    user = 'https://bugs.python.org/torm'

    bugs.python.org fields:

    activity = <Date 2014-12-31.16:30:33.367>
    actor = 'belopolsky'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-12-31.16:30:33.370>
    closer = 'belopolsky'
    components = []
    creation = <Date 2014-12-30.18:04:02.231>
    creator = 'torm'
    dependencies = []
    files = ['37566']
    hgrepos = []
    issue_num = 23134
    keywords = []
    message_count = 4.0
    messages = ['233213', '233215', '233239', '233249']
    nosy_count = 4.0
    nosy_names = ['belopolsky', 'martin.panter', 'serhiy.storchaka', 'torm']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '23136'
    type = 'behavior'
    url = 'https://bugs.python.org/issue23134'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3', 'Python 3.4', 'Python 3.5']

    @torm
    Copy link
    Mannequin Author

    torm mannequin commented Dec 30, 2014

    I've found wrong behaviour datetime.strptim function at the turn of the year.

    I've created datetime object base on the week number (%W), year (%Y) and day of week (%w). The date for Tuesday in the first week in 2015 is wrong:

    >>> from datetime import datetime
    >>> datetime.strptime('%s %s %s' % (0, 2015, 1), '%W %Y %w').date()
    datetime.date(2014, 12, 29) # OK
    
    >>> datetime.strptime('%s %s %s' % (0, 2015, 2), '%W %Y %w').date()
    datetime.date(2015, 1, 1) # WRONG !!!
    
    >>> datetime.strptime('%s %s %s' % (0, 2015, 3), '%W %Y %w').date()
    datetime.date(2014, 12, 31) # OK
    
    >>> datetime.strptime('%s %s %s' % (0, 2015, 4), '%W %Y %w').date()
    datetime.date(2015, 1, 1) # OK
    
    >>> datetime.strptime('%s %s %s' % (0, 2015, 5), '%W %Y %w').date()
    datetime.date(2015, 1, 2) # OK
    
    >>> datetime.strptime('%s %s %s' % (0, 2015, 6), '%W %Y %w').date()
    datetime.date(2015, 1, 3) # OK
    
    >>> datetime.strptime('%s %s %s' % (0, 2015, 0), '%W %Y %w').date()
    datetime.date(2015, 1, 4) # OK

    The same error exists in another years.

    Link to my post about this on stackoverflow:
    http://stackoverflow.com/questions/27708833/why-does-datetime-strptime-get-an-incorrect-date-for-tuesday-in-the-week-0-of

    @torm torm mannequin added the type-bug An unexpected behavior, bug, or error label Dec 30, 2014
    @serhiy-storchaka
    Copy link
    Member

    In C the strptime function doesn't return valid data if an input is invalid.

    $ ./strptimetest "0 2015 1" "%W %Y %w"
    2015-00--2 00:00:00
    $ ./strptimetest "0 2015 2" "%W %Y %w"
    2015-00--1 00:00:00
    $ ./strptimetest "0 2015 3" "%W %Y %w"
    2015-00-00 00:00:00
    $ ./strptimetest "0 2015 4" "%W %Y %w"
    2015-01-01 00:00:00
    $ ./strptimetest "0 2015 5" "%W %Y %w"
    2015-01-02 00:00:00
    $ ./strptimetest "0 2015 6" "%W %Y %w"
    2015-01-03 00:00:00
    $ ./strptimetest "0 2015 7" "%W %Y %w"
    2015-01-00 00:00:00

    So this behavior likely is not a bug and doesn't need to be fixed in maintained releases. But it would be good to make strptime() more consistant and either extend it to support week and weekday numbers out of current valid range, or raise an exception.

    @vadmium
    Copy link
    Member

    vadmium commented Dec 31, 2014

    bpo-23136 looks like a duplicate, but has a potential patch

    @abalkin
    Copy link
    Member

    abalkin commented Dec 31, 2014

    Closing as a duplicate of bpo-23136.

    @abalkin abalkin closed this as completed Dec 31, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants