Coverage Report

Created: 2025-07-12 07:23

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