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