@anduril462: locale -k LC_TIME always lists Sunday first. If it didn't, how would you know which day was which in other languages and locales? 
Edited: The first_weekday and first_workday fields do provide the correct information, however. In most locales they're a relatively new addition, so you might wish to let the user override it just in case. (I'm getting senile, had to edit this post a few times. Hope you didn't see the garbage versions.)
The POSIX date-time manipulation functions like strftime have support for all three conventions:- %U is the week number, when first day of first week of the year is Sunday,
- %W is the week number, when first day of first week of the year is Monday
- %V is the ISO 8601 week number.
In ISO 8601 standard, the first week of the year is the one that has at least four days. Each week starts on a Monday.
Although US seems to mostly ignore the ISO 8601 standard, is is very practical: for one, the string dates sort correctly by default. See the ISO 8601 Wikipedia article for details and background. I strongly recommend defaulting to ISO 8601 formats, but allowing the user to override it if they really want to. (Most users only complain up to the first time they need to sort dates; then they're quietly grateful.)
On POSIX systems, using C, use setlocale(LC_TIME, ""); to set the locale, and then you can use the nl_langinfo() to find out the details. Here is an example program:
Code:
#include <stdio.h>
#include <locale.h>
#include <langinfo.h>
static inline int first_weekday(void)
{
const char *const s = nl_langinfo(_NL_TIME_FIRST_WEEKDAY);
if (s && *s >= 1 && *s <= 7)
return (int)*s;
/* Default to Sunday, 1. */
return 1;
}
static inline int first_workday(void)
{
const char *const s = nl_langinfo(_NL_TIME_FIRST_WORKDAY);
if (s && *s >= 1 && *s <= 7)
return (int)*s;
/* Default to Sunday, 1. */
return 1;
}
static const char *wkday(const int d)
{
switch (d) {
case 1: return nl_langinfo(ABDAY_1); /* Sun */
case 2: return nl_langinfo(ABDAY_2); /* Mon */
case 3: return nl_langinfo(ABDAY_3); /* Tue */
case 4: return nl_langinfo(ABDAY_4); /* Wed */
case 5: return nl_langinfo(ABDAY_5); /* Thu */
case 6: return nl_langinfo(ABDAY_6); /* Fri */
case 7: return nl_langinfo(ABDAY_7); /* Sat */
default: return "";
}
}
static const char *weekday(const int d)
{
switch (d) {
case 1: return nl_langinfo(DAY_1); /* Sunday */
case 2: return nl_langinfo(DAY_2); /* Monday */
case 3: return nl_langinfo(DAY_3); /* Tuesday */
case 4: return nl_langinfo(DAY_4); /* Wednesday */
case 5: return nl_langinfo(DAY_5); /* Thursday */
case 6: return nl_langinfo(DAY_6); /* Friday */
case 7: return nl_langinfo(DAY_7); /* Saturday */
default: return "";
}
}
int main(void)
{
setlocale(LC_TIME, "");
printf("%s\n", wkday(first_weekday()));
printf("%s\n", weekday(first_workday()));
return 0;
}
The program will output two lines: the abbreviated weekday name for the first day of the week on the first line, and the full weekday name for the first workday of the week on the second line.
The setlocale(LC_TIME,"") is needed at the start of the program to set up the locale database; using an empty string is just shorthand for "as set in the environment variables". Check the man pages for further functionality; you will need to run it for other locale features too before querying them. After thatis done, *(char *)nl_langinfo(_NL_TIME_FIRST_WEEKDAY); yields the first day of week (1 for Sunday, 2 for Monday, .., 7 for Saturday), and *(char *)nl_langinfo(_NL_TIME_FIRST_WORKDAY); the first workday of the week.
nl_langinfo() always returns a string. For _NL_TIME_FIRST_WORKDAY and _NL_TIME_FIRST_WEEKDAY, the first character of that string is the numeric value, i.e. the string will be either empty, or contain just \001 to \007.
These are pretty new features in locale data, so there have been errors in many locales. You might wish to let the user override it using a command-line switch.
Finally, some example results from running LC_TIME=locale ./program :
Code:
$ LC_TIME=C ./program -- POSIX/C locale
Sun
Monday
$ LC_TIME=fi_FI.utf8 ./program -- Finnish locale
ma
maanantai
$ LC_TIME=en_DK.utf8 ./program -- English in Denmark
Mon
Monday
$ LC_TIME=en_US.utf8 ./program -- US English
Sun
Monday
[/COLOR]
@anduril462: Thanks for your post. Without it, this post of mine would have been completely bogus.