Avoid forcing extra newlines when using template files. (#171005)
[glib.git] / glib / gdate.c
blob4e204f1771830e12de6288b69f5474e0be045784
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe
31 #include "config.h"
33 #define DEBUG_MSG(x) /* */
34 #ifdef G_ENABLE_DEBUG
35 /* #define DEBUG_MSG(args) g_message args ; */
36 #endif
38 #include "glib.h"
40 #include <time.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <locale.h>
45 #include "galias.h"
47 GDate*
48 g_date_new (void)
50 GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
52 return d;
55 GDate*
56 g_date_new_dmy (GDateDay day, GDateMonth m, GDateYear y)
58 GDate *d;
59 g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
61 d = g_new (GDate, 1);
63 d->julian = FALSE;
64 d->dmy = TRUE;
66 d->month = m;
67 d->day = day;
68 d->year = y;
70 g_assert (g_date_valid (d));
72 return d;
75 GDate*
76 g_date_new_julian (guint32 j)
78 GDate *d;
79 g_return_val_if_fail (g_date_valid_julian (j), NULL);
81 d = g_new (GDate, 1);
83 d->julian = TRUE;
84 d->dmy = FALSE;
86 d->julian_days = j;
88 g_assert (g_date_valid (d));
90 return d;
93 void
94 g_date_free (GDate *d)
96 g_return_if_fail (d != NULL);
98 g_free (d);
101 gboolean
102 g_date_valid (const GDate *d)
104 g_return_val_if_fail (d != NULL, FALSE);
106 return (d->julian || d->dmy);
109 static const guint8 days_in_months[2][13] =
110 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
111 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
112 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
115 static const guint16 days_in_year[2][14] =
116 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
117 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
118 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
121 gboolean
122 g_date_valid_month (GDateMonth m)
124 return ( (m > G_DATE_BAD_MONTH) && (m < 13) );
127 gboolean
128 g_date_valid_year (GDateYear y)
130 return ( y > G_DATE_BAD_YEAR );
133 gboolean
134 g_date_valid_day (GDateDay d)
136 return ( (d > G_DATE_BAD_DAY) && (d < 32) );
139 gboolean
140 g_date_valid_weekday (GDateWeekday w)
142 return ( (w > G_DATE_BAD_WEEKDAY) && (w < 8) );
145 gboolean
146 g_date_valid_julian (guint32 j)
148 return (j > G_DATE_BAD_JULIAN);
151 gboolean
152 g_date_valid_dmy (GDateDay d,
153 GDateMonth m,
154 GDateYear y)
156 return ( (m > G_DATE_BAD_MONTH) &&
157 (m < 13) &&
158 (d > G_DATE_BAD_DAY) &&
159 (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
160 (d <= (g_date_is_leap_year (y) ?
161 days_in_months[1][m] : days_in_months[0][m])) );
165 /* "Julian days" just means an absolute number of days, where Day 1 ==
166 * Jan 1, Year 1
168 static void
169 g_date_update_julian (const GDate *const_d)
171 GDate *d = (GDate *) const_d;
172 GDateYear year;
173 gint index;
175 g_return_if_fail (d != NULL);
176 g_return_if_fail (d->dmy);
177 g_return_if_fail (!d->julian);
178 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
180 /* What we actually do is: multiply years * 365 days in the year,
181 * add the number of years divided by 4, subtract the number of
182 * years divided by 100 and add the number of years divided by 400,
183 * which accounts for leap year stuff. Code from Steffen Beyer's
184 * DateCalc.
187 year = d->year - 1; /* we know d->year > 0 since it's valid */
189 d->julian_days = year * 365U;
190 d->julian_days += (year >>= 2); /* divide by 4 and add */
191 d->julian_days -= (year /= 25); /* divides original # years by 100 */
192 d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
194 index = g_date_is_leap_year (d->year) ? 1 : 0;
196 d->julian_days += days_in_year[index][d->month] + d->day;
198 g_return_if_fail (g_date_valid_julian (d->julian_days));
200 d->julian = TRUE;
203 static void
204 g_date_update_dmy (const GDate *const_d)
206 GDate *d = (GDate *) const_d;
207 GDateYear y;
208 GDateMonth m;
209 GDateDay day;
211 guint32 A, B, C, D, E, M;
213 g_return_if_fail (d != NULL);
214 g_return_if_fail (d->julian);
215 g_return_if_fail (!d->dmy);
216 g_return_if_fail (g_date_valid_julian (d->julian_days));
218 /* Formula taken from the Calendar FAQ; the formula was for the
219 * Julian Period which starts on 1 January 4713 BC, so we add
220 * 1,721,425 to the number of days before doing the formula.
222 * I'm sure this can be simplified for our 1 January 1 AD period
223 * start, but I can't figure out how to unpack the formula.
226 A = d->julian_days + 1721425 + 32045;
227 B = ( 4 *(A + 36524) )/ 146097 - 1;
228 C = A - (146097 * B)/4;
229 D = ( 4 * (C + 365) ) / 1461 - 1;
230 E = C - ((1461*D) / 4);
231 M = (5 * (E - 1) + 2)/153;
233 m = M + 3 - (12*(M/10));
234 day = E - (153*M + 2)/5;
235 y = 100 * B + D - 4800 + (M/10);
237 #ifdef G_ENABLE_DEBUG
238 if (!g_date_valid_dmy (day, m, y))
240 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
241 d->julian_days, day, m, y);
243 #endif
245 d->month = m;
246 d->day = day;
247 d->year = y;
249 d->dmy = TRUE;
252 GDateWeekday
253 g_date_get_weekday (const GDate *d)
255 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
257 if (!d->julian)
259 g_date_update_julian (d);
261 g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
263 return ((d->julian_days - 1) % 7) + 1;
266 GDateMonth
267 g_date_get_month (const GDate *d)
269 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
271 if (!d->dmy)
273 g_date_update_dmy (d);
275 g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
277 return d->month;
280 GDateYear
281 g_date_get_year (const GDate *d)
283 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
285 if (!d->dmy)
287 g_date_update_dmy (d);
289 g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
291 return d->year;
294 GDateDay
295 g_date_get_day (const GDate *d)
297 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
299 if (!d->dmy)
301 g_date_update_dmy (d);
303 g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
305 return d->day;
308 guint32
309 g_date_get_julian (const GDate *d)
311 g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
313 if (!d->julian)
315 g_date_update_julian (d);
317 g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
319 return d->julian_days;
322 guint
323 g_date_get_day_of_year (const GDate *d)
325 gint index;
327 g_return_val_if_fail (g_date_valid (d), 0);
329 if (!d->dmy)
331 g_date_update_dmy (d);
333 g_return_val_if_fail (d->dmy, 0);
335 index = g_date_is_leap_year (d->year) ? 1 : 0;
337 return (days_in_year[index][d->month] + d->day);
340 guint
341 g_date_get_monday_week_of_year (const GDate *d)
343 GDateWeekday wd;
344 guint day;
345 GDate first;
347 g_return_val_if_fail (g_date_valid (d), 0);
349 if (!d->dmy)
351 g_date_update_dmy (d);
353 g_return_val_if_fail (d->dmy, 0);
355 g_date_clear (&first, 1);
357 g_date_set_dmy (&first, 1, 1, d->year);
359 wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
360 day = g_date_get_day_of_year (d) - 1;
362 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
365 guint
366 g_date_get_sunday_week_of_year (const GDate *d)
368 GDateWeekday wd;
369 guint day;
370 GDate first;
372 g_return_val_if_fail (g_date_valid (d), 0);
374 if (!d->dmy)
376 g_date_update_dmy (d);
378 g_return_val_if_fail (d->dmy, 0);
380 g_date_clear (&first, 1);
382 g_date_set_dmy (&first, 1, 1, d->year);
384 wd = g_date_get_weekday (&first);
385 if (wd == 7) wd = 0; /* make Sunday day 0 */
386 day = g_date_get_day_of_year (d) - 1;
388 return ((day + wd)/7U + (wd == 0 ? 1 : 0));
392 * g_date_get_iso8601_week_of_year:
393 * @date: a valid #GDate
395 * Returns the week of the year, where weeks are interpreted according
396 * to ISO 8601.
398 * Returns: ISO 8601 week number of the year.
400 * Since: 2.6
402 guint
403 g_date_get_iso8601_week_of_year (const GDate *d)
405 guint j, d4, L, d1, w;
407 g_return_val_if_fail (g_date_valid (d), 0);
409 if (!d->julian)
410 g_date_update_julian (d);
411 g_return_val_if_fail (d->julian, 0);
413 /* Formula taken from the Calendar FAQ; the formula was for the
414 * Julian Period which starts on 1 January 4713 BC, so we add
415 * 1,721,425 to the number of days before doing the formula.
417 j = d->julian + 1721425;
418 d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
419 L = d4 / 1460;
420 d1 = ((d4 - L) % 365) + L;
421 w = d1 / 7 + 1;
423 return w;
426 gint
427 g_date_days_between (const GDate *d1,
428 const GDate *d2)
430 g_return_val_if_fail (g_date_valid (d1), 0);
431 g_return_val_if_fail (g_date_valid (d2), 0);
433 return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
436 void
437 g_date_clear (GDate *d, guint ndates)
439 g_return_if_fail (d != NULL);
440 g_return_if_fail (ndates != 0);
442 memset (d, 0x0, ndates*sizeof (GDate));
445 G_LOCK_DEFINE_STATIC (g_date_global);
447 /* These are for the parser, output to the user should use *
448 * g_date_strftime () - this creates more never-freed memory to annoy
449 * all those memory debugger users. :-)
452 static gchar *long_month_names[13] =
454 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
457 static gchar *short_month_names[13] =
459 "Error", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
462 /* This tells us if we need to update the parse info */
463 static gchar *current_locale = NULL;
465 /* order of these in the current locale */
466 static GDateDMY dmy_order[3] =
468 G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
471 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
472 * 29 and below are counted as in the year 2000, numbers 30 and above
473 * are counted as in the year 1900.
476 static GDateYear twodigit_start_year = 1930;
478 /* It is impossible to enter a year between 1 AD and 99 AD with this
479 * in effect.
481 static gboolean using_twodigit_years = FALSE;
483 /* Adjustment of locale era to AD, non-zero means using locale era
485 static gint locale_era_adjust = 0;
487 struct _GDateParseTokens {
488 gint num_ints;
489 gint n[3];
490 guint month;
493 typedef struct _GDateParseTokens GDateParseTokens;
495 #define NUM_LEN 10
497 /* HOLDS: g_date_global_lock */
498 static void
499 g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
501 gchar num[4][NUM_LEN+1];
502 gint i;
503 const guchar *s;
505 /* We count 4, but store 3; so we can give an error
506 * if there are 4.
508 num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
510 s = (const guchar *) str;
511 pt->num_ints = 0;
512 while (*s && pt->num_ints < 4)
515 i = 0;
516 while (*s && g_ascii_isdigit (*s) && i <= NUM_LEN)
518 num[pt->num_ints][i] = *s;
519 ++s;
520 ++i;
523 if (i > 0)
525 num[pt->num_ints][i] = '\0';
526 ++(pt->num_ints);
529 if (*s == '\0') break;
531 ++s;
534 pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
535 pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
536 pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
538 pt->month = G_DATE_BAD_MONTH;
540 if (pt->num_ints < 3)
542 gchar *casefold;
543 gchar *normalized;
545 casefold = g_utf8_casefold (str, -1);
546 normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
547 g_free (casefold);
549 i = 1;
550 while (i < 13)
552 if (long_month_names[i] != NULL)
554 const gchar *found = strstr (normalized, long_month_names[i]);
556 if (found != NULL)
558 pt->month = i;
559 break;
563 if (short_month_names[i] != NULL)
565 const gchar *found = strstr (normalized, short_month_names[i]);
567 if (found != NULL)
569 pt->month = i;
570 break;
574 ++i;
577 g_free (normalized);
581 /* HOLDS: g_date_global_lock */
582 static void
583 g_date_prepare_to_parse (const gchar *str, GDateParseTokens *pt)
585 const gchar *locale = setlocale (LC_TIME, NULL);
586 gboolean recompute_localeinfo = FALSE;
587 GDate d;
589 g_return_if_fail (locale != NULL); /* should not happen */
591 g_date_clear (&d, 1); /* clear for scratch use */
593 if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
595 recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
598 if (recompute_localeinfo)
600 int i = 1;
601 GDateParseTokens testpt;
602 gchar buf[128];
604 g_free (current_locale); /* still works if current_locale == NULL */
606 current_locale = g_strdup (locale);
608 while (i < 13)
610 gchar *casefold;
612 g_date_set_dmy (&d, 1, i, 1);
614 g_return_if_fail (g_date_valid (&d));
616 g_date_strftime (buf, 127, "%b", &d);
618 casefold = g_utf8_casefold (buf, -1);
619 g_free (short_month_names[i]);
620 short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
621 g_free (casefold);
623 g_date_strftime (buf, 127, "%B", &d);
624 casefold = g_utf8_casefold (buf, -1);
625 g_free (long_month_names[i]);
626 long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
627 g_free (casefold);
629 ++i;
632 /* Determine DMY order */
634 /* had to pick a random day - don't change this, some strftimes
635 * are broken on some days, and this one is good so far. */
636 g_date_set_dmy (&d, 4, 7, 1976);
638 g_date_strftime (buf, 127, "%x", &d);
640 g_date_fill_parse_tokens (buf, &testpt);
642 i = 0;
643 while (i < testpt.num_ints)
645 switch (testpt.n[i])
647 case 7:
648 dmy_order[i] = G_DATE_MONTH;
649 break;
650 case 4:
651 dmy_order[i] = G_DATE_DAY;
652 break;
653 case 76:
654 using_twodigit_years = TRUE; /* FALL THRU */
655 case 1976:
656 dmy_order[i] = G_DATE_YEAR;
657 break;
658 default:
659 /* assume locale era */
660 locale_era_adjust = 1976 - testpt.n[i];
661 dmy_order[i] = G_DATE_YEAR;
662 break;
664 ++i;
667 #ifdef G_ENABLE_DEBUG
668 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
669 i = 1;
670 while (i < 13)
672 DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
673 ++i;
675 if (using_twodigit_years)
676 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
678 gchar *strings[3];
679 i = 0;
680 while (i < 3)
682 switch (dmy_order[i])
684 case G_DATE_MONTH:
685 strings[i] = "Month";
686 break;
687 case G_DATE_YEAR:
688 strings[i] = "Year";
689 break;
690 case G_DATE_DAY:
691 strings[i] = "Day";
692 break;
693 default:
694 strings[i] = NULL;
695 break;
697 ++i;
699 DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
700 DEBUG_MSG (("**Sample date in this locale: `%s'", buf));
702 #endif
705 g_date_fill_parse_tokens (str, pt);
708 void
709 g_date_set_parse (GDate *d,
710 const gchar *str)
712 GDateParseTokens pt;
713 guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
715 g_return_if_fail (d != NULL);
717 /* set invalid */
718 g_date_clear (d, 1);
720 G_LOCK (g_date_global);
722 g_date_prepare_to_parse (str, &pt);
724 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
725 pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
728 if (pt.num_ints == 4)
730 G_UNLOCK (g_date_global);
731 return; /* presumably a typo; bail out. */
734 if (pt.num_ints > 1)
736 int i = 0;
737 int j = 0;
739 g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
741 while (i < pt.num_ints && j < 3)
743 switch (dmy_order[j])
745 case G_DATE_MONTH:
747 if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
749 m = pt.month;
750 ++j; /* skip months, but don't skip this number */
751 continue;
753 else
754 m = pt.n[i];
756 break;
757 case G_DATE_DAY:
759 if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
761 day = 1;
762 ++j; /* skip days, since we may have month/year */
763 continue;
765 day = pt.n[i];
767 break;
768 case G_DATE_YEAR:
770 y = pt.n[i];
772 if (locale_era_adjust != 0)
774 y += locale_era_adjust;
776 else if (using_twodigit_years && y < 100)
778 guint two = twodigit_start_year % 100;
779 guint century = (twodigit_start_year / 100) * 100;
781 if (y < two)
782 century += 100;
784 y += century;
787 break;
788 default:
789 break;
792 ++i;
793 ++j;
797 if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
799 /* Try YYYY MM DD */
800 y = pt.n[0];
801 m = pt.n[1];
802 day = pt.n[2];
804 if (using_twodigit_years && y < 100)
805 y = G_DATE_BAD_YEAR; /* avoids ambiguity */
807 else if (pt.num_ints == 2)
809 if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
811 m = pt.month;
815 else if (pt.num_ints == 1)
817 if (pt.month != G_DATE_BAD_MONTH)
819 /* Month name and year? */
820 m = pt.month;
821 day = 1;
822 y = pt.n[0];
824 else
826 /* Try yyyymmdd and yymmdd */
828 m = (pt.n[0]/100) % 100;
829 day = pt.n[0] % 100;
830 y = pt.n[0]/10000;
832 /* FIXME move this into a separate function */
833 if (using_twodigit_years && y < 100)
835 guint two = twodigit_start_year % 100;
836 guint century = (twodigit_start_year / 100) * 100;
838 if (y < two)
839 century += 100;
841 y += century;
846 /* See if we got anything valid out of all this. */
847 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
848 if (y < 8000 && g_date_valid_dmy (day, m, y))
850 d->month = m;
851 d->day = day;
852 d->year = y;
853 d->dmy = TRUE;
855 #ifdef G_ENABLE_DEBUG
856 else
857 DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
858 #endif
859 G_UNLOCK (g_date_global);
862 void
863 g_date_set_time (GDate *d,
864 GTime time)
866 time_t t = time;
867 struct tm tm;
869 g_return_if_fail (d != NULL);
871 #ifdef HAVE_LOCALTIME_R
872 localtime_r (&t, &tm);
873 #else
875 struct tm *ptm = localtime (&t);
877 if (ptm == NULL)
879 /* Happens at least in Microsoft's C library if you pass a
880 * negative time_t. Use 2000-01-01 as default date.
882 #ifndef G_DISABLE_CHECKS
883 g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
884 #endif
886 tm.tm_mon = 0;
887 tm.tm_mday = 1;
888 tm.tm_year = 100;
890 else
891 memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
893 #endif
895 d->julian = FALSE;
897 d->month = tm.tm_mon + 1;
898 d->day = tm.tm_mday;
899 d->year = tm.tm_year + 1900;
901 g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
903 d->dmy = TRUE;
906 void
907 g_date_set_month (GDate *d,
908 GDateMonth m)
910 g_return_if_fail (d != NULL);
911 g_return_if_fail (g_date_valid_month (m));
913 if (d->julian && !d->dmy) g_date_update_dmy(d);
914 d->julian = FALSE;
916 d->month = m;
918 if (g_date_valid_dmy (d->day, d->month, d->year))
919 d->dmy = TRUE;
920 else
921 d->dmy = FALSE;
924 void
925 g_date_set_day (GDate *d,
926 GDateDay day)
928 g_return_if_fail (d != NULL);
929 g_return_if_fail (g_date_valid_day (day));
931 if (d->julian && !d->dmy) g_date_update_dmy(d);
932 d->julian = FALSE;
934 d->day = day;
936 if (g_date_valid_dmy (d->day, d->month, d->year))
937 d->dmy = TRUE;
938 else
939 d->dmy = FALSE;
942 void
943 g_date_set_year (GDate *d,
944 GDateYear y)
946 g_return_if_fail (d != NULL);
947 g_return_if_fail (g_date_valid_year (y));
949 if (d->julian && !d->dmy) g_date_update_dmy(d);
950 d->julian = FALSE;
952 d->year = y;
954 if (g_date_valid_dmy (d->day, d->month, d->year))
955 d->dmy = TRUE;
956 else
957 d->dmy = FALSE;
960 void
961 g_date_set_dmy (GDate *d,
962 GDateDay day,
963 GDateMonth m,
964 GDateYear y)
966 g_return_if_fail (d != NULL);
967 g_return_if_fail (g_date_valid_dmy (day, m, y));
969 d->julian = FALSE;
971 d->month = m;
972 d->day = day;
973 d->year = y;
975 d->dmy = TRUE;
978 void
979 g_date_set_julian (GDate *d, guint32 j)
981 g_return_if_fail (d != NULL);
982 g_return_if_fail (g_date_valid_julian (j));
984 d->julian_days = j;
985 d->julian = TRUE;
986 d->dmy = FALSE;
990 gboolean
991 g_date_is_first_of_month (const GDate *d)
993 g_return_val_if_fail (g_date_valid (d), FALSE);
995 if (!d->dmy)
997 g_date_update_dmy (d);
999 g_return_val_if_fail (d->dmy, FALSE);
1001 if (d->day == 1) return TRUE;
1002 else return FALSE;
1005 gboolean
1006 g_date_is_last_of_month (const GDate *d)
1008 gint index;
1010 g_return_val_if_fail (g_date_valid (d), FALSE);
1012 if (!d->dmy)
1014 g_date_update_dmy (d);
1016 g_return_val_if_fail (d->dmy, FALSE);
1018 index = g_date_is_leap_year (d->year) ? 1 : 0;
1020 if (d->day == days_in_months[index][d->month]) return TRUE;
1021 else return FALSE;
1024 void
1025 g_date_add_days (GDate *d, guint ndays)
1027 g_return_if_fail (g_date_valid (d));
1029 if (!d->julian)
1031 g_date_update_julian (d);
1033 g_return_if_fail (d->julian);
1035 d->julian_days += ndays;
1036 d->dmy = FALSE;
1039 void
1040 g_date_subtract_days (GDate *d, guint ndays)
1042 g_return_if_fail (g_date_valid (d));
1044 if (!d->julian)
1046 g_date_update_julian (d);
1048 g_return_if_fail (d->julian);
1049 g_return_if_fail (d->julian_days > ndays);
1051 d->julian_days -= ndays;
1052 d->dmy = FALSE;
1055 void
1056 g_date_add_months (GDate *d,
1057 guint nmonths)
1059 guint years, months;
1060 gint index;
1062 g_return_if_fail (g_date_valid (d));
1064 if (!d->dmy)
1066 g_date_update_dmy (d);
1068 g_return_if_fail (d->dmy);
1070 nmonths += d->month - 1;
1072 years = nmonths/12;
1073 months = nmonths%12;
1075 d->month = months + 1;
1076 d->year += years;
1078 index = g_date_is_leap_year (d->year) ? 1 : 0;
1080 if (d->day > days_in_months[index][d->month])
1081 d->day = days_in_months[index][d->month];
1083 d->julian = FALSE;
1085 g_return_if_fail (g_date_valid (d));
1088 void
1089 g_date_subtract_months (GDate *d,
1090 guint nmonths)
1092 guint years, months;
1093 gint index;
1095 g_return_if_fail (g_date_valid (d));
1097 if (!d->dmy)
1099 g_date_update_dmy (d);
1101 g_return_if_fail (d->dmy);
1103 years = nmonths/12;
1104 months = nmonths%12;
1106 g_return_if_fail (d->year > years);
1108 d->year -= years;
1110 if (d->month > months) d->month -= months;
1111 else
1113 months -= d->month;
1114 d->month = 12 - months;
1115 d->year -= 1;
1118 index = g_date_is_leap_year (d->year) ? 1 : 0;
1120 if (d->day > days_in_months[index][d->month])
1121 d->day = days_in_months[index][d->month];
1123 d->julian = FALSE;
1125 g_return_if_fail (g_date_valid (d));
1128 void
1129 g_date_add_years (GDate *d,
1130 guint nyears)
1132 g_return_if_fail (g_date_valid (d));
1134 if (!d->dmy)
1136 g_date_update_dmy (d);
1138 g_return_if_fail (d->dmy);
1140 d->year += nyears;
1142 if (d->month == 2 && d->day == 29)
1144 if (!g_date_is_leap_year (d->year))
1146 d->day = 28;
1150 d->julian = FALSE;
1153 void
1154 g_date_subtract_years (GDate *d,
1155 guint nyears)
1157 g_return_if_fail (g_date_valid (d));
1159 if (!d->dmy)
1161 g_date_update_dmy (d);
1163 g_return_if_fail (d->dmy);
1164 g_return_if_fail (d->year > nyears);
1166 d->year -= nyears;
1168 if (d->month == 2 && d->day == 29)
1170 if (!g_date_is_leap_year (d->year))
1172 d->day = 28;
1176 d->julian = FALSE;
1180 gboolean
1181 g_date_is_leap_year (GDateYear year)
1183 g_return_val_if_fail (g_date_valid_year (year), FALSE);
1185 return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1186 (year % 400) == 0 );
1189 guint8
1190 g_date_get_days_in_month (GDateMonth month,
1191 GDateYear year)
1193 gint index;
1195 g_return_val_if_fail (g_date_valid_year (year), 0);
1196 g_return_val_if_fail (g_date_valid_month (month), 0);
1198 index = g_date_is_leap_year (year) ? 1 : 0;
1200 return days_in_months[index][month];
1203 guint8
1204 g_date_get_monday_weeks_in_year (GDateYear year)
1206 GDate d;
1208 g_return_val_if_fail (g_date_valid_year (year), 0);
1210 g_date_clear (&d, 1);
1211 g_date_set_dmy (&d, 1, 1, year);
1212 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1213 g_date_set_dmy (&d, 31, 12, year);
1214 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1215 if (g_date_is_leap_year (year))
1217 g_date_set_dmy (&d, 2, 1, year);
1218 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1219 g_date_set_dmy (&d, 30, 12, year);
1220 if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1222 return 52;
1225 guint8
1226 g_date_get_sunday_weeks_in_year (GDateYear year)
1228 GDate d;
1230 g_return_val_if_fail (g_date_valid_year (year), 0);
1232 g_date_clear (&d, 1);
1233 g_date_set_dmy (&d, 1, 1, year);
1234 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1235 g_date_set_dmy (&d, 31, 12, year);
1236 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1237 if (g_date_is_leap_year (year))
1239 g_date_set_dmy (&d, 2, 1, year);
1240 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1241 g_date_set_dmy (&d, 30, 12, year);
1242 if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
1244 return 52;
1247 gint
1248 g_date_compare (const GDate *lhs,
1249 const GDate *rhs)
1251 g_return_val_if_fail (lhs != NULL, 0);
1252 g_return_val_if_fail (rhs != NULL, 0);
1253 g_return_val_if_fail (g_date_valid (lhs), 0);
1254 g_return_val_if_fail (g_date_valid (rhs), 0);
1256 /* Remember the self-comparison case! I think it works right now. */
1258 while (TRUE)
1261 if (lhs->julian && rhs->julian)
1263 if (lhs->julian_days < rhs->julian_days) return -1;
1264 else if (lhs->julian_days > rhs->julian_days) return 1;
1265 else return 0;
1267 else if (lhs->dmy && rhs->dmy)
1269 if (lhs->year < rhs->year) return -1;
1270 else if (lhs->year > rhs->year) return 1;
1271 else
1273 if (lhs->month < rhs->month) return -1;
1274 else if (lhs->month > rhs->month) return 1;
1275 else
1277 if (lhs->day < rhs->day) return -1;
1278 else if (lhs->day > rhs->day) return 1;
1279 else return 0;
1285 else
1287 if (!lhs->julian) g_date_update_julian (lhs);
1288 if (!rhs->julian) g_date_update_julian (rhs);
1289 g_return_val_if_fail (lhs->julian, 0);
1290 g_return_val_if_fail (rhs->julian, 0);
1294 return 0; /* warnings */
1298 void
1299 g_date_to_struct_tm (const GDate *d,
1300 struct tm *tm)
1302 GDateWeekday day;
1304 g_return_if_fail (g_date_valid (d));
1305 g_return_if_fail (tm != NULL);
1307 if (!d->dmy)
1309 g_date_update_dmy (d);
1311 g_return_if_fail (d->dmy);
1313 /* zero all the irrelevant fields to be sure they're valid */
1315 /* On Linux and maybe other systems, there are weird non-POSIX
1316 * fields on the end of struct tm that choke strftime if they
1317 * contain garbage. So we need to 0 the entire struct, not just the
1318 * fields we know to exist.
1321 memset (tm, 0x0, sizeof (struct tm));
1323 tm->tm_mday = d->day;
1324 tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
1325 tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
1327 day = g_date_get_weekday (d);
1328 if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1330 tm->tm_wday = (int)day;
1332 tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
1333 tm->tm_isdst = -1; /* -1 means "information not available" */
1336 void
1337 g_date_clamp (GDate *date,
1338 const GDate *min_date,
1339 const GDate *max_date)
1341 g_return_if_fail (g_date_valid (date));
1343 if (min_date != NULL)
1344 g_return_if_fail (g_date_valid (min_date));
1345 if (max_date != NULL)
1346 g_return_if_fail (g_date_valid (max_date));
1347 if (min_date != NULL && max_date != NULL)
1348 g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
1350 if (min_date && g_date_compare (date, min_date) < 0)
1351 *date = *min_date;
1353 if (max_date && g_date_compare (max_date, date) < 0)
1354 *date = *max_date;
1357 void
1358 g_date_order (GDate *date1,
1359 GDate *date2)
1361 g_return_if_fail (g_date_valid (date1));
1362 g_return_if_fail (g_date_valid (date2));
1364 if (g_date_compare (date1, date2) > 0)
1366 GDate tmp = *date1;
1367 *date1 = *date2;
1368 *date2 = tmp;
1372 gsize
1373 g_date_strftime (gchar *s,
1374 gsize slen,
1375 const gchar *format,
1376 const GDate *d)
1378 struct tm tm;
1379 gsize locale_format_len = 0;
1380 gchar *locale_format;
1381 gsize tmplen;
1382 gchar *tmpbuf;
1383 gsize tmpbufsize;
1384 gsize convlen = 0;
1385 gchar *convbuf;
1386 GError *error = NULL;
1387 gsize retval;
1389 g_return_val_if_fail (g_date_valid (d), 0);
1390 g_return_val_if_fail (slen > 0, 0);
1391 g_return_val_if_fail (format != 0, 0);
1392 g_return_val_if_fail (s != 0, 0);
1394 g_date_to_struct_tm (d, &tm);
1396 locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
1398 if (error)
1400 g_warning (G_STRLOC "Error converting format to locale encoding: %s\n", error->message);
1401 g_error_free (error);
1403 s[0] = '\0';
1404 return 0;
1407 tmpbufsize = MAX (128, locale_format_len * 2);
1408 while (TRUE)
1410 tmpbuf = g_malloc (tmpbufsize);
1412 /* Set the first byte to something other than '\0', to be able to
1413 * recognize whether strftime actually failed or just returned "".
1415 tmpbuf[0] = '\1';
1416 tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
1418 if (tmplen == 0 && tmpbuf[0] != '\0')
1420 g_free (tmpbuf);
1421 tmpbufsize *= 2;
1423 if (tmpbufsize > 65536)
1425 g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up\n");
1426 g_free (locale_format);
1428 s[0] = '\0';
1429 return 0;
1432 else
1433 break;
1435 g_free (locale_format);
1437 convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
1438 g_free (tmpbuf);
1440 if (error)
1442 g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s\n", error->message);
1443 g_error_free (error);
1445 s[0] = '\0';
1446 return 0;
1449 if (slen <= convlen)
1451 /* Ensure only whole characters are copied into the buffer.
1453 gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
1454 g_assert (end != NULL);
1455 convlen = end - convbuf;
1457 /* Return 0 because the buffer isn't large enough.
1459 retval = 0;
1461 else
1462 retval = convlen;
1464 memcpy (s, convbuf, convlen);
1465 s[convlen] = '\0';
1466 g_free (convbuf);
1468 return retval;
1471 #define __G_DATE_C__
1472 #include "galiasdef.c"