This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.datetime.strptime('200722', '%Y%U')
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: matheus.v.portela, mrabarnett, musically_ut, p-ganssle, serge-bon, winton, xtreak
Priority: normal Keywords: patch, patch, patch

Created on 2017-06-29 08:58 by winton, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 11594 closed serge-bon, 2019-01-17 18:04
PR 11594 closed serge-bon, 2019-01-17 18:04
PR 11594 closed serge-bon, 2019-01-17 18:04
Messages (11)
msg297265 - (view) Author: winton Wang (winton) Date: 2017-06-29 08:58
datetime stiptime error according to "%U" format.

Python version: 3.4.3, 2.7.13 ,3.6.0, 3.5.2


from datetime import datetime

input:
  datetime.strptime('201726', '%Y%U')

output:
  datetime.datetime(2007, 1, 1, 0, 0)
msg297266 - (view) Author: winton Wang (winton) Date: 2017-06-29 09:03
the output:
   datetime.datetime(2017, 1, 1, 0, 0)

I question about the week format if without the week of the weekday, the datetime should be the fist day of week as the default?
msg297274 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2017-06-29 14:59
Expected result is datetime.datetime(2017, 6, 25, 0, 0).
msg297714 - (view) Author: Utkarsh Upadhyay (musically_ut) * Date: 2017-07-05 08:24
This case is explicitly mentioned in the documentation: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior


> 7. When used with the strptime() method, %U and %W are only used in calculations when the day of the week and the calendar year (%Y) are specified.

The documentation also says that the %u (day of the week) format specifier was added only in Python 3.6.

~
ut
msg298840 - (view) Author: Matheus Vieira Portela (matheus.v.portela) * Date: 2017-07-22 08:02
Therefore, should we implement the behavior of assuming the first day of the week if none is provided or not?
msg299114 - (view) Author: Utkarsh Upadhyay (musically_ut) * Date: 2017-07-25 15:39
@Matheus: What other languages support this? Is this (i.e. defaulting to Sunday, or Monday?) the default behavior there?
msg299124 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2017-07-25 17:35
I think the relevant standard is ISO 8601:

https://en.wikipedia.org/wiki/ISO_8601

The first day of the week is Monday.

Note particularly the examples it gives:

    Monday 29 December 2008 is written "2009-W01-1"
    Sunday 3 January 2010 is written "2009-W53-7"

So the first few days of January can be in the last week of the previous year!
msg299446 - (view) Author: Matheus Vieira Portela (matheus.v.portela) * Date: 2017-07-29 02:02
Agreed that we should attain to ISO 8601, as it solves two questions that occurred in this thread:

- According to the specification, it is clear that a week must start on Monday and end on Sunday. Hence datetime.strptime('201726', '%Y%U') could be safely assigned to 2017/06/26 without ambiguity

- It also makes clear on what to do when a week overlaps two years.

IMHO there is no reason to not implement this feature and remove the requirement of using %U with the year and day of the week.
msg332399 - (view) Author: Serge Bon. (serge-bon) * Date: 2018-12-24 02:30
Documentation says:

%U - Week number of the year (Sunday as the first day of the week)
%W - Week number of the year (Monday as the first day of the week)

It wouldn't be intuitive if %U assumed Sunday when weekday provided and Monday otherwise. There are two separate directives specifically to distinguish these two cases, so which day of the week is the first one should be determined on the used directive:

>>> datetime.strptime ('2018 Mon 52', '%Y %a %W')
datetime.datetime(2018, 12, 24, 0, 0)

>>> datetime.strptime ('2018 52',     '%Y %W')
datetime.datetime(2018, 12, 24, 0, 0)

>>> datetime.strptime ('2018 Sun 52', '%Y %a %U')
datetime.datetime(2018, 12, 30, 0, 0)

>>> datetime.strptime ('2018 52',     '%Y %U')
datetime.datetime(2018, 12, 30, 0, 0)
msg332400 - (view) Author: Serge Bon. (serge-bon) * Date: 2018-12-24 02:46
Not following msg332388 would lead to this:


# Sunday of the week 53

>>> datetime.strptime ('2017 Sun 53', '%Y %a %U')
datetime.datetime(2017, 12, 31, 0, 0)


# First day of the week 53
# oops! assumed Monday is the first day of the week but 2017 ends on Sunday

>>> datetime.strptime ('2017 53',     '%Y %U')
datetime.datetime(2018, 1, 1, 0, 0)

>>> datetime.strptime ('2017 Mon 53', '%Y %a %U')
datetime.datetime(2018, 1, 1, 0, 0)
msg332456 - (view) Author: Serge Bon. (serge-bon) * Date: 2018-12-24 14:21
If %W and %U are allowed to be used without specifying the weekday by assuming it to be the first day of the week, then the same rule should apply to %V (ISO 8601 week) for consistency.
History
Date User Action Args
2022-04-11 14:58:48adminsetgithub: 74985
2019-05-22 13:43:18p-gansslesetkeywords: patch, patch, patch
nosy: + p-ganssle
2019-01-17 18:05:14serge-bonsetkeywords: + patch
stage: patch review
pull_requests: + pull_request11296
2019-01-17 18:05:01serge-bonsetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11295
2019-01-17 18:04:47serge-bonsetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11294
2018-12-24 14:21:13serge-bonsetmessages: + msg332456
2018-12-24 02:46:06serge-bonsetmessages: + msg332400
2018-12-24 02:30:10serge-bonsetnosy: + serge-bon
messages: + msg332399
2018-12-19 19:10:30xtreaksetnosy: + xtreak

versions: + Python 3.7, Python 3.8, - Python 3.3, Python 3.4, Python 3.5, Python 3.6
2017-07-29 02:02:56matheus.v.portelasetmessages: + msg299446
2017-07-25 17:35:16mrabarnettsetmessages: + msg299124
2017-07-25 15:39:13musically_utsetmessages: + msg299114
2017-07-22 08:02:31matheus.v.portelasetnosy: + matheus.v.portela
messages: + msg298840
2017-07-05 08:24:37musically_utsetnosy: + musically_ut
messages: + msg297714
2017-06-29 14:59:37mrabarnettsetnosy: + mrabarnett
messages: + msg297274
2017-06-29 09:03:11wintonsetmessages: + msg297266
2017-06-29 08:58:08wintoncreate