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