strptime ignores weeks! (under certain circumstances…)

I discovered something unexpected today: strptime from the Python time package ignores weeks in certain situations! This was unexpected.

For background, I’ve been doing some work lately with assigning user IDs to cohorts, then doing some data analytics “business intelligence” processing on the activities of members of each cohort. I have been assigning user IDs to week numbers because we are very early in the process. We don’t have much data. Ultimately we want cohort membership to be month based. We’ll shift to that when the time comes. For now, everything is based on weeks, e.g. ‘2015-24’.

The strptime problem surfaced when I attempted to convert the week string to time. My code looked something like this:

starting_time = strptime(week_string, '%Y-%U')
print starting_time

starting_time was *always* January 1st of the year. ‘2015-07’ > 1-1-2015. ‘2015-24’ > 1-1-2015. ‘2014-50’ > 1-1-2014. Convinced I was doing something wrong, I searched until I found note 6 on this page:

https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

It turns out the designers of strptime decided that the uncertainty of a year-week combination (which day of that week does one pick?) justified the additional requirement that the day of the week must also be included.

Advertisements