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/.
31 #define DEBUG_MSG(x) /* */
33 /* #define DEBUG_MSG(args) g_message args ; */
51 GDate
*d
= g_new0 (GDate
, 1); /* happily, 0 is the invalid flag for everything. */
57 g_date_new_dmy (GDateDay day
, GDateMonth m
, GDateYear y
)
60 g_return_val_if_fail (g_date_valid_dmy (day
, m
, y
), NULL
);
71 g_assert (g_date_valid (d
));
77 g_date_new_julian (guint32 j
)
80 g_return_val_if_fail (g_date_valid_julian (j
), NULL
);
89 g_assert (g_date_valid (d
));
95 g_date_free (GDate
*d
)
97 g_return_if_fail (d
!= NULL
);
103 g_date_valid (GDate
*d
)
105 g_return_val_if_fail (d
!= NULL
, FALSE
);
107 return (d
->julian
|| d
->dmy
);
110 static const guint8 days_in_months
[2][13] =
111 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
112 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
113 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
116 static const guint16 days_in_year
[2][14] =
117 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
118 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
119 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
123 g_date_valid_month (GDateMonth m
)
125 return ( (m
> G_DATE_BAD_MONTH
) && (m
< 13) );
129 g_date_valid_year (GDateYear y
)
131 return ( y
> G_DATE_BAD_YEAR
);
135 g_date_valid_day (GDateDay d
)
137 return ( (d
> G_DATE_BAD_DAY
) && (d
< 32) );
141 g_date_valid_weekday (GDateWeekday w
)
143 return ( (w
> G_DATE_BAD_WEEKDAY
) && (w
< 8) );
147 g_date_valid_julian (guint32 j
)
149 return (j
> G_DATE_BAD_JULIAN
);
153 g_date_valid_dmy (GDateDay d
,
157 return ( (m
> G_DATE_BAD_MONTH
) &&
159 (d
> G_DATE_BAD_DAY
) &&
160 (y
> G_DATE_BAD_YEAR
) && /* must check before using g_date_is_leap_year */
161 (d
<= (g_date_is_leap_year (y
) ?
162 days_in_months
[1][m
] : days_in_months
[0][m
])) );
166 /* "Julian days" just means an absolute number of days, where Day 1 ==
170 g_date_update_julian (GDate
*d
)
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
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
));
204 g_date_update_dmy (GDate
*d
)
210 guint32 A
, B
, C
, D
, E
, M
;
212 g_return_if_fail (d
!= NULL
);
213 g_return_if_fail (d
->julian
);
214 g_return_if_fail (!d
->dmy
);
215 g_return_if_fail (g_date_valid_julian (d
->julian_days
));
217 /* Formula taken from the Calendar FAQ; the formula was for the
218 * Julian Period which starts on 1 January 4713 BC, so we add
219 * 1,721,425 to the number of days before doing the formula.
221 * I'm sure this can be simplified for our 1 January 1 AD period
222 * start, but I can't figure out how to unpack the formula.
225 A
= d
->julian_days
+ 1721425 + 32045;
226 B
= ( 4 *(A
+ 36524) )/ 146097 - 1;
227 C
= A
- (146097 * B
)/4;
228 D
= ( 4 * (C
+ 365) ) / 1461 - 1;
229 E
= C
- ((1461*D
) / 4);
230 M
= (5 * (E
- 1) + 2)/153;
232 m
= M
+ 3 - (12*(M
/10));
233 day
= E
- (153*M
+ 2)/5;
234 y
= 100 * B
+ D
- 4800 + (M
/10);
236 #ifdef G_ENABLE_DEBUG
237 if (!g_date_valid_dmy (day
, m
, y
))
239 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
240 d
->julian_days
, day
, m
, y
);
252 g_date_get_weekday (GDate
*d
)
254 g_return_val_if_fail (d
!= NULL
, G_DATE_BAD_WEEKDAY
);
255 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_WEEKDAY
);
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;
267 g_date_get_month (GDate
*d
)
269 g_return_val_if_fail (d
!= NULL
, G_DATE_BAD_MONTH
);
270 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_MONTH
);
274 g_date_update_dmy (d
);
276 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_MONTH
);
282 g_date_get_year (GDate
*d
)
284 g_return_val_if_fail (d
!= NULL
, G_DATE_BAD_YEAR
);
285 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_YEAR
);
289 g_date_update_dmy (d
);
291 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_YEAR
);
297 g_date_get_day (GDate
*d
)
299 g_return_val_if_fail (d
!= NULL
, G_DATE_BAD_DAY
);
300 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_DAY
);
304 g_date_update_dmy (d
);
306 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_DAY
);
312 g_date_get_julian (GDate
*d
)
314 g_return_val_if_fail (d
!= NULL
, G_DATE_BAD_JULIAN
);
315 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_JULIAN
);
319 g_date_update_julian (d
);
321 g_return_val_if_fail (d
->julian
, G_DATE_BAD_JULIAN
);
323 return d
->julian_days
;
327 g_date_get_day_of_year (GDate
*d
)
331 g_return_val_if_fail (d
!= NULL
, 0);
332 g_return_val_if_fail (g_date_valid (d
), 0);
336 g_date_update_dmy (d
);
338 g_return_val_if_fail (d
->dmy
, 0);
340 index
= g_date_is_leap_year (d
->year
) ? 1 : 0;
342 return (days_in_year
[index
][d
->month
] + d
->day
);
346 g_date_get_monday_week_of_year (GDate
*d
)
352 g_return_val_if_fail (d
!= NULL
, 0);
353 g_return_val_if_fail (g_date_valid (d
), 0);
357 g_date_update_dmy (d
);
359 g_return_val_if_fail (d
->dmy
, 0);
361 g_date_clear (&first
, 1);
363 g_date_set_dmy (&first
, 1, 1, d
->year
);
365 wd
= g_date_get_weekday (&first
) - 1; /* make Monday day 0 */
366 day
= g_date_get_day_of_year (d
) - 1;
368 return ((day
+ wd
)/7U + (wd
== 0 ? 1 : 0));
372 g_date_get_sunday_week_of_year (GDate
*d
)
378 g_return_val_if_fail (d
!= NULL
, 0);
379 g_return_val_if_fail (g_date_valid (d
), 0);
383 g_date_update_dmy (d
);
385 g_return_val_if_fail (d
->dmy
, 0);
387 g_date_clear (&first
, 1);
389 g_date_set_dmy (&first
, 1, 1, d
->year
);
391 wd
= g_date_get_weekday (&first
);
392 if (wd
== 7) wd
= 0; /* make Sunday day 0 */
393 day
= g_date_get_day_of_year (d
) - 1;
395 return ((day
+ wd
)/7U + (wd
== 0 ? 1 : 0));
399 g_date_clear (GDate
*d
, guint ndates
)
401 g_return_if_fail (d
!= NULL
);
402 g_return_if_fail (ndates
!= 0);
404 memset (d
, 0x0, ndates
*sizeof (GDate
));
407 G_LOCK_DEFINE_STATIC (g_date_global
);
409 /* These are for the parser, output to the user should use *
410 * g_date_strftime () - this creates more never-freed memory to annoy
411 * all those memory debugger users. :-)
414 static gchar
*long_month_names
[13] =
416 "Error", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
419 static gchar
*short_month_names
[13] =
421 "Error", NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
424 /* This tells us if we need to update the parse info */
425 static gchar
*current_locale
= NULL
;
427 /* order of these in the current locale */
428 static GDateDMY dmy_order
[3] =
430 G_DATE_DAY
, G_DATE_MONTH
, G_DATE_YEAR
433 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
434 * 29 and below are counted as in the year 2000, numbers 30 and above
435 * are counted as in the year 1900.
438 static GDateYear twodigit_start_year
= 1930;
440 /* It is impossible to enter a year between 1 AD and 99 AD with this
443 static gboolean using_twodigit_years
= FALSE
;
445 struct _GDateParseTokens
{
451 typedef struct _GDateParseTokens GDateParseTokens
;
455 /* HOLDS: g_date_global_lock */
457 g_date_fill_parse_tokens (const gchar
*str
, GDateParseTokens
*pt
)
459 gchar num
[4][NUM_LEN
+1];
463 /* We count 4, but store 3; so we can give an error
466 num
[0][0] = num
[1][0] = num
[2][0] = num
[3][0] = '\0';
468 s
= (const guchar
*) str
;
470 while (*s
&& pt
->num_ints
< 4)
474 while (*s
&& isdigit (*s
) && i
<= NUM_LEN
)
476 num
[pt
->num_ints
][i
] = *s
;
483 num
[pt
->num_ints
][i
] = '\0';
487 if (*s
== '\0') break;
492 pt
->n
[0] = pt
->num_ints
> 0 ? atoi (num
[0]) : 0;
493 pt
->n
[1] = pt
->num_ints
> 1 ? atoi (num
[1]) : 0;
494 pt
->n
[2] = pt
->num_ints
> 2 ? atoi (num
[2]) : 0;
496 pt
->month
= G_DATE_BAD_MONTH
;
498 if (pt
->num_ints
< 3)
503 strncpy (lcstr
, str
, 127);
508 if (long_month_names
[i
] != NULL
)
510 const gchar
*found
= strstr (lcstr
, long_month_names
[i
]);
519 if (short_month_names
[i
] != NULL
)
521 const gchar
*found
= strstr (lcstr
, short_month_names
[i
]);
535 /* HOLDS: g_date_global_lock */
537 g_date_prepare_to_parse (const gchar
*str
, GDateParseTokens
*pt
)
539 const gchar
*locale
= setlocale (LC_TIME
, NULL
);
540 gboolean recompute_localeinfo
= FALSE
;
543 g_return_if_fail (locale
!= NULL
); /* should not happen */
545 g_date_clear (&d
, 1); /* clear for scratch use */
547 if ( (current_locale
== NULL
) || (strcmp (locale
, current_locale
) != 0) )
549 recompute_localeinfo
= TRUE
; /* Uh, there used to be a reason for the temporary */
552 if (recompute_localeinfo
)
555 GDateParseTokens testpt
;
558 g_free (current_locale
); /* still works if current_locale == NULL */
560 current_locale
= g_strdup (locale
);
564 g_date_set_dmy (&d
, 1, i
, 1);
566 g_return_if_fail (g_date_valid (&d
));
568 g_date_strftime (buf
, 127, "%b", &d
);
569 g_free (short_month_names
[i
]);
571 short_month_names
[i
] = g_strdup (buf
);
575 g_date_strftime (buf
, 127, "%B", &d
);
576 g_free (long_month_names
[i
]);
578 long_month_names
[i
] = g_strdup (buf
);
583 /* Determine DMY order */
585 /* had to pick a random day - don't change this, some strftimes
586 * are broken on some days, and this one is good so far. */
587 g_date_set_dmy (&d
, 4, 7, 1976);
589 g_date_strftime (buf
, 127, "%x", &d
);
591 g_date_fill_parse_tokens (buf
, &testpt
);
594 while (i
< testpt
.num_ints
)
599 dmy_order
[i
] = G_DATE_MONTH
;
602 dmy_order
[i
] = G_DATE_DAY
;
605 using_twodigit_years
= TRUE
; /* FALL THRU */
607 dmy_order
[i
] = G_DATE_YEAR
;
610 /* leave it unchanged */
616 #ifdef G_ENABLE_DEBUG
617 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
621 DEBUG_MSG ((" %s %s", long_month_names
[i
], short_month_names
[i
]));
624 if (using_twodigit_years
)
625 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year
));
631 switch (dmy_order
[i
])
634 strings
[i
] = "Month";
648 DEBUG_MSG (("**Order: %s, %s, %s", strings
[0], strings
[1], strings
[2]));
649 DEBUG_MSG (("**Sample date in this locale: `%s'", buf
));
654 g_date_fill_parse_tokens (str
, pt
);
658 g_date_set_parse (GDate
*d
,
662 guint m
= G_DATE_BAD_MONTH
, day
= G_DATE_BAD_DAY
, y
= G_DATE_BAD_YEAR
;
664 g_return_if_fail (d
!= NULL
);
669 G_LOCK (g_date_global
);
671 g_date_prepare_to_parse (str
, &pt
);
673 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
674 pt
.num_ints
, pt
.n
[0], pt
.n
[1], pt
.n
[2], pt
.month
));
677 if (pt
.num_ints
== 4)
679 G_UNLOCK (g_date_global
);
680 return; /* presumably a typo; bail out. */
688 g_assert (pt
.num_ints
< 4); /* i.e., it is 2 or 3 */
690 while (i
< pt
.num_ints
&& j
< 3)
692 switch (dmy_order
[j
])
696 if (pt
.num_ints
== 2 && pt
.month
!= G_DATE_BAD_MONTH
)
699 ++j
; /* skip months, but don't skip this number */
708 if (pt
.num_ints
== 2 && pt
.month
== G_DATE_BAD_MONTH
)
711 ++j
; /* skip days, since we may have month/year */
721 if (using_twodigit_years
&& y
< 100)
723 guint two
= twodigit_start_year
% 100;
724 guint century
= (twodigit_start_year
/ 100) * 100;
742 if (pt
.num_ints
== 3 && !g_date_valid_dmy (day
, m
, y
))
749 if (using_twodigit_years
&& y
< 100)
750 y
= G_DATE_BAD_YEAR
; /* avoids ambiguity */
753 else if (pt
.num_ints
== 1)
755 if (pt
.month
!= G_DATE_BAD_MONTH
)
757 /* Month name and year? */
764 /* Try yyyymmdd and yymmdd */
766 m
= (pt
.n
[0]/100) % 100;
770 /* FIXME move this into a separate function */
771 if (using_twodigit_years
&& y
< 100)
773 guint two
= twodigit_start_year
% 100;
774 guint century
= (twodigit_start_year
/ 100) * 100;
784 /* See if we got anything valid out of all this. */
785 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
786 if (y
< 8000 && g_date_valid_dmy (day
, m
, y
))
793 #ifdef G_ENABLE_DEBUG
795 DEBUG_MSG (("Rejected DMY %u %u %u", day
, m
, y
));
797 G_UNLOCK (g_date_global
);
801 g_date_set_time (GDate
*d
,
807 g_return_if_fail (d
!= NULL
);
809 #ifdef HAVE_LOCALTIME_R
810 localtime_r (&t
, &tm
);
813 struct tm
*ptm
= localtime (&t
);
815 memcpy ((void *) &tm
, (void *) ptm
, sizeof(struct tm
));
821 d
->month
= tm
.tm_mon
+ 1;
823 d
->year
= tm
.tm_year
+ 1900;
825 g_return_if_fail (g_date_valid_dmy (d
->day
, d
->month
, d
->year
));
831 g_date_set_month (GDate
*d
,
834 g_return_if_fail (d
!= NULL
);
835 g_return_if_fail (g_date_valid_month (m
));
837 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
842 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
849 g_date_set_day (GDate
*d
,
852 g_return_if_fail (d
!= NULL
);
853 g_return_if_fail (g_date_valid_day (day
));
855 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
860 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
867 g_date_set_year (GDate
*d
,
870 g_return_if_fail (d
!= NULL
);
871 g_return_if_fail (g_date_valid_year (y
));
873 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
878 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
885 g_date_set_dmy (GDate
*d
,
890 g_return_if_fail (d
!= NULL
);
891 g_return_if_fail (g_date_valid_dmy (day
, m
, y
));
903 g_date_set_julian (GDate
*d
, guint32 j
)
905 g_return_if_fail (d
!= NULL
);
906 g_return_if_fail (g_date_valid_julian (j
));
915 g_date_is_first_of_month (GDate
*d
)
917 g_return_val_if_fail (d
!= NULL
, FALSE
);
918 g_return_val_if_fail (g_date_valid (d
), FALSE
);
922 g_date_update_dmy (d
);
924 g_return_val_if_fail (d
->dmy
, FALSE
);
926 if (d
->day
== 1) return TRUE
;
931 g_date_is_last_of_month (GDate
*d
)
935 g_return_val_if_fail (d
!= NULL
, FALSE
);
936 g_return_val_if_fail (g_date_valid (d
), FALSE
);
940 g_date_update_dmy (d
);
942 g_return_val_if_fail (d
->dmy
, FALSE
);
944 index
= g_date_is_leap_year (d
->year
) ? 1 : 0;
946 if (d
->day
== days_in_months
[index
][d
->month
]) return TRUE
;
951 g_date_add_days (GDate
*d
, guint ndays
)
953 g_return_if_fail (d
!= NULL
);
954 g_return_if_fail (g_date_valid (d
));
958 g_date_update_julian (d
);
960 g_return_if_fail (d
->julian
);
962 d
->julian_days
+= ndays
;
967 g_date_subtract_days (GDate
*d
, guint ndays
)
969 g_return_if_fail (d
!= NULL
);
970 g_return_if_fail (g_date_valid (d
));
974 g_date_update_julian (d
);
976 g_return_if_fail (d
->julian
);
977 g_return_if_fail (d
->julian_days
> ndays
);
979 d
->julian_days
-= ndays
;
984 g_date_add_months (GDate
*d
,
990 g_return_if_fail (d
!= NULL
);
991 g_return_if_fail (g_date_valid (d
));
995 g_date_update_dmy (d
);
997 g_return_if_fail (d
->dmy
);
999 nmonths
+= d
->month
- 1;
1002 months
= nmonths
%12;
1004 d
->month
= months
+ 1;
1007 index
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1009 if (d
->day
> days_in_months
[index
][d
->month
])
1010 d
->day
= days_in_months
[index
][d
->month
];
1014 g_return_if_fail (g_date_valid (d
));
1018 g_date_subtract_months (GDate
*d
,
1021 guint years
, months
;
1024 g_return_if_fail (d
!= NULL
);
1025 g_return_if_fail (g_date_valid (d
));
1029 g_date_update_dmy (d
);
1031 g_return_if_fail (d
->dmy
);
1034 months
= nmonths
%12;
1036 g_return_if_fail (d
->year
> years
);
1040 if (d
->month
> months
) d
->month
-= months
;
1044 d
->month
= 12 - months
;
1048 index
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1050 if (d
->day
> days_in_months
[index
][d
->month
])
1051 d
->day
= days_in_months
[index
][d
->month
];
1055 g_return_if_fail (g_date_valid (d
));
1059 g_date_add_years (GDate
*d
,
1062 g_return_if_fail (d
!= NULL
);
1063 g_return_if_fail (g_date_valid (d
));
1067 g_date_update_dmy (d
);
1069 g_return_if_fail (d
->dmy
);
1073 if (d
->month
== 2 && d
->day
== 29)
1075 if (!g_date_is_leap_year (d
->year
))
1085 g_date_subtract_years (GDate
*d
,
1088 g_return_if_fail (d
!= NULL
);
1089 g_return_if_fail (g_date_valid (d
));
1093 g_date_update_dmy (d
);
1095 g_return_if_fail (d
->dmy
);
1096 g_return_if_fail (d
->year
> nyears
);
1100 if (d
->month
== 2 && d
->day
== 29)
1102 if (!g_date_is_leap_year (d
->year
))
1113 g_date_is_leap_year (GDateYear year
)
1115 g_return_val_if_fail (g_date_valid_year (year
), FALSE
);
1117 return ( (((year
% 4) == 0) && ((year
% 100) != 0)) ||
1118 (year
% 400) == 0 );
1122 g_date_get_days_in_month (GDateMonth month
,
1127 g_return_val_if_fail (g_date_valid_year (year
), 0);
1128 g_return_val_if_fail (g_date_valid_month (month
), 0);
1130 index
= g_date_is_leap_year (year
) ? 1 : 0;
1132 return days_in_months
[index
][month
];
1136 g_date_get_monday_weeks_in_year (GDateYear year
)
1140 g_return_val_if_fail (g_date_valid_year (year
), 0);
1142 g_date_clear (&d
, 1);
1143 g_date_set_dmy (&d
, 1, 1, year
);
1144 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1145 g_date_set_dmy (&d
, 31, 12, year
);
1146 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1147 if (g_date_is_leap_year (year
))
1149 g_date_set_dmy (&d
, 2, 1, year
);
1150 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1151 g_date_set_dmy (&d
, 30, 12, year
);
1152 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1158 g_date_get_sunday_weeks_in_year (GDateYear year
)
1162 g_return_val_if_fail (g_date_valid_year (year
), 0);
1164 g_date_clear (&d
, 1);
1165 g_date_set_dmy (&d
, 1, 1, year
);
1166 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1167 g_date_set_dmy (&d
, 31, 12, year
);
1168 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1169 if (g_date_is_leap_year (year
))
1171 g_date_set_dmy (&d
, 2, 1, year
);
1172 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1173 g_date_set_dmy (&d
, 30, 12, year
);
1174 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1180 g_date_compare (GDate
*lhs
,
1183 g_return_val_if_fail (lhs
!= NULL
, 0);
1184 g_return_val_if_fail (rhs
!= NULL
, 0);
1185 g_return_val_if_fail (g_date_valid (lhs
), 0);
1186 g_return_val_if_fail (g_date_valid (rhs
), 0);
1188 /* Remember the self-comparison case! I think it works right now. */
1193 if (lhs
->julian
&& rhs
->julian
)
1195 if (lhs
->julian_days
< rhs
->julian_days
) return -1;
1196 else if (lhs
->julian_days
> rhs
->julian_days
) return 1;
1199 else if (lhs
->dmy
&& rhs
->dmy
)
1201 if (lhs
->year
< rhs
->year
) return -1;
1202 else if (lhs
->year
> rhs
->year
) return 1;
1205 if (lhs
->month
< rhs
->month
) return -1;
1206 else if (lhs
->month
> rhs
->month
) return 1;
1209 if (lhs
->day
< rhs
->day
) return -1;
1210 else if (lhs
->day
> rhs
->day
) return 1;
1219 if (!lhs
->julian
) g_date_update_julian (lhs
);
1220 if (!rhs
->julian
) g_date_update_julian (rhs
);
1221 g_return_val_if_fail (lhs
->julian
, 0);
1222 g_return_val_if_fail (rhs
->julian
, 0);
1226 return 0; /* warnings */
1231 g_date_to_struct_tm (GDate
*d
,
1236 g_return_if_fail (d
!= NULL
);
1237 g_return_if_fail (g_date_valid (d
));
1238 g_return_if_fail (tm
!= NULL
);
1242 g_date_update_dmy (d
);
1244 g_return_if_fail (d
->dmy
);
1246 /* zero all the irrelevant fields to be sure they're valid */
1248 /* On Linux and maybe other systems, there are weird non-POSIX
1249 * fields on the end of struct tm that choke strftime if they
1250 * contain garbage. So we need to 0 the entire struct, not just the
1251 * fields we know to exist.
1254 memset (tm
, 0x0, sizeof (struct tm
));
1256 tm
->tm_mday
= d
->day
;
1257 tm
->tm_mon
= d
->month
- 1; /* 0-11 goes in tm */
1258 tm
->tm_year
= ((int)d
->year
) - 1900; /* X/Open says tm_year can be negative */
1260 day
= g_date_get_weekday (d
);
1261 if (day
== 7) day
= 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1263 tm
->tm_wday
= (int)day
;
1265 tm
->tm_yday
= g_date_get_day_of_year (d
) - 1; /* 0 to 365 */
1266 tm
->tm_isdst
= -1; /* -1 means "information not available" */
1270 g_date_strftime (gchar
*s
,
1272 const gchar
*format
,
1278 g_return_val_if_fail (d
!= NULL
, 0);
1279 g_return_val_if_fail (g_date_valid (d
), 0);
1280 g_return_val_if_fail (slen
> 0, 0);
1281 g_return_val_if_fail (format
!= 0, 0);
1282 g_return_val_if_fail (s
!= 0, 0);
1284 g_date_to_struct_tm (d
, &tm
);
1286 retval
= strftime (s
, slen
, format
, &tm
);
1289 /* If retval == 0, the contents of s are undefined. We define