/* Test first weekday settings correctness. */
/* (c) Petr Baudis <pasky@ucw.cz> 2009 */
/* (c) Samuel Thibault <samuel.thibault@ens-lyon.org> 2009 */
/* Public domain. */

#include <langinfo.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int
main() {
	char *locale = setlocale(LC_ALL, "");

	int first_weekday = nl_langinfo (_NL_TIME_FIRST_WEEKDAY)[0];
	long week_1stday_l = (long) nl_langinfo (_NL_TIME_WEEK_1STDAY);
	int week_1stday;
	if (week_1stday_l == 19971130) week_1stday = 0; /* Sun */
	else if (week_1stday_l == 19971201) week_1stday = 1; /* Mon */
	else return EXIT_FAILURE;

	/* week_1stday specifies which day of week is the days[]
	 * (DAY_1...) array based on; first_weekday is 1-based
	 * offset of the first day of week in the days[] array. */

	/* First day according to the DAY_1 langinfo. */
	char *days_first = nl_langinfo (DAY_1 + (first_weekday - 1) % 7);

	/* First day according to strftime(). */
	char strftime_first[64];
	/* 1970-01-01 was Thu, +3 = Sun. */
	time_t time_first = 86400 * (3 + week_1stday + (first_weekday - 1));
	strftime (strftime_first, sizeof (strftime_first), "%A", gmtime (&time_first));
	const char *english_days [] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

	printf ("[%s] week_1stday %d, first_weekday %d, english[first] %s, days[first] %s, strftime[first] %s\n",
		locale, week_1stday, first_weekday,
		english_days [(week_1stday + first_weekday - 1) % 7],
		days_first, strftime_first);


	return EXIT_SUCCESS;
}
