Coverage Report

Created: 2025-08-29 06:14

/src/tinysparql/subprojects/glib-2.80.3/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
#ifdef G_ENABLE_DEBUG
588
  if (!g_date_valid_dmy (day, m, y)) 
589
    g_warning ("OOPS julian: %u  computed dmy: %u %u %u",
590
         d->julian_days, day, m, y);
591
#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 *d)
746
0
{
747
0
  GDateWeekday wd;
748
0
  guint day;
749
0
  GDate first;
750
  
751
0
  g_return_val_if_fail (g_date_valid (d), 0);
752
  
753
0
  if (!d->dmy) 
754
0
    g_date_update_dmy (d);
755
756
0
  g_return_val_if_fail (d->dmy, 0);  
757
  
758
0
  g_date_clear (&first, 1);
759
  
760
0
  g_date_set_dmy (&first, 1, 1, d->year);
761
  
762
0
  wd = g_date_get_weekday (&first) - 1; /* make Monday day 0 */
763
0
  day = g_date_get_day_of_year (d) - 1;
764
  
765
0
  return ((day + wd)/7U + (wd == 0 ? 1 : 0));
766
0
}
767
768
/**
769
 * g_date_get_sunday_week_of_year:
770
 * @date: a #GDate
771
 *
772
 * Returns the week of the year during which this date falls, if
773
 * weeks are understood to begin on Sunday. The date must be valid.
774
 * Can return 0 if the day is before the first Sunday of the year.
775
 *
776
 * Returns: week number
777
 */
778
guint        
779
g_date_get_sunday_week_of_year (const GDate *d)
780
0
{
781
0
  GDateWeekday wd;
782
0
  guint day;
783
0
  GDate first;
784
  
785
0
  g_return_val_if_fail (g_date_valid (d), 0);
786
  
787
0
  if (!d->dmy) 
788
0
    g_date_update_dmy (d);
789
790
0
  g_return_val_if_fail (d->dmy, 0);  
791
  
792
0
  g_date_clear (&first, 1);
793
  
794
0
  g_date_set_dmy (&first, 1, 1, d->year);
795
  
796
0
  wd = g_date_get_weekday (&first);
797
0
  if (wd == 7) wd = 0; /* make Sunday day 0 */
798
0
  day = g_date_get_day_of_year (d) - 1;
799
  
800
0
  return ((day + wd)/7U + (wd == 0 ? 1 : 0));
801
0
}
802
803
/**
804
 * g_date_get_iso8601_week_of_year:
805
 * @date: a valid #GDate
806
 *
807
 * Returns the week of the year, where weeks are interpreted according
808
 * to ISO 8601. 
809
 * 
810
 * Returns: ISO 8601 week number of the year.
811
 *
812
 * Since: 2.6
813
 **/
814
guint
815
g_date_get_iso8601_week_of_year (const GDate *d)
816
0
{
817
0
  guint j, d4, L, d1, w;
818
819
0
  g_return_val_if_fail (g_date_valid (d), 0);
820
  
821
0
  if (!d->julian)
822
0
    g_date_update_julian (d);
823
824
0
  g_return_val_if_fail (d->julian, 0);
825
826
  /* Formula taken from the Calendar FAQ; the formula was for the
827
   * Julian Period which starts on 1 January 4713 BC, so we add
828
   * 1,721,425 to the number of days before doing the formula. 
829
   */
830
0
  j  = d->julian_days + 1721425;
831
0
  d4 = (j + 31741 - (j % 7)) % 146097 % 36524 % 1461;
832
0
  L  = d4 / 1460;
833
0
  d1 = ((d4 - L) % 365) + L;
834
0
  w  = d1 / 7 + 1;
835
836
0
  return w;
837
0
}
838
839
/**
840
 * g_date_days_between:
841
 * @date1: the first date
842
 * @date2: the second date
843
 *
844
 * Computes the number of days between two dates.
845
 * If @date2 is prior to @date1, the returned value is negative.
846
 * Both dates must be valid.
847
 *
848
 * Returns: the number of days between @date1 and @date2
849
 */
850
gint
851
g_date_days_between (const GDate *d1,
852
         const GDate *d2)
853
0
{
854
0
  g_return_val_if_fail (g_date_valid (d1), 0);
855
0
  g_return_val_if_fail (g_date_valid (d2), 0);
856
857
0
  return (gint)g_date_get_julian (d2) - (gint)g_date_get_julian (d1);
858
0
}
859
860
/**
861
 * g_date_clear:
862
 * @date: pointer to one or more dates to clear
863
 * @n_dates: number of dates to clear
864
 *
865
 * Initializes one or more #GDate structs to a safe but invalid
866
 * state. The cleared dates will not represent an existing date, but will
867
 * not contain garbage. Useful to init a date declared on the stack.
868
 * Validity can be tested with g_date_valid().
869
 */
870
void         
871
g_date_clear (GDate *d, guint ndates)
872
0
{
873
0
  g_return_if_fail (d != NULL);
874
0
  g_return_if_fail (ndates != 0);
875
  
876
0
  memset (d, 0x0, ndates*sizeof (GDate)); 
877
0
}
878
879
G_LOCK_DEFINE_STATIC (g_date_global);
880
881
/* These are for the parser, output to the user should use *
882
 * g_date_strftime () - this creates more never-freed memory to annoy
883
 * all those memory debugger users. :-) 
884
 */
885
886
static gchar *long_month_names[13] = 
887
{ 
888
  NULL,
889
};
890
891
static gchar *long_month_names_alternative[13] =
892
{
893
  NULL,
894
};
895
896
static gchar *short_month_names[13] = 
897
{
898
  NULL, 
899
};
900
901
static gchar *short_month_names_alternative[13] =
902
{
903
  NULL,
904
};
905
906
/* This tells us if we need to update the parse info */
907
static gchar *current_locale = NULL;
908
909
/* order of these in the current locale */
910
static GDateDMY dmy_order[3] = 
911
{
912
   G_DATE_DAY, G_DATE_MONTH, G_DATE_YEAR
913
};
914
915
/* Where to chop two-digit years: i.e., for the 1930 default, numbers
916
 * 29 and below are counted as in the year 2000, numbers 30 and above
917
 * are counted as in the year 1900.  
918
 */
919
920
static const GDateYear twodigit_start_year = 1930;
921
922
/* It is impossible to enter a year between 1 AD and 99 AD with this
923
 * in effect.  
924
 */
925
static gboolean using_twodigit_years = FALSE;
926
927
/* Adjustment of locale era to AD, non-zero means using locale era
928
 */
929
static gint locale_era_adjust = 0;
930
931
struct _GDateParseTokens {
932
  gint num_ints;
933
  gint n[3];
934
  guint month;
935
};
936
937
typedef struct _GDateParseTokens GDateParseTokens;
938
939
static inline gboolean
940
update_month_match (gsize *longest,
941
                    const gchar *haystack,
942
                    const gchar *needle)
943
0
{
944
0
  gsize length;
945
946
0
  if (needle == NULL)
947
0
    return FALSE;
948
949
0
  length = strlen (needle);
950
0
  if (*longest >= length)
951
0
    return FALSE;
952
953
0
  if (strstr (haystack, needle) == NULL)
954
0
    return FALSE;
955
956
0
  *longest = length;
957
0
  return TRUE;
958
0
}
959
960
0
#define NUM_LEN 10
961
962
/* HOLDS: g_date_global_lock */
963
static void
964
g_date_fill_parse_tokens (const gchar *str, GDateParseTokens *pt)
965
0
{
966
0
  gchar num[4][NUM_LEN+1];
967
0
  gint i;
968
0
  const guchar *s;
969
  
970
  /* We count 4, but store 3; so we can give an error
971
   * if there are 4.
972
   */
973
0
  num[0][0] = num[1][0] = num[2][0] = num[3][0] = '\0';
974
  
975
0
  s = (const guchar *) str;
976
0
  pt->num_ints = 0;
977
0
  while (*s && pt->num_ints < 4) 
978
0
    {
979
      
980
0
      i = 0;
981
0
      while (*s && g_ascii_isdigit (*s) && i < NUM_LEN)
982
0
        {
983
0
          num[pt->num_ints][i] = *s;
984
0
          ++s; 
985
0
          ++i;
986
0
        }
987
      
988
0
      if (i > 0) 
989
0
        {
990
0
          num[pt->num_ints][i] = '\0';
991
0
          ++(pt->num_ints);
992
0
        }
993
      
994
0
      if (*s == '\0') break;
995
      
996
0
      ++s;
997
0
    }
998
  
999
0
  pt->n[0] = pt->num_ints > 0 ? atoi (num[0]) : 0;
1000
0
  pt->n[1] = pt->num_ints > 1 ? atoi (num[1]) : 0;
1001
0
  pt->n[2] = pt->num_ints > 2 ? atoi (num[2]) : 0;
1002
  
1003
0
  pt->month = G_DATE_BAD_MONTH;
1004
  
1005
0
  if (pt->num_ints < 3)
1006
0
    {
1007
0
      gsize longest = 0;
1008
0
      gchar *casefold;
1009
0
      gchar *normalized;
1010
      
1011
0
      casefold = g_utf8_casefold (str, -1);
1012
0
      normalized = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1013
0
      g_free (casefold);
1014
1015
0
      for (i = 1; i < 13; ++i)
1016
0
        {
1017
          /* Here month names may be in a genitive case if the language
1018
           * grammatical rules require it.
1019
           * Examples of how January may look in some languages:
1020
           * Catalan: "de gener", Croatian: "siječnja", Polish: "stycznia",
1021
           * Upper Sorbian: "januara".
1022
           * Note that most of the languages can't or don't use the the
1023
           * genitive case here so they use nominative everywhere.
1024
           * For example, English always uses "January".
1025
           */
1026
0
          if (update_month_match (&longest, normalized, long_month_names[i]))
1027
0
            pt->month = i;
1028
1029
          /* Here month names will be in a nominative case.
1030
           * Examples of how January may look in some languages:
1031
           * Catalan: "gener", Croatian: "Siječanj", Polish: "styczeń",
1032
           * Upper Sorbian: "Januar".
1033
           */
1034
0
          if (update_month_match (&longest, normalized, long_month_names_alternative[i]))
1035
0
            pt->month = i;
1036
1037
          /* Differences between abbreviated nominative and abbreviated
1038
           * genitive month names are visible in very few languages but
1039
           * let's handle them.
1040
           */
1041
0
          if (update_month_match (&longest, normalized, short_month_names[i]))
1042
0
            pt->month = i;
1043
1044
0
          if (update_month_match (&longest, normalized, short_month_names_alternative[i]))
1045
0
            pt->month = i;
1046
0
        }
1047
1048
0
      g_free (normalized);
1049
0
    }
1050
0
}
1051
1052
/* HOLDS: g_date_global_lock */
1053
static void
1054
g_date_prepare_to_parse (const gchar      *str, 
1055
                         GDateParseTokens *pt)
1056
0
{
1057
0
  const gchar *locale = setlocale (LC_TIME, NULL);
1058
0
  gboolean recompute_localeinfo = FALSE;
1059
0
  GDate d;
1060
  
1061
0
  g_return_if_fail (locale != NULL); /* should not happen */
1062
  
1063
0
  g_date_clear (&d, 1);              /* clear for scratch use */
1064
  
1065
0
  if ( (current_locale == NULL) || (strcmp (locale, current_locale) != 0) ) 
1066
0
    recompute_localeinfo = TRUE;  /* Uh, there used to be a reason for the temporary */
1067
  
1068
0
  if (recompute_localeinfo)
1069
0
    {
1070
0
      int i = 1;
1071
0
      GDateParseTokens testpt;
1072
0
      gchar buf[128];
1073
      
1074
0
      g_free (current_locale); /* still works if current_locale == NULL */
1075
      
1076
0
      current_locale = g_strdup (locale);
1077
      
1078
0
      short_month_names[0] = "Error";
1079
0
      long_month_names[0] = "Error";
1080
1081
0
      while (i < 13) 
1082
0
        {
1083
0
    gchar *casefold;
1084
    
1085
0
          g_date_set_dmy (&d, 1, i, 1976);
1086
    
1087
0
          g_return_if_fail (g_date_valid (&d));
1088
    
1089
0
          g_date_strftime (buf, 127, "%b", &d);
1090
1091
0
    casefold = g_utf8_casefold (buf, -1);
1092
0
          g_free (short_month_names[i]);
1093
0
          short_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1094
0
    g_free (casefold);
1095
    
1096
0
          g_date_strftime (buf, 127, "%B", &d);
1097
0
    casefold = g_utf8_casefold (buf, -1);
1098
0
          g_free (long_month_names[i]);
1099
0
          long_month_names[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1100
0
    g_free (casefold);
1101
          
1102
0
          g_date_strftime (buf, 127, "%Ob", &d);
1103
0
          casefold = g_utf8_casefold (buf, -1);
1104
0
          g_free (short_month_names_alternative[i]);
1105
0
          short_month_names_alternative[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 (long_month_names_alternative[i]);
1111
0
          long_month_names_alternative[i] = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL);
1112
0
          g_free (casefold);
1113
1114
0
          ++i;
1115
0
        }
1116
      
1117
      /* Determine DMY order */
1118
      
1119
      /* had to pick a random day - don't change this, some strftimes
1120
       * are broken on some days, and this one is good so far. */
1121
0
      g_date_set_dmy (&d, 4, 7, 1976);
1122
      
1123
0
      g_date_strftime (buf, 127, "%x", &d);
1124
      
1125
0
      g_date_fill_parse_tokens (buf, &testpt);
1126
1127
0
      using_twodigit_years = FALSE;
1128
0
      locale_era_adjust = 0;
1129
0
      dmy_order[0] = G_DATE_DAY;
1130
0
      dmy_order[1] = G_DATE_MONTH;
1131
0
      dmy_order[2] = G_DATE_YEAR;
1132
      
1133
0
      i = 0;
1134
0
      while (i < testpt.num_ints)
1135
0
        {
1136
0
          switch (testpt.n[i])
1137
0
            {
1138
0
            case 7:
1139
0
              dmy_order[i] = G_DATE_MONTH;
1140
0
              break;
1141
0
            case 4:
1142
0
              dmy_order[i] = G_DATE_DAY;
1143
0
              break;
1144
0
            case 76:
1145
0
              using_twodigit_years = TRUE;
1146
0
              G_GNUC_FALLTHROUGH;
1147
0
            case 1976:
1148
0
              dmy_order[i] = G_DATE_YEAR;
1149
0
              break;
1150
0
            default:
1151
              /* assume locale era */
1152
0
              locale_era_adjust = 1976 - testpt.n[i];
1153
0
              dmy_order[i] = G_DATE_YEAR;
1154
0
              break;
1155
0
            }
1156
0
          ++i;
1157
0
        }
1158
      
1159
#if defined(G_ENABLE_DEBUG) && 0
1160
      DEBUG_MSG (("**GDate prepared a new set of locale-specific parse rules."));
1161
      i = 1;
1162
      while (i < 13) 
1163
        {
1164
          DEBUG_MSG (("  %s   %s", long_month_names[i], short_month_names[i]));
1165
          ++i;
1166
        }
1167
      DEBUG_MSG (("Alternative month names:"));
1168
      i = 1;
1169
      while (i < 13)
1170
        {
1171
          DEBUG_MSG (("  %s   %s", long_month_names_alternative[i], short_month_names_alternative[i]));
1172
          ++i;
1173
        }
1174
      if (using_twodigit_years)
1175
        {
1176
    DEBUG_MSG (("**Using twodigit years with cutoff year: %u", twodigit_start_year));
1177
        }
1178
      { 
1179
        gchar *strings[3];
1180
        i = 0;
1181
        while (i < 3)
1182
          {
1183
            switch (dmy_order[i])
1184
              {
1185
              case G_DATE_MONTH:
1186
                strings[i] = "Month";
1187
                break;
1188
              case G_DATE_YEAR:
1189
                strings[i] = "Year";
1190
                break;
1191
              case G_DATE_DAY:
1192
                strings[i] = "Day";
1193
                break;
1194
              default:
1195
                strings[i] = NULL;
1196
                break;
1197
              }
1198
            ++i;
1199
          }
1200
        DEBUG_MSG (("**Order: %s, %s, %s", strings[0], strings[1], strings[2]));
1201
        DEBUG_MSG (("**Sample date in this locale: '%s'", buf));
1202
      }
1203
#endif
1204
0
    }
1205
  
1206
0
  g_date_fill_parse_tokens (str, pt);
1207
0
}
1208
1209
static guint
1210
convert_twodigit_year (guint y)
1211
0
{
1212
0
  if (using_twodigit_years && y < 100)
1213
0
    {
1214
0
      guint two     =  twodigit_start_year % 100;
1215
0
      guint century = (twodigit_start_year / 100) * 100;
1216
1217
0
      if (y < two)
1218
0
        century += 100;
1219
1220
0
      y += century;
1221
0
    }
1222
0
  return y;
1223
0
}
1224
1225
/**
1226
 * g_date_set_parse:
1227
 * @date: a #GDate to fill in
1228
 * @str: string to parse
1229
 *
1230
 * Parses a user-inputted string @str, and try to figure out what date it
1231
 * represents, taking the [current locale][setlocale] into account. If the
1232
 * string is successfully parsed, the date will be valid after the call.
1233
 * Otherwise, it will be invalid. You should check using g_date_valid()
1234
 * to see whether the parsing succeeded.
1235
 *
1236
 * This function is not appropriate for file formats and the like; it
1237
 * isn't very precise, and its exact behavior varies with the locale.
1238
 * It's intended to be a heuristic routine that guesses what the user
1239
 * means by a given string (and it does work pretty well in that
1240
 * capacity).
1241
 */
1242
void         
1243
g_date_set_parse (GDate       *d, 
1244
                  const gchar *str)
1245
0
{
1246
0
  GDateParseTokens pt;
1247
0
  guint m = G_DATE_BAD_MONTH, day = G_DATE_BAD_DAY, y = G_DATE_BAD_YEAR;
1248
0
  gsize str_len;
1249
  
1250
0
  g_return_if_fail (d != NULL);
1251
  
1252
  /* set invalid */
1253
0
  g_date_clear (d, 1);
1254
1255
  /* Anything longer than this is ridiculous and could take a while to normalize.
1256
   * This limit is chosen arbitrarily. */
1257
0
  str_len = strlen (str);
1258
0
  if (str_len > 200)
1259
0
    return;
1260
1261
  /* The input has to be valid UTF-8. */
1262
0
  if (!g_utf8_validate_len (str, str_len, NULL))
1263
0
    return;
1264
1265
0
  G_LOCK (g_date_global);
1266
1267
0
  g_date_prepare_to_parse (str, &pt);
1268
  
1269
0
  DEBUG_MSG (("Found %d ints, '%d' '%d' '%d' and written out month %d",
1270
0
        pt.num_ints, pt.n[0], pt.n[1], pt.n[2], pt.month));
1271
  
1272
  
1273
0
  if (pt.num_ints == 4) 
1274
0
    {
1275
0
      G_UNLOCK (g_date_global);
1276
0
      return; /* presumably a typo; bail out. */
1277
0
    }
1278
  
1279
0
  if (pt.num_ints > 1)
1280
0
    {
1281
0
      int i = 0;
1282
0
      int j = 0;
1283
      
1284
0
      g_assert (pt.num_ints < 4); /* i.e., it is 2 or 3 */
1285
      
1286
0
      while (i < pt.num_ints && j < 3) 
1287
0
        {
1288
0
          switch (dmy_order[j])
1289
0
            {
1290
0
            case G_DATE_MONTH:
1291
0
      {
1292
0
        if (pt.num_ints == 2 && pt.month != G_DATE_BAD_MONTH)
1293
0
    {
1294
0
      m = pt.month;
1295
0
      ++j;      /* skip months, but don't skip this number */
1296
0
      continue;
1297
0
    }
1298
0
        else 
1299
0
    m = pt.n[i];
1300
0
      }
1301
0
      break;
1302
0
            case G_DATE_DAY:
1303
0
      {
1304
0
        if (pt.num_ints == 2 && pt.month == G_DATE_BAD_MONTH)
1305
0
    {
1306
0
      day = 1;
1307
0
      ++j;      /* skip days, since we may have month/year */
1308
0
      continue;
1309
0
    }
1310
0
        day = pt.n[i];
1311
0
      }
1312
0
      break;
1313
0
            case G_DATE_YEAR:
1314
0
      {
1315
0
        y  = pt.n[i];
1316
        
1317
0
        if (locale_era_adjust != 0)
1318
0
          {
1319
0
      y += locale_era_adjust;
1320
0
          }
1321
1322
0
        y = convert_twodigit_year (y);
1323
0
      }
1324
0
      break;
1325
0
            default:
1326
0
              break;
1327
0
            }
1328
    
1329
0
          ++i;
1330
0
          ++j;
1331
0
        }
1332
      
1333
      
1334
0
      if (pt.num_ints == 3 && !g_date_valid_dmy (day, m, y))
1335
0
        {
1336
          /* Try YYYY MM DD */
1337
0
          y   = pt.n[0];
1338
0
          m   = pt.n[1];
1339
0
          day = pt.n[2];
1340
          
1341
0
          if (using_twodigit_years && y < 100) 
1342
0
            y = G_DATE_BAD_YEAR; /* avoids ambiguity */
1343
0
        }
1344
0
      else if (pt.num_ints == 2)
1345
0
  {
1346
0
    if (m == G_DATE_BAD_MONTH && pt.month != G_DATE_BAD_MONTH)
1347
0
      m = pt.month;
1348
0
  }
1349
0
    }
1350
0
  else if (pt.num_ints == 1) 
1351
0
    {
1352
0
      if (pt.month != G_DATE_BAD_MONTH)
1353
0
        {
1354
          /* Month name and year? */
1355
0
          m    = pt.month;
1356
0
          day  = 1;
1357
0
          y = pt.n[0];
1358
0
        }
1359
0
      else
1360
0
        {
1361
          /* Try yyyymmdd and yymmdd */
1362
    
1363
0
          m   = (pt.n[0]/100) % 100;
1364
0
          day = pt.n[0] % 100;
1365
0
          y   = pt.n[0]/10000;
1366
1367
0
          y   = convert_twodigit_year (y);
1368
0
        }
1369
0
    }
1370
  
1371
  /* See if we got anything valid out of all this. */
1372
  /* y < 8000 is to catch 19998 style typos; the library is OK up to 65535 or so */
1373
0
  if (y < 8000 && g_date_valid_dmy (day, m, y)) 
1374
0
    {
1375
0
      d->month = m;
1376
0
      d->day   = day;
1377
0
      d->year  = y;
1378
0
      d->dmy   = TRUE;
1379
0
    }
1380
#ifdef G_ENABLE_DEBUG
1381
  else 
1382
    {
1383
      DEBUG_MSG (("Rejected DMY %u %u %u", day, m, y));
1384
    }
1385
#endif
1386
0
  G_UNLOCK (g_date_global);
1387
0
}
1388
1389
gboolean
1390
_g_localtime (time_t timet, struct tm *out_tm)
1391
0
{
1392
0
  gboolean success = TRUE;
1393
1394
0
#ifdef HAVE_LOCALTIME_R
1395
0
  if (!localtime_r (&timet, out_tm))
1396
0
    success = FALSE;
1397
#else
1398
  {
1399
    struct tm *ptm = localtime (&timet);
1400
1401
    if (ptm == NULL)
1402
      {
1403
        /* Happens at least in Microsoft's C library if you pass a
1404
         * negative time_t.
1405
         */
1406
        success = FALSE;
1407
      }
1408
    else
1409
      memcpy (out_tm, ptm, sizeof (struct tm));
1410
  }
1411
#endif
1412
1413
0
  return success;
1414
0
}
1415
1416
/**
1417
 * g_date_set_time_t:
1418
 * @date: a #GDate 
1419
 * @timet: time_t value to set
1420
 *
1421
 * Sets the value of a date to the date corresponding to a time 
1422
 * specified as a time_t. The time to date conversion is done using 
1423
 * the user's current timezone.
1424
 *
1425
 * To set the value of a date to the current day, you could write:
1426
 * |[<!-- language="C" -->
1427
 *  time_t now = time (NULL);
1428
 *  if (now == (time_t) -1)
1429
 *    // handle the error
1430
 *  g_date_set_time_t (date, now);
1431
 * ]|
1432
 *
1433
 * Since: 2.10
1434
 */
1435
void         
1436
g_date_set_time_t (GDate *date,
1437
       time_t timet)
1438
0
{
1439
0
  struct tm tm;
1440
0
  gboolean success;
1441
1442
0
  g_return_if_fail (date != NULL);
1443
1444
0
  success = _g_localtime (timet, &tm);
1445
0
  if (!success)
1446
0
    {
1447
      /* Still set a default date, 2000-01-01.
1448
       *
1449
       * We may assert out below. */
1450
0
      tm.tm_mon = 0;
1451
0
      tm.tm_mday = 1;
1452
0
      tm.tm_year = 100;
1453
0
    }
1454
1455
0
  date->julian = FALSE;
1456
  
1457
0
  date->month = tm.tm_mon + 1;
1458
0
  date->day   = tm.tm_mday;
1459
0
  date->year  = tm.tm_year + 1900;
1460
  
1461
0
  g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
1462
  
1463
0
  date->dmy    = TRUE;
1464
1465
0
#ifndef G_DISABLE_CHECKS
1466
0
  if (!success)
1467
0
    g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "localtime() == NULL");
1468
0
#endif
1469
0
}
1470
1471
1472
/**
1473
 * g_date_set_time:
1474
 * @date: a #GDate.
1475
 * @time_: #GTime value to set.
1476
 *
1477
 * Sets the value of a date from a #GTime value.
1478
 * The time to date conversion is done using the user's current timezone.
1479
 *
1480
 * Deprecated: 2.10: Use g_date_set_time_t() instead.
1481
 */
1482
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1483
void
1484
g_date_set_time (GDate *date,
1485
     GTime  time_)
1486
0
{
1487
0
  g_date_set_time_t (date, (time_t) time_);
1488
0
}
1489
G_GNUC_END_IGNORE_DEPRECATIONS
1490
1491
/**
1492
 * g_date_set_time_val:
1493
 * @date: a #GDate 
1494
 * @timeval: #GTimeVal value to set
1495
 *
1496
 * Sets the value of a date from a #GTimeVal value.  Note that the
1497
 * @tv_usec member is ignored, because #GDate can't make use of the
1498
 * additional precision.
1499
 *
1500
 * The time to date conversion is done using the user's current timezone.
1501
 *
1502
 * Since: 2.10
1503
 * Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use g_date_set_time_t()
1504
 *    instead.
1505
 */
1506
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
1507
void
1508
g_date_set_time_val (GDate    *date,
1509
         GTimeVal *timeval)
1510
0
{
1511
0
  g_date_set_time_t (date, (time_t) timeval->tv_sec);
1512
0
}
1513
G_GNUC_END_IGNORE_DEPRECATIONS
1514
1515
/**
1516
 * g_date_set_month:
1517
 * @date: a #GDate
1518
 * @month: month to set
1519
 *
1520
 * Sets the month of the year for a #GDate.  If the resulting
1521
 * day-month-year triplet is invalid, the date will be invalid.
1522
 */
1523
void         
1524
g_date_set_month (GDate     *d, 
1525
                  GDateMonth m)
1526
0
{
1527
0
  g_return_if_fail (d != NULL);
1528
0
  g_return_if_fail (g_date_valid_month (m));
1529
1530
0
  if (d->julian && !d->dmy) g_date_update_dmy(d);
1531
0
  d->julian = FALSE;
1532
  
1533
0
  d->month = m;
1534
  
1535
0
  if (g_date_valid_dmy (d->day, d->month, d->year))
1536
0
    d->dmy = TRUE;
1537
0
  else 
1538
0
    d->dmy = FALSE;
1539
0
}
1540
1541
/**
1542
 * g_date_set_day:
1543
 * @date: a #GDate
1544
 * @day: day to set
1545
 *
1546
 * Sets the day of the month for a #GDate. If the resulting
1547
 * day-month-year triplet is invalid, the date will be invalid.
1548
 */
1549
void         
1550
g_date_set_day (GDate    *d, 
1551
                GDateDay  day)
1552
0
{
1553
0
  g_return_if_fail (d != NULL);
1554
0
  g_return_if_fail (g_date_valid_day (day));
1555
  
1556
0
  if (d->julian && !d->dmy) g_date_update_dmy(d);
1557
0
  d->julian = FALSE;
1558
  
1559
0
  d->day = day;
1560
  
1561
0
  if (g_date_valid_dmy (d->day, d->month, d->year))
1562
0
    d->dmy = TRUE;
1563
0
  else 
1564
0
    d->dmy = FALSE;
1565
0
}
1566
1567
/**
1568
 * g_date_set_year:
1569
 * @date: a #GDate
1570
 * @year: year to set
1571
 *
1572
 * Sets the year for a #GDate. If the resulting day-month-year
1573
 * triplet is invalid, the date will be invalid.
1574
 */
1575
void         
1576
g_date_set_year (GDate     *d, 
1577
                 GDateYear  y)
1578
0
{
1579
0
  g_return_if_fail (d != NULL);
1580
0
  g_return_if_fail (g_date_valid_year (y));
1581
  
1582
0
  if (d->julian && !d->dmy) g_date_update_dmy(d);
1583
0
  d->julian = FALSE;
1584
  
1585
0
  d->year = y;
1586
  
1587
0
  if (g_date_valid_dmy (d->day, d->month, d->year))
1588
0
    d->dmy = TRUE;
1589
0
  else 
1590
0
    d->dmy = FALSE;
1591
0
}
1592
1593
/**
1594
 * g_date_set_dmy:
1595
 * @date: a #GDate
1596
 * @day: day
1597
 * @month: month
1598
 * @y: year
1599
 *
1600
 * Sets the value of a #GDate from a day, month, and year.
1601
 * The day-month-year triplet must be valid; if you aren't
1602
 * sure it is, call g_date_valid_dmy() to check before you
1603
 * set it.
1604
 */
1605
void         
1606
g_date_set_dmy (GDate      *d, 
1607
                GDateDay    day, 
1608
                GDateMonth  m, 
1609
                GDateYear   y)
1610
0
{
1611
0
  g_return_if_fail (d != NULL);
1612
0
  g_return_if_fail (g_date_valid_dmy (day, m, y));
1613
  
1614
0
  d->julian = FALSE;
1615
  
1616
0
  d->month = m;
1617
0
  d->day   = day;
1618
0
  d->year  = y;
1619
  
1620
0
  d->dmy = TRUE;
1621
0
}
1622
1623
/**
1624
 * g_date_set_julian:
1625
 * @date: a #GDate
1626
 * @julian_date: Julian day number (days since January 1, Year 1)
1627
 *
1628
 * Sets the value of a #GDate from a Julian day number.
1629
 */
1630
void         
1631
g_date_set_julian (GDate   *d, 
1632
                   guint32  j)
1633
0
{
1634
0
  g_return_if_fail (d != NULL);
1635
0
  g_return_if_fail (g_date_valid_julian (j));
1636
  
1637
0
  d->julian_days = j;
1638
0
  d->julian = TRUE;
1639
0
  d->dmy = FALSE;
1640
0
}
1641
1642
/**
1643
 * g_date_is_first_of_month:
1644
 * @date: a #GDate to check
1645
 *
1646
 * Returns %TRUE if the date is on the first of a month.
1647
 * The date must be valid.
1648
 *
1649
 * Returns: %TRUE if the date is the first of the month
1650
 */
1651
gboolean     
1652
g_date_is_first_of_month (const GDate *d)
1653
0
{
1654
0
  g_return_val_if_fail (g_date_valid (d), FALSE);
1655
  
1656
0
  if (!d->dmy) 
1657
0
    g_date_update_dmy (d);
1658
1659
0
  g_return_val_if_fail (d->dmy, FALSE);  
1660
  
1661
0
  if (d->day == 1) return TRUE;
1662
0
  else return FALSE;
1663
0
}
1664
1665
/**
1666
 * g_date_is_last_of_month:
1667
 * @date: a #GDate to check
1668
 *
1669
 * Returns %TRUE if the date is the last day of the month.
1670
 * The date must be valid.
1671
 *
1672
 * Returns: %TRUE if the date is the last day of the month
1673
 */
1674
gboolean     
1675
g_date_is_last_of_month (const GDate *d)
1676
0
{
1677
0
  gint idx;
1678
  
1679
0
  g_return_val_if_fail (g_date_valid (d), FALSE);
1680
  
1681
0
  if (!d->dmy) 
1682
0
    g_date_update_dmy (d);
1683
1684
0
  g_return_val_if_fail (d->dmy, FALSE);  
1685
  
1686
0
  idx = g_date_is_leap_year (d->year) ? 1 : 0;
1687
  
1688
0
  if (d->day == days_in_months[idx][d->month]) return TRUE;
1689
0
  else return FALSE;
1690
0
}
1691
1692
/**
1693
 * g_date_add_days:
1694
 * @date: a #GDate to increment
1695
 * @n_days: number of days to move the date forward
1696
 *
1697
 * Increments a date some number of days.
1698
 * To move forward by weeks, add weeks*7 days.
1699
 * The date must be valid.
1700
 */
1701
void         
1702
g_date_add_days (GDate *d, 
1703
                 guint  ndays)
1704
0
{
1705
0
  g_return_if_fail (g_date_valid (d));
1706
  
1707
0
  if (!d->julian)
1708
0
    g_date_update_julian (d);
1709
1710
0
  g_return_if_fail (d->julian);
1711
0
  g_return_if_fail (ndays <= G_MAXUINT32 - d->julian_days);
1712
  
1713
0
  d->julian_days += ndays;
1714
0
  d->dmy = FALSE;
1715
0
}
1716
1717
/**
1718
 * g_date_subtract_days:
1719
 * @date: a #GDate to decrement
1720
 * @n_days: number of days to move
1721
 *
1722
 * Moves a date some number of days into the past.
1723
 * To move by weeks, just move by weeks*7 days.
1724
 * The date must be valid.
1725
 */
1726
void         
1727
g_date_subtract_days (GDate *d, 
1728
                      guint  ndays)
1729
0
{
1730
0
  g_return_if_fail (g_date_valid (d));
1731
  
1732
0
  if (!d->julian)
1733
0
    g_date_update_julian (d);
1734
1735
0
  g_return_if_fail (d->julian);
1736
0
  g_return_if_fail (d->julian_days > ndays);
1737
  
1738
0
  d->julian_days -= ndays;
1739
0
  d->dmy = FALSE;
1740
0
}
1741
1742
/**
1743
 * g_date_add_months:
1744
 * @date: a #GDate to increment
1745
 * @n_months: number of months to move forward
1746
 *
1747
 * Increments a date by some number of months.
1748
 * If the day of the month is greater than 28,
1749
 * this routine may change the day of the month
1750
 * (because the destination month may not have
1751
 * the current day in it). The date must be valid.
1752
 */
1753
void         
1754
g_date_add_months (GDate *d, 
1755
                   guint  nmonths)
1756
0
{
1757
0
  guint years, months;
1758
0
  gint idx;
1759
  
1760
0
  g_return_if_fail (g_date_valid (d));
1761
  
1762
0
  if (!d->dmy) 
1763
0
    g_date_update_dmy (d);
1764
1765
0
  g_return_if_fail (d->dmy != 0);
1766
0
  g_return_if_fail (nmonths <= G_MAXUINT - (d->month - 1));
1767
1768
0
  nmonths += d->month - 1;
1769
  
1770
0
  years  = nmonths/12;
1771
0
  months = nmonths%12;
1772
1773
0
  g_return_if_fail (years <= (guint) (G_MAXUINT16 - d->year));
1774
1775
0
  d->month = months + 1;
1776
0
  d->year  += years;
1777
  
1778
0
  idx = g_date_is_leap_year (d->year) ? 1 : 0;
1779
  
1780
0
  if (d->day > days_in_months[idx][d->month])
1781
0
    d->day = days_in_months[idx][d->month];
1782
  
1783
0
  d->julian = FALSE;
1784
  
1785
0
  g_return_if_fail (g_date_valid (d));
1786
0
}
1787
1788
/**
1789
 * g_date_subtract_months:
1790
 * @date: a #GDate to decrement
1791
 * @n_months: number of months to move
1792
 *
1793
 * Moves a date some number of months into the past.
1794
 * If the current day of the month doesn't exist in
1795
 * the destination month, the day of the month
1796
 * may change. The date must be valid.
1797
 */
1798
void         
1799
g_date_subtract_months (GDate *d, 
1800
                        guint  nmonths)
1801
0
{
1802
0
  guint years, months;
1803
0
  gint idx;
1804
  
1805
0
  g_return_if_fail (g_date_valid (d));
1806
  
1807
0
  if (!d->dmy) 
1808
0
    g_date_update_dmy (d);
1809
1810
0
  g_return_if_fail (d->dmy != 0);
1811
  
1812
0
  years  = nmonths/12;
1813
0
  months = nmonths%12;
1814
  
1815
0
  g_return_if_fail (d->year > years);
1816
  
1817
0
  d->year  -= years;
1818
  
1819
0
  if (d->month > months) d->month -= months;
1820
0
  else 
1821
0
    {
1822
0
      months -= d->month;
1823
0
      d->month = 12 - months;
1824
0
      d->year -= 1;
1825
0
    }
1826
  
1827
0
  idx = g_date_is_leap_year (d->year) ? 1 : 0;
1828
  
1829
0
  if (d->day > days_in_months[idx][d->month])
1830
0
    d->day = days_in_months[idx][d->month];
1831
  
1832
0
  d->julian = FALSE;
1833
  
1834
0
  g_return_if_fail (g_date_valid (d));
1835
0
}
1836
1837
/**
1838
 * g_date_add_years:
1839
 * @date: a #GDate to increment
1840
 * @n_years: number of years to move forward
1841
 *
1842
 * Increments a date by some number of years.
1843
 * If the date is February 29, and the destination
1844
 * year is not a leap year, the date will be changed
1845
 * to February 28. The date must be valid.
1846
 */
1847
void         
1848
g_date_add_years (GDate *d, 
1849
                  guint  nyears)
1850
0
{
1851
0
  g_return_if_fail (g_date_valid (d));
1852
  
1853
0
  if (!d->dmy) 
1854
0
    g_date_update_dmy (d);
1855
1856
0
  g_return_if_fail (d->dmy != 0);
1857
0
  g_return_if_fail (nyears <= (guint) (G_MAXUINT16 - d->year));
1858
1859
0
  d->year += nyears;
1860
  
1861
0
  if (d->month == 2 && d->day == 29)
1862
0
    {
1863
0
      if (!g_date_is_leap_year (d->year))
1864
0
        d->day = 28;
1865
0
    }
1866
  
1867
0
  d->julian = FALSE;
1868
0
}
1869
1870
/**
1871
 * g_date_subtract_years:
1872
 * @date: a #GDate to decrement
1873
 * @n_years: number of years to move
1874
 *
1875
 * Moves a date some number of years into the past.
1876
 * If the current day doesn't exist in the destination
1877
 * year (i.e. it's February 29 and you move to a non-leap-year)
1878
 * then the day is changed to February 29. The date
1879
 * must be valid.
1880
 */
1881
void         
1882
g_date_subtract_years (GDate *d, 
1883
                       guint  nyears)
1884
0
{
1885
0
  g_return_if_fail (g_date_valid (d));
1886
  
1887
0
  if (!d->dmy) 
1888
0
    g_date_update_dmy (d);
1889
1890
0
  g_return_if_fail (d->dmy != 0);
1891
0
  g_return_if_fail (d->year > nyears);
1892
  
1893
0
  d->year -= nyears;
1894
  
1895
0
  if (d->month == 2 && d->day == 29)
1896
0
    {
1897
0
      if (!g_date_is_leap_year (d->year))
1898
0
        d->day = 28;
1899
0
    }
1900
  
1901
0
  d->julian = FALSE;
1902
0
}
1903
1904
/**
1905
 * g_date_is_leap_year:
1906
 * @year: year to check
1907
 *
1908
 * Returns %TRUE if the year is a leap year.
1909
 *
1910
 * For the purposes of this function, leap year is every year
1911
 * divisible by 4 unless that year is divisible by 100. If it
1912
 * is divisible by 100 it would be a leap year only if that year
1913
 * is also divisible by 400.
1914
 *
1915
 * Returns: %TRUE if the year is a leap year
1916
 */
1917
gboolean     
1918
g_date_is_leap_year (GDateYear year)
1919
0
{
1920
0
  g_return_val_if_fail (g_date_valid_year (year), FALSE);
1921
  
1922
0
  return ( (((year % 4) == 0) && ((year % 100) != 0)) ||
1923
0
           (year % 400) == 0 );
1924
0
}
1925
1926
/**
1927
 * g_date_get_days_in_month:
1928
 * @month: month
1929
 * @year: year
1930
 *
1931
 * Returns the number of days in a month, taking leap
1932
 * years into account.
1933
 *
1934
 * Returns: number of days in @month during the @year
1935
 */
1936
guint8         
1937
g_date_get_days_in_month (GDateMonth month, 
1938
                          GDateYear  year)
1939
0
{
1940
0
  gint idx;
1941
  
1942
0
  g_return_val_if_fail (g_date_valid_year (year), 0);
1943
0
  g_return_val_if_fail (g_date_valid_month (month), 0);
1944
  
1945
0
  idx = g_date_is_leap_year (year) ? 1 : 0;
1946
  
1947
0
  return days_in_months[idx][month];
1948
0
}
1949
1950
/**
1951
 * g_date_get_monday_weeks_in_year:
1952
 * @year: a year
1953
 *
1954
 * Returns the number of weeks in the year, where weeks
1955
 * are taken to start on Monday. Will be 52 or 53. The
1956
 * date must be valid. (Years always have 52 7-day periods,
1957
 * plus 1 or 2 extra days depending on whether it's a leap
1958
 * year. This function is basically telling you how many
1959
 * Mondays are in the year, i.e. there are 53 Mondays if
1960
 * one of the extra days happens to be a Monday.)
1961
 *
1962
 * Returns: number of Mondays in the year
1963
 */
1964
guint8       
1965
g_date_get_monday_weeks_in_year (GDateYear year)
1966
0
{
1967
0
  GDate d;
1968
  
1969
0
  g_return_val_if_fail (g_date_valid_year (year), 0);
1970
  
1971
0
  g_date_clear (&d, 1);
1972
0
  g_date_set_dmy (&d, 1, 1, year);
1973
0
  if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1974
0
  g_date_set_dmy (&d, 31, 12, year);
1975
0
  if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1976
0
  if (g_date_is_leap_year (year)) 
1977
0
    {
1978
0
      g_date_set_dmy (&d, 2, 1, year);
1979
0
      if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1980
0
      g_date_set_dmy (&d, 30, 12, year);
1981
0
      if (g_date_get_weekday (&d) == G_DATE_MONDAY) return 53;
1982
0
    }
1983
0
  return 52;
1984
0
}
1985
1986
/**
1987
 * g_date_get_sunday_weeks_in_year:
1988
 * @year: year to count weeks in
1989
 *
1990
 * Returns the number of weeks in the year, where weeks
1991
 * are taken to start on Sunday. Will be 52 or 53. The
1992
 * date must be valid. (Years always have 52 7-day periods,
1993
 * plus 1 or 2 extra days depending on whether it's a leap
1994
 * year. This function is basically telling you how many
1995
 * Sundays are in the year, i.e. there are 53 Sundays if
1996
 * one of the extra days happens to be a Sunday.)
1997
 *
1998
 * Returns: the number of weeks in @year
1999
 */
2000
guint8       
2001
g_date_get_sunday_weeks_in_year (GDateYear year)
2002
0
{
2003
0
  GDate d;
2004
  
2005
0
  g_return_val_if_fail (g_date_valid_year (year), 0);
2006
  
2007
0
  g_date_clear (&d, 1);
2008
0
  g_date_set_dmy (&d, 1, 1, year);
2009
0
  if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2010
0
  g_date_set_dmy (&d, 31, 12, year);
2011
0
  if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2012
0
  if (g_date_is_leap_year (year)) 
2013
0
    {
2014
0
      g_date_set_dmy (&d, 2, 1, year);
2015
0
      if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2016
0
      g_date_set_dmy (&d, 30, 12, year);
2017
0
      if (g_date_get_weekday (&d) == G_DATE_SUNDAY) return 53;
2018
0
    }
2019
0
  return 52;
2020
0
}
2021
2022
/**
2023
 * g_date_compare:
2024
 * @lhs: first date to compare
2025
 * @rhs: second date to compare
2026
 *
2027
 * qsort()-style comparison function for dates.
2028
 * Both dates must be valid.
2029
 *
2030
 * Returns: 0 for equal, less than zero if @lhs is less than @rhs,
2031
 *     greater than zero if @lhs is greater than @rhs
2032
 */
2033
gint         
2034
g_date_compare (const GDate *lhs, 
2035
                const GDate *rhs)
2036
0
{
2037
0
  g_return_val_if_fail (lhs != NULL, 0);
2038
0
  g_return_val_if_fail (rhs != NULL, 0);
2039
0
  g_return_val_if_fail (g_date_valid (lhs), 0);
2040
0
  g_return_val_if_fail (g_date_valid (rhs), 0);
2041
  
2042
  /* Remember the self-comparison case! I think it works right now. */
2043
  
2044
0
  while (TRUE)
2045
0
    {
2046
0
      if (lhs->julian && rhs->julian) 
2047
0
        {
2048
0
          if (lhs->julian_days < rhs->julian_days) return -1;
2049
0
          else if (lhs->julian_days > rhs->julian_days) return 1;
2050
0
          else                                          return 0;
2051
0
        }
2052
0
      else if (lhs->dmy && rhs->dmy) 
2053
0
        {
2054
0
          if (lhs->year < rhs->year)               return -1;
2055
0
          else if (lhs->year > rhs->year)               return 1;
2056
0
          else 
2057
0
            {
2058
0
              if (lhs->month < rhs->month)         return -1;
2059
0
              else if (lhs->month > rhs->month)         return 1;
2060
0
              else 
2061
0
                {
2062
0
                  if (lhs->day < rhs->day)              return -1;
2063
0
                  else if (lhs->day > rhs->day)              return 1;
2064
0
                  else                                       return 0;
2065
0
                }
2066
              
2067
0
            }
2068
          
2069
0
        }
2070
0
      else
2071
0
        {
2072
0
          if (!lhs->julian) g_date_update_julian (lhs);
2073
0
          if (!rhs->julian) g_date_update_julian (rhs);
2074
0
          g_return_val_if_fail (lhs->julian, 0);
2075
0
          g_return_val_if_fail (rhs->julian, 0);
2076
0
        }
2077
      
2078
0
    }
2079
0
  return 0; /* warnings */
2080
0
}
2081
2082
/**
2083
 * g_date_to_struct_tm:
2084
 * @date: a #GDate to set the struct tm from
2085
 * @tm: (not nullable): struct tm to fill
2086
 *
2087
 * Fills in the date-related bits of a struct tm using the @date value.
2088
 * Initializes the non-date parts with something safe but meaningless.
2089
 */
2090
void        
2091
g_date_to_struct_tm (const GDate *d, 
2092
                     struct tm   *tm)
2093
0
{
2094
0
  GDateWeekday day;
2095
     
2096
0
  g_return_if_fail (g_date_valid (d));
2097
0
  g_return_if_fail (tm != NULL);
2098
  
2099
0
  if (!d->dmy) 
2100
0
    g_date_update_dmy (d);
2101
2102
0
  g_return_if_fail (d->dmy != 0);
2103
  
2104
  /* zero all the irrelevant fields to be sure they're valid */
2105
  
2106
  /* On Linux and maybe other systems, there are weird non-POSIX
2107
   * fields on the end of struct tm that choke strftime if they
2108
   * contain garbage.  So we need to 0 the entire struct, not just the
2109
   * fields we know to exist. 
2110
   */
2111
  
2112
0
  memset (tm, 0x0, sizeof (struct tm));
2113
  
2114
0
  tm->tm_mday = d->day;
2115
0
  tm->tm_mon  = d->month - 1; /* 0-11 goes in tm */
2116
0
  tm->tm_year = ((int)d->year) - 1900; /* X/Open says tm_year can be negative */
2117
  
2118
0
  day = g_date_get_weekday (d);
2119
0
  if (day == 7) day = 0; /* struct tm wants days since Sunday, so Sunday is 0 */
2120
  
2121
0
  tm->tm_wday = (int)day;
2122
  
2123
0
  tm->tm_yday = g_date_get_day_of_year (d) - 1; /* 0 to 365 */
2124
0
  tm->tm_isdst = -1; /* -1 means "information not available" */
2125
0
}
2126
2127
/**
2128
 * g_date_clamp:
2129
 * @date: a #GDate to clamp
2130
 * @min_date: minimum accepted value for @date
2131
 * @max_date: maximum accepted value for @date
2132
 *
2133
 * If @date is prior to @min_date, sets @date equal to @min_date.
2134
 * If @date falls after @max_date, sets @date equal to @max_date.
2135
 * Otherwise, @date is unchanged.
2136
 * Either of @min_date and @max_date may be %NULL.
2137
 * All non-%NULL dates must be valid.
2138
 */
2139
void
2140
g_date_clamp (GDate       *date,
2141
        const GDate *min_date,
2142
        const GDate *max_date)
2143
0
{
2144
0
  g_return_if_fail (g_date_valid (date));
2145
2146
0
  if (min_date != NULL)
2147
0
    g_return_if_fail (g_date_valid (min_date));
2148
2149
0
  if (max_date != NULL)
2150
0
    g_return_if_fail (g_date_valid (max_date));
2151
2152
0
  if (min_date != NULL && max_date != NULL)
2153
0
    g_return_if_fail (g_date_compare (min_date, max_date) <= 0);
2154
2155
0
  if (min_date && g_date_compare (date, min_date) < 0)
2156
0
    *date = *min_date;
2157
2158
0
  if (max_date && g_date_compare (max_date, date) < 0)
2159
0
    *date = *max_date;
2160
0
}
2161
2162
/**
2163
 * g_date_order:
2164
 * @date1: the first date
2165
 * @date2: the second date
2166
 *
2167
 * Checks if @date1 is less than or equal to @date2,
2168
 * and swap the values if this is not the case.
2169
 */
2170
void
2171
g_date_order (GDate *date1,
2172
              GDate *date2)
2173
0
{
2174
0
  g_return_if_fail (g_date_valid (date1));
2175
0
  g_return_if_fail (g_date_valid (date2));
2176
2177
0
  if (g_date_compare (date1, date2) > 0)
2178
0
    {
2179
0
      GDate tmp = *date1;
2180
0
      *date1 = *date2;
2181
0
      *date2 = tmp;
2182
0
    }
2183
0
}
2184
2185
#ifdef G_OS_WIN32
2186
static gboolean
2187
append_month_name (GArray     *result,
2188
       LCID        lcid,
2189
       SYSTEMTIME *systemtime,
2190
       gboolean    abbreviated,
2191
       gboolean    alternative)
2192
{
2193
  int n;
2194
  WORD base;
2195
  LPCWSTR lpFormat;
2196
2197
  if (alternative)
2198
    {
2199
      base = abbreviated ? LOCALE_SABBREVMONTHNAME1 : LOCALE_SMONTHNAME1;
2200
      n = GetLocaleInfoW (lcid, base + systemtime->wMonth - 1, NULL, 0);
2201
      if (n == 0)
2202
        return FALSE;
2203
2204
      g_array_set_size (result, result->len + n);
2205
      if (GetLocaleInfoW (lcid, base + systemtime->wMonth - 1,
2206
                          ((wchar_t *) result->data) + result->len - n, n) != n)
2207
        return FALSE;
2208
2209
      g_array_set_size (result, result->len - 1);
2210
    }
2211
  else
2212
    {
2213
      /* According to MSDN, this is the correct method to obtain
2214
       * the form of the month name used when formatting a full
2215
       * date; it must be a genitive case in some languages.
2216
       *
2217
       * (n == 0) indicates an error, whereas (n < 2) is something we’d never
2218
       * expect from the given format string, and would break the subsequent code.
2219
       */
2220
      lpFormat = abbreviated ? L"ddMMM" : L"ddMMMM";
2221
      n = GetDateFormatW (lcid, 0, systemtime, lpFormat, NULL, 0);
2222
      if (n < 2)
2223
        return FALSE;
2224
2225
      g_array_set_size (result, result->len + n);
2226
      if (GetDateFormatW (lcid, 0, systemtime, lpFormat,
2227
                          ((wchar_t *) result->data) + result->len - n, n) != n)
2228
        return FALSE;
2229
2230
      /* We have obtained a day number as two digits and the month name.
2231
       * Now let's get rid of those two digits: overwrite them with the
2232
       * month name.
2233
       */
2234
      memmove (((wchar_t *) result->data) + result->len - n,
2235
         ((wchar_t *) result->data) + result->len - n + 2,
2236
         (n - 2) * sizeof (wchar_t));
2237
      g_array_set_size (result, result->len - 3);
2238
    }
2239
2240
  return TRUE;
2241
}
2242
2243
static gsize
2244
win32_strftime_helper (const GDate     *d,
2245
           const gchar     *format,
2246
           const struct tm *tm,
2247
           gchar           *s,
2248
           gsize          slen)
2249
{
2250
  SYSTEMTIME systemtime;
2251
  TIME_ZONE_INFORMATION tzinfo;
2252
  LCID lcid;
2253
  int n, k;
2254
  GArray *result;
2255
  const gchar *p;
2256
  gunichar c, modifier;
2257
  const wchar_t digits[] = L"0123456789";
2258
  gchar *convbuf;
2259
  glong convlen = 0;
2260
  gsize retval;
2261
2262
  systemtime.wYear = tm->tm_year + 1900;
2263
  systemtime.wMonth = tm->tm_mon + 1;
2264
  systemtime.wDayOfWeek = tm->tm_wday;
2265
  systemtime.wDay = tm->tm_mday;
2266
  systemtime.wHour = tm->tm_hour;
2267
  systemtime.wMinute = tm->tm_min;
2268
  systemtime.wSecond = tm->tm_sec;
2269
  systemtime.wMilliseconds = 0;
2270
  
2271
  lcid = GetThreadLocale ();
2272
  result = g_array_sized_new (FALSE, FALSE, sizeof (wchar_t), MAX (128, strlen (format) * 2));
2273
2274
  p = format;
2275
  while (*p)
2276
    {
2277
      c = g_utf8_get_char (p);
2278
      if (c == '%')
2279
  {
2280
    p = g_utf8_next_char (p);
2281
    if (!*p)
2282
      {
2283
        s[0] = '\0';
2284
        g_array_free (result, TRUE);
2285
2286
        return 0;
2287
      }
2288
2289
    modifier = '\0';
2290
    c = g_utf8_get_char (p);
2291
    if (c == 'E' || c == 'O')
2292
      {
2293
        /* "%OB", "%Ob", and "%Oh" are supported, ignore other modified
2294
         * conversion specifiers for now.
2295
         */
2296
        modifier = c;
2297
        p = g_utf8_next_char (p);
2298
        if (!*p)
2299
    {
2300
      s[0] = '\0';
2301
      g_array_free (result, TRUE);
2302
2303
      return 0;
2304
    }
2305
2306
        c = g_utf8_get_char (p);
2307
      }
2308
2309
    switch (c)
2310
      {
2311
      case 'a':
2312
        if (systemtime.wDayOfWeek == 0)
2313
    k = 6;
2314
        else
2315
    k = systemtime.wDayOfWeek - 1;
2316
        n = GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, NULL, 0);
2317
        g_array_set_size (result, result->len + n);
2318
        GetLocaleInfoW (lcid, LOCALE_SABBREVDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2319
        g_array_set_size (result, result->len - 1);
2320
        break;
2321
      case 'A':
2322
        if (systemtime.wDayOfWeek == 0)
2323
    k = 6;
2324
        else
2325
    k = systemtime.wDayOfWeek - 1;
2326
        n = GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, NULL, 0);
2327
        g_array_set_size (result, result->len + n);
2328
        GetLocaleInfoW (lcid, LOCALE_SDAYNAME1+k, ((wchar_t *) result->data) + result->len - n, n);
2329
        g_array_set_size (result, result->len - 1);
2330
        break;
2331
      case 'b':
2332
      case 'h':
2333
              if (!append_month_name (result, lcid, &systemtime, TRUE, modifier == 'O'))
2334
                {
2335
                  /* Ignore the error; this placeholder will be replaced with nothing */
2336
                }
2337
        break;
2338
      case 'B':
2339
              if (!append_month_name (result, lcid, &systemtime, FALSE, modifier == 'O'))
2340
                {
2341
                  /* Ignore the error; this placeholder will be replaced with nothing */
2342
                }
2343
        break;
2344
      case 'c':
2345
        n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2346
        if (n > 0)
2347
    {
2348
      g_array_set_size (result, result->len + n);
2349
      GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2350
      g_array_set_size (result, result->len - 1);
2351
    }
2352
        g_array_append_vals (result, L" ", 1);
2353
        n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2354
        if (n > 0)
2355
    {
2356
      g_array_set_size (result, result->len + n);
2357
      GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2358
      g_array_set_size (result, result->len - 1);
2359
    }
2360
        break;
2361
      case 'C':
2362
        g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2363
        g_array_append_vals (result, digits + (systemtime.wYear/1000)%10, 1);
2364
        break;
2365
      case 'd':
2366
        g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2367
        g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2368
        break;
2369
      case 'D':
2370
        g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2371
        g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2372
        g_array_append_vals (result, L"/", 1);
2373
        g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2374
        g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2375
        g_array_append_vals (result, L"/", 1);
2376
        g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2377
        g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2378
        break;
2379
      case 'e':
2380
        if (systemtime.wDay >= 10)
2381
    g_array_append_vals (result, digits + systemtime.wDay/10, 1);
2382
        else
2383
    g_array_append_vals (result, L" ", 1);
2384
        g_array_append_vals (result, digits + systemtime.wDay%10, 1);
2385
        break;
2386
2387
        /* A GDate has no time fields, so for now we can
2388
         * hardcode all time conversions into zeros (or 12 for
2389
         * %I). The alternative code snippets in the #else
2390
         * branches are here ready to be taken into use when
2391
         * needed by a g_strftime() or g_date_and_time_format()
2392
         * or whatever.
2393
         */
2394
      case 'H':
2395
#if 1
2396
        g_array_append_vals (result, L"00", 2);
2397
#else
2398
        g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2399
        g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2400
#endif
2401
        break;
2402
      case 'I':
2403
#if 1
2404
        g_array_append_vals (result, L"12", 2);
2405
#else
2406
        if (systemtime.wHour == 0)
2407
    g_array_append_vals (result, L"12", 2);
2408
        else
2409
    {
2410
      g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2411
      g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2412
    }
2413
#endif
2414
        break;
2415
      case  'j':
2416
        g_array_append_vals (result, digits + (tm->tm_yday+1)/100, 1);
2417
        g_array_append_vals (result, digits + ((tm->tm_yday+1)/10)%10, 1);
2418
        g_array_append_vals (result, digits + (tm->tm_yday+1)%10, 1);
2419
        break;
2420
      case 'm':
2421
        g_array_append_vals (result, digits + systemtime.wMonth/10, 1);
2422
        g_array_append_vals (result, digits + systemtime.wMonth%10, 1);
2423
        break;
2424
      case 'M':
2425
#if 1
2426
        g_array_append_vals (result, L"00", 2);
2427
#else
2428
        g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2429
        g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2430
#endif
2431
        break;
2432
      case 'n':
2433
        g_array_append_vals (result, L"\n", 1);
2434
        break;
2435
      case 'p':
2436
        n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2437
        if (n > 0)
2438
    {
2439
      g_array_set_size (result, result->len + n);
2440
      GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2441
      g_array_set_size (result, result->len - 1);
2442
    }
2443
        break;
2444
      case 'r':
2445
        /* This is a rather odd format. Hard to say what to do.
2446
         * Let's always use the POSIX %I:%M:%S %p
2447
         */
2448
#if 1
2449
        g_array_append_vals (result, L"12:00:00", 8);
2450
#else
2451
        if (systemtime.wHour == 0)
2452
    g_array_append_vals (result, L"12", 2);
2453
        else
2454
    {
2455
      g_array_append_vals (result, digits + (systemtime.wHour%12)/10, 1);
2456
      g_array_append_vals (result, digits + (systemtime.wHour%12)%10, 1);
2457
    }
2458
        g_array_append_vals (result, L":", 1);
2459
        g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2460
        g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2461
        g_array_append_vals (result, L":", 1);
2462
        g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2463
        g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2464
        g_array_append_vals (result, L" ", 1);
2465
#endif
2466
        n = GetTimeFormatW (lcid, 0, &systemtime, L"tt", NULL, 0);
2467
        if (n > 0)
2468
    {
2469
      g_array_set_size (result, result->len + n);
2470
      GetTimeFormatW (lcid, 0, &systemtime, L"tt", ((wchar_t *) result->data) + result->len - n, n);
2471
      g_array_set_size (result, result->len - 1);
2472
    }
2473
        break;
2474
      case 'R':
2475
#if 1
2476
        g_array_append_vals (result, L"00:00", 5);
2477
#else
2478
        g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2479
        g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2480
        g_array_append_vals (result, L":", 1);
2481
        g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2482
        g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2483
#endif
2484
        break;
2485
      case 'S':
2486
#if 1
2487
        g_array_append_vals (result, L"00", 2);
2488
#else
2489
        g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2490
        g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2491
#endif
2492
        break;
2493
      case 't':
2494
        g_array_append_vals (result, L"\t", 1);
2495
        break;
2496
      case 'T':
2497
#if 1
2498
        g_array_append_vals (result, L"00:00:00", 8);
2499
#else
2500
        g_array_append_vals (result, digits + systemtime.wHour/10, 1);
2501
        g_array_append_vals (result, digits + systemtime.wHour%10, 1);
2502
        g_array_append_vals (result, L":", 1);
2503
        g_array_append_vals (result, digits + systemtime.wMinute/10, 1);
2504
        g_array_append_vals (result, digits + systemtime.wMinute%10, 1);
2505
        g_array_append_vals (result, L":", 1);
2506
        g_array_append_vals (result, digits + systemtime.wSecond/10, 1);
2507
        g_array_append_vals (result, digits + systemtime.wSecond%10, 1);
2508
#endif
2509
        break;
2510
      case 'u':
2511
        if (systemtime.wDayOfWeek == 0)
2512
    g_array_append_vals (result, L"7", 1);
2513
        else
2514
    g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2515
        break;
2516
      case 'U':
2517
        n = g_date_get_sunday_week_of_year (d);
2518
        g_array_append_vals (result, digits + n/10, 1);
2519
        g_array_append_vals (result, digits + n%10, 1);
2520
        break;
2521
      case 'V':
2522
        n = g_date_get_iso8601_week_of_year (d);
2523
        g_array_append_vals (result, digits + n/10, 1);
2524
        g_array_append_vals (result, digits + n%10, 1);
2525
        break;
2526
      case 'w':
2527
        g_array_append_vals (result, digits + systemtime.wDayOfWeek, 1);
2528
        break;
2529
      case 'W':
2530
        n = g_date_get_monday_week_of_year (d);
2531
        g_array_append_vals (result, digits + n/10, 1);
2532
        g_array_append_vals (result, digits + n%10, 1);
2533
        break;
2534
      case 'x':
2535
        n = GetDateFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2536
        if (n > 0)
2537
    {
2538
      g_array_set_size (result, result->len + n);
2539
      GetDateFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2540
      g_array_set_size (result, result->len - 1);
2541
    }
2542
        break;
2543
      case 'X':
2544
        n = GetTimeFormatW (lcid, 0, &systemtime, NULL, NULL, 0);
2545
        if (n > 0)
2546
    {
2547
      g_array_set_size (result, result->len + n);
2548
      GetTimeFormatW (lcid, 0, &systemtime, NULL, ((wchar_t *) result->data) + result->len - n, n);
2549
      g_array_set_size (result, result->len - 1);
2550
    }
2551
        break;
2552
      case 'y':
2553
        g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2554
        g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2555
        break;
2556
      case 'Y':
2557
        g_array_append_vals (result, digits + systemtime.wYear/1000, 1);
2558
        g_array_append_vals (result, digits + (systemtime.wYear/100)%10, 1);
2559
        g_array_append_vals (result, digits + (systemtime.wYear/10)%10, 1);
2560
        g_array_append_vals (result, digits + systemtime.wYear%10, 1);
2561
        break;
2562
      case 'Z':
2563
        n = GetTimeZoneInformation (&tzinfo);
2564
        if (n == TIME_ZONE_ID_UNKNOWN || n == TIME_ZONE_ID_STANDARD)
2565
    g_array_append_vals (result, tzinfo.StandardName, wcslen (tzinfo.StandardName));
2566
        else if (n == TIME_ZONE_ID_DAYLIGHT)
2567
    g_array_append_vals (result, tzinfo.DaylightName, wcslen (tzinfo.DaylightName));
2568
        break;
2569
      case '%':
2570
        g_array_append_vals (result, L"%", 1);
2571
        break;
2572
      }      
2573
  } 
2574
      else if (c <= 0xFFFF)
2575
  {
2576
    wchar_t wc = c;
2577
    g_array_append_vals (result, &wc, 1);
2578
  }
2579
      else
2580
  {
2581
    glong nwc;
2582
    wchar_t *ws;
2583
2584
    ws = g_ucs4_to_utf16 (&c, 1, NULL, &nwc, NULL);
2585
    g_array_append_vals (result, ws, nwc);
2586
    g_free (ws);
2587
  }
2588
      p = g_utf8_next_char (p);
2589
    }
2590
  
2591
  convbuf = g_utf16_to_utf8 ((wchar_t *) result->data, result->len, NULL, &convlen, NULL);
2592
  g_array_free (result, TRUE);
2593
2594
  if (!convbuf)
2595
    {
2596
      s[0] = '\0';
2597
      return 0;
2598
    }
2599
  
2600
  g_assert (convlen >= 0);
2601
  if ((gsize) convlen >= slen)
2602
    {
2603
      /* Ensure only whole characters are copied into the buffer. */
2604
      gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2605
      g_assert (end != NULL);
2606
      convlen = end - convbuf;
2607
2608
      /* Return 0 because the buffer isn't large enough. */
2609
      retval = 0;
2610
    }
2611
  else
2612
    retval = convlen;
2613
2614
  memcpy (s, convbuf, convlen);
2615
  s[convlen] = '\0';
2616
  g_free (convbuf);
2617
2618
  return retval;
2619
}
2620
2621
#endif
2622
2623
/**
2624
 * g_date_strftime:
2625
 * @s: destination buffer
2626
 * @slen: buffer size
2627
 * @format: format string
2628
 * @date: valid #GDate
2629
 *
2630
 * Generates a printed representation of the date, in a
2631
 * [locale][setlocale]-specific way.
2632
 * Works just like the platform's C library strftime() function,
2633
 * but only accepts date-related formats; time-related formats
2634
 * give undefined results. Date must be valid. Unlike strftime()
2635
 * (which uses the locale encoding), works on a UTF-8 format
2636
 * string and stores a UTF-8 result.
2637
 *
2638
 * This function does not provide any conversion specifiers in
2639
 * addition to those implemented by the platform's C library.
2640
 * For example, don't expect that using g_date_strftime() would
2641
 * make the \%F provided by the C99 strftime() work on Windows
2642
 * where the C library only complies to C89.
2643
 *
2644
 * Returns: number of characters written to the buffer, or 0 the buffer was too small
2645
 */
2646
#pragma GCC diagnostic push
2647
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
2648
2649
gsize     
2650
g_date_strftime (gchar       *s, 
2651
                 gsize        slen, 
2652
                 const gchar *format, 
2653
                 const GDate *d)
2654
0
{
2655
0
  struct tm tm;
2656
0
#ifndef G_OS_WIN32
2657
0
  gsize locale_format_len = 0;
2658
0
  gchar *locale_format;
2659
0
  gsize tmplen;
2660
0
  gchar *tmpbuf;
2661
0
  gsize tmpbufsize;
2662
0
  gsize convlen = 0;
2663
0
  gchar *convbuf;
2664
0
  GError *error = NULL;
2665
0
  gsize retval;
2666
0
#endif
2667
2668
0
  g_return_val_if_fail (g_date_valid (d), 0);
2669
0
  g_return_val_if_fail (slen > 0, 0); 
2670
0
  g_return_val_if_fail (format != NULL, 0);
2671
0
  g_return_val_if_fail (s != NULL, 0);
2672
2673
0
  g_date_to_struct_tm (d, &tm);
2674
2675
#ifdef G_OS_WIN32
2676
  if (!g_utf8_validate (format, -1, NULL))
2677
    {
2678
      s[0] = '\0';
2679
      return 0;
2680
    }
2681
  return win32_strftime_helper (d, format, &tm, s, slen);
2682
#else
2683
2684
0
  locale_format = g_locale_from_utf8 (format, -1, NULL, &locale_format_len, &error);
2685
2686
0
  if (error)
2687
0
    {
2688
0
      g_warning (G_STRLOC "Error converting format to locale encoding: %s", error->message);
2689
0
      g_error_free (error);
2690
2691
0
      s[0] = '\0';
2692
0
      return 0;
2693
0
    }
2694
2695
0
  tmpbufsize = MAX (128, locale_format_len * 2);
2696
0
  while (TRUE)
2697
0
    {
2698
0
      tmpbuf = g_malloc (tmpbufsize);
2699
2700
      /* Set the first byte to something other than '\0', to be able to
2701
       * recognize whether strftime actually failed or just returned "".
2702
       */
2703
0
      tmpbuf[0] = '\1';
2704
0
      tmplen = strftime (tmpbuf, tmpbufsize, locale_format, &tm);
2705
2706
0
      if (tmplen == 0 && tmpbuf[0] != '\0')
2707
0
        {
2708
0
          g_free (tmpbuf);
2709
0
          tmpbufsize *= 2;
2710
2711
0
          if (tmpbufsize > 65536)
2712
0
            {
2713
0
              g_warning (G_STRLOC "Maximum buffer size for g_date_strftime exceeded: giving up");
2714
0
              g_free (locale_format);
2715
2716
0
              s[0] = '\0';
2717
0
              return 0;
2718
0
            }
2719
0
        }
2720
0
      else
2721
0
        break;
2722
0
    }
2723
0
  g_free (locale_format);
2724
2725
0
  convbuf = g_locale_to_utf8 (tmpbuf, tmplen, NULL, &convlen, &error);
2726
0
  g_free (tmpbuf);
2727
2728
0
  if (error)
2729
0
    {
2730
0
      g_warning (G_STRLOC "Error converting results of strftime to UTF-8: %s", error->message);
2731
0
      g_error_free (error);
2732
2733
0
      g_assert (convbuf == NULL);
2734
2735
0
      s[0] = '\0';
2736
0
      return 0;
2737
0
    }
2738
2739
0
  if (slen <= convlen)
2740
0
    {
2741
      /* Ensure only whole characters are copied into the buffer.
2742
       */
2743
0
      gchar *end = g_utf8_find_prev_char (convbuf, convbuf + slen);
2744
0
      g_assert (end != NULL);
2745
0
      convlen = end - convbuf;
2746
2747
      /* Return 0 because the buffer isn't large enough.
2748
       */
2749
0
      retval = 0;
2750
0
    }
2751
0
  else
2752
0
    retval = convlen;
2753
2754
0
  memcpy (s, convbuf, convlen);
2755
0
  s[convlen] = '\0';
2756
0
  g_free (convbuf);
2757
2758
0
  return retval;
2759
0
#endif
2760
0
}
2761
2762
#pragma GCC diagnostic pop