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/.
32 #include "glibconfig.h"
34 #define DEBUG_MSG(x) /* */
36 /* #define DEBUG_MSG(args) g_message args ; */
52 #include "gstrfuncs.h"
53 #include "gtestutils.h"
64 GDate
*d
= g_new0 (GDate
, 1); /* happily, 0 is the invalid flag for everything. */
70 g_date_new_dmy (GDateDay day
,
75 g_return_val_if_fail (g_date_valid_dmy (day
, m
, y
), NULL
);
86 g_assert (g_date_valid (d
));
92 g_date_new_julian (guint32 j
)
95 g_return_val_if_fail (g_date_valid_julian (j
), NULL
);
104 g_assert (g_date_valid (d
));
110 g_date_free (GDate
*d
)
112 g_return_if_fail (d
!= NULL
);
118 g_date_valid (const GDate
*d
)
120 g_return_val_if_fail (d
!= NULL
, FALSE
);
122 return (d
->julian
|| d
->dmy
);
125 static const guint8 days_in_months
[2][13] =
126 { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
127 { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
128 { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
131 static const guint16 days_in_year
[2][14] =
132 { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
133 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
134 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
138 g_date_valid_month (GDateMonth m
)
140 return ( (m
> G_DATE_BAD_MONTH
) && (m
< 13) );
144 g_date_valid_year (GDateYear y
)
146 return ( y
> G_DATE_BAD_YEAR
);
150 g_date_valid_day (GDateDay d
)
152 return ( (d
> G_DATE_BAD_DAY
) && (d
< 32) );
156 g_date_valid_weekday (GDateWeekday w
)
158 return ( (w
> G_DATE_BAD_WEEKDAY
) && (w
< 8) );
162 g_date_valid_julian (guint32 j
)
164 return (j
> G_DATE_BAD_JULIAN
);
168 g_date_valid_dmy (GDateDay d
,
172 return ( (m
> G_DATE_BAD_MONTH
) &&
174 (d
> G_DATE_BAD_DAY
) &&
175 (y
> G_DATE_BAD_YEAR
) && /* must check before using g_date_is_leap_year */
176 (d
<= (g_date_is_leap_year (y
) ?
177 days_in_months
[1][m
] : days_in_months
[0][m
])) );
181 /* "Julian days" just means an absolute number of days, where Day 1 ==
185 g_date_update_julian (const GDate
*const_d
)
187 GDate
*d
= (GDate
*) const_d
;
191 g_return_if_fail (d
!= NULL
);
192 g_return_if_fail (d
->dmy
);
193 g_return_if_fail (!d
->julian
);
194 g_return_if_fail (g_date_valid_dmy (d
->day
, d
->month
, d
->year
));
196 /* What we actually do is: multiply years * 365 days in the year,
197 * add the number of years divided by 4, subtract the number of
198 * years divided by 100 and add the number of years divided by 400,
199 * which accounts for leap year stuff. Code from Steffen Beyer's
203 year
= d
->year
- 1; /* we know d->year > 0 since it's valid */
205 d
->julian_days
= year
* 365U;
206 d
->julian_days
+= (year
>>= 2); /* divide by 4 and add */
207 d
->julian_days
-= (year
/= 25); /* divides original # years by 100 */
208 d
->julian_days
+= year
>> 2; /* divides by 4, which divides original by 400 */
210 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
212 d
->julian_days
+= days_in_year
[idx
][d
->month
] + d
->day
;
214 g_return_if_fail (g_date_valid_julian (d
->julian_days
));
220 g_date_update_dmy (const GDate
*const_d
)
222 GDate
*d
= (GDate
*) const_d
;
227 guint32 A
, B
, C
, D
, E
, M
;
229 g_return_if_fail (d
!= NULL
);
230 g_return_if_fail (d
->julian
);
231 g_return_if_fail (!d
->dmy
);
232 g_return_if_fail (g_date_valid_julian (d
->julian_days
));
234 /* Formula taken from the Calendar FAQ; the formula was for the
235 * Julian Period which starts on 1 January 4713 BC, so we add
236 * 1,721,425 to the number of days before doing the formula.
238 * I'm sure this can be simplified for our 1 January 1 AD period
239 * start, but I can't figure out how to unpack the formula.
242 A
= d
->julian_days
+ 1721425 + 32045;
243 B
= ( 4 *(A
+ 36524) )/ 146097 - 1;
244 C
= A
- (146097 * B
)/4;
245 D
= ( 4 * (C
+ 365) ) / 1461 - 1;
246 E
= C
- ((1461*D
) / 4);
247 M
= (5 * (E
- 1) + 2)/153;
249 m
= M
+ 3 - (12*(M
/10));
250 day
= E
- (153*M
+ 2)/5;
251 y
= 100 * B
+ D
- 4800 + (M
/10);
253 #ifdef G_ENABLE_DEBUG
254 if (!g_date_valid_dmy (day
, m
, y
))
255 g_warning ("\nOOPS julian: %u computed dmy: %u %u %u\n",
256 d
->julian_days
, day
, m
, y
);
267 g_date_get_weekday (const GDate
*d
)
269 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_WEEKDAY
);
272 g_date_update_julian (d
);
274 g_return_val_if_fail (d
->julian
, G_DATE_BAD_WEEKDAY
);
276 return ((d
->julian_days
- 1) % 7) + 1;
280 g_date_get_month (const GDate
*d
)
282 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_MONTH
);
285 g_date_update_dmy (d
);
287 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_MONTH
);
293 g_date_get_year (const GDate
*d
)
295 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_YEAR
);
298 g_date_update_dmy (d
);
300 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_YEAR
);
306 g_date_get_day (const GDate
*d
)
308 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_DAY
);
311 g_date_update_dmy (d
);
313 g_return_val_if_fail (d
->dmy
, G_DATE_BAD_DAY
);
319 g_date_get_julian (const GDate
*d
)
321 g_return_val_if_fail (g_date_valid (d
), G_DATE_BAD_JULIAN
);
324 g_date_update_julian (d
);
326 g_return_val_if_fail (d
->julian
, G_DATE_BAD_JULIAN
);
328 return d
->julian_days
;
332 g_date_get_day_of_year (const GDate
*d
)
336 g_return_val_if_fail (g_date_valid (d
), 0);
339 g_date_update_dmy (d
);
341 g_return_val_if_fail (d
->dmy
, 0);
343 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
345 return (days_in_year
[idx
][d
->month
] + d
->day
);
349 g_date_get_monday_week_of_year (const GDate
*d
)
355 g_return_val_if_fail (g_date_valid (d
), 0);
358 g_date_update_dmy (d
);
360 g_return_val_if_fail (d
->dmy
, 0);
362 g_date_clear (&first
, 1);
364 g_date_set_dmy (&first
, 1, 1, d
->year
);
366 wd
= g_date_get_weekday (&first
) - 1; /* make Monday day 0 */
367 day
= g_date_get_day_of_year (d
) - 1;
369 return ((day
+ wd
)/7U + (wd
== 0 ? 1 : 0));
373 g_date_get_sunday_week_of_year (const GDate
*d
)
379 g_return_val_if_fail (g_date_valid (d
), 0);
382 g_date_update_dmy (d
);
384 g_return_val_if_fail (d
->dmy
, 0);
386 g_date_clear (&first
, 1);
388 g_date_set_dmy (&first
, 1, 1, d
->year
);
390 wd
= g_date_get_weekday (&first
);
391 if (wd
== 7) wd
= 0; /* make Sunday day 0 */
392 day
= g_date_get_day_of_year (d
) - 1;
394 return ((day
+ wd
)/7U + (wd
== 0 ? 1 : 0));
398 * g_date_get_iso8601_week_of_year:
399 * @date: a valid #GDate
401 * Returns the week of the year, where weeks are interpreted according
404 * Returns: ISO 8601 week number of the year.
409 g_date_get_iso8601_week_of_year (const GDate
*d
)
411 guint j
, d4
, L
, d1
, w
;
413 g_return_val_if_fail (g_date_valid (d
), 0);
416 g_date_update_julian (d
);
418 g_return_val_if_fail (d
->julian
, 0);
420 /* Formula taken from the Calendar FAQ; the formula was for the
421 * Julian Period which starts on 1 January 4713 BC, so we add
422 * 1,721,425 to the number of days before doing the formula.
424 j
= d
->julian_days
+ 1721425;
425 d4
= (j
+ 31741 - (j
% 7)) % 146097 % 36524 % 1461;
427 d1
= ((d4
- L
) % 365) + L
;
434 g_date_days_between (const GDate
*d1
,
437 g_return_val_if_fail (g_date_valid (d1
), 0);
438 g_return_val_if_fail (g_date_valid (d2
), 0);
440 return (gint
)g_date_get_julian (d2
) - (gint
)g_date_get_julian (d1
);
444 g_date_clear (GDate
*d
, guint ndates
)
446 g_return_if_fail (d
!= NULL
);
447 g_return_if_fail (ndates
!= 0);
449 memset (d
, 0x0, ndates
*sizeof (GDate
));
452 G_LOCK_DEFINE_STATIC (g_date_global
);
454 /* These are for the parser, output to the user should use *
455 * g_date_strftime () - this creates more never-freed memory to annoy
456 * all those memory debugger users. :-)
459 static gchar
*long_month_names
[13] =
464 static gchar
*short_month_names
[13] =
469 /* This tells us if we need to update the parse info */
470 static gchar
*current_locale
= NULL
;
472 /* order of these in the current locale */
473 static GDateDMY dmy_order
[3] =
475 G_DATE_DAY
, G_DATE_MONTH
, G_DATE_YEAR
478 /* Where to chop two-digit years: i.e., for the 1930 default, numbers
479 * 29 and below are counted as in the year 2000, numbers 30 and above
480 * are counted as in the year 1900.
483 static const GDateYear twodigit_start_year
= 1930;
485 /* It is impossible to enter a year between 1 AD and 99 AD with this
488 static gboolean using_twodigit_years
= FALSE
;
490 /* Adjustment of locale era to AD, non-zero means using locale era
492 static gint locale_era_adjust
= 0;
494 struct _GDateParseTokens
{
500 typedef struct _GDateParseTokens GDateParseTokens
;
504 /* HOLDS: g_date_global_lock */
506 g_date_fill_parse_tokens (const gchar
*str
, GDateParseTokens
*pt
)
508 gchar num
[4][NUM_LEN
+1];
512 /* We count 4, but store 3; so we can give an error
515 num
[0][0] = num
[1][0] = num
[2][0] = num
[3][0] = '\0';
517 s
= (const guchar
*) str
;
519 while (*s
&& pt
->num_ints
< 4)
523 while (*s
&& g_ascii_isdigit (*s
) && i
< NUM_LEN
)
525 num
[pt
->num_ints
][i
] = *s
;
532 num
[pt
->num_ints
][i
] = '\0';
536 if (*s
== '\0') break;
541 pt
->n
[0] = pt
->num_ints
> 0 ? atoi (num
[0]) : 0;
542 pt
->n
[1] = pt
->num_ints
> 1 ? atoi (num
[1]) : 0;
543 pt
->n
[2] = pt
->num_ints
> 2 ? atoi (num
[2]) : 0;
545 pt
->month
= G_DATE_BAD_MONTH
;
547 if (pt
->num_ints
< 3)
552 casefold
= g_utf8_casefold (str
, -1);
553 normalized
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_ALL
);
559 if (long_month_names
[i
] != NULL
)
561 const gchar
*found
= strstr (normalized
, long_month_names
[i
]);
570 if (short_month_names
[i
] != NULL
)
572 const gchar
*found
= strstr (normalized
, short_month_names
[i
]);
588 /* HOLDS: g_date_global_lock */
590 g_date_prepare_to_parse (const gchar
*str
,
591 GDateParseTokens
*pt
)
593 const gchar
*locale
= setlocale (LC_TIME
, NULL
);
594 gboolean recompute_localeinfo
= FALSE
;
597 g_return_if_fail (locale
!= NULL
); /* should not happen */
599 g_date_clear (&d
, 1); /* clear for scratch use */
601 if ( (current_locale
== NULL
) || (strcmp (locale
, current_locale
) != 0) )
602 recompute_localeinfo
= TRUE
; /* Uh, there used to be a reason for the temporary */
604 if (recompute_localeinfo
)
607 GDateParseTokens testpt
;
610 g_free (current_locale
); /* still works if current_locale == NULL */
612 current_locale
= g_strdup (locale
);
614 short_month_names
[0] = "Error";
615 long_month_names
[0] = "Error";
621 g_date_set_dmy (&d
, 1, i
, 1);
623 g_return_if_fail (g_date_valid (&d
));
625 g_date_strftime (buf
, 127, "%b", &d
);
627 casefold
= g_utf8_casefold (buf
, -1);
628 g_free (short_month_names
[i
]);
629 short_month_names
[i
] = g_utf8_normalize (casefold
, -1, G_NORMALIZE_ALL
);
632 g_date_strftime (buf
, 127, "%B", &d
);
633 casefold
= g_utf8_casefold (buf
, -1);
634 g_free (long_month_names
[i
]);
635 long_month_names
[i
] = g_utf8_normalize (casefold
, -1, G_NORMALIZE_ALL
);
641 /* Determine DMY order */
643 /* had to pick a random day - don't change this, some strftimes
644 * are broken on some days, and this one is good so far. */
645 g_date_set_dmy (&d
, 4, 7, 1976);
647 g_date_strftime (buf
, 127, "%x", &d
);
649 g_date_fill_parse_tokens (buf
, &testpt
);
652 while (i
< testpt
.num_ints
)
657 dmy_order
[i
] = G_DATE_MONTH
;
660 dmy_order
[i
] = G_DATE_DAY
;
663 using_twodigit_years
= TRUE
; /* FALL THRU */
665 dmy_order
[i
] = G_DATE_YEAR
;
668 /* assume locale era */
669 locale_era_adjust
= 1976 - testpt
.n
[i
];
670 dmy_order
[i
] = G_DATE_YEAR
;
676 #if defined(G_ENABLE_DEBUG) && 0
677 DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
681 DEBUG_MSG ((" %s %s", long_month_names
[i
], short_month_names
[i
]));
684 if (using_twodigit_years
)
686 DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year
));
693 switch (dmy_order
[i
])
696 strings
[i
] = "Month";
710 DEBUG_MSG (("**Order: %s, %s, %s", strings
[0], strings
[1], strings
[2]));
711 DEBUG_MSG (("**Sample date in this locale: `%s'", buf
));
716 g_date_fill_parse_tokens (str
, pt
);
720 g_date_set_parse (GDate
*d
,
724 guint m
= G_DATE_BAD_MONTH
, day
= G_DATE_BAD_DAY
, y
= G_DATE_BAD_YEAR
;
726 g_return_if_fail (d
!= NULL
);
731 G_LOCK (g_date_global
);
733 g_date_prepare_to_parse (str
, &pt
);
735 DEBUG_MSG (("Found %d ints, `%d' `%d' `%d' and written out month %d",
736 pt
.num_ints
, pt
.n
[0], pt
.n
[1], pt
.n
[2], pt
.month
));
739 if (pt
.num_ints
== 4)
741 G_UNLOCK (g_date_global
);
742 return; /* presumably a typo; bail out. */
750 g_assert (pt
.num_ints
< 4); /* i.e., it is 2 or 3 */
752 while (i
< pt
.num_ints
&& j
< 3)
754 switch (dmy_order
[j
])
758 if (pt
.num_ints
== 2 && pt
.month
!= G_DATE_BAD_MONTH
)
761 ++j
; /* skip months, but don't skip this number */
770 if (pt
.num_ints
== 2 && pt
.month
== G_DATE_BAD_MONTH
)
773 ++j
; /* skip days, since we may have month/year */
783 if (locale_era_adjust
!= 0)
785 y
+= locale_era_adjust
;
787 else if (using_twodigit_years
&& y
< 100)
789 guint two
= twodigit_start_year
% 100;
790 guint century
= (twodigit_start_year
/ 100) * 100;
808 if (pt
.num_ints
== 3 && !g_date_valid_dmy (day
, m
, y
))
815 if (using_twodigit_years
&& y
< 100)
816 y
= G_DATE_BAD_YEAR
; /* avoids ambiguity */
818 else if (pt
.num_ints
== 2)
820 if (m
== G_DATE_BAD_MONTH
&& pt
.month
!= G_DATE_BAD_MONTH
)
824 else if (pt
.num_ints
== 1)
826 if (pt
.month
!= G_DATE_BAD_MONTH
)
828 /* Month name and year? */
835 /* Try yyyymmdd and yymmdd */
837 m
= (pt
.n
[0]/100) % 100;
841 /* FIXME move this into a separate function */
842 if (using_twodigit_years
&& y
< 100)
844 guint two
= twodigit_start_year
% 100;
845 guint century
= (twodigit_start_year
/ 100) * 100;
855 /* See if we got anything valid out of all this. */
856 /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
857 if (y
< 8000 && g_date_valid_dmy (day
, m
, y
))
864 #ifdef G_ENABLE_DEBUG
867 DEBUG_MSG (("Rejected DMY %u %u %u", day
, m
, y
));
870 G_UNLOCK (g_date_global
);
876 * @timet: <type>time_t</type> value to set
878 * Sets the value of a date to the date corresponding to a time
879 * specified as a time_t. The time to date conversion is done using
880 * the user's current timezone.
882 * To set the value of a date to the current day, you could write:
884 * g_date_set_time_t (date, time (NULL));
890 g_date_set_time_t (GDate
*date
,
895 g_return_if_fail (date
!= NULL
);
897 #ifdef HAVE_LOCALTIME_R
898 localtime_r (&timet
, &tm
);
901 struct tm
*ptm
= localtime (&timet
);
905 /* Happens at least in Microsoft's C library if you pass a
906 * negative time_t. Use 2000-01-01 as default date.
908 #ifndef G_DISABLE_CHECKS
909 g_return_if_fail_warning (G_LOG_DOMAIN
, "g_date_set_time", "ptm != NULL");
917 memcpy ((void *) &tm
, (void *) ptm
, sizeof(struct tm
));
921 date
->julian
= FALSE
;
923 date
->month
= tm
.tm_mon
+ 1;
924 date
->day
= tm
.tm_mday
;
925 date
->year
= tm
.tm_year
+ 1900;
927 g_return_if_fail (g_date_valid_dmy (date
->day
, date
->month
, date
->year
));
936 * @time_: #GTime value to set.
938 * Sets the value of a date from a #GTime value.
939 * The time to date conversion is done using the user's current timezone.
941 * Deprecated: 2.10: Use g_date_set_time_t() instead.
944 g_date_set_time (GDate
*date
,
947 g_date_set_time_t (date
, (time_t) time_
);
951 * g_date_set_time_val:
953 * @timeval: #GTimeVal value to set
955 * Sets the value of a date from a #GTimeVal value. Note that the
956 * @tv_usec member is ignored, because #GDate can't make use of the
957 * additional precision.
959 * The time to date conversion is done using the user's current timezone.
964 g_date_set_time_val (GDate
*date
,
967 g_date_set_time_t (date
, (time_t) timeval
->tv_sec
);
971 g_date_set_month (GDate
*d
,
974 g_return_if_fail (d
!= NULL
);
975 g_return_if_fail (g_date_valid_month (m
));
977 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
982 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
989 g_date_set_day (GDate
*d
,
992 g_return_if_fail (d
!= NULL
);
993 g_return_if_fail (g_date_valid_day (day
));
995 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
1000 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
1007 g_date_set_year (GDate
*d
,
1010 g_return_if_fail (d
!= NULL
);
1011 g_return_if_fail (g_date_valid_year (y
));
1013 if (d
->julian
&& !d
->dmy
) g_date_update_dmy(d
);
1018 if (g_date_valid_dmy (d
->day
, d
->month
, d
->year
))
1025 g_date_set_dmy (GDate
*d
,
1030 g_return_if_fail (d
!= NULL
);
1031 g_return_if_fail (g_date_valid_dmy (day
, m
, y
));
1043 g_date_set_julian (GDate
*d
,
1046 g_return_if_fail (d
!= NULL
);
1047 g_return_if_fail (g_date_valid_julian (j
));
1056 g_date_is_first_of_month (const GDate
*d
)
1058 g_return_val_if_fail (g_date_valid (d
), FALSE
);
1061 g_date_update_dmy (d
);
1063 g_return_val_if_fail (d
->dmy
, FALSE
);
1065 if (d
->day
== 1) return TRUE
;
1070 g_date_is_last_of_month (const GDate
*d
)
1074 g_return_val_if_fail (g_date_valid (d
), FALSE
);
1077 g_date_update_dmy (d
);
1079 g_return_val_if_fail (d
->dmy
, FALSE
);
1081 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1083 if (d
->day
== days_in_months
[idx
][d
->month
]) return TRUE
;
1088 g_date_add_days (GDate
*d
,
1091 g_return_if_fail (g_date_valid (d
));
1094 g_date_update_julian (d
);
1096 g_return_if_fail (d
->julian
);
1098 d
->julian_days
+= ndays
;
1103 g_date_subtract_days (GDate
*d
,
1106 g_return_if_fail (g_date_valid (d
));
1109 g_date_update_julian (d
);
1111 g_return_if_fail (d
->julian
);
1112 g_return_if_fail (d
->julian_days
> ndays
);
1114 d
->julian_days
-= ndays
;
1119 g_date_add_months (GDate
*d
,
1122 guint years
, months
;
1125 g_return_if_fail (g_date_valid (d
));
1128 g_date_update_dmy (d
);
1130 g_return_if_fail (d
->dmy
);
1132 nmonths
+= d
->month
- 1;
1135 months
= nmonths
%12;
1137 d
->month
= months
+ 1;
1140 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1142 if (d
->day
> days_in_months
[idx
][d
->month
])
1143 d
->day
= days_in_months
[idx
][d
->month
];
1147 g_return_if_fail (g_date_valid (d
));
1151 g_date_subtract_months (GDate
*d
,
1154 guint years
, months
;
1157 g_return_if_fail (g_date_valid (d
));
1160 g_date_update_dmy (d
);
1162 g_return_if_fail (d
->dmy
);
1165 months
= nmonths
%12;
1167 g_return_if_fail (d
->year
> years
);
1171 if (d
->month
> months
) d
->month
-= months
;
1175 d
->month
= 12 - months
;
1179 idx
= g_date_is_leap_year (d
->year
) ? 1 : 0;
1181 if (d
->day
> days_in_months
[idx
][d
->month
])
1182 d
->day
= days_in_months
[idx
][d
->month
];
1186 g_return_if_fail (g_date_valid (d
));
1190 g_date_add_years (GDate
*d
,
1193 g_return_if_fail (g_date_valid (d
));
1196 g_date_update_dmy (d
);
1198 g_return_if_fail (d
->dmy
);
1202 if (d
->month
== 2 && d
->day
== 29)
1204 if (!g_date_is_leap_year (d
->year
))
1212 g_date_subtract_years (GDate
*d
,
1215 g_return_if_fail (g_date_valid (d
));
1218 g_date_update_dmy (d
);
1220 g_return_if_fail (d
->dmy
);
1221 g_return_if_fail (d
->year
> nyears
);
1225 if (d
->month
== 2 && d
->day
== 29)
1227 if (!g_date_is_leap_year (d
->year
))
1235 g_date_is_leap_year (GDateYear year
)
1237 g_return_val_if_fail (g_date_valid_year (year
), FALSE
);
1239 return ( (((year
% 4) == 0) && ((year
% 100) != 0)) ||
1240 (year
% 400) == 0 );
1244 g_date_get_days_in_month (GDateMonth month
,
1249 g_return_val_if_fail (g_date_valid_year (year
), 0);
1250 g_return_val_if_fail (g_date_valid_month (month
), 0);
1252 idx
= g_date_is_leap_year (year
) ? 1 : 0;
1254 return days_in_months
[idx
][month
];
1258 g_date_get_monday_weeks_in_year (GDateYear year
)
1262 g_return_val_if_fail (g_date_valid_year (year
), 0);
1264 g_date_clear (&d
, 1);
1265 g_date_set_dmy (&d
, 1, 1, year
);
1266 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1267 g_date_set_dmy (&d
, 31, 12, year
);
1268 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1269 if (g_date_is_leap_year (year
))
1271 g_date_set_dmy (&d
, 2, 1, year
);
1272 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1273 g_date_set_dmy (&d
, 30, 12, year
);
1274 if (g_date_get_weekday (&d
) == G_DATE_MONDAY
) return 53;
1280 g_date_get_sunday_weeks_in_year (GDateYear year
)
1284 g_return_val_if_fail (g_date_valid_year (year
), 0);
1286 g_date_clear (&d
, 1);
1287 g_date_set_dmy (&d
, 1, 1, year
);
1288 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1289 g_date_set_dmy (&d
, 31, 12, year
);
1290 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1291 if (g_date_is_leap_year (year
))
1293 g_date_set_dmy (&d
, 2, 1, year
);
1294 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1295 g_date_set_dmy (&d
, 30, 12, year
);
1296 if (g_date_get_weekday (&d
) == G_DATE_SUNDAY
) return 53;
1302 g_date_compare (const GDate
*lhs
,
1305 g_return_val_if_fail (lhs
!= NULL
, 0);
1306 g_return_val_if_fail (rhs
!= NULL
, 0);
1307 g_return_val_if_fail (g_date_valid (lhs
), 0);
1308 g_return_val_if_fail (g_date_valid (rhs
), 0);
1310 /* Remember the self-comparison case! I think it works right now. */
1314 if (lhs
->julian
&& rhs
->julian
)
1316 if (lhs
->julian_days
< rhs
->julian_days
) return -1;
1317 else if (lhs
->julian_days
> rhs
->julian_days
) return 1;
1320 else if (lhs
->dmy
&& rhs
->dmy
)
1322 if (lhs
->year
< rhs
->year
) return -1;
1323 else if (lhs
->year
> rhs
->year
) return 1;
1326 if (lhs
->month
< rhs
->month
) return -1;
1327 else if (lhs
->month
> rhs
->month
) return 1;
1330 if (lhs
->day
< rhs
->day
) return -1;
1331 else if (lhs
->day
> rhs
->day
) return 1;
1340 if (!lhs
->julian
) g_date_update_julian (lhs
);
1341 if (!rhs
->julian
) g_date_update_julian (rhs
);
1342 g_return_val_if_fail (lhs
->julian
, 0);
1343 g_return_val_if_fail (rhs
->julian
, 0);
1347 return 0; /* warnings */
1352 g_date_to_struct_tm (const GDate
*d
,
1357 g_return_if_fail (g_date_valid (d
));
1358 g_return_if_fail (tm
!= NULL
);
1361 g_date_update_dmy (d
);
1363 g_return_if_fail (d
->dmy
);
1365 /* zero all the irrelevant fields to be sure they're valid */
1367 /* On Linux and maybe other systems, there are weird non-POSIX
1368 * fields on the end of struct tm that choke strftime if they
1369 * contain garbage. So we need to 0 the entire struct, not just the
1370 * fields we know to exist.
1373 memset (tm
, 0x0, sizeof (struct tm
));
1375 tm
->tm_mday
= d
->day
;
1376 tm
->tm_mon
= d
->month
- 1; /* 0-11 goes in tm */
1377 tm
->tm_year
= ((int)d
->year
) - 1900; /* X/Open says tm_year can be negative */
1379 day
= g_date_get_weekday (d
);
1380 if (day
== 7) day
= 0; /* struct tm wants days since Sunday, so Sunday is 0 */
1382 tm
->tm_wday
= (int)day
;
1384 tm
->tm_yday
= g_date_get_day_of_year (d
) - 1; /* 0 to 365 */
1385 tm
->tm_isdst
= -1; /* -1 means "information not available" */
1389 g_date_clamp (GDate
*date
,
1390 const GDate
*min_date
,
1391 const GDate
*max_date
)
1393 g_return_if_fail (g_date_valid (date
));
1395 if (min_date
!= NULL
)
1396 g_return_if_fail (g_date_valid (min_date
));
1398 if (max_date
!= NULL
)
1399 g_return_if_fail (g_date_valid (max_date
));
1401 if (min_date
!= NULL
&& max_date
!= NULL
)
1402 g_return_if_fail (g_date_compare (min_date
, max_date
) <= 0);
1404 if (min_date
&& g_date_compare (date
, min_date
) < 0)
1407 if (max_date
&& g_date_compare (max_date
, date
) < 0)
1412 g_date_order (GDate
*date1
,
1415 g_return_if_fail (g_date_valid (date1
));
1416 g_return_if_fail (g_date_valid (date2
));
1418 if (g_date_compare (date1
, date2
) > 0)
1428 win32_strftime_helper (const GDate
*d
,
1429 const gchar
*format
,
1430 const struct tm
*tm
,
1434 SYSTEMTIME systemtime
;
1435 TIME_ZONE_INFORMATION tzinfo
;
1441 const wchar_t digits
[] = L
"0123456789";
1446 systemtime
.wYear
= tm
->tm_year
+ 1900;
1447 systemtime
.wMonth
= tm
->tm_mon
+ 1;
1448 systemtime
.wDayOfWeek
= tm
->tm_wday
;
1449 systemtime
.wDay
= tm
->tm_mday
;
1450 systemtime
.wHour
= tm
->tm_hour
;
1451 systemtime
.wMinute
= tm
->tm_min
;
1452 systemtime
.wSecond
= tm
->tm_sec
;
1453 systemtime
.wMilliseconds
= 0;
1455 lcid
= GetThreadLocale ();
1456 result
= g_array_sized_new (FALSE
, FALSE
, sizeof (wchar_t), MAX (128, strlen (format
) * 2));
1461 c
= g_utf8_get_char (p
);
1464 p
= g_utf8_next_char (p
);
1468 g_array_free (result
, TRUE
);
1473 c
= g_utf8_get_char (p
);
1474 if (c
== 'E' || c
== 'O')
1476 /* Ignore modified conversion specifiers for now. */
1477 p
= g_utf8_next_char (p
);
1481 g_array_free (result
, TRUE
);
1486 c
= g_utf8_get_char (p
);
1492 if (systemtime
.wDayOfWeek
== 0)
1495 k
= systemtime
.wDayOfWeek
- 1;
1496 n
= GetLocaleInfoW (lcid
, LOCALE_SABBREVDAYNAME1
+k
, NULL
, 0);
1497 g_array_set_size (result
, result
->len
+ n
);
1498 GetLocaleInfoW (lcid
, LOCALE_SABBREVDAYNAME1
+k
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1499 g_array_set_size (result
, result
->len
- 1);
1502 if (systemtime
.wDayOfWeek
== 0)
1505 k
= systemtime
.wDayOfWeek
- 1;
1506 n
= GetLocaleInfoW (lcid
, LOCALE_SDAYNAME1
+k
, NULL
, 0);
1507 g_array_set_size (result
, result
->len
+ n
);
1508 GetLocaleInfoW (lcid
, LOCALE_SDAYNAME1
+k
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1509 g_array_set_size (result
, result
->len
- 1);
1513 n
= GetLocaleInfoW (lcid
, LOCALE_SABBREVMONTHNAME1
+systemtime
.wMonth
-1, NULL
, 0);
1514 g_array_set_size (result
, result
->len
+ n
);
1515 GetLocaleInfoW (lcid
, LOCALE_SABBREVMONTHNAME1
+systemtime
.wMonth
-1, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1516 g_array_set_size (result
, result
->len
- 1);
1519 n
= GetLocaleInfoW (lcid
, LOCALE_SMONTHNAME1
+systemtime
.wMonth
-1, NULL
, 0);
1520 g_array_set_size (result
, result
->len
+ n
);
1521 GetLocaleInfoW (lcid
, LOCALE_SMONTHNAME1
+systemtime
.wMonth
-1, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1522 g_array_set_size (result
, result
->len
- 1);
1525 n
= GetDateFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
1528 g_array_set_size (result
, result
->len
+ n
);
1529 GetDateFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1530 g_array_set_size (result
, result
->len
- 1);
1532 g_array_append_vals (result
, L
" ", 1);
1533 n
= GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
1536 g_array_set_size (result
, result
->len
+ n
);
1537 GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1538 g_array_set_size (result
, result
->len
- 1);
1542 g_array_append_vals (result
, digits
+ systemtime
.wYear
/1000, 1);
1543 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/1000)%10, 1);
1546 g_array_append_vals (result
, digits
+ systemtime
.wDay
/10, 1);
1547 g_array_append_vals (result
, digits
+ systemtime
.wDay
%10, 1);
1550 g_array_append_vals (result
, digits
+ systemtime
.wMonth
/10, 1);
1551 g_array_append_vals (result
, digits
+ systemtime
.wMonth
%10, 1);
1552 g_array_append_vals (result
, L
"/", 1);
1553 g_array_append_vals (result
, digits
+ systemtime
.wDay
/10, 1);
1554 g_array_append_vals (result
, digits
+ systemtime
.wDay
%10, 1);
1555 g_array_append_vals (result
, L
"/", 1);
1556 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/10)%10, 1);
1557 g_array_append_vals (result
, digits
+ systemtime
.wYear
%10, 1);
1560 if (systemtime
.wDay
>= 10)
1561 g_array_append_vals (result
, digits
+ systemtime
.wDay
/10, 1);
1563 g_array_append_vals (result
, L
" ", 1);
1564 g_array_append_vals (result
, digits
+ systemtime
.wDay
%10, 1);
1567 /* A GDate has no time fields, so for now we can
1568 * hardcode all time conversions into zeros (or 12 for
1569 * %I). The alternative code snippets in the #else
1570 * branches are here ready to be taken into use when
1571 * needed by a g_strftime() or g_date_and_time_format()
1576 g_array_append_vals (result
, L
"00", 2);
1578 g_array_append_vals (result
, digits
+ systemtime
.wHour
/10, 1);
1579 g_array_append_vals (result
, digits
+ systemtime
.wHour
%10, 1);
1584 g_array_append_vals (result
, L
"12", 2);
1586 if (systemtime
.wHour
== 0)
1587 g_array_append_vals (result
, L
"12", 2);
1590 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)/10, 1);
1591 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)%10, 1);
1596 g_array_append_vals (result
, digits
+ (tm
->tm_yday
+1)/100, 1);
1597 g_array_append_vals (result
, digits
+ ((tm
->tm_yday
+1)/10)%10, 1);
1598 g_array_append_vals (result
, digits
+ (tm
->tm_yday
+1)%10, 1);
1601 g_array_append_vals (result
, digits
+ systemtime
.wMonth
/10, 1);
1602 g_array_append_vals (result
, digits
+ systemtime
.wMonth
%10, 1);
1606 g_array_append_vals (result
, L
"00", 2);
1608 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
1609 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
1613 g_array_append_vals (result
, L
"\n", 1);
1616 n
= GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", NULL
, 0);
1619 g_array_set_size (result
, result
->len
+ n
);
1620 GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1621 g_array_set_size (result
, result
->len
- 1);
1625 /* This is a rather odd format. Hard to say what to do.
1626 * Let's always use the POSIX %I:%M:%S %p
1629 g_array_append_vals (result
, L
"12:00:00", 8);
1631 if (systemtime
.wHour
== 0)
1632 g_array_append_vals (result
, L
"12", 2);
1635 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)/10, 1);
1636 g_array_append_vals (result
, digits
+ (systemtime
.wHour
%12)%10, 1);
1638 g_array_append_vals (result
, L
":", 1);
1639 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
1640 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
1641 g_array_append_vals (result
, L
":", 1);
1642 g_array_append_vals (result
, digits
+ systemtime
.wSecond
/10, 1);
1643 g_array_append_vals (result
, digits
+ systemtime
.wSecond
%10, 1);
1644 g_array_append_vals (result
, L
" ", 1);
1646 n
= GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", NULL
, 0);
1649 g_array_set_size (result
, result
->len
+ n
);
1650 GetTimeFormatW (lcid
, 0, &systemtime
, L
"tt", ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1651 g_array_set_size (result
, result
->len
- 1);
1656 g_array_append_vals (result
, L
"00:00", 5);
1658 g_array_append_vals (result
, digits
+ systemtime
.wHour
/10, 1);
1659 g_array_append_vals (result
, digits
+ systemtime
.wHour
%10, 1);
1660 g_array_append_vals (result
, L
":", 1);
1661 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
1662 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
1667 g_array_append_vals (result
, L
"00", 2);
1669 g_array_append_vals (result
, digits
+ systemtime
.wSecond
/10, 1);
1670 g_array_append_vals (result
, digits
+ systemtime
.wSecond
%10, 1);
1674 g_array_append_vals (result
, L
"\t", 1);
1678 g_array_append_vals (result
, L
"00:00:00", 8);
1680 g_array_append_vals (result
, digits
+ systemtime
.wHour
/10, 1);
1681 g_array_append_vals (result
, digits
+ systemtime
.wHour
%10, 1);
1682 g_array_append_vals (result
, L
":", 1);
1683 g_array_append_vals (result
, digits
+ systemtime
.wMinute
/10, 1);
1684 g_array_append_vals (result
, digits
+ systemtime
.wMinute
%10, 1);
1685 g_array_append_vals (result
, L
":", 1);
1686 g_array_append_vals (result
, digits
+ systemtime
.wSecond
/10, 1);
1687 g_array_append_vals (result
, digits
+ systemtime
.wSecond
%10, 1);
1691 if (systemtime
.wDayOfWeek
== 0)
1692 g_array_append_vals (result
, L
"7", 1);
1694 g_array_append_vals (result
, digits
+ systemtime
.wDayOfWeek
, 1);
1697 n
= g_date_get_sunday_week_of_year (d
);
1698 g_array_append_vals (result
, digits
+ n
/10, 1);
1699 g_array_append_vals (result
, digits
+ n
%10, 1);
1702 n
= g_date_get_iso8601_week_of_year (d
);
1703 g_array_append_vals (result
, digits
+ n
/10, 1);
1704 g_array_append_vals (result
, digits
+ n
%10, 1);
1707 g_array_append_vals (result
, digits
+ systemtime
.wDayOfWeek
, 1);
1710 n
= g_date_get_monday_week_of_year (d
);
1711 g_array_append_vals (result
, digits
+ n
/10, 1);
1712 g_array_append_vals (result
, digits
+ n
%10, 1);
1715 n
= GetDateFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
1718 g_array_set_size (result
, result
->len
+ n
);
1719 GetDateFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1720 g_array_set_size (result
, result
->len
- 1);
1724 n
= GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, NULL
, 0);
1727 g_array_set_size (result
, result
->len
+ n
);
1728 GetTimeFormatW (lcid
, 0, &systemtime
, NULL
, ((wchar_t *) result
->data
) + result
->len
- n
, n
);
1729 g_array_set_size (result
, result
->len
- 1);
1733 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/10)%10, 1);
1734 g_array_append_vals (result
, digits
+ systemtime
.wYear
%10, 1);
1737 g_array_append_vals (result
, digits
+ systemtime
.wYear
/1000, 1);
1738 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/100)%10, 1);
1739 g_array_append_vals (result
, digits
+ (systemtime
.wYear
/10)%10, 1);
1740 g_array_append_vals (result
, digits
+ systemtime
.wYear
%10, 1);
1743 n
= GetTimeZoneInformation (&tzinfo
);
1744 if (n
== TIME_ZONE_ID_UNKNOWN
)
1746 else if (n
== TIME_ZONE_ID_STANDARD
)
1747 g_array_append_vals (result
, tzinfo
.StandardName
, wcslen (tzinfo
.StandardName
));
1748 else if (n
== TIME_ZONE_ID_DAYLIGHT
)
1749 g_array_append_vals (result
, tzinfo
.DaylightName
, wcslen (tzinfo
.DaylightName
));
1752 g_array_append_vals (result
, L
"%", 1);
1756 else if (c
<= 0xFFFF)
1759 g_array_append_vals (result
, &wc
, 1);
1766 ws
= g_ucs4_to_utf16 (&c
, 1, NULL
, &nwc
, NULL
);
1767 g_array_append_vals (result
, ws
, nwc
);
1770 p
= g_utf8_next_char (p
);
1773 convbuf
= g_utf16_to_utf8 ((wchar_t *) result
->data
, result
->len
, NULL
, &convlen
, NULL
);
1774 g_array_free (result
, TRUE
);
1782 if (slen
<= convlen
)
1784 /* Ensure only whole characters are copied into the buffer. */
1785 gchar
*end
= g_utf8_find_prev_char (convbuf
, convbuf
+ slen
);
1786 g_assert (end
!= NULL
);
1787 convlen
= end
- convbuf
;
1789 /* Return 0 because the buffer isn't large enough. */
1795 memcpy (s
, convbuf
, convlen
);
1805 g_date_strftime (gchar
*s
,
1807 const gchar
*format
,
1812 gsize locale_format_len
= 0;
1813 gchar
*locale_format
;
1819 GError
*error
= NULL
;
1823 g_return_val_if_fail (g_date_valid (d
), 0);
1824 g_return_val_if_fail (slen
> 0, 0);
1825 g_return_val_if_fail (format
!= NULL
, 0);
1826 g_return_val_if_fail (s
!= NULL
, 0);
1828 g_date_to_struct_tm (d
, &tm
);
1831 if (!g_utf8_validate (format
, -1, NULL
))
1836 return win32_strftime_helper (d
, format
, &tm
, s
, slen
);
1839 locale_format
= g_locale_from_utf8 (format
, -1, NULL
, &locale_format_len
, &error
);
1843 g_warning (G_STRLOC
"Error converting format to locale encoding: %s\n", error
->message
);
1844 g_error_free (error
);
1850 tmpbufsize
= MAX (128, locale_format_len
* 2);
1853 tmpbuf
= g_malloc (tmpbufsize
);
1855 /* Set the first byte to something other than '\0', to be able to
1856 * recognize whether strftime actually failed or just returned "".
1859 tmplen
= strftime (tmpbuf
, tmpbufsize
, locale_format
, &tm
);
1861 if (tmplen
== 0 && tmpbuf
[0] != '\0')
1866 if (tmpbufsize
> 65536)
1868 g_warning (G_STRLOC
"Maximum buffer size for g_date_strftime exceeded: giving up\n");
1869 g_free (locale_format
);
1878 g_free (locale_format
);
1880 convbuf
= g_locale_to_utf8 (tmpbuf
, tmplen
, NULL
, &convlen
, &error
);
1885 g_warning (G_STRLOC
"Error converting results of strftime to UTF-8: %s\n", error
->message
);
1886 g_error_free (error
);
1892 if (slen
<= convlen
)
1894 /* Ensure only whole characters are copied into the buffer.
1896 gchar
*end
= g_utf8_find_prev_char (convbuf
, convbuf
+ slen
);
1897 g_assert (end
!= NULL
);
1898 convlen
= end
- convbuf
;
1900 /* Return 0 because the buffer isn't large enough.
1907 memcpy (s
, convbuf
, convlen
);