Definitely an interesting one. I’ll have to run some of my own tests to see whether this is expected or not. Just to understand how this is happening as well, could you advise the MongoDB server version you’re running?
I had a peek at this as well, looking at the source code for the server, it seems to call outputDateWithFormat in date_time_support.h and the two options for days are:
case 'w': // Day of week
if (auto status = insertPadded(os, dayOfWeek(date), 1); status != Status::OK())
return status;
break;
case 'u': // Iso day of week
if (auto status = insertPadded(os, isoDayOfWeek(date), 1);
status != Status::OK())
return status;
break;
I.e.
|%u|ISO 8601 weekday as number with Monday as 1 (1-7)|4|
|%w|Weekday as a decimal number with Sunday as 0 (0-6)|4|
Checking the code for %w it calls dayOfWeek, the tooltip for this function returns 1-7 which is at odds of the documentation.
dayOfWeek is defined as:
int TimeZone::dayOfWeek(Date_t date) const {
auto time = getTimelibTime(date);
// timelib_day_of_week() returns a number in the range [0,6], we want [1,7], so add one.
return timelib_day_of_week(time->y, time->m, time->d) + 1;
}
Which seems to indicate that it shoudl be 0-6 but it’s being transformed to 1-7 by adding 1 to the output.
Of course I’m not a C++ expert (or competent!) But if the documentation and other definitions are correct it seems that the server code could be misbehaving…
/Edit I could also not see a unit test for the server for this scenario, but that could just be me missing it.
It didn’t occur to me that the server code was available for inspection:-)
I also don’t know much C/C++, but that comment makes it clear to me. However, I would think most devs would be quite happy to start counting at 0. The standard for our org is Sunday=0, and so I’ll need to adjust every single record to accomplish this project because I’m relying on pulling day of the week info from a timestamp.