patch: gnome-libs stable dateedit internationalisation



Hi...

This patch against stable gnome-libs fixes half of bug #5879 - namely it adds correct internationalisation of dates in the GnomeDateEdit widget. Dates pre-1900 are still broken (this would require incompatible changes in the interface and implementation, namely the dumping of time_t structures for GDate).

This does introduce a behaviour change - if a user types in an invalid date, such as "20/20/2000", the today's date will be selected, rather than 2000/12/20 (Y/M/D), which was the old behaviour (date values clamped to limits). This is because GDate's parsing doesn't try to deal with broken dates, leaving us with no way to make a realistic guess at what is meant.

Could people please test this patch so it could be applied against stable gnome-libs? 

=)
Peter
Index: gnome-dateedit.c
===================================================================
RCS file: /cvs/gnome/gnome-libs/libgnomeui/gnome-dateedit.c,v
retrieving revision 1.30.4.3
diff -u -r1.30.4.3 gnome-dateedit.c
--- gnome-dateedit.c	2000/09/10 21:35:50	1.30.4.3
+++ gnome-dateedit.c	2000/11/06 12:56:57
@@ -76,11 +76,14 @@
 {
 	char buffer [40];
 	gint year, month, day;
+	GDate *date;
 
 	gtk_calendar_get_date (calendar, &year, &month, &day);
 
-	/* FIXME: internationalize this - strftime()*/
-	g_snprintf (buffer, sizeof(buffer), "%d/%d/%d", month + 1, day, year);
+	date = g_date_new_dmy (day, month, year);
+	g_date_strftime (buffer, sizeof(buffer), "%x", date);
+	g_date_free (date);
+
 	gtk_entry_set_text (GTK_ENTRY (gde->date_entry), buffer);
 	gtk_signal_emit (GTK_OBJECT (gde), date_edit_signals [DATE_CHANGED]);
 }
@@ -175,26 +178,23 @@
 static void
 select_clicked (GtkWidget *widget, GnomeDateEdit *gde)
 {
-	struct tm mtm = {0};
 	GdkCursor *cursor;
+	GDate *date;
 
-        /* This code is pretty much just copied from gtk_date_edit_get_date */
-      	sscanf (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)), "%d/%d/%d",
-		&mtm.tm_mon, &mtm.tm_mday, &mtm.tm_year); /* FIXME: internationalize this - strptime()*/
+	date = g_date_new ();
 
-	mtm.tm_mon = CLAMP (mtm.tm_mon, 1, 12);
-	mtm.tm_mday = CLAMP (mtm.tm_mday, 1, 31);
+	g_date_set_parse (date, gtk_entry_get_text (GTK_ENTRY (gde->date_entry)));
 
-        mtm.tm_mon--;
+	if (!g_date_valid (date))
+	{
+		/* Shrug... Will today do? */
+		g_date_set_time (date, time (NULL));
+	}
 
-	/* Hope the user does not actually mean years early in the A.D. days...
-	 * This date widget will obviously not work for a history program :-)
-	 */
-	if (mtm.tm_year >= 1900)
-		mtm.tm_year -= 1900;
+	gtk_calendar_select_month (GTK_CALENDAR (gde->calendar), g_date_month (date) - 1, g_date_year (date));
+        gtk_calendar_select_day (GTK_CALENDAR (gde->calendar), g_date_day (date));
 
-	gtk_calendar_select_month (GTK_CALENDAR (gde->calendar), mtm.tm_mon, 1900 + mtm.tm_year);
-        gtk_calendar_select_day (GTK_CALENDAR (gde->calendar), mtm.tm_mday);
+	g_date_free (date);
 
         position_popup (gde);
        
@@ -399,10 +399,7 @@
 	mytm = localtime (&the_time);
 
 	/* Set the date */
-	g_snprintf (buffer, sizeof(buffer), "%d/%d/%d",
-		    mytm->tm_mon + 1,
-		    mytm->tm_mday,
-		    1900 + mytm->tm_year);
+	strftime(buffer, sizeof(buffer), "%x", mytm);
 	gtk_entry_set_text (GTK_ENTRY (gde->date_entry), buffer);
 
 	/* Set the time */
@@ -574,18 +571,30 @@
 {
 	struct tm tm = {0};
 	char *str, *flags = NULL;
+	GDate *date;
 
 	/* Assert, because we're just hosed if it's NULL */
 	g_assert(gde != NULL);
 	g_assert(GNOME_IS_DATE_EDIT(gde));
 	
-	sscanf (gtk_entry_get_text (GTK_ENTRY (gde->date_entry)), "%d/%d/%d",
-		&tm.tm_mon, &tm.tm_mday, &tm.tm_year); /* FIXME: internationalize this - strptime()*/
+	date = g_date_new ();
 
-	tm.tm_mon = CLAMP (tm.tm_mon, 1, 12);
-	tm.tm_mday = CLAMP (tm.tm_mday, 1, 31);
+	g_date_set_parse (date, gtk_entry_get_text (GTK_ENTRY (gde->date_entry)));
 
+	/* FIXME: Is this the correct behaviour for an invalid date? */
+	if (!g_date_valid (date))
+	{
+		/* Shrug. Will today do? */
+		g_date_set_time (date, time (NULL));
+	}
+
+	tm.tm_mon = g_date_month (date); 
+	tm.tm_mday = g_date_day (date);
+
 	tm.tm_mon--;
+	tm.tm_year = g_date_year (date);
+
+	g_date_free (date);
 
 	/* Hope the user does not actually mean years early in the A.D. days...
 	 * This date widget will obviously not work for a history program :-)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]