3

Based on the locale I need to find what is the first day of the week (Sunday/Monday) In JAVA I would do:

Calendar FR_cal = Calendar.getInstance(Locale.FRANCE);
Calendar CA_cal = Calendar.getInstance(Locale.CANADA);

DateFormatSymbols dfs = new DateFormatSymbols();
String weekdays[] = dfs.getWeekdays();

System.out.println(weekdays[JO_cal.getFirstDayOfWeek()]);
System.out.println(weekdays[FR_cal.getFirstDayOfWeek()]);

How can I do it in python ?

Eyal
  • 137
  • 3
  • 9
  • I saw this http://stackoverflow.com/questions/727471/how-do-i-get-the-first-day-of-the-week-for-the-current-locale-php-l8n so maybe I can implement it myself – Eyal Nov 25 '10 at 07:30

4 Answers4

7

I could only figure out how to do this with the Babel library. It's available through easy_install.

>>> import babel
>>> locale = babel.Locale('en', 'US')
>>> locale.first_week_day
6
>>> locale.days['format']['wide'][locale.first_week_day]
u'Sunday'

turns out that the following doesn't work as Nate was kind enough to point out. If anybody knows why, please post and answer showing how to do it right. This should be doable with the standard library.

If you just want the number of the day, then you can use calendar.LocalTextCalendar.

>>> import calendar
>>> c = calendar.LocaleTextCalendar(locale='de_DE') # locale=('en_US', 'UTF8') fails too.
>>> c.firstweekday
0

There is also the iterweekdays method.

>>> list(c.iterweekdays())
[0, 1, 2, 3, 4, 5, 6]
aaronasterling
  • 68,820
  • 20
  • 127
  • 125
  • 1
    Unfortunately this doesn’t work either. With `locale='en_US'` it returns `0` when it should return `6`. – Nate Nov 24 '10 at 10:48
  • That's confusing. 0=Monday, 6=Sunday? Can't think of anywhere else I've seen that. Doesn't make much sense either! Fortunately you should always use the constants `calendar.SUNDAY`, etc. – Chris Morgan Nov 24 '10 at 11:55
  • @Chris Morgan. It makes sense if the ISO standard is for Monday to be the first day of the week (It is). That would make 0=Monday and 6=Sunday. This is the way it works for the constants you mention as well, I just checked. The US is idiosyncratic in having Sunday be the first day of the week. Also, using the constants is no good here unless I'm missing something. We're trying to figure out which one is the first day. – aaronasterling Nov 24 '10 at 12:00
  • Being broken sure doesn't help. The US isn't alone in having Sunday as the first day of the week - it is in England, Australia, and traditionally always has been. I believe it's only parts of Europe that have decided they want to be different (I presume because the Catholic church declared Sunday as the Sabbath) – Chris Morgan Nov 24 '10 at 12:04
  • @Chris Morgan, Well, maybe I'm wrong on monday being standard but, as I mentioned in my above comment(in an edit), the constants that you mention order it the same way. `calendar.MONDAY==0 and calendar.SUNDAY==6` – aaronasterling Nov 24 '10 at 12:06
  • 3
    The documentation for the calendar module only says that locale calendars will offer the local names: you have to set the first day of the week yourself. Not technically broken, but rather misleading. – Thomas K Nov 24 '10 at 13:37
  • The following Wikipedia article has good info on the origins and current practices regarding the first day of the week. – martineau Nov 24 '10 at 19:32
  • 1
    Surprisingly to me, the ANSI C locale implementation and POSIX standard don't define anything to define this -- and since that is what the `locale` module is based on, neither does Python AFAIK. – martineau Nov 24 '10 at 19:45
1

This functionality seems to be missing from the python standard library up to and including (at least) Python 3.1.2.


Some clues:

Since information about this is usually stored along with the systems l10n data (in the GNU world that would be the locale def's likely in /usr/share/i18n/locales/) my first reaction was to use something like locale.nl_langinfo(), but unfortunately there is nothing like locale.FIRST_WEEKDAY in the locale module :-(

Aside from the Babel lib mention by aaronasterling, I found this example of a solution for the named problem used inside a GNOME applet. Also worth noting is the accompanying blog post.

conny
  • 9,973
  • 6
  • 38
  • 47
  • Sadly, yes the `popen("locale...` code will only work on a GNU (Linux)-like system. It probably won't work on a BSD derivative, and not at all on Windows. Like I wrote, too bad it isn't already in the Python Standard Library :-/ – conny Nov 24 '10 at 12:56
0

Calendars in many European countries, in particular, now follow the ISO decision by starting the week on Monday. Airline timetables also number the days from Monday as 1, Tuesday as 2, Wednesday as 3, etc.

The above states that Monday should always be the first day of the week.

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • How would you determine this given only the locale? Otherwise you have to build a table listing every possible locale and which is its first day of the week. – Nate Nov 24 '10 at 10:39
-1

Take a look at the calendar module. Might come handy for this!

ygbr
  • 752
  • 9
  • 14