On December 31, 2018, site owners who looked at their weekly stats saw nothing for the current week. The data was there. It had been written into the table for the first week of 2018.
What happened
A daily job computes unique visitors for every site and copies the weekly totals into MySQL, where the stats pages read them. Each week has its own table, named after the year and the week number, so the week starting December 31 should have gone into a table ending in 2019_01. The script that built the table name did the equivalent of this (simplified):
YEAR=$(date -d "$DAY" +%Y)
WEEK=$(date -d "$DAY" +%V)
TABLE="weekly_uniques_${YEAR}_${WEEK}"
%V is the ISO 8601 week number. ISO weeks start on Monday, and week 1 of a year is the week that contains its first Thursday. January 3, 2019 was a Thursday, so week 1 of 2019 started on Monday, December 31, 2018. For that day, %V correctly returned 01, and %Y correctly returned 2018. Put together, they named the table for the first week of 2018:
$ date -d 2018-12-31 +%Y_%V
2018_01
$ date -d 2018-12-31 +%G_%V
2019_01
The year and the week number were each right. They just belonged to different calendars. The year that goes with an ISO week number is the ISO week-based year, which is %G, not %Y.
So the stats pages found an empty table for the current week, and last January’s weekly numbers were overwritten. A colleague noticed in the afternoon, and the fix was deployed about an hour later. We restored the overwritten table from a backup and replaced fewer than a thousand affected summary rows, so no data was lost in the end.
Why it had worked until then
For most of the year, %Y and %G are the same. They only differ for a few days around New Year, and only in some years: when December 29, 30 or 31 fall in week 1 of the next year, or when January 1, 2 or 3 still belong to the last week of the previous one. January 1, 2021, for example, will be in week 53 of 2020. A script like this one can run correctly for years and fail on a single day.
The same bug in other languages
Most date libraries make the distinction, and most make it easy to miss:
- strftime (C, shell, PHP’s
strftime, Python’sstrftime):%Gand%Vgo together;%Yand%Uor%Wgo together. - Python:
date.isocalendar()returns the ISO year, week and weekday as one tuple, so you can’t mix them up by accident. - PHP
date():ois the ISO year to use withW;Yis the calendar year. - Java
DateTimeFormatter:Yis the week-based year andyis the calendar year. This is the same bug in reverse:YYYY-MM-ddlooks right and prints the wrong year for those same few days.
What we changed
The fix removed the hand-built date math and took the year and week from the same source. Beyond this one script:
- Never combine a week number with a calendar year. If you use ISO weeks, use the ISO year with them, and get both from the same call.
- Test date code with the days that break it: December 29 to January 3, across a few different years, including one that has a week 53.
- If something is keyed by week, consider keying it by the date the week starts.
2018-12-31is unambiguous in a way that2019_01isn’t.
Leave a Reply