How to determine the first day of week

Today we got a report for bug #554256 in Hamster. The first day of week was reported as Sunday for some locales that certainly used Monday. I did some research and found that before Hamster relied on a simple check to determine the result:

  • Run locale first_weekday (by the way, we have to use os.popen here as these don’t seem to be accessible from Python)
  • If the result is 1, use Sunday, else use Monday (literally, it would treat 2 and 6 as Monday)

That seemed to work at least for some countries but was wrong which became apparent when more people started using Hamster (and to put myself in shame: I didn’t notice this before but it was also broken for me). It seems the correct version is:

  • Run locale first_weekday week-1stday
  • Parse week-1stday as date with %Y%m%d
  • Move the result first_weekday - 1 days forward
  • Check the weekday of the result

Rationale:

While locale manual pages are horribly outdated, all web searches point to week-1stday as the beginning of the first week of tracked Unix time. first_weekday is the 1-based offset of that week’s start day. It seems it’s only there so you can specify first_weekday=2 and first_workday=1 for the rare cases where working days span across two weeks.

Please do correct me if this is wrong as all of the above are assumptions based on glibc code and Google results. Hope the above helps someone in the future.

Update: vuntz was kind enough to point me to gtkcalendar.c, part of GTK+. It does the same thing described above so it seems the method is correct.

Leave a Reply

Your email address will not be published. Required fields are marked *

Creative Commons Attribution-NonCommercial-ShareAlike 2.5 Poland
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 Poland.