GNOME Bugzilla – Bug 358116
gtk_calendar_select_month does not update the month day when needed
Last modified: 2018-02-10 03:45:37 UTC
Please describe the problem: gtk_calendar_select_month can be used to move month selection to a month with a lower number of days (for example from January to February), but it does not check if currently selected day is valid for the newly selected month. This can result in an invalid date (for example 31th of February). Steps to reproduce: #include <gtk/gtk.h> int main () { GtkCalendar *calendar; guint year = 2006; guint month = 0; guint day = 31; gtk_init (0, NULL); calendar = GTK_CALENDAR (gtk_calendar_new ()); gtk_calendar_select_month (calendar, month, year); gtk_calendar_select_day (calendar, day); gtk_calendar_get_date (calendar, &year, &month, &day); g_print ("\nYear=%d, month=%d, day=%d\n", year, month, day); year = 2006; month = 1; gtk_calendar_select_month (calendar, month, year); gtk_calendar_get_date (calendar, &year, &month, &day); g_print ("\nYear=%d, month=%d, day=%d\n", year, month, day); gtk_widget_destroy (GTK_WIDGET (calendar)); return 0; } Actual results: Year=2006, month=0, day=31 Year=2008, month=1, day=31 -> Invalid date Expected results: Year=2006, month=0, day=31 Year=2006, month=1, day=28 Does this happen every time? Yes Other information:
Created attachment 73550 [details] [review] When the number of days of the newly selected month is lower than selected_day, sets selected_day to the number of days of the month
Created attachment 73551 [details] [review] Same patch, but adding notification on the day property when needed Fixes the previous patch by adding g_object_notify for "day" property when needed.
Not sure that this is really the expected behaviour. It means for instance that switching from January to February and back can change the selected day.
Mmm... yes, you are right. However, if I would have to select a behavior I'd prefer the calendar to be consistent (never provide an invalid date) than maintaining the day between to month selections. In order to solve the problem you point out I guess it would be possible to store the day number. For example, if I have 31th Jan and switch the month to Feb, I can detect that I have to fix the day and save the old valid day number (31). Then, when a selection of a month with 31 days happens, I can set the day number back to 31. A complete example would be like this: 31th Jan -> 28th Feb -> 28/30th Apr -> 31th Jan (1) (2) (3) (1) saved=31, fixed=28 (2) Month has more days than last fixed month, so try to set day number back again. However this month has 30 days, not 31, should we set day number to 30 or leave it be 28? (3) Month has more days than last fixed month and the saved number of days fits in it, so we can set day number back.
We're moving to gitlab! As part of this move, we are closing bugs that haven't seen activity in more than 5 years. If this issue is still imporant to you and still relevant with GTK+ 3.22 or master, please consider creating a gitlab issue for it.