GNOME Bugzilla – Bug 314473
gtk calendar not calculating first week day correctly
Last modified: 2011-02-04 16:19:55 UTC
Please describe the problem: gtkcalendar.c contains: #ifdef HAVE__NL_TIME_FIRST_WEEKDAY week_start = nl_langinfo (_NL_TIME_FIRST_WEEKDAY); priv->week_start = *((unsigned char *) week_start) % 7 - 1; #else week_start is 7 priv->week_start calculates to -1. Also note, that against glibc 2.3.5, the days appear to go from 1 (monday) to 7 (sunday). Steps to reproduce: Actual results: Expected results: Does this happen every time? Other information:
The test program: #include <langinfo.h> #include <stdio.h> int main (int argc, char **argv) { char *week_start; week_start = nl_langinfo (_NL_TIME_FIRST_WEEKDAY); printf ("Week start day %d\n", *week_start); printf ("Gtk calculated week start day %d\n", *((unsigned char *) week_start) % 7 - 1); }
jpr@bishop:~> ./langinfo Week start day 7 Gtk calculated week start day -1 jpr@bishop:~> locale -k -c LC_TIME LC_TIME abday="Sun;Mon;Tue;Wed;Thu;Fri;Sat" day="Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday" abmon="Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec" mon="January;February;March;April;May;June;July;August;September;October;November;December" am_pm="AM;PM" d_t_fmt="%a %d %b %Y %r %Z" d_fmt="%m/%d/%Y" t_fmt="%r" t_fmt_ampm="%I:%M:%S %p" era= era_year="" era_d_fmt="" alt_digits= era_d_t_fmt="" era_t_fmt="" time-era-num-entries=0 time-era-entries="S" week-ndays=7 week-1stday=19971201 week-1stweek=4 first_weekday=1 first_workday=1 cal_direction=1 timezone="" date_fmt="%a %b %e %H:%M:%S %Z %Y" time-codeset="UTF-8"
So instead of : *((unsigned char *) week_start) % 7 - 1 If the week days are 1 (sunday) to 7 (saturday): (*((unsigned char *) week_start) - 1 ) % 7 And if they are 1 (monday) to 7 (sunday): *((unsigned char *) week_start) % 7 Or something like that - not sure why we are %7 in the first case, would the return value really overflow 1-7?
2005-08-26 Matthias Clasen <mclasen@redhat.com> * gtk/gtkcalendar.c (gtk_calendar_init): Fix the calculation of week_start. (#314473, JP Rosevaar)
Created attachment 51928 [details] [review] gtk+-first_weekday.patch Fix in gtk+-2.8.3 does not work properly with latest SuSE 10.0 beta glibc. This patch works for me with latest SuSE glibc and C en_US.UTF-8 and cs_CZ.UTF-8 locales. I guess that it maybe will not work properly in older glibc versions.
Related: https://bugzilla.novell.com/show_bug.cgi?id=104417
Please reopen bugs when adding information
Hmm, patch looks right according to my study of iso 14652. I guess we can assume that _NL_TIME_WEEK_1STDAY is defined when _NL_TIME_FIRST_WEEKDAY is.
Patch needs to be tested with older glibc versions. You can make added code optional with HAVE__NL_TIME_WEEK_1STDAY. Default value of week_startsub defined above is 1, i. e. original value, which was working with old glibc. I do not have permission to reopen bugs not reported by me or assigned to me.
When I asked Uli Drepper, he said that the frirst_weekday handling in glibc has not changed in a long time, so if it works on current glibc, it should work on suse 9.3 as well.
Yes, I tested "locale -k -c LC_TIME" in 9.3 and it returns the same output as in 10.0. But old code in gtk+-2.6.x was working OK in SuSE Linux 9.3, but new gtk+-2.8.3 code in 10.0 does not. I don't know, where the change was done.
Could be that the localedata changed...
2005-09-09 Matthias Clasen <mclasen@redhat.com> * gtk/gtkcalendar.c (gtk_calendar_init): first_weekday is relative to week_1stday, not to Sunday. Gotta love the ISO 14652 guys... (#314473, Stanislav Brabec)
I'm not sure if GTK is to blame but since GTK 2.8.4 some locales are not correct. An example: http://bugzilla.ubuntu.com/show_bug.cgi?id=16658 for the sv: week-1stday=19971130 first_weekday=2 But the first week day should be monday ... is there a place explaining exactly the difference between week-1stday and first_weekday? Why is there 2 different first days?
It is probably described in ISO 14652 (I don't have access to it). More can be found in: http://lists.debian.org/debian-i18n/2005/07/msg00039.html It would be good to know, whether there are any locale data, which cause bad result with this patch.
the example from comment #14 is with GTK 2.8.4 "de" locale also has the issue: week-1stday=19971201 first_weekday=1 So GTK says it starts on sunday where it should start on monday
There must be a bug somewhere or another magic. For example LANG=de_DE.UTF-8 locale -k -c LC_TIME | grep '\(day\)' LANG=en_US.UTF-8 locale -k -c LC_TIME | grep '\(day\)' returns exactly the same (with exception of translated names). How one can guess, that de_DE should start by Monday and en_US by Sunday? CS: Should be Monday week-1stday=19971130 (week_startsub = 0) first_weekday=1 DE: Should be Monday week-1stday=19971201 (week_startsub = 1) first_weekday=1 US: Should be Sunday week-1stday=19971201 (week_startsub = 1) first_weekday=1 C: Should be Sunday? week-1stday=19971130 (week_startsub = 0) first_weekday=7
so after another close reading of tr 14562, the correct formula is (*((unsigned char *) week_start) - 1 + week_startsub) % 7; but that is not entirely satisfactory for the current locale data either; it gives: CS: Sunday DE: Monday US: Monday SV: Monday C: Saturday
*** Bug 317723 has been marked as a duplicate of this bug. ***
Assuming this is fixed now, except for bugs in locale data.
I'm not sure this is completely correct now. For sv_SE monday is correctly displayed as the first day, but it believes today (2005-10-03) is a Tuesday when it in fact is Monday. The same thing happens with en_US, which also has Monday as the first day. But I figure this is also incorrect since Sunday is the first day over there? en_GB has Sunday as the first day and also calculates the correct weekday for a date. C goes really funky and starts the week with Saturday and thinks it's Sunday today. (Cannot reopen because I'm not owner.)
I'll stop hacking and become a farmer :-(
reopening
Found the problem (besides from the locale bugs). The computation of the first week day is done _after_ it is used in various places in the calender widget. So some reorganisation of gtk_calendar_init() is desperatly needed.
At least it has nothing to do with the nl_langinfo() computations...
2005-10-03 Matthias Clasen <mclasen@redhat.com> * gtk/gtkcalendar.c (gtk_calendar_init): Call calendar_compute_days() after setting priv->week_start.
Good boy. You can drop the plow for now. ;) (I.e. patch works fine.)
*** Bug 317946 has been marked as a duplicate of this bug. ***
Created attachment 59937 [details] [review] gtk+-first_weekday-new.patch Patch that handles weeks starting by other days than Sunday or Monday
Which locale uses a week origin different from 19971130 and 19971201 ?
Can't reopen neither :-( gtk+-first_weekday.patch handles only Monday and Sunday, but there are locales starting with Saturday (grep 'calendar:week_start:6' po/*.po). This patch also handles week-1stday values which aren't in range 19971130..19971206, I don't know if it's really necessary, drop that part if you don't like it. What's the "right way" to call strptime() in Gtk+ btw? There is g_date_strftime(), but no counterpart.
(In reply to comment #30) > Which locale uses a week origin different from 19971130 > and 19971201 ? None atm :-) But I have put together a patch to glibc's locales based on the translations of calendar:week_start:0, which also contains locales starting with Saturday (19971206). See https://bugzilla.novell.com/show_bug.cgi?id=104417#c20 Maybe something like that will appear in glibc.
Related glibc localedata bug: http://sources.redhat.com/bugzilla/show_bug.cgi?id=2388 To Matthias Clasen (comment #30): Many locales use first_weekday=2 (may be a bug), locales for Syrian Arab Republic and Iran use 19971206. Patches from upper mentioned bug need further review from native people of many countries.