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