Branch data Line data Source code
1 : : /* GLIB - Library of useful routines for C programming
2 : : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : */
19 : :
20 : : /*
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/.
25 : : */
26 : :
27 : : /*
28 : : * MT safe
29 : : */
30 : :
31 : : #include "config.h"
32 : : #include "glibconfig.h"
33 : :
34 : : #define DEBUG_MSG(x) /* */
35 : : #ifdef G_ENABLE_DEBUG
36 : : /* #define DEBUG_MSG(args) g_message args ; */
37 : : #endif
38 : :
39 : : #include <time.h>
40 : : #include <string.h>
41 : : #include <stdlib.h>
42 : : #include <locale.h>
43 : :
44 : : #ifdef G_OS_WIN32
45 : : #include <windows.h>
46 : : #endif
47 : :
48 : : #include "gdate.h"
49 : :
50 : : #include "gconvert.h"
51 : : #include "gmem.h"
52 : : #include "gstrfuncs.h"
53 : : #include "gtestutils.h"
54 : : #include "gthread.h"
55 : : #include "gunicode.h"
56 : : #include "gutilsprivate.h"
57 : :
58 : : #ifdef G_OS_WIN32
59 : : #include "garray.h"
60 : : #endif
61 : :
62 : : /**
63 : : * GDate:
64 : : * @julian_days: the Julian representation of the date
65 : : * @julian: this bit is set if @julian_days is valid
66 : : * @dmy: this is set if @day, @month and @year are valid
67 : : * @day: the day of the day-month-year representation of the date,
68 : : * as a number between 1 and 31
69 : : * @month: the month of the day-month-year representation of the date,
70 : : * as a number between 1 and 12
71 : : * @year: the year of the day-month-year representation of the date
72 : : *
73 : : * `GDate` is a struct for calendrical calculations.
74 : : *
75 : : * The `GDate` data structure represents a day between January 1, Year 1,
76 : : * and sometime a few thousand years in the future (right now it will go
77 : : * to the year 65535 or so, but [method@GLib.Date.set_parse] only parses up to the
78 : : * year 8000 or so - just count on "a few thousand"). `GDate` is meant to
79 : : * represent everyday dates, not astronomical dates or historical dates
80 : : * or ISO timestamps or the like. It extrapolates the current Gregorian
81 : : * calendar forward and backward in time; there is no attempt to change
82 : : * the calendar to match time periods or locations. `GDate` does not store
83 : : * time information; it represents a day.
84 : : *
85 : : * The `GDate` implementation has several nice features; it is only a
86 : : * 64-bit struct, so storing large numbers of dates is very efficient. It
87 : : * can keep both a Julian and day-month-year representation of the date,
88 : : * since some calculations are much easier with one representation or the
89 : : * other. A Julian representation is simply a count of days since some
90 : : * fixed day in the past; for #GDate the fixed day is January 1, 1 AD.
91 : : * ("Julian" dates in the #GDate API aren't really Julian dates in the
92 : : * technical sense; technically, Julian dates count from the start of the
93 : : * Julian period, Jan 1, 4713 BC).
94 : : *
95 : : * `GDate` is simple to use. First you need a "blank" date; you can get a
96 : : * dynamically allocated date from [ctor@GLib.Date.new], or you can declare an
97 : : * automatic variable or array and initialize it by calling [method@GLib.Date.clear].
98 : : * A cleared date is safe; it's safe to call [method@GLib.Date.set_dmy] and the other
99 : : * mutator functions to initialize the value of a cleared date. However, a cleared date
100 : : * is initially invalid, meaning that it doesn't represent a day that exists.
101 : : * It is undefined to call any of the date calculation routines on an invalid date.
102 : : * If you obtain a date from a user or other unpredictable source, you should check
103 : : * its validity with the [method@GLib.Date.valid] predicate. [method@GLib.Date.valid]
104 : : * is also used to check for errors with [method@GLib.Date.set_parse] and other functions
105 : : * that can fail. Dates can be invalidated by calling [method@GLib.Date.clear] again.
106 : : *
107 : : * It is very important to use the API to access the `GDate` struct. Often only the
108 : : * day-month-year or only the Julian representation is valid. Sometimes neither is valid.
109 : : * Use the API.
110 : : *
111 : : * GLib also features `GDateTime` which represents a precise time.
112 : : */
113 : :
114 : : /**
115 : : * G_USEC_PER_SEC:
116 : : *
117 : : * Number of microseconds in one second (1 million).
118 : : * This macro is provided for code readability.
119 : : */
120 : :
121 : : /**
122 : : * GTimeVal:
123 : : * @tv_sec: seconds
124 : : * @tv_usec: microseconds
125 : : *
126 : : * Represents a precise time, with seconds and microseconds.
127 : : *
128 : : * Similar to the struct timeval returned by the `gettimeofday()`
129 : : * UNIX system call.
130 : : *
131 : : * GLib is attempting to unify around the use of 64-bit integers to
132 : : * represent microsecond-precision time. As such, this type will be
133 : : * removed from a future version of GLib. A consequence of using `glong` for
134 : : * `tv_sec` is that on 32-bit systems `GTimeVal` is subject to the year 2038
135 : : * problem.
136 : : *
137 : : * Deprecated: 2.62: Use #GDateTime or #guint64 instead.
138 : : */
139 : :
140 : : /**
141 : : * GTime:
142 : : *
143 : : * Simply a replacement for `time_t`. It has been deprecated
144 : : * since it is not equivalent to `time_t` on 64-bit platforms
145 : : * with a 64-bit `time_t`.
146 : : *
147 : : * Unrelated to #GTimer.
148 : : *
149 : : * Note that #GTime is defined to always be a 32-bit integer,
150 : : * unlike `time_t` which may be 64-bit on some systems. Therefore,
151 : : * #GTime will overflow in the year 2038, and you cannot use the
152 : : * address of a #GTime variable as argument to the UNIX time()
153 : : * function.
154 : : *
155 : : * Instead, do the following:
156 : : *
157 : : * |[<!-- language="C" -->
158 : : * time_t ttime;
159 : : * GTime gtime;
160 : : *
161 : : * time (&ttime);
162 : : * gtime = (GTime)ttime;
163 : : * ]|
164 : : *
165 : : * Deprecated: 2.62: This is not [Y2038-safe](https://en.wikipedia.org/wiki/Year_2038_problem).
166 : : * Use #GDateTime or #time_t instead.
167 : : */
168 : :
169 : : /**
170 : : * GDateDMY:
171 : : * @G_DATE_DAY: a day
172 : : * @G_DATE_MONTH: a month
173 : : * @G_DATE_YEAR: a year
174 : : *
175 : : * This enumeration isn't used in the API, but may be useful if you need
176 : : * to mark a number as a day, month, or year.
177 : : */
178 : :
179 : : /**
180 : : * GDateDay:
181 : : *
182 : : * Integer representing a day of the month; between 1 and 31.
183 : : *
184 : : * The %G_DATE_BAD_DAY value represents an invalid day of the month.
185 : : */
186 : :
187 : : /**
188 : : * GDateMonth:
189 : : * @G_DATE_BAD_MONTH: invalid value
190 : : * @G_DATE_JANUARY: January
191 : : * @G_DATE_FEBRUARY: February
192 : : * @G_DATE_MARCH: March
193 : : * @G_DATE_APRIL: April
194 : : * @G_DATE_MAY: May
195 : : * @G_DATE_JUNE: June
196 : : * @G_DATE_JULY: July
197 : : * @G_DATE_AUGUST: August
198 : : * @G_DATE_SEPTEMBER: September
199 : : * @G_DATE_OCTOBER: October
200 : : * @G_DATE_NOVEMBER: November
201 : : * @G_DATE_DECEMBER: December
202 : : *
203 : : * Enumeration representing a month; values are %G_DATE_JANUARY,
204 : : * %G_DATE_FEBRUARY, etc. %G_DATE_BAD_MONTH is the invalid value.
205 : : */
206 : :
207 : : /**
208 : : * GDateYear:
209 : : *
210 : : * Integer type representing a year.
211 : : *
212 : : * The %G_DATE_BAD_YEAR value is the invalid value. The year
213 : : * must be 1 or higher; negative ([BCE](https://en.wikipedia.org/wiki/Common_Era))
214 : : * years are not allowed.
215 : : *
216 : : * The year is represented with four digits.
217 : : */
218 : :
219 : : /**
220 : : * GDateWeekday:
221 : : * @G_DATE_BAD_WEEKDAY: invalid value
222 : : * @G_DATE_MONDAY: Monday
223 : : * @G_DATE_TUESDAY: Tuesday
224 : : * @G_DATE_WEDNESDAY: Wednesday
225 : : * @G_DATE_THURSDAY: Thursday
226 : : * @G_DATE_FRIDAY: Friday
227 : : * @G_DATE_SATURDAY: Saturday
228 : : * @G_DATE_SUNDAY: Sunday
229 : : *
230 : : * Enumeration representing a day of the week; %G_DATE_MONDAY,
231 : : * %G_DATE_TUESDAY, etc. %G_DATE_BAD_WEEKDAY is an invalid weekday.
232 : : */
233 : :
234 : : /**
235 : : * G_DATE_BAD_DAY:
236 : : *
237 : : * Represents an invalid #GDateDay.
238 : : */
239 : :
240 : : /**
241 : : * G_DATE_BAD_JULIAN:
242 : : *
243 : : * Represents an invalid Julian day number.
244 : : */
245 : :
246 : : /**
247 : : * G_DATE_BAD_YEAR:
248 : : *
249 : : * Represents an invalid year.
250 : : */
251 : :
252 : : /**
253 : : * g_date_new:
254 : : *
255 : : * Allocates a #GDate and initializes
256 : : * it to a safe state. The new date will
257 : : * be cleared (as if you'd called g_date_clear()) but invalid (it won't
258 : : * represent an existing day). Free the return value with g_date_free().
259 : : *
260 : : * Returns: a newly-allocated #GDate
261 : : */
262 : : GDate*
263 : 26 : g_date_new (void)
264 : : {
265 : 26 : GDate *d = g_new0 (GDate, 1); /* happily, 0 is the invalid flag for everything. */
266 : :
267 : 26 : return d;
268 : : }
269 : :
270 : : /**
271 : : * g_date_new_dmy:
272 : : * @day: day of the month
273 : : * @month: month of the year
274 : : * @year: year
275 : : *
276 : : * Create a new #GDate representing the given day-month-year triplet.
277 : : *
278 : : * The triplet you pass in must represent a valid date. Use g_date_valid_dmy()
279 : : * if needed to validate it. The returned #GDate is guaranteed to be non-%NULL
280 : : * and valid.
281 : : *
282 : : * Returns: (transfer full) (not nullable): a newly-allocated #GDate
283 : : * initialized with @day, @month, and @year
284 : : */
285 : : GDate*
286 : 10 : g_date_new_dmy (GDateDay day,
287 : : GDateMonth m,
288 : : GDateYear y)
289 : : {
290 : : GDate *d;
291 : 10 : g_return_val_if_fail (g_date_valid_dmy (day, m, y), NULL);
292 : :
293 : 9 : d = g_new (GDate, 1);
294 : :
295 : 9 : d->julian = FALSE;
296 : 9 : d->dmy = TRUE;
297 : :
298 : 9 : d->month = m;
299 : 9 : d->day = day;
300 : 9 : d->year = y;
301 : :
302 : 9 : g_assert (g_date_valid (d));
303 : :
304 : 9 : return d;
305 : : }
306 : :
307 : : /**
308 : : * g_date_new_julian:
309 : : * @julian_day: days since January 1, Year 1
310 : : *
311 : : * Create a new #GDate representing the given Julian date.
312 : : *
313 : : * The @julian_day you pass in must be valid. Use g_date_valid_julian() if
314 : : * needed to validate it. The returned #GDate is guaranteed to be non-%NULL and
315 : : * valid.
316 : : *
317 : : * Returns: (transfer full) (not nullable): a newly-allocated #GDate initialized
318 : : * with @julian_day
319 : : */
320 : : GDate*
321 : 20 : g_date_new_julian (guint32 julian_day)
322 : : {
323 : : GDate *d;
324 : 20 : g_return_val_if_fail (g_date_valid_julian (julian_day), NULL);
325 : :
326 : 19 : d = g_new (GDate, 1);
327 : :
328 : 19 : d->julian = TRUE;
329 : 19 : d->dmy = FALSE;
330 : :
331 : 19 : d->julian_days = julian_day;
332 : :
333 : 19 : g_assert (g_date_valid (d));
334 : :
335 : 19 : return d;
336 : : }
337 : :
338 : : /**
339 : : * g_date_free:
340 : : * @date: a #GDate to free
341 : : *
342 : : * Frees a #GDate returned from g_date_new().
343 : : */
344 : : void
345 : 55 : g_date_free (GDate *date)
346 : : {
347 : 55 : g_return_if_fail (date != NULL);
348 : :
349 : 54 : g_free (date);
350 : : }
351 : :
352 : : /**
353 : : * g_date_copy:
354 : : * @date: a #GDate to copy
355 : : *
356 : : * Copies a GDate to a newly-allocated GDate. If the input was invalid
357 : : * (as determined by g_date_valid()), the invalid state will be copied
358 : : * as is into the new object.
359 : : *
360 : : * Returns: (transfer full): a newly-allocated #GDate initialized from @date
361 : : *
362 : : * Since: 2.56
363 : : */
364 : : GDate *
365 : 5 : g_date_copy (const GDate *date)
366 : : {
367 : : GDate *res;
368 : 5 : g_return_val_if_fail (date != NULL, NULL);
369 : :
370 [ + + ]: 4 : if (g_date_valid (date))
371 : 2 : res = g_date_new_julian (g_date_get_julian (date));
372 : : else
373 : : {
374 : 2 : res = g_date_new ();
375 : 2 : *res = *date;
376 : : }
377 : :
378 : 4 : return res;
379 : : }
380 : :
381 : : /**
382 : : * g_date_valid:
383 : : * @date: a #GDate to check
384 : : *
385 : : * Returns %TRUE if the #GDate represents an existing day. The date must not
386 : : * contain garbage; it should have been initialized with g_date_clear()
387 : : * if it wasn't allocated by one of the g_date_new() variants.
388 : : *
389 : : * Returns: Whether the date is valid
390 : : */
391 : : gboolean
392 : 37115546 : g_date_valid (const GDate *d)
393 : : {
394 : 37115546 : g_return_val_if_fail (d != NULL, FALSE);
395 : :
396 [ + + + + ]: 37115545 : return (d->julian || d->dmy);
397 : : }
398 : :
399 : : static const guint8 days_in_months[2][13] =
400 : : { /* error, jan feb mar apr may jun jul aug sep oct nov dec */
401 : : { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
402 : : { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } /* leap year */
403 : : };
404 : :
405 : : static const guint16 days_in_year[2][14] =
406 : : { /* 0, jan feb mar apr may jun jul aug sep oct nov dec */
407 : : { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
408 : : { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
409 : : };
410 : :
411 : : /**
412 : : * g_date_valid_month:
413 : : * @month: month
414 : : *
415 : : * Returns %TRUE if the month value is valid. The 12 #GDateMonth
416 : : * enumeration values are the only valid months.
417 : : *
418 : : * Returns: %TRUE if the month is valid
419 : : */
420 : : gboolean
421 : 26333 : g_date_valid_month (GDateMonth m)
422 : : {
423 [ + + + + ]: 26333 : return (((gint) m > G_DATE_BAD_MONTH) && ((gint) m < 13));
424 : : }
425 : :
426 : : /**
427 : : * g_date_valid_year:
428 : : * @year: year
429 : : *
430 : : * Returns %TRUE if the year is valid. Any year greater than 0 is valid,
431 : : * though there is a 16-bit limit to what #GDate will understand.
432 : : *
433 : : * Returns: %TRUE if the year is valid
434 : : */
435 : : gboolean
436 : 12818898 : g_date_valid_year (GDateYear y)
437 : : {
438 : 12818898 : return ( y > G_DATE_BAD_YEAR );
439 : : }
440 : :
441 : : /**
442 : : * g_date_valid_day:
443 : : * @day: day to check
444 : : *
445 : : * Returns %TRUE if the day of the month is valid (a day is valid if it's
446 : : * between 1 and 31 inclusive).
447 : : *
448 : : * Returns: %TRUE if the day is valid
449 : : */
450 : :
451 : : gboolean
452 : 291601 : g_date_valid_day (GDateDay d)
453 : : {
454 [ + + + + ]: 291601 : return ( (d > G_DATE_BAD_DAY) && (d < 32) );
455 : : }
456 : :
457 : : /**
458 : : * g_date_valid_weekday:
459 : : * @weekday: weekday
460 : : *
461 : : * Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration
462 : : * values are the only valid weekdays.
463 : : *
464 : : * Returns: %TRUE if the weekday is valid
465 : : */
466 : : gboolean
467 : 3 : g_date_valid_weekday (GDateWeekday w)
468 : : {
469 [ + + + + ]: 3 : return (((gint) w > G_DATE_BAD_WEEKDAY) && ((gint) w < 8));
470 : : }
471 : :
472 : : /**
473 : : * g_date_valid_julian:
474 : : * @julian_date: Julian day to check
475 : : *
476 : : * Returns %TRUE if the Julian day is valid. Anything greater than zero
477 : : * is basically a valid Julian, though there is a 32-bit limit.
478 : : *
479 : : * Returns: %TRUE if the Julian day is valid
480 : : */
481 : : gboolean
482 : 5060561 : g_date_valid_julian (guint32 j)
483 : : {
484 : 5060561 : return (j > G_DATE_BAD_JULIAN);
485 : : }
486 : :
487 : : /**
488 : : * g_date_valid_dmy:
489 : : * @day: day
490 : : * @month: month
491 : : * @year: year
492 : : *
493 : : * Returns %TRUE if the day-month-year triplet forms a valid, existing day
494 : : * in the range of days #GDate understands (Year 1 or later, no more than
495 : : * a few thousand years in the future).
496 : : *
497 : : * Returns: %TRUE if the date is a valid one
498 : : */
499 : : gboolean
500 : 5793277 : g_date_valid_dmy (GDateDay d,
501 : : GDateMonth m,
502 : : GDateYear y)
503 : : {
504 : : /* No need to check the upper bound of @y, because #GDateYear is 16 bits wide,
505 : : * just like #GDate.year. */
506 [ + + ]: 5793273 : return ( (m > G_DATE_BAD_MONTH) &&
507 [ + - ]: 5793268 : (m < 13) &&
508 [ + + ]: 5793268 : (d > G_DATE_BAD_DAY) &&
509 [ + + + + ]: 11586550 : (y > G_DATE_BAD_YEAR) && /* must check before using g_date_is_leap_year */
510 : 5793266 : (d <= (g_date_is_leap_year (y) ?
511 [ + + ]: 5793266 : days_in_months[1][m] : days_in_months[0][m])) );
512 : : }
513 : :
514 : :
515 : : /* "Julian days" just means an absolute number of days, where Day 1 ==
516 : : * Jan 1, Year 1
517 : : */
518 : : static void
519 : 3479482 : g_date_update_julian (const GDate *const_d)
520 : : {
521 : 3479482 : GDate *d = (GDate *) const_d;
522 : : GDateYear year;
523 : : gint idx;
524 : :
525 : 3479482 : g_return_if_fail (d != NULL);
526 : 3479482 : g_return_if_fail (d->dmy != 0);
527 : 3479482 : g_return_if_fail (!d->julian);
528 : 3479482 : g_return_if_fail (g_date_valid_dmy (d->day, d->month, d->year));
529 : :
530 : : /* What we actually do is: multiply years * 365 days in the year,
531 : : * add the number of years divided by 4, subtract the number of
532 : : * years divided by 100 and add the number of years divided by 400,
533 : : * which accounts for leap year stuff. Code from Steffen Beyer's
534 : : * DateCalc.
535 : : */
536 : :
537 : 3479482 : year = d->year - 1; /* we know d->year > 0 since it's valid */
538 : :
539 : 3479482 : d->julian_days = year * 365U;
540 : 3479482 : d->julian_days += (year >>= 2); /* divide by 4 and add */
541 : 3479482 : d->julian_days -= (year /= 25); /* divides original # years by 100 */
542 : 3479482 : d->julian_days += year >> 2; /* divides by 4, which divides original by 400 */
543 : :
544 : 3479482 : idx = g_date_is_leap_year (d->year) ? 1 : 0;
545 : :
546 : 3479482 : d->julian_days += days_in_year[idx][d->month] + d->day;
547 : :
548 : 3479482 : g_return_if_fail (g_date_valid_julian (d->julian_days));
549 : :
550 : 3479482 : d->julian = TRUE;
551 : : }
552 : :
553 : : static void
554 : 1581039 : g_date_update_dmy (const GDate *const_d)
555 : : {
556 : 1581039 : GDate *d = (GDate *) const_d;
557 : : GDateYear y;
558 : : GDateMonth m;
559 : : GDateDay day;
560 : :
561 : : guint32 A, B, C, D, E, M;
562 : :
563 : 1581039 : g_return_if_fail (d != NULL);
564 : 1581039 : g_return_if_fail (d->julian);
565 : 1581039 : g_return_if_fail (!d->dmy);
566 : 1581039 : g_return_if_fail (g_date_valid_julian (d->julian_days));
567 : :
568 : : /* Formula taken from the Calendar FAQ; the formula was for the
569 : : * Julian Period which starts on 1 January 4713 BC, so we add
570 : : * 1,721,425 to the number of days before doing the formula.
571 : : *
572 : : * I'm sure this can be simplified for our 1 January 1 AD period
573 : : * start, but I can't figure out how to unpack the formula.
574 : : */
575 : :
576 : 1581039 : A = d->julian_days + 1721425 + 32045;
577 : 1581039 : B = ( 4 *(A + 36524) )/ 146097 - 1;
578 : 1581039 : C = A - (146097 * B)/4;
579 : 1581039 : D = ( 4 * (C + 365) ) / 1461 - 1;
580 : 1581039 : E = C - ((1461*D) / 4);
581 : 1581039 : M = (5 * (E - 1) + 2)/153;
582 : :
583 : 1581039 : m = M + 3 - (12*(M/10));
584 : 1581039 : day = E - (153*M + 2)/5;
585 : 1581039 : y = 100 * B + D - 4800 + (M/10);
586 : :
587 : : #ifdef G_ENABLE_DEBUG
588 [ - + ]: 1581039 : if (!g_date_valid_dmy (day, m, y))
589 : 0 : g_warning ("OOPS julian: %u computed dmy: %u %u %u",
590 : : d->julian_days, day, m, y);
591 : : #endif
592 : :
593 : 1581039 : d->month = m;
594 : 1581039 : d->day = day;
595 : 1581039 : d->year = y;
596 : :
597 : 1581039 : d->dmy = TRUE;
598 : : }
599 : :
600 : : /**
601 : : * g_date_get_weekday:
602 : : * @date: a #GDate
603 : : *
604 : : * Returns the day of the week for a #GDate. The date must be valid.
605 : : *
606 : : * Returns: day of the week as a #GDateWeekday.
607 : : */
608 : : GDateWeekday
609 : 404371 : g_date_get_weekday (const GDate *d)
610 : : {
611 : 404371 : g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_WEEKDAY);
612 : :
613 [ + + ]: 404370 : if (!d->julian)
614 : 328186 : g_date_update_julian (d);
615 : :
616 : 404370 : g_return_val_if_fail (d->julian, G_DATE_BAD_WEEKDAY);
617 : :
618 : 404370 : return ((d->julian_days - 1) % 7) + 1;
619 : : }
620 : :
621 : : /**
622 : : * g_date_get_month:
623 : : * @date: a #GDate to get the month from
624 : : *
625 : : * Returns the month of the year. The date must be valid.
626 : : *
627 : : * Returns: month of the year as a #GDateMonth
628 : : */
629 : : GDateMonth
630 : 4709569 : g_date_get_month (const GDate *d)
631 : : {
632 : 4709569 : g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_MONTH);
633 : :
634 [ + + ]: 4709568 : if (!d->dmy)
635 : 13 : g_date_update_dmy (d);
636 : :
637 : 4709568 : g_return_val_if_fail (d->dmy, G_DATE_BAD_MONTH);
638 : :
639 : 4709568 : return d->month;
640 : : }
641 : :
642 : : /**
643 : : * g_date_get_year:
644 : : * @date: a #GDate
645 : : *
646 : : * Returns the year of a #GDate. The date must be valid.
647 : : *
648 : : * Returns: year in which the date falls
649 : : */
650 : : GDateYear
651 : 4709557 : g_date_get_year (const GDate *d)
652 : : {
653 : 4709557 : g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_YEAR);
654 : :
655 [ + + ]: 4709556 : if (!d->dmy)
656 : 1 : g_date_update_dmy (d);
657 : :
658 : 4709556 : g_return_val_if_fail (d->dmy, G_DATE_BAD_YEAR);
659 : :
660 : 4709556 : return d->year;
661 : : }
662 : :
663 : : /**
664 : : * g_date_get_day:
665 : : * @date: a #GDate to extract the day of the month from
666 : : *
667 : : * Returns the day of the month. The date must be valid.
668 : : *
669 : : * Returns: day of the month
670 : : */
671 : : GDateDay
672 : 4441803 : g_date_get_day (const GDate *d)
673 : : {
674 : 4441803 : g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_DAY);
675 : :
676 [ + + ]: 4441802 : if (!d->dmy)
677 : 1581009 : g_date_update_dmy (d);
678 : :
679 : 4441802 : g_return_val_if_fail (d->dmy, G_DATE_BAD_DAY);
680 : :
681 : 4441802 : return d->day;
682 : : }
683 : :
684 : : /**
685 : : * g_date_get_julian:
686 : : * @date: a #GDate to extract the Julian day from
687 : : *
688 : : * Returns the Julian day or "serial number" of the #GDate. The
689 : : * Julian day is simply the number of days since January 1, Year 1; i.e.,
690 : : * January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
691 : : * etc. The date must be valid.
692 : : *
693 : : * Returns: Julian day
694 : : */
695 : : guint32
696 : 113105 : g_date_get_julian (const GDate *d)
697 : : {
698 : 113105 : g_return_val_if_fail (g_date_valid (d), G_DATE_BAD_JULIAN);
699 : :
700 [ + + ]: 113104 : if (!d->julian)
701 : 112893 : g_date_update_julian (d);
702 : :
703 : 113104 : g_return_val_if_fail (d->julian, G_DATE_BAD_JULIAN);
704 : :
705 : 113104 : return d->julian_days;
706 : : }
707 : :
708 : : /**
709 : : * g_date_get_day_of_year:
710 : : * @date: a #GDate to extract day of year from
711 : : *
712 : : * Returns the day of the year, where Jan 1 is the first day of the
713 : : * year. The date must be valid.
714 : : *
715 : : * Returns: day of the year
716 : : */
717 : : guint
718 : 380203 : g_date_get_day_of_year (const GDate *d)
719 : : {
720 : : gint idx;
721 : :
722 : 380203 : g_return_val_if_fail (g_date_valid (d), 0);
723 : :
724 [ + + ]: 380202 : if (!d->dmy)
725 : 1 : g_date_update_dmy (d);
726 : :
727 : 380202 : g_return_val_if_fail (d->dmy, 0);
728 : :
729 : 380202 : idx = g_date_is_leap_year (d->year) ? 1 : 0;
730 : :
731 : 380202 : return (days_in_year[idx][d->month] + d->day);
732 : : }
733 : :
734 : : /**
735 : : * g_date_get_monday_week_of_year:
736 : : * @date: a #GDate
737 : : *
738 : : * Returns the week of the year, where weeks are understood to start on
739 : : * Monday. If the date is before the first Monday of the year, return 0.
740 : : * The date must be valid.
741 : : *
742 : : * Returns: week of the year
743 : : */
744 : : guint
745 : 151922 : g_date_get_monday_week_of_year (const GDate *d)
746 : : {
747 : : GDateWeekday wd;
748 : : guint day;
749 : : GDate first;
750 : :
751 : 151922 : g_return_val_if_fail (g_date_valid (d), 0);
752 : :
753 [ + + ]: 151921 : if (!d->dmy)
754 : 1 : g_date_update_dmy (d);
755 : :
756 : 151921 : g_return_val_if_fail (d->dmy, 0);
757 : :
758 : 151921 : g_date_clear (&first, 1);
759 : :
760 : 151921 : g_date_set_dmy (&first, 1, 1, d->year);
761 : :
762 : 151921 : wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
763 : 151921 : day = g_date_get_day_of_year (d) - 1;
764 : :
765 [ + + ]: 151921 : return ((day + wd)/7U + (wd == 0 ? 1 : 0));
766 : : }
767 : :
768 : : /**
769 : : * g_date_get_sunday_week_of_year:
770 : : * @date: a #GDate
771 : : *
772 : : * Returns the week of the year during which this date falls, if
773 : : * weeks are understood to begin on Sunday. The date must be valid.
774 : : * Can return 0 if the day is before the first Sunday of the year.
775 : : *
776 : : * Returns: week number
777 : : */
778 : : guint
779 : 151922 : g_date_get_sunday_week_of_year (const GDate *d)
780 : : {
781 : : GDateWeekday wd;
782 : : guint day;
783 : : GDate first;
784 : :
785 : 151922 : g_return_val_if_fail (g_date_valid (d), 0);
786 : :
787 [ + + ]: 151921 : if (!d->dmy)
788 : 1 : g_date_update_dmy (d);
789 : :
790 : 151921 : g_return_val_if_fail (d->dmy, 0);
791 : :
792 : 151921 : g_date_clear (&first, 1);
793 : :
794 : 151921 : g_date_set_dmy (&first, 1, 1, d->year);
795 : :
796 : 151921 : wd = g_date_get_weekday (&first);
797 [ + + ]: 151921 : if (wd == 7) wd = 0; /* make Sunday day 0 */
798 : 151921 : day = g_date_get_day_of_year (d) - 1;
799 : :
800 [ + + ]: 151921 : return ((day + wd)/7U + (wd == 0 ? 1 : 0));
801 : : }
802 : :
803 : : /**
804 : : * g_date_get_iso8601_week_of_year:
805 : : * @date: a valid #GDate
806 : : *
807 : : * Returns the week of the year, where weeks are interpreted according
808 : : * to ISO 8601.
809 : : *
810 : : * Returns: ISO 8601 week number of the year.
811 : : *
812 : : * Since: 2.6
813 : : **/
814 : : guint
815 : 75873 : g_date_get_iso8601_week_of_year (const GDate *d)
816 : : {
817 : : guint j, d4, L, d1, w;
818 : :
819 : 75873 : g_return_val_if_fail (g_date_valid (d), 0);
820 : :
821 [ + + ]: 75872 : if (!d->julian)
822 : 1 : g_date_update_julian (d);
823 : :
824 : 75872 : g_return_val_if_fail (d->julian, 0);
825 : :
826 : : /* Formula taken from the Calendar FAQ; the formula was for the
827 : : * Julian Period which starts on 1 January 4713 BC, so we add
828 : : * 1,721,425 to the number of days before doing the formula.
829 : : */
830 : 75872 : j = d->julian_days + 1721425;
831 : 75872 : d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
832 : 75872 : L = d4 / 1460;
833 : 75872 : d1 = ((d4 - L) % 365) + L;
834 : 75872 : w = d1 / 7 + 1;
835 : :
836 : 75872 : return w;
837 : : }
838 : :
839 : : /**
840 : : * g_date_days_between:
841 : : * @date1: the first date
842 : : * @date2: the second date
843 : : *
844 : : * Computes the number of days between two dates.
845 : : * If @date2 is prior to @date1, the returned value is negative.
846 : : * Both dates must be valid.
847 : : *
848 : : * Returns: the number of days between @date1 and @date2
849 : : */
850 : : gint
851 : 3 : g_date_days_between (const GDate *d1,
852 : : const GDate *d2)
853 : : {
854 : 3 : g_return_val_if_fail (g_date_valid (d1), 0);
855 : 2 : g_return_val_if_fail (g_date_valid (d2), 0);
856 : :
857 : 1 : return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
858 : : }
859 : :
860 : : /**
861 : : * g_date_clear:
862 : : * @date: pointer to one or more dates to clear
863 : : * @n_dates: number of dates to clear
864 : : *
865 : : * Initializes one or more #GDate structs to a safe but invalid
866 : : * state. The cleared dates will not represent an existing date, but will
867 : : * not contain garbage. Useful to init a date declared on the stack.
868 : : * Validity can be tested with g_date_valid().
869 : : */
870 : : void
871 : 366024 : g_date_clear (GDate *d, guint ndates)
872 : : {
873 : 366024 : g_return_if_fail (d != NULL);
874 : 366023 : g_return_if_fail (ndates != 0);
875 : :
876 : 366022 : memset (d, 0x0, ndates*sizeof (GDate));
877 : : }
878 : :
879 : : G_LOCK_DEFINE_STATIC (g_date_global);
880 : :
881 : : /* These are for the parser, output to the user should use *
882 : : * g_date_strftime () - this creates more never-freed memory to annoy
883 : : * all those memory debugger users. :-)
884 : : */
885 : :
886 : : static gchar *long_month_names[13] =
887 : : {
888 : : NULL,
889 : : };
890 : :
891 : : static gchar *long_month_names_alternative[13] =
892 : : {
893 : : NULL,
894 : : };
895 : :
896 : : static gchar *short_month_names[13] =
897 : : {
898 : : NULL,
899 : : };
900 : :
901 : : static gchar *short_month_names_alternative[13] =
902 : : {
903 : : NULL,
904 : : };
905 : :
906 : : /* This tells us if we need to update the parse info */
907 : : static gchar *current_locale = NULL;
908 : :
909 : : /* order of these in the current locale */
910 : : static GDateDMY dmy_order[3] =
911 : : {
912 : : G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
913 : : };
914 : :
915 : : /* Where to chop two-digit years: i.e., for the 1930 default, numbers
916 : : * 29 and below are counted as in the year 2000, numbers 30 and above
917 : : * are counted as in the year 1900.
918 : : */
919 : :
920 : : static const GDateYear twodigit_start_year = 1930;
921 : :
922 : : /* It is impossible to enter a year between 1 AD and 99 AD with this
923 : : * in effect.
924 : : */
925 : : static gboolean using_twodigit_years = FALSE;
926 : :
927 : : /* Adjustment of locale era to AD, non-zero means using locale era
928 : : */
929 : : static gint locale_era_adjust = 0;
930 : :
931 : : struct _GDateParseTokens {
932 : : gint num_ints;
933 : : gint n[3];
934 : : guint month;
935 : : };
936 : :
937 : : typedef struct _GDateParseTokens GDateParseTokens;
938 : :
939 : : static inline gboolean
940 : 576 : update_month_match (gsize *longest,
941 : : const gchar *haystack,
942 : : const gchar *needle)
943 : : {
944 : : gsize length;
945 : :
946 [ - + ]: 576 : if (needle == NULL)
947 : 0 : return FALSE;
948 : :
949 : 576 : length = strlen (needle);
950 [ + + ]: 576 : if (*longest >= length)
951 : 91 : return FALSE;
952 : :
953 [ + + ]: 485 : if (strstr (haystack, needle) == NULL)
954 : 476 : return FALSE;
955 : :
956 : 9 : *longest = length;
957 : 9 : return TRUE;
958 : : }
959 : :
960 : : #define NUM_LEN 10
961 : :
962 : : /* HOLDS: g_date_global_lock */
963 : : static void
964 : 31 : g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
965 : : {
966 : : gchar num[4][NUM_LEN+1];
967 : : gint i;
968 : : const guchar *s;
969 : :
970 : : /* We count 4, but store 3; so we can give an error
971 : : * if there are 4.
972 : : */
973 : 31 : num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
974 : :
975 : 31 : s = (const guchar *) str;
976 : 31 : pt->num_ints = 0;
977 [ + - + - ]: 224 : while (*s && pt->num_ints < 4)
978 : : {
979 : :
980 : 224 : i = 0;
981 [ + + + + : 418 : while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
+ - ]
982 : : {
983 : 194 : num[pt->num_ints][i] = *s;
984 : 194 : ++s;
985 : 194 : ++i;
986 : : }
987 : :
988 [ + + ]: 224 : if (i > 0)
989 : : {
990 : 73 : num[pt->num_ints][i] = '\0';
991 : 73 : ++(pt->num_ints);
992 : : }
993 : :
994 [ + + ]: 224 : if (*s == '\0') break;
995 : :
996 : 193 : ++s;
997 : : }
998 : :
999 [ + - ]: 31 : pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
1000 [ + + ]: 31 : pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
1001 [ + + ]: 31 : pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
1002 : :
1003 : 31 : pt->month = G_DATE_BAD_MONTH;
1004 : :
1005 [ + + ]: 31 : if (pt->num_ints < 3)
1006 : : {
1007 : 12 : gsize longest = 0;
1008 : : gchar *casefold;
1009 : : gchar *normalized;
1010 : :
1011 : 12 : casefold = g_utf8_casefold (str, -1);
1012 : 12 : normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1013 : 12 : g_free (casefold);
1014 : :
1015 [ + + ]: 156 : for (i = 1; i < 13; ++i)
1016 : : {
1017 : : /* Here month names may be in a genitive case if the language
1018 : : * grammatical rules require it.
1019 : : * Examples of how January may look in some languages:
1020 : : * Catalan: "de gener", Croatian: "siječnja", Polish: "stycznia",
1021 : : * Upper Sorbian: "januara".
1022 : : * Note that most of the languages can't or don't use the the
1023 : : * genitive case here so they use nominative everywhere.
1024 : : * For example, English always uses "January".
1025 : : */
1026 [ + + ]: 144 : if (update_month_match (&longest, normalized, long_month_names[i]))
1027 : 3 : pt->month = i;
1028 : :
1029 : : /* Here month names will be in a nominative case.
1030 : : * Examples of how January may look in some languages:
1031 : : * Catalan: "gener", Croatian: "Siječanj", Polish: "styczeń",
1032 : : * Upper Sorbian: "Januar".
1033 : : */
1034 [ + + ]: 144 : if (update_month_match (&longest, normalized, long_month_names_alternative[i]))
1035 : 2 : pt->month = i;
1036 : :
1037 : : /* Differences between abbreviated nominative and abbreviated
1038 : : * genitive month names are visible in very few languages but
1039 : : * let's handle them.
1040 : : */
1041 [ + + ]: 144 : if (update_month_match (&longest, normalized, short_month_names[i]))
1042 : 4 : pt->month = i;
1043 : :
1044 [ - + ]: 144 : if (update_month_match (&longest, normalized, short_month_names_alternative[i]))
1045 : 0 : pt->month = i;
1046 : : }
1047 : :
1048 : 12 : g_free (normalized);
1049 : : }
1050 : 31 : }
1051 : :
1052 : : /* HOLDS: g_date_global_lock */
1053 : : static void
1054 : 26 : g_date_prepare_to_parse (const gchar *str,
1055 : : GDateParseTokens *pt)
1056 : : {
1057 : 26 : const gchar *locale = setlocale (LC_TIME, NULL);
1058 : 26 : gboolean recompute_localeinfo = FALSE;
1059 : : GDate d;
1060 : :
1061 : 26 : g_return_if_fail (locale != NULL); /* should not happen */
1062 : :
1063 : 26 : g_date_clear (&d, 1); /* clear for scratch use */
1064 : :
1065 [ + + + + ]: 26 : if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) )
1066 : 5 : recompute_localeinfo = TRUE; /* Uh, there used to be a reason for the temporary */
1067 : :
1068 [ + + ]: 26 : if (recompute_localeinfo)
1069 : : {
1070 : 5 : int i = 1;
1071 : : GDateParseTokens testpt;
1072 : : gchar buf[128];
1073 : :
1074 : 5 : g_free (current_locale); /* still works if current_locale == NULL */
1075 : :
1076 : 5 : current_locale = g_strdup (locale);
1077 : :
1078 : 5 : short_month_names[0] = "Error";
1079 : 5 : long_month_names[0] = "Error";
1080 : :
1081 [ + + ]: 65 : while (i < 13)
1082 : : {
1083 : : gchar *casefold;
1084 : :
1085 : 60 : g_date_set_dmy (&d, 1, i, 1976);
1086 : :
1087 : 60 : g_return_if_fail (g_date_valid (&d));
1088 : :
1089 : 60 : g_date_strftime (buf, 127, "%b", &d);
1090 : :
1091 : 60 : casefold = g_utf8_casefold (buf, -1);
1092 : 60 : g_free (short_month_names[i]);
1093 : 60 : short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1094 : 60 : g_free (casefold);
1095 : :
1096 : 60 : g_date_strftime (buf, 127, "%B", &d);
1097 : 60 : casefold = g_utf8_casefold (buf, -1);
1098 : 60 : g_free (long_month_names[i]);
1099 : 60 : long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1100 : 60 : g_free (casefold);
1101 : :
1102 : 60 : g_date_strftime (buf, 127, "%Ob", &d);
1103 : 60 : casefold = g_utf8_casefold (buf, -1);
1104 : 60 : g_free (short_month_names_alternative[i]);
1105 : 60 : short_month_names_alternative[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1106 : 60 : g_free (casefold);
1107 : :
1108 : 60 : g_date_strftime (buf, 127, "%OB", &d);
1109 : 60 : casefold = g_utf8_casefold (buf, -1);
1110 : 60 : g_free (long_month_names_alternative[i]);
1111 : 60 : long_month_names_alternative[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1112 : 60 : g_free (casefold);
1113 : :
1114 : 60 : ++i;
1115 : : }
1116 : :
1117 : : /* Determine DMY order */
1118 : :
1119 : : /* had to pick a random day - don't change this, some strftimes
1120 : : * are broken on some days, and this one is good so far. */
1121 : 5 : g_date_set_dmy (&d, 4, 7, 1976);
1122 : :
1123 : 5 : g_date_strftime (buf, 127, "%x", &d);
1124 : :
1125 : 5 : g_date_fill_parse_tokens (buf, &testpt);
1126 : :
1127 : 5 : using_twodigit_years = FALSE;
1128 : 5 : locale_era_adjust = 0;
1129 : 5 : dmy_order[0] = G_DATE_DAY;
1130 : 5 : dmy_order[1] = G_DATE_MONTH;
1131 : 5 : dmy_order[2] = G_DATE_YEAR;
1132 : :
1133 : 5 : i = 0;
1134 [ + + ]: 20 : while (i < testpt.num_ints)
1135 : : {
1136 [ + + + + : 15 : switch (testpt.n[i])
+ ]
1137 : : {
1138 : 5 : case 7:
1139 : 5 : dmy_order[i] = G_DATE_MONTH;
1140 : 5 : break;
1141 : 5 : case 4:
1142 : 5 : dmy_order[i] = G_DATE_DAY;
1143 : 5 : break;
1144 : 2 : case 76:
1145 : 2 : using_twodigit_years = TRUE;
1146 : : G_GNUC_FALLTHROUGH;
1147 : 4 : case 1976:
1148 : 4 : dmy_order[i] = G_DATE_YEAR;
1149 : 4 : break;
1150 : 1 : default:
1151 : : /* assume locale era */
1152 : 1 : locale_era_adjust = 1976 - testpt.n[i];
1153 : 1 : dmy_order[i] = G_DATE_YEAR;
1154 : 1 : break;
1155 : : }
1156 : 15 : ++i;
1157 : : }
1158 : :
1159 : : #if defined(G_ENABLE_DEBUG) && 0
1160 : : DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
1161 : : i = 1;
1162 : : while (i < 13)
1163 : : {
1164 : : DEBUG_MSG ((" %s %s", long_month_names[i], short_month_names[i]));
1165 : : ++i;
1166 : : }
1167 : : DEBUG_MSG (("Alternative month names:"));
1168 : : i = 1;
1169 : : while (i < 13)
1170 : : {
1171 : : DEBUG_MSG ((" %s %s", long_month_names_alternative[i], short_month_names_alternative[i]));
1172 : : ++i;
1173 : : }
1174 : : if (using_twodigit_years)
1175 : : {
1176 : : DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
1177 : : }
1178 : : {
1179 : : gchar *strings[3];
1180 : : i = 0;
1181 : : while (i < 3)
1182 : : {
1183 : : switch (dmy_order[i])
1184 : : {
1185 : : case G_DATE_MONTH:
1186 : : strings[i] = "Month";
1187 : : break;
1188 : : case G_DATE_YEAR:
1189 : : strings[i] = "Year";
1190 : : break;
1191 : : case G_DATE_DAY:
1192 : : strings[i] = "Day";
1193 : : break;
1194 : : default:
1195 : : strings[i] = NULL;
1196 : : break;
1197 : : }
1198 : : ++i;
1199 : : }
1200 : : DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
1201 : : DEBUG_MSG (("**Sample date in this locale: '%s'", buf));
1202 : : }
1203 : : #endif
1204 : : }
1205 : :
1206 : 26 : g_date_fill_parse_tokens (str, pt);
1207 : : }
1208 : :
1209 : : static guint
1210 : 19 : convert_twodigit_year (guint y)
1211 : : {
1212 [ + + + - ]: 19 : if (using_twodigit_years && y < 100)
1213 : : {
1214 : 3 : guint two = twodigit_start_year % 100;
1215 : 3 : guint century = (twodigit_start_year / 100) * 100;
1216 : :
1217 [ + + ]: 3 : if (y < two)
1218 : 1 : century += 100;
1219 : :
1220 : 3 : y += century;
1221 : : }
1222 : 19 : return y;
1223 : : }
1224 : :
1225 : : /**
1226 : : * g_date_set_parse:
1227 : : * @date: a #GDate to fill in
1228 : : * @str: string to parse
1229 : : *
1230 : : * Parses a user-inputted string @str, and try to figure out what date it
1231 : : * represents, taking the [current locale][setlocale] into account. If the
1232 : : * string is successfully parsed, the date will be valid after the call.
1233 : : * Otherwise, it will be invalid. You should check using g_date_valid()
1234 : : * to see whether the parsing succeeded.
1235 : : *
1236 : : * This function is not appropriate for file formats and the like; it
1237 : : * isn't very precise, and its exact behavior varies with the locale.
1238 : : * It's intended to be a heuristic routine that guesses what the user
1239 : : * means by a given string (and it does work pretty well in that
1240 : : * capacity).
1241 : : */
1242 : : void
1243 : 30 : g_date_set_parse (GDate *d,
1244 : : const gchar *str)
1245 : : {
1246 : : GDateParseTokens pt;
1247 : 30 : guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
1248 : : gsize str_len;
1249 : :
1250 : 34 : g_return_if_fail (d != NULL);
1251 : :
1252 : : /* set invalid */
1253 : 29 : g_date_clear (d, 1);
1254 : :
1255 : : /* Anything longer than this is ridiculous and could take a while to normalize.
1256 : : * This limit is chosen arbitrarily. */
1257 : 29 : str_len = strlen (str);
1258 [ + + ]: 29 : if (str_len > 200)
1259 : 2 : return;
1260 : :
1261 : : /* The input has to be valid UTF-8. */
1262 [ + + ]: 27 : if (!g_utf8_validate_len (str, str_len, NULL))
1263 : 1 : return;
1264 : :
1265 : 26 : G_LOCK (g_date_global);
1266 : :
1267 : 26 : g_date_prepare_to_parse (str, &pt);
1268 : :
1269 : : DEBUG_MSG (("Found %d ints, '%d' '%d' '%d' and written out month %d",
1270 : : pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
1271 : :
1272 : :
1273 [ + + ]: 26 : if (pt.num_ints == 4)
1274 : : {
1275 : 1 : G_UNLOCK (g_date_global);
1276 : 1 : return; /* presumably a typo; bail out. */
1277 : : }
1278 : :
1279 [ + + ]: 25 : if (pt.num_ints > 1)
1280 : : {
1281 : 16 : int i = 0;
1282 : 16 : int j = 0;
1283 : :
1284 : 16 : g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
1285 : :
1286 [ + + + - ]: 64 : while (i < pt.num_ints && j < 3)
1287 : : {
1288 [ + + + - ]: 48 : switch (dmy_order[j])
1289 : : {
1290 : 16 : case G_DATE_MONTH:
1291 : : {
1292 [ + + + + ]: 16 : if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
1293 : : {
1294 : 2 : m = pt.month;
1295 : 2 : ++j; /* skip months, but don't skip this number */
1296 : 2 : continue;
1297 : : }
1298 : : else
1299 : 14 : m = pt.n[i];
1300 : : }
1301 : 14 : break;
1302 : 16 : case G_DATE_DAY:
1303 : : {
1304 [ + + + + ]: 16 : if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
1305 : : {
1306 : 1 : day = 1;
1307 : 1 : ++j; /* skip days, since we may have month/year */
1308 : 1 : continue;
1309 : : }
1310 : 15 : day = pt.n[i];
1311 : : }
1312 : 15 : break;
1313 : 16 : case G_DATE_YEAR:
1314 : : {
1315 : 16 : y = pt.n[i];
1316 : :
1317 [ + + ]: 16 : if (locale_era_adjust != 0)
1318 : : {
1319 : 1 : y += locale_era_adjust;
1320 : : }
1321 : :
1322 : 16 : y = convert_twodigit_year (y);
1323 : : }
1324 : 16 : break;
1325 : 0 : default:
1326 : 0 : break;
1327 : : }
1328 : :
1329 : 45 : ++i;
1330 : 45 : ++j;
1331 : : }
1332 : :
1333 : :
1334 [ + + + + ]: 16 : if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
1335 : : {
1336 : : /* Try YYYY MM DD */
1337 : 3 : y = pt.n[0];
1338 : 3 : m = pt.n[1];
1339 : 3 : day = pt.n[2];
1340 : :
1341 [ - + - - ]: 3 : if (using_twodigit_years && y < 100)
1342 : 0 : y = G_DATE_BAD_YEAR; /* avoids ambiguity */
1343 : : }
1344 [ + + ]: 13 : else if (pt.num_ints == 2)
1345 : : {
1346 [ - + - - ]: 3 : if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
1347 : 0 : m = pt.month;
1348 : : }
1349 : : }
1350 [ + - ]: 9 : else if (pt.num_ints == 1)
1351 : : {
1352 [ + + ]: 9 : if (pt.month != G_DATE_BAD_MONTH)
1353 : : {
1354 : : /* Month name and year? */
1355 : 6 : m = pt.month;
1356 : 6 : day = 1;
1357 : 6 : y = pt.n[0];
1358 : : }
1359 : : else
1360 : : {
1361 : : /* Try yyyymmdd and yymmdd */
1362 : :
1363 : 3 : m = (pt.n[0]/100) % 100;
1364 : 3 : day = pt.n[0] % 100;
1365 : 3 : y = pt.n[0]/10000;
1366 : :
1367 : 3 : y = convert_twodigit_year (y);
1368 : : }
1369 : : }
1370 : :
1371 : : /* See if we got anything valid out of all this. */
1372 : : /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
1373 [ + + + + ]: 25 : if (y < 8000 && g_date_valid_dmy (day, m, y))
1374 : : {
1375 : 22 : d->month = m;
1376 : 22 : d->day = day;
1377 : 22 : d->year = y;
1378 : 22 : d->dmy = TRUE;
1379 : : }
1380 : : #ifdef G_ENABLE_DEBUG
1381 : : else
1382 : : {
1383 : : DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
1384 : : }
1385 : : #endif
1386 : 25 : G_UNLOCK (g_date_global);
1387 : : }
1388 : :
1389 : : gboolean
1390 : 202 : _g_localtime (time_t timet, struct tm *out_tm)
1391 : : {
1392 : 202 : gboolean success = TRUE;
1393 : :
1394 : : #ifdef HAVE_LOCALTIME_R
1395 [ - + ]: 202 : if (!localtime_r (&timet, out_tm))
1396 : 0 : success = FALSE;
1397 : : #else
1398 : : {
1399 : : struct tm *ptm = localtime (&timet);
1400 : :
1401 : : if (ptm == NULL)
1402 : : {
1403 : : /* Happens at least in Microsoft's C library if you pass a
1404 : : * negative time_t.
1405 : : */
1406 : : success = FALSE;
1407 : : }
1408 : : else
1409 : : memcpy (out_tm, ptm, sizeof (struct tm));
1410 : : }
1411 : : #endif
1412 : :
1413 : 202 : return success;
1414 : : }
1415 : :
1416 : : /**
1417 : : * g_date_set_time_t:
1418 : : * @date: a #GDate
1419 : : * @timet: time_t value to set
1420 : : *
1421 : : * Sets the value of a date to the date corresponding to a time
1422 : : * specified as a time_t. The time to date conversion is done using
1423 : : * the user's current timezone.
1424 : : *
1425 : : * To set the value of a date to the current day, you could write:
1426 : : * |[<!-- language="C" -->
1427 : : * time_t now = time (NULL);
1428 : : * if (now == (time_t) -1)
1429 : : * // handle the error
1430 : : * g_date_set_time_t (date, now);
1431 : : * ]|
1432 : : *
1433 : : * Since: 2.10
1434 : : */
1435 : : void
1436 : 5 : g_date_set_time_t (GDate *date,
1437 : : time_t timet)
1438 : : {
1439 : : struct tm tm;
1440 : : gboolean success;
1441 : :
1442 : 5 : g_return_if_fail (date != NULL);
1443 : :
1444 : 4 : success = _g_localtime (timet, &tm);
1445 [ - + ]: 4 : if (!success)
1446 : : {
1447 : : /* Still set a default date, 2000-01-01.
1448 : : *
1449 : : * We may assert out below. */
1450 : 0 : tm.tm_mon = 0;
1451 : 0 : tm.tm_mday = 1;
1452 : 0 : tm.tm_year = 100;
1453 : : }
1454 : :
1455 : 4 : date->julian = FALSE;
1456 : :
1457 : 4 : date->month = tm.tm_mon + 1;
1458 : 4 : date->day = tm.tm_mday;
1459 : 4 : date->year = tm.tm_year + 1900;
1460 : :
1461 : 4 : g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
1462 : :
1463 : 4 : date->dmy = TRUE;
1464 : :
1465 : : #ifndef G_DISABLE_CHECKS
1466 [ - + ]: 4 : if (!success)
1467 : 0 : g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "localtime() == NULL");
1468 : : #endif
1469 : : }
1470 : :
1471 : :
1472 : : /**
1473 : : * g_date_set_time:
1474 : : * @date: a #GDate.
1475 : : * @time_: #GTime value to set.
1476 : : *
1477 : : * Sets the value of a date from a #GTime value.
1478 : : * The time to date conversion is done using the user's current timezone.
1479 : : *
1480 : : * Deprecated: 2.10: Use g_date_set_time_t() instead.
1481 : : */
1482 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1483 : : void
1484 : 3 : g_date_set_time (GDate *date,
1485 : : GTime time_)
1486 : : {
1487 : 3 : g_date_set_time_t (date, (time_t) time_);
1488 : 3 : }
1489 : : G_GNUC_END_IGNORE_DEPRECATIONS
1490 : :
1491 : : /**
1492 : : * g_date_set_time_val:
1493 : : * @date: a #GDate
1494 : : * @timeval: #GTimeVal value to set
1495 : : *
1496 : : * Sets the value of a date from a #GTimeVal value. Note that the
1497 : : * @tv_usec member is ignored, because #GDate can't make use of the
1498 : : * additional precision.
1499 : : *
1500 : : * The time to date conversion is done using the user's current timezone.
1501 : : *
1502 : : * Since: 2.10
1503 : : * Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use g_date_set_time_t()
1504 : : * instead.
1505 : : */
1506 : : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1507 : : void
1508 : 1 : g_date_set_time_val (GDate *date,
1509 : : GTimeVal *timeval)
1510 : : {
1511 : 1 : g_date_set_time_t (date, (time_t) timeval->tv_sec);
1512 : 1 : }
1513 : : G_GNUC_END_IGNORE_DEPRECATIONS
1514 : :
1515 : : /**
1516 : : * g_date_set_month:
1517 : : * @date: a #GDate
1518 : : * @month: month to set
1519 : : *
1520 : : * Sets the month of the year for a #GDate. If the resulting
1521 : : * day-month-year triplet is invalid, the date will be invalid.
1522 : : */
1523 : : void
1524 : 7 : g_date_set_month (GDate *d,
1525 : : GDateMonth m)
1526 : : {
1527 : 7 : g_return_if_fail (d != NULL);
1528 : 6 : g_return_if_fail (g_date_valid_month (m));
1529 : :
1530 [ + + + - ]: 5 : if (d->julian && !d->dmy) g_date_update_dmy(d);
1531 : 5 : d->julian = FALSE;
1532 : :
1533 : 5 : d->month = m;
1534 : :
1535 [ + + ]: 5 : if (g_date_valid_dmy (d->day, d->month, d->year))
1536 : 3 : d->dmy = TRUE;
1537 : : else
1538 : 2 : d->dmy = FALSE;
1539 : : }
1540 : :
1541 : : /**
1542 : : * g_date_set_day:
1543 : : * @date: a #GDate
1544 : : * @day: day to set
1545 : : *
1546 : : * Sets the day of the month for a #GDate. If the resulting
1547 : : * day-month-year triplet is invalid, the date will be invalid.
1548 : : */
1549 : : void
1550 : 291600 : g_date_set_day (GDate *d,
1551 : : GDateDay day)
1552 : : {
1553 : 291600 : g_return_if_fail (d != NULL);
1554 : 291599 : g_return_if_fail (g_date_valid_day (day));
1555 : :
1556 [ + + + - ]: 291598 : if (d->julian && !d->dmy) g_date_update_dmy(d);
1557 : 291598 : d->julian = FALSE;
1558 : :
1559 : 291598 : d->day = day;
1560 : :
1561 [ + + ]: 291598 : if (g_date_valid_dmy (d->day, d->month, d->year))
1562 : 291596 : d->dmy = TRUE;
1563 : : else
1564 : 2 : d->dmy = FALSE;
1565 : : }
1566 : :
1567 : : /**
1568 : : * g_date_set_year:
1569 : : * @date: a #GDate
1570 : : * @year: year to set
1571 : : *
1572 : : * Sets the year for a #GDate. If the resulting day-month-year
1573 : : * triplet is invalid, the date will be invalid.
1574 : : */
1575 : : void
1576 : 7 : g_date_set_year (GDate *d,
1577 : : GDateYear y)
1578 : : {
1579 : 7 : g_return_if_fail (d != NULL);
1580 : 6 : g_return_if_fail (g_date_valid_year (y));
1581 : :
1582 [ + + + - ]: 5 : if (d->julian && !d->dmy) g_date_update_dmy(d);
1583 : 5 : d->julian = FALSE;
1584 : :
1585 : 5 : d->year = y;
1586 : :
1587 [ + + ]: 5 : if (g_date_valid_dmy (d->day, d->month, d->year))
1588 : 4 : d->dmy = TRUE;
1589 : : else
1590 : 1 : d->dmy = FALSE;
1591 : : }
1592 : :
1593 : : /**
1594 : : * g_date_set_dmy:
1595 : : * @date: a #GDate
1596 : : * @day: day
1597 : : * @month: month
1598 : : * @y: year
1599 : : *
1600 : : * Sets the value of a #GDate from a day, month, and year.
1601 : : * The day-month-year triplet must be valid; if you aren't
1602 : : * sure it is, call g_date_valid_dmy() to check before you
1603 : : * set it.
1604 : : */
1605 : : void
1606 : 403111 : g_date_set_dmy (GDate *d,
1607 : : GDateDay day,
1608 : : GDateMonth m,
1609 : : GDateYear y)
1610 : : {
1611 : 403111 : g_return_if_fail (d != NULL);
1612 : 403110 : g_return_if_fail (g_date_valid_dmy (day, m, y));
1613 : :
1614 : 403109 : d->julian = FALSE;
1615 : :
1616 : 403109 : d->month = m;
1617 : 403109 : d->day = day;
1618 : 403109 : d->year = y;
1619 : :
1620 : 403109 : d->dmy = TRUE;
1621 : : }
1622 : :
1623 : : /**
1624 : : * g_date_set_julian:
1625 : : * @date: a #GDate
1626 : : * @julian_date: Julian day number (days since January 1, Year 1)
1627 : : *
1628 : : * Sets the value of a #GDate from a Julian day number.
1629 : : */
1630 : : void
1631 : 20 : g_date_set_julian (GDate *d,
1632 : : guint32 j)
1633 : : {
1634 : 20 : g_return_if_fail (d != NULL);
1635 : 19 : g_return_if_fail (g_date_valid_julian (j));
1636 : :
1637 : 18 : d->julian_days = j;
1638 : 18 : d->julian = TRUE;
1639 : 18 : d->dmy = FALSE;
1640 : : }
1641 : :
1642 : : /**
1643 : : * g_date_is_first_of_month:
1644 : : * @date: a #GDate to check
1645 : : *
1646 : : * Returns %TRUE if the date is on the first of a month.
1647 : : * The date must be valid.
1648 : : *
1649 : : * Returns: %TRUE if the date is the first of the month
1650 : : */
1651 : : gboolean
1652 : 4 : g_date_is_first_of_month (const GDate *d)
1653 : : {
1654 : 4 : g_return_val_if_fail (g_date_valid (d), FALSE);
1655 : :
1656 [ + + ]: 3 : if (!d->dmy)
1657 : 1 : g_date_update_dmy (d);
1658 : :
1659 : 3 : g_return_val_if_fail (d->dmy, FALSE);
1660 : :
1661 [ + + ]: 3 : if (d->day == 1) return TRUE;
1662 : 1 : else return FALSE;
1663 : : }
1664 : :
1665 : : /**
1666 : : * g_date_is_last_of_month:
1667 : : * @date: a #GDate to check
1668 : : *
1669 : : * Returns %TRUE if the date is the last day of the month.
1670 : : * The date must be valid.
1671 : : *
1672 : : * Returns: %TRUE if the date is the last day of the month
1673 : : */
1674 : : gboolean
1675 : 3 : g_date_is_last_of_month (const GDate *d)
1676 : : {
1677 : : gint idx;
1678 : :
1679 : 3 : g_return_val_if_fail (g_date_valid (d), FALSE);
1680 : :
1681 [ + + ]: 2 : if (!d->dmy)
1682 : 1 : g_date_update_dmy (d);
1683 : :
1684 : 2 : g_return_val_if_fail (d->dmy, FALSE);
1685 : :
1686 : 2 : idx = g_date_is_leap_year (d->year) ? 1 : 0;
1687 : :
1688 [ + + ]: 2 : if (d->day == days_in_months[idx][d->month]) return TRUE;
1689 : 1 : else return FALSE;
1690 : : }
1691 : :
1692 : : /**
1693 : : * g_date_add_days:
1694 : : * @date: a #GDate to increment
1695 : : * @n_days: number of days to move the date forward
1696 : : *
1697 : : * Increments a date some number of days.
1698 : : * To move forward by weeks, add weeks*7 days.
1699 : : * The date must be valid.
1700 : : */
1701 : : void
1702 : 1581113 : g_date_add_days (GDate *d,
1703 : : guint ndays)
1704 : : {
1705 : 1581113 : g_return_if_fail (g_date_valid (d));
1706 : :
1707 [ + + ]: 1581112 : if (!d->julian)
1708 : 1519200 : g_date_update_julian (d);
1709 : :
1710 : 1581112 : g_return_if_fail (d->julian);
1711 : 1581112 : g_return_if_fail (ndays <= G_MAXUINT32 - d->julian_days);
1712 : :
1713 : 1581112 : d->julian_days += ndays;
1714 : 1581112 : d->dmy = FALSE;
1715 : : }
1716 : :
1717 : : /**
1718 : : * g_date_subtract_days:
1719 : : * @date: a #GDate to decrement
1720 : : * @n_days: number of days to move
1721 : : *
1722 : : * Moves a date some number of days into the past.
1723 : : * To move by weeks, just move by weeks*7 days.
1724 : : * The date must be valid.
1725 : : */
1726 : : void
1727 : 1557183 : g_date_subtract_days (GDate *d,
1728 : : guint ndays)
1729 : : {
1730 : 1557183 : g_return_if_fail (g_date_valid (d));
1731 : :
1732 [ + + ]: 1557182 : if (!d->julian)
1733 : 1 : g_date_update_julian (d);
1734 : :
1735 : 1557182 : g_return_if_fail (d->julian);
1736 : 1557182 : g_return_if_fail (d->julian_days > ndays);
1737 : :
1738 : 1557181 : d->julian_days -= ndays;
1739 : 1557181 : d->dmy = FALSE;
1740 : : }
1741 : :
1742 : : /**
1743 : : * g_date_add_months:
1744 : : * @date: a #GDate to increment
1745 : : * @n_months: number of months to move forward
1746 : : *
1747 : : * Increments a date by some number of months.
1748 : : * If the day of the month is greater than 28,
1749 : : * this routine may change the day of the month
1750 : : * (because the destination month may not have
1751 : : * the current day in it). The date must be valid.
1752 : : */
1753 : : void
1754 : 1557182 : g_date_add_months (GDate *d,
1755 : : guint nmonths)
1756 : : {
1757 : : guint years, months;
1758 : : gint idx;
1759 : :
1760 : 1557182 : g_return_if_fail (g_date_valid (d));
1761 : :
1762 [ + + ]: 1557181 : if (!d->dmy)
1763 : 1 : g_date_update_dmy (d);
1764 : :
1765 : 1557181 : g_return_if_fail (d->dmy != 0);
1766 : 1557181 : g_return_if_fail (nmonths <= G_MAXUINT - (d->month - 1));
1767 : :
1768 : 1557181 : nmonths += d->month - 1;
1769 : :
1770 : 1557181 : years = nmonths/12;
1771 : 1557181 : months = nmonths%12;
1772 : :
1773 : 1557181 : g_return_if_fail (years <= (guint) (G_MAXUINT16 - d->year));
1774 : :
1775 : 1557181 : d->month = months + 1;
1776 : 1557181 : d->year += years;
1777 : :
1778 : 1557181 : idx = g_date_is_leap_year (d->year) ? 1 : 0;
1779 : :
1780 [ + + ]: 1557181 : if (d->day > days_in_months[idx][d->month])
1781 : 20259 : d->day = days_in_months[idx][d->month];
1782 : :
1783 : 1557181 : d->julian = FALSE;
1784 : :
1785 : 1557181 : g_return_if_fail (g_date_valid (d));
1786 : : }
1787 : :
1788 : : /**
1789 : : * g_date_subtract_months:
1790 : : * @date: a #GDate to decrement
1791 : : * @n_months: number of months to move
1792 : : *
1793 : : * Moves a date some number of months into the past.
1794 : : * If the current day of the month doesn't exist in
1795 : : * the destination month, the day of the month
1796 : : * may change. The date must be valid.
1797 : : */
1798 : : void
1799 : 1557185 : g_date_subtract_months (GDate *d,
1800 : : guint nmonths)
1801 : : {
1802 : : guint years, months;
1803 : : gint idx;
1804 : :
1805 : 1557185 : g_return_if_fail (g_date_valid (d));
1806 : :
1807 [ + + ]: 1557184 : if (!d->dmy)
1808 : 2 : g_date_update_dmy (d);
1809 : :
1810 : 1557184 : g_return_if_fail (d->dmy != 0);
1811 : :
1812 : 1557184 : years = nmonths/12;
1813 : 1557184 : months = nmonths%12;
1814 : :
1815 : 1557184 : g_return_if_fail (d->year > years);
1816 : :
1817 : 1557184 : d->year -= years;
1818 : :
1819 [ + + ]: 1557184 : if (d->month > months) d->month -= months;
1820 : : else
1821 : : {
1822 : 790647 : months -= d->month;
1823 : 790647 : d->month = 12 - months;
1824 : 790647 : d->year -= 1;
1825 : : }
1826 : :
1827 : 1557184 : idx = g_date_is_leap_year (d->year) ? 1 : 0;
1828 : :
1829 [ + + ]: 1557184 : if (d->day > days_in_months[idx][d->month])
1830 : 1 : d->day = days_in_months[idx][d->month];
1831 : :
1832 : 1557184 : d->julian = FALSE;
1833 : :
1834 : 1557184 : g_return_if_fail (g_date_valid (d));
1835 : : }
1836 : :
1837 : : /**
1838 : : * g_date_add_years:
1839 : : * @date: a #GDate to increment
1840 : : * @n_years: number of years to move forward
1841 : : *
1842 : : * Increments a date by some number of years.
1843 : : * If the date is February 29, and the destination
1844 : : * year is not a leap year, the date will be changed
1845 : : * to February 28. The date must be valid.
1846 : : */
1847 : : void
1848 : 1557182 : g_date_add_years (GDate *d,
1849 : : guint nyears)
1850 : : {
1851 : 1557182 : g_return_if_fail (g_date_valid (d));
1852 : :
1853 [ + + ]: 1557181 : if (!d->dmy)
1854 : 1 : g_date_update_dmy (d);
1855 : :
1856 : 1557181 : g_return_if_fail (d->dmy != 0);
1857 : 1557181 : g_return_if_fail (nyears <= (guint) (G_MAXUINT16 - d->year));
1858 : :
1859 : 1557181 : d->year += nyears;
1860 : :
1861 [ + + + + ]: 1557181 : if (d->month == 2 && d->day == 29)
1862 : : {
1863 [ + - ]: 820 : if (!g_date_is_leap_year (d->year))
1864 : 820 : d->day = 28;
1865 : : }
1866 : :
1867 : 1557181 : d->julian = FALSE;
1868 : : }
1869 : :
1870 : : /**
1871 : : * g_date_subtract_years:
1872 : : * @date: a #GDate to decrement
1873 : : * @n_years: number of years to move
1874 : : *
1875 : : * Moves a date some number of years into the past.
1876 : : * If the current day doesn't exist in the destination
1877 : : * year (i.e. it's February 29 and you move to a non-leap-year)
1878 : : * then the day is changed to February 29. The date
1879 : : * must be valid.
1880 : : */
1881 : : void
1882 : 1557185 : g_date_subtract_years (GDate *d,
1883 : : guint nyears)
1884 : : {
1885 : 1557185 : g_return_if_fail (g_date_valid (d));
1886 : :
1887 [ + + ]: 1557184 : if (!d->dmy)
1888 : 2 : g_date_update_dmy (d);
1889 : :
1890 : 1557184 : g_return_if_fail (d->dmy != 0);
1891 : 1557184 : g_return_if_fail (d->year > nyears);
1892 : :
1893 : 1557183 : d->year -= nyears;
1894 : :
1895 [ + + + + ]: 1557183 : if (d->month == 2 && d->day == 29)
1896 : : {
1897 [ + - ]: 1 : if (!g_date_is_leap_year (d->year))
1898 : 1 : d->day = 28;
1899 : : }
1900 : :
1901 : 1557183 : d->julian = FALSE;
1902 : : }
1903 : :
1904 : : /**
1905 : : * g_date_is_leap_year:
1906 : : * @year: year to check
1907 : : *
1908 : : * Returns %TRUE if the year is a leap year.
1909 : : *
1910 : : * For the purposes of this function, leap year is every year
1911 : : * divisible by 4 unless that year is divisible by 100. If it
1912 : : * is divisible by 100 it would be a leap year only if that year
1913 : : * is also divisible by 400.
1914 : : *
1915 : : * Returns: %TRUE if the year is a leap year
1916 : : */
1917 : : gboolean
1918 : 12793496 : g_date_is_leap_year (GDateYear year)
1919 : : {
1920 : 12793496 : g_return_val_if_fail (g_date_valid_year (year), FALSE);
1921 : :
1922 [ + + + + ]: 23466691 : return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1923 [ + + ]: 10673197 : (year % 400) == 0 );
1924 : : }
1925 : :
1926 : : /**
1927 : : * g_date_get_days_in_month:
1928 : : * @month: month
1929 : : * @year: year
1930 : : *
1931 : : * Returns the number of days in a month, taking leap
1932 : : * years into account.
1933 : : *
1934 : : * Returns: number of days in @month during the @year
1935 : : */
1936 : : guint8
1937 : 25078 : g_date_get_days_in_month (GDateMonth month,
1938 : : GDateYear year)
1939 : : {
1940 : : gint idx;
1941 : :
1942 : 25078 : g_return_val_if_fail (g_date_valid_year (year), 0);
1943 : 25077 : g_return_val_if_fail (g_date_valid_month (month), 0);
1944 : :
1945 : 25076 : idx = g_date_is_leap_year (year) ? 1 : 0;
1946 : :
1947 : 25076 : return days_in_months[idx][month];
1948 : : }
1949 : :
1950 : : /**
1951 : : * g_date_get_monday_weeks_in_year:
1952 : : * @year: a year
1953 : : *
1954 : : * Returns the number of weeks in the year, where weeks
1955 : : * are taken to start on Monday. Will be 52 or 53. The
1956 : : * date must be valid. (Years always have 52 7-day periods,
1957 : : * plus 1 or 2 extra days depending on whether it's a leap
1958 : : * year. This function is basically telling you how many
1959 : : * Mondays are in the year, i.e. there are 53 Mondays if
1960 : : * one of the extra days happens to be a Monday.)
1961 : : *
1962 : : * Returns: number of Mondays in the year
1963 : : */
1964 : : guint8
1965 : 107 : g_date_get_monday_weeks_in_year (GDateYear year)
1966 : : {
1967 : : GDate d;
1968 : :
1969 : 107 : g_return_val_if_fail (g_date_valid_year (year), 0);
1970 : :
1971 : 106 : g_date_clear (&d, 1);
1972 : 106 : g_date_set_dmy (&d, 1, 1, year);
1973 [ + + ]: 106 : if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1974 : 90 : g_date_set_dmy (&d, 31, 12, year);
1975 [ + + ]: 90 : if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1976 [ + + ]: 86 : if (g_date_is_leap_year (year))
1977 : : {
1978 : 15 : g_date_set_dmy (&d, 2, 1, year);
1979 [ - + ]: 15 : if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1980 : 15 : g_date_set_dmy (&d, 30, 12, year);
1981 [ - + ]: 15 : if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1982 : : }
1983 : 86 : return 52;
1984 : : }
1985 : :
1986 : : /**
1987 : : * g_date_get_sunday_weeks_in_year:
1988 : : * @year: year to count weeks in
1989 : : *
1990 : : * Returns the number of weeks in the year, where weeks
1991 : : * are taken to start on Sunday. Will be 52 or 53. The
1992 : : * date must be valid. (Years always have 52 7-day periods,
1993 : : * plus 1 or 2 extra days depending on whether it's a leap
1994 : : * year. This function is basically telling you how many
1995 : : * Sundays are in the year, i.e. there are 53 Sundays if
1996 : : * one of the extra days happens to be a Sunday.)
1997 : : *
1998 : : * Returns: the number of weeks in @year
1999 : : */
2000 : : guint8
2001 : 106 : g_date_get_sunday_weeks_in_year (GDateYear year)
2002 : : {
2003 : : GDate d;
2004 : :
2005 : 106 : g_return_val_if_fail (g_date_valid_year (year), 0);
2006 : :
2007 : 105 : g_date_clear (&d, 1);
2008 : 105 : g_date_set_dmy (&d, 1, 1, year);
2009 [ + + ]: 105 : if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2010 : 90 : g_date_set_dmy (&d, 31, 12, year);
2011 [ + + ]: 90 : if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2012 [ + + ]: 85 : if (g_date_is_leap_year (year))
2013 : : {
2014 : 12 : g_date_set_dmy (&d, 2, 1, year);
2015 [ - + ]: 12 : if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2016 : 12 : g_date_set_dmy (&d, 30, 12, year);
2017 [ - + ]: 12 : if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2018 : : }
2019 : 85 : return 52;
2020 : : }
2021 : :
2022 : : /**
2023 : : * g_date_compare:
2024 : : * @lhs: first date to compare
2025 : : * @rhs: second date to compare
2026 : : *
2027 : : * qsort()-style comparison function for dates.
2028 : : * Both dates must be valid.
2029 : : *
2030 : : * Returns: 0 for equal, less than zero if @lhs is less than @rhs,
2031 : : * greater than zero if @lhs is greater than @rhs
2032 : : */
2033 : : gint
2034 : 4709553 : g_date_compare (const GDate *lhs,
2035 : : const GDate *rhs)
2036 : : {
2037 : 4709553 : g_return_val_if_fail (lhs != NULL, 0);
2038 : 4709552 : g_return_val_if_fail (rhs != NULL, 0);
2039 : 4709551 : g_return_val_if_fail (g_date_valid (lhs), 0);
2040 : 4709550 : g_return_val_if_fail (g_date_valid (rhs), 0);
2041 : :
2042 : : /* Remember the self-comparison case! I think it works right now. */
2043 : :
2044 : : while (TRUE)
2045 : : {
2046 [ + + + + ]: 6228750 : if (lhs->julian && rhs->julian)
2047 : : {
2048 [ + + ]: 1595171 : if (lhs->julian_days < rhs->julian_days) return -1;
2049 [ + + ]: 1595167 : else if (lhs->julian_days > rhs->julian_days) return 1;
2050 : 37984 : else return 0;
2051 : : }
2052 [ + + + - ]: 4633579 : else if (lhs->dmy && rhs->dmy)
2053 : : {
2054 [ + + ]: 3114378 : if (lhs->year < rhs->year) return -1;
2055 [ + + ]: 3114372 : else if (lhs->year > rhs->year) return 1;
2056 : : else
2057 : : {
2058 [ + + ]: 37986 : if (lhs->month < rhs->month) return -1;
2059 [ + + ]: 37985 : else if (lhs->month > rhs->month) return 1;
2060 : : else
2061 : : {
2062 [ + + ]: 4 : if (lhs->day < rhs->day) return -1;
2063 [ + + ]: 3 : else if (lhs->day > rhs->day) return 1;
2064 : 2 : else return 0;
2065 : : }
2066 : :
2067 : : }
2068 : :
2069 : : }
2070 : : else
2071 : : {
2072 [ - + ]: 1519201 : if (!lhs->julian) g_date_update_julian (lhs);
2073 [ + - ]: 1519201 : if (!rhs->julian) g_date_update_julian (rhs);
2074 : 1519201 : g_return_val_if_fail (lhs->julian, 0);
2075 : 1519201 : g_return_val_if_fail (rhs->julian, 0);
2076 : : }
2077 : :
2078 : : }
2079 : : return 0; /* warnings */
2080 : : }
2081 : :
2082 : : /**
2083 : : * g_date_to_struct_tm:
2084 : : * @date: a #GDate to set the struct tm from
2085 : : * @tm: (not nullable): struct tm to fill
2086 : : *
2087 : : * Fills in the date-related bits of a struct tm using the @date value.
2088 : : * Initializes the non-date parts with something safe but meaningless.
2089 : : */
2090 : : void
2091 : 297 : g_date_to_struct_tm (const GDate *d,
2092 : : struct tm *tm)
2093 : : {
2094 : : GDateWeekday day;
2095 : :
2096 : 297 : g_return_if_fail (g_date_valid (d));
2097 : 296 : g_return_if_fail (tm != NULL);
2098 : :
2099 [ + + ]: 295 : if (!d->dmy)
2100 : 1 : g_date_update_dmy (d);
2101 : :
2102 : 295 : g_return_if_fail (d->dmy != 0);
2103 : :
2104 : : /* zero all the irrelevant fields to be sure they're valid */
2105 : :
2106 : : /* On Linux and maybe other systems, there are weird non-POSIX
2107 : : * fields on the end of struct tm that choke strftime if they
2108 : : * contain garbage. So we need to 0 the entire struct, not just the
2109 : : * fields we know to exist.
2110 : : */
2111 : :
2112 : 295 : memset (tm, 0x0, sizeof (struct tm));
2113 : :
2114 : 295 : tm->tm_mday = d->day;
2115 : 295 : tm->tm_mon = d->month - 1; /* 0-11 goes in tm */
2116 : 295 : tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
2117 : :
2118 : 295 : day = g_date_get_weekday (d);
2119 [ + + ]: 295 : if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
2120 : :
2121 : 295 : tm->tm_wday = (int)day;
2122 : :
2123 : 295 : tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
2124 : 295 : tm->tm_isdst = -1; /* -1 means "information not available" */
2125 : : }
2126 : :
2127 : : /**
2128 : : * g_date_clamp:
2129 : : * @date: a #GDate to clamp
2130 : : * @min_date: minimum accepted value for @date
2131 : : * @max_date: maximum accepted value for @date
2132 : : *
2133 : : * If @date is prior to @min_date, sets @date equal to @min_date.
2134 : : * If @date falls after @max_date, sets @date equal to @max_date.
2135 : : * Otherwise, @date is unchanged.
2136 : : * Either of @min_date and @max_date may be %NULL.
2137 : : * All non-%NULL dates must be valid.
2138 : : */
2139 : : void
2140 : 10 : g_date_clamp (GDate *date,
2141 : : const GDate *min_date,
2142 : : const GDate *max_date)
2143 : : {
2144 : 10 : g_return_if_fail (g_date_valid (date));
2145 : :
2146 [ + + ]: 9 : if (min_date != NULL)
2147 : 6 : g_return_if_fail (g_date_valid (min_date));
2148 : :
2149 [ + + ]: 7 : if (max_date != NULL)
2150 : 5 : g_return_if_fail (g_date_valid (max_date));
2151 : :
2152 [ + + + - ]: 5 : if (min_date != NULL && max_date != NULL)
2153 : 3 : g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
2154 : :
2155 [ + + + + ]: 4 : if (min_date && g_date_compare (date, min_date) < 0)
2156 : 1 : *date = *min_date;
2157 : :
2158 [ + + + + ]: 4 : if (max_date && g_date_compare (max_date, date) < 0)
2159 : 1 : *date = *max_date;
2160 : : }
2161 : :
2162 : : /**
2163 : : * g_date_order:
2164 : : * @date1: the first date
2165 : : * @date2: the second date
2166 : : *
2167 : : * Checks if @date1 is less than or equal to @date2,
2168 : : * and swap the values if this is not the case.
2169 : : */
2170 : : void
2171 : 4 : g_date_order (GDate *date1,
2172 : : GDate *date2)
2173 : : {
2174 : 4 : g_return_if_fail (g_date_valid (date1));
2175 : 3 : g_return_if_fail (g_date_valid (date2));
2176 : :
2177 [ + + ]: 2 : if (g_date_compare (date1, date2) > 0)
2178 : : {
2179 : 1 : GDate tmp = *date1;
2180 : 1 : *date1 = *date2;
2181 : 1 : *date2 = tmp;
2182 : : }
2183 : : }
2184 : :
2185 : : #ifdef G_OS_WIN32
2186 : : static gboolean
2187 : : append_month_name (GArray *result,
2188 : : LCID lcid,
2189 : : SYSTEMTIME *systemtime,
2190 : : gboolean abbreviated,
2191 : : gboolean alternative)
2192 : : {
2193 : : int n;
2194 : : WORD base;
2195 : : LPCWSTR lpFormat;
2196 : :
2197 : : if (alternative)
2198 : : {
2199 : : base = abbreviated ? LOCALE_SABBREVMONTHNAME1 : LOCALE_SMONTHNAME1;
2200 : : n = GetLocaleInfoW (lcid, base + systemtime->wMonth - 1, NULL, 0);
2201 : : if (n == 0)
2202 : : return FALSE;
2203 : :
2204 : : g_array_set_size (result, result->len + n);
2205 : : if (GetLocaleInfoW (lcid, base + systemtime->wMonth - 1,
2206 : : ((wchar_t *) result->data) + result->len - n, n) != n)
2207 : : return FALSE;
2208 : :
2209 : : g_array_set_size (result, result->len - 1);
2210 : : }
2211 : : else
2212 : : {
2213 : : /* According to MSDN, this is the correct method to obtain
2214 : : * the form of the month name used when formatting a full
2215 : : * date; it must be a genitive case in some languages.
2216 : : *
2217 : : * (n == 0) indicates an error, whereas (n < 2) is something we’d never
2218 : : * expect from the given format string, and would break the subsequent code.
2219 : : */
2220 : : lpFormat = abbreviated ? L"ddMMM" : L"ddMMMM";
2221 : : n = GetDateFormatW (lcid, 0, systemtime, lpFormat, NULL, 0);
2222 : : if (n < 2)
2223 : : return FALSE;
2224 : :
2225 : : g_array_set_size (result, result->len + n);
2226 : : if (GetDateFormatW (lcid, 0, systemtime, lpFormat,
2227 : : ((wchar_t *) result->data) + result->len - n, n) != n)
2228 : : return FALSE;
2229 : :
2230 : : /* We have obtained a day number as two digits and the month name.
2231 : : * Now let's get rid of those two digits: overwrite them with the
2232 : : * month name.
2233 : : */
2234 : : memmove (((wchar_t *) result->data) + result->len - n,
2235 : : ((wchar_t *) result->data) + result->len - n + 2,
2236 : : (n - 2) * sizeof (wchar_t));
2237 : : g_array_set_size (result, result->len - 3);
2238 : : }
2239 : :
2240 : : return TRUE;
2241 : : }
2242 : :
2243 : : static gsize
2244 : : win32_strftime_helper (const GDate *d,
2245 : : const gchar *format,
2246 : : const struct tm *tm,
2247 : : gchar *s,
2248 : : gsize slen)
2249 : : {
2250 : : SYSTEMTIME systemtime;
2251 : : TIME_ZONE_INFORMATION tzinfo;
2252 : : LCID lcid;
2253 : : int n, k;
2254 : : GArray *result;
2255 : : const gchar *p;
2256 : : gunichar c, modifier;
2257 : : const wchar_t digits[] = L"0123456789";
2258 : : gchar *convbuf;
2259 : : glong convlen = 0;
2260 : : gsize retval;
2261 : :
2262 : : systemtime.wYear = tm->tm_year + 1900;
2263 : : systemtime.wMonth = tm->tm_mon + 1;
2264 : : systemtime.wDayOfWeek = tm->tm_wday;
2265 : : systemtime.wDay = tm->tm_mday;
2266 : : systemtime.wHour = tm->tm_hour;
2267 : : systemtime.wMinute = tm->tm_min;
2268 : : systemtime.wSecond = tm->tm_sec;
2269 : : systemtime.wMilliseconds = 0;
2270 : :
2271 : : lcid = GetThreadLocale ();
2272 : : result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
2273 : :
2274 : : p = format;
2275 : : while (*p)
2276 : : {
2277 : : c = g_utf8_get_char (p);
2278 : : if (c == '%')
2279 : : {
2280 : : p = g_utf8_next_char (p);
2281 : : if (!*p)
2282 : : {
2283 : : s[0] = '\0';
2284 : : g_array_free (result, TRUE);
2285 : :
2286 : : return 0;
2287 : : }
2288 : :
2289 : : modifier = '\0';
2290 : : c = g_utf8_get_char (p);
2291 : : if (c == 'E' || c == 'O')
2292 : : {
2293 : : /* "%OB", "%Ob", and "%Oh" are supported, ignore other modified
2294 : : * conversion specifiers for now.
2295 : : */
2296 : : modifier = c;
2297 : : p = g_utf8_next_char (p);
2298 : : if (!*p)
2299 : : {
2300 : : s[0] = '\0';
2301 : : g_array_free (result, TRUE);
2302 : :
2303 : : return 0;
2304 : : }
2305 : :
2306 : : c = g_utf8_get_char (p);
2307 : : }
2308 : :
2309 : : switch (c)
2310 : : {
2311 : : case 'a':
2312 : : if (systemtime.wDayOfWeek == 0)
2313 : : k = 6;
2314 : : else
2315 : : k = systemtime.wDayOfWeek - 1;
2316 : : n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
2317 : : g_array_set_size (result, result->len + n);
2318 : : GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2319 : : g_array_set_size (result, result->len - 1);
2320 : : break;
2321 : : case 'A':
2322 : : if (systemtime.wDayOfWeek == 0)
2323 : : k = 6;
2324 : : else
2325 : : k = systemtime.wDayOfWeek - 1;
2326 : : n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
2327 : : g_array_set_size (result, result->len + n);
2328 : : GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2329 : : g_array_set_size (result, result->len - 1);
2330 : : break;
2331 : : case 'b':
2332 : : case 'h':
2333 : : if (!append_month_name (result, lcid, &systemtime, TRUE, modifier == 'O'))
2334 : : {
2335 : : /* Ignore the error; this placeholder will be replaced with nothing */
2336 : : }
2337 : : break;
2338 : : case 'B':
2339 : : if (!append_month_name (result, lcid, &systemtime, FALSE, modifier == 'O'))
2340 : : {
2341 : : /* Ignore the error; this placeholder will be replaced with nothing */
2342 : : }
2343 : : break;
2344 : : case 'c':
2345 : : n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2346 : : if (n > 0)
2347 : : {
2348 : : g_array_set_size (result, result->len + n);
2349 : : GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2350 : : g_array_set_size (result, result->len - 1);
2351 : : }
2352 : : g_array_append_vals (result, L" ", 1);
2353 : : n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2354 : : if (n > 0)
2355 : : {
2356 : : g_array_set_size (result, result->len + n);
2357 : : GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2358 : : g_array_set_size (result, result->len - 1);
2359 : : }
2360 : : break;
2361 : : case 'C':
2362 : : g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2363 : : g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
2364 : : break;
2365 : : case 'd':
2366 : : g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2367 : : g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2368 : : break;
2369 : : case 'D':
2370 : : g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2371 : : g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2372 : : g_array_append_vals (result, L"/", 1);
2373 : : g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2374 : : g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2375 : : g_array_append_vals (result, L"/", 1);
2376 : : g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2377 : : g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2378 : : break;
2379 : : case 'e':
2380 : : if (systemtime.wDay >= 10)
2381 : : g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2382 : : else
2383 : : g_array_append_vals (result, L" ", 1);
2384 : : g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2385 : : break;
2386 : :
2387 : : /* A GDate has no time fields, so for now we can
2388 : : * hardcode all time conversions into zeros (or 12 for
2389 : : * %I). The alternative code snippets in the #else
2390 : : * branches are here ready to be taken into use when
2391 : : * needed by a g_strftime() or g_date_and_time_format()
2392 : : * or whatever.
2393 : : */
2394 : : case 'H':
2395 : : #if 1
2396 : : g_array_append_vals (result, L"00", 2);
2397 : : #else
2398 : : g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2399 : : g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2400 : : #endif
2401 : : break;
2402 : : case 'I':
2403 : : #if 1
2404 : : g_array_append_vals (result, L"12", 2);
2405 : : #else
2406 : : if (systemtime.wHour == 0)
2407 : : g_array_append_vals (result, L"12", 2);
2408 : : else
2409 : : {
2410 : : g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2411 : : g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2412 : : }
2413 : : #endif
2414 : : break;
2415 : : case 'j':
2416 : : g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
2417 : : g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
2418 : : g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
2419 : : break;
2420 : : case 'm':
2421 : : g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2422 : : g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2423 : : break;
2424 : : case 'M':
2425 : : #if 1
2426 : : g_array_append_vals (result, L"00", 2);
2427 : : #else
2428 : : g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2429 : : g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2430 : : #endif
2431 : : break;
2432 : : case 'n':
2433 : : g_array_append_vals (result, L"\n", 1);
2434 : : break;
2435 : : case 'p':
2436 : : n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2437 : : if (n > 0)
2438 : : {
2439 : : g_array_set_size (result, result->len + n);
2440 : : GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2441 : : g_array_set_size (result, result->len - 1);
2442 : : }
2443 : : break;
2444 : : case 'r':
2445 : : /* This is a rather odd format. Hard to say what to do.
2446 : : * Let's always use the POSIX %I:%M:%S %p
2447 : : */
2448 : : #if 1
2449 : : g_array_append_vals (result, L"12:00:00", 8);
2450 : : #else
2451 : : if (systemtime.wHour == 0)
2452 : : g_array_append_vals (result, L"12", 2);
2453 : : else
2454 : : {
2455 : : g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2456 : : g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2457 : : }
2458 : : g_array_append_vals (result, L":", 1);
2459 : : g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2460 : : g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2461 : : g_array_append_vals (result, L":", 1);
2462 : : g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2463 : : g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2464 : : g_array_append_vals (result, L" ", 1);
2465 : : #endif
2466 : : n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2467 : : if (n > 0)
2468 : : {
2469 : : g_array_set_size (result, result->len + n);
2470 : : GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2471 : : g_array_set_size (result, result->len - 1);
2472 : : }
2473 : : break;
2474 : : case 'R':
2475 : : #if 1
2476 : : g_array_append_vals (result, L"00:00", 5);
2477 : : #else
2478 : : g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2479 : : g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2480 : : g_array_append_vals (result, L":", 1);
2481 : : g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2482 : : g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2483 : : #endif
2484 : : break;
2485 : : case 'S':
2486 : : #if 1
2487 : : g_array_append_vals (result, L"00", 2);
2488 : : #else
2489 : : g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2490 : : g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2491 : : #endif
2492 : : break;
2493 : : case 't':
2494 : : g_array_append_vals (result, L"\t", 1);
2495 : : break;
2496 : : case 'T':
2497 : : #if 1
2498 : : g_array_append_vals (result, L"00:00:00", 8);
2499 : : #else
2500 : : g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2501 : : g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2502 : : g_array_append_vals (result, L":", 1);
2503 : : g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2504 : : g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2505 : : g_array_append_vals (result, L":", 1);
2506 : : g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2507 : : g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2508 : : #endif
2509 : : break;
2510 : : case 'u':
2511 : : if (systemtime.wDayOfWeek == 0)
2512 : : g_array_append_vals (result, L"7", 1);
2513 : : else
2514 : : g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2515 : : break;
2516 : : case 'U':
2517 : : n = g_date_get_sunday_week_of_year (d);
2518 : : g_array_append_vals (result, digits + n/10, 1);
2519 : : g_array_append_vals (result, digits + n%10, 1);
2520 : : break;
2521 : : case 'V':
2522 : : n = g_date_get_iso8601_week_of_year (d);
2523 : : g_array_append_vals (result, digits + n/10, 1);
2524 : : g_array_append_vals (result, digits + n%10, 1);
2525 : : break;
2526 : : case 'w':
2527 : : g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2528 : : break;
2529 : : case 'W':
2530 : : n = g_date_get_monday_week_of_year (d);
2531 : : g_array_append_vals (result, digits + n/10, 1);
2532 : : g_array_append_vals (result, digits + n%10, 1);
2533 : : break;
2534 : : case 'x':
2535 : : n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2536 : : if (n > 0)
2537 : : {
2538 : : g_array_set_size (result, result->len + n);
2539 : : GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2540 : : g_array_set_size (result, result->len - 1);
2541 : : }
2542 : : break;
2543 : : case 'X':
2544 : : n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2545 : : if (n > 0)
2546 : : {
2547 : : g_array_set_size (result, result->len + n);
2548 : : GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2549 : : g_array_set_size (result, result->len - 1);
2550 : : }
2551 : : break;
2552 : : case 'y':
2553 : : g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2554 : : g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2555 : : break;
2556 : : case 'Y':
2557 : : g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2558 : : g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
2559 : : g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2560 : : g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2561 : : break;
2562 : : case 'Z':
2563 : : n = GetTimeZoneInformation (&tzinfo);
2564 : : if (n == TIME_ZONE_ID_UNKNOWN || n == TIME_ZONE_ID_STANDARD)
2565 : : g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
2566 : : else if (n == TIME_ZONE_ID_DAYLIGHT)
2567 : : g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
2568 : : break;
2569 : : case '%':
2570 : : g_array_append_vals (result, L"%", 1);
2571 : : break;
2572 : : }
2573 : : }
2574 : : else if (c <= 0xFFFF)
2575 : : {
2576 : : wchar_t wc = c;
2577 : : g_array_append_vals (result, &wc, 1);
2578 : : }
2579 : : else
2580 : : {
2581 : : glong nwc;
2582 : : wchar_t *ws;
2583 : :
2584 : : ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
2585 : : g_array_append_vals (result, ws, nwc);
2586 : : g_free (ws);
2587 : : }
2588 : : p = g_utf8_next_char (p);
2589 : : }
2590 : :
2591 : : convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
2592 : : g_array_free (result, TRUE);
2593 : :
2594 : : if (!convbuf)
2595 : : {
2596 : : s[0] = '\0';
2597 : : return 0;
2598 : : }
2599 : :
2600 : : g_assert (convlen >= 0);
2601 : : if ((gsize) convlen >= slen)
2602 : : {
2603 : : /* Ensure only whole characters are copied into the buffer. */
2604 : : gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2605 : : g_assert (end != NULL);
2606 : : convlen = end - convbuf;
2607 : :
2608 : : /* Return 0 because the buffer isn't large enough. */
2609 : : retval = 0;
2610 : : }
2611 : : else
2612 : : retval = convlen;
2613 : :
2614 : : memcpy (s, convbuf, convlen);
2615 : : s[convlen] = '\0';
2616 : : g_free (convbuf);
2617 : :
2618 : : return retval;
2619 : : }
2620 : :
2621 : : #endif
2622 : :
2623 : : /**
2624 : : * g_date_strftime:
2625 : : * @s: destination buffer
2626 : : * @slen: buffer size
2627 : : * @format: format string
2628 : : * @date: valid #GDate
2629 : : *
2630 : : * Generates a printed representation of the date, in a
2631 : : * [locale][setlocale]-specific way.
2632 : : * Works just like the platform's C library strftime() function,
2633 : : * but only accepts date-related formats; time-related formats
2634 : : * give undefined results. Date must be valid. Unlike strftime()
2635 : : * (which uses the locale encoding), works on a UTF-8 format
2636 : : * string and stores a UTF-8 result.
2637 : : *
2638 : : * This function does not provide any conversion specifiers in
2639 : : * addition to those implemented by the platform's C library.
2640 : : * For example, don't expect that using g_date_strftime() would
2641 : : * make the \%F provided by the C99 strftime() work on Windows
2642 : : * where the C library only complies to C89.
2643 : : *
2644 : : * Returns: number of characters written to the buffer, or 0 the buffer was too small
2645 : : */
2646 : : #pragma GCC diagnostic push
2647 : : #pragma GCC diagnostic ignored "-Wformat-nonliteral"
2648 : :
2649 : : gsize
2650 : 299 : g_date_strftime (gchar *s,
2651 : : gsize slen,
2652 : : const gchar *format,
2653 : : const GDate *d)
2654 : : {
2655 : : struct tm tm;
2656 : : #ifndef G_OS_WIN32
2657 : 299 : gsize locale_format_len = 0;
2658 : : gchar *locale_format;
2659 : : gsize tmplen;
2660 : : gchar *tmpbuf;
2661 : : gsize tmpbufsize;
2662 : 299 : gsize convlen = 0;
2663 : : gchar *convbuf;
2664 : 299 : GError *error = NULL;
2665 : : gsize retval;
2666 : : #endif
2667 : :
2668 : 299 : g_return_val_if_fail (g_date_valid (d), 0);
2669 : 298 : g_return_val_if_fail (slen > 0, 0);
2670 : 297 : g_return_val_if_fail (format != NULL, 0);
2671 : 296 : g_return_val_if_fail (s != NULL, 0);
2672 : :
2673 : 295 : g_date_to_struct_tm (d, &tm);
2674 : :
2675 : : #ifdef G_OS_WIN32
2676 : : if (!g_utf8_validate (format, -1, NULL))
2677 : : {
2678 : : s[0] = '\0';
2679 : : return 0;
2680 : : }
2681 : : return win32_strftime_helper (d, format, &tm, s, slen);
2682 : : #else
2683 : :
2684 : 295 : locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2685 : :
2686 [ + + ]: 295 : if (error)
2687 : : {
2688 : 1 : g_warning (G_STRLOC "Error converting format to locale encoding: %s", error->message);
2689 : 1 : g_error_free (error);
2690 : :
2691 : 1 : s[0] = '\0';
2692 : 1 : return 0;
2693 : : }
2694 : :
2695 : 294 : tmpbufsize = MAX (128, locale_format_len * 2);
2696 : : while (TRUE)
2697 : : {
2698 : 294 : tmpbuf = g_malloc (tmpbufsize);
2699 : :
2700 : : /* Set the first byte to something other than '\0', to be able to
2701 : : * recognize whether strftime actually failed or just returned "".
2702 : : */
2703 : 294 : tmpbuf[0] = '\1';
2704 : 294 : tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
2705 : :
2706 [ + + - + ]: 294 : if (tmplen == 0 && tmpbuf[0] != '\0')
2707 : : {
2708 : 0 : g_free (tmpbuf);
2709 : 0 : tmpbufsize *= 2;
2710 : :
2711 [ # # ]: 0 : if (tmpbufsize > 65536)
2712 : : {
2713 : 0 : g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up");
2714 : 0 : g_free (locale_format);
2715 : :
2716 : 0 : s[0] = '\0';
2717 : 0 : return 0;
2718 : : }
2719 : : }
2720 : : else
2721 : : break;
2722 : : }
2723 : 294 : g_free (locale_format);
2724 : :
2725 : 294 : convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2726 : 294 : g_free (tmpbuf);
2727 : :
2728 [ - + ]: 294 : if (error)
2729 : : {
2730 : 0 : g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s", error->message);
2731 : 0 : g_error_free (error);
2732 : :
2733 : 0 : g_assert (convbuf == NULL);
2734 : :
2735 : 0 : s[0] = '\0';
2736 : 0 : return 0;
2737 : : }
2738 : :
2739 [ - + ]: 294 : if (slen <= convlen)
2740 : : {
2741 : : /* Ensure only whole characters are copied into the buffer.
2742 : : */
2743 : 0 : gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2744 : 0 : g_assert (end != NULL);
2745 : 0 : convlen = end - convbuf;
2746 : :
2747 : : /* Return 0 because the buffer isn't large enough.
2748 : : */
2749 : 0 : retval = 0;
2750 : : }
2751 : : else
2752 : 294 : retval = convlen;
2753 : :
2754 : 294 : memcpy (s, convbuf, convlen);
2755 : 294 : s[convlen] = '\0';
2756 : 294 : g_free (convbuf);
2757 : :
2758 : 294 : return retval;
2759 : : #endif
2760 : : }
2761 : :
2762 : : #pragma GCC diagnostic pop
|