/src/glib/glib/gdatetime.c
Line | Count | Source |
1 | | /* gdatetime.c |
2 | | * |
3 | | * Copyright (C) 2009-2010 Christian Hergert <chris@dronelabs.com> |
4 | | * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk> |
5 | | * Copyright (C) 2010 Emmanuele Bassi <ebassi@linux.intel.com> |
6 | | * Copyright © 2010 Codethink Limited |
7 | | * Copyright © 2018 Tomasz Miąsko |
8 | | * |
9 | | * This library is free software; you can redistribute it and/or modify |
10 | | * it under the terms of the GNU Lesser General Public License as |
11 | | * published by the Free Software Foundation; either version 2.1 of the |
12 | | * licence, or (at your option) any later version. |
13 | | * |
14 | | * This is distributed in the hope that it will be useful, but WITHOUT |
15 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
16 | | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | | * License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this library; if not, see <http://www.gnu.org/licenses/>. |
21 | | * |
22 | | * Authors: Christian Hergert <chris@dronelabs.com> |
23 | | * Thiago Santos <thiago.sousa.santos@collabora.co.uk> |
24 | | * Emmanuele Bassi <ebassi@linux.intel.com> |
25 | | * Ryan Lortie <desrt@desrt.ca> |
26 | | * Robert Ancell <robert.ancell@canonical.com> |
27 | | */ |
28 | | |
29 | | /* Algorithms within this file are based on the Calendar FAQ by |
30 | | * Claus Tondering. It can be found at |
31 | | * http://www.tondering.dk/claus/cal/calendar29.txt |
32 | | * |
33 | | * Copyright and disclaimer |
34 | | * ------------------------ |
35 | | * This document is Copyright (C) 2008 by Claus Tondering. |
36 | | * E-mail: claus@tondering.dk. (Please include the word |
37 | | * "calendar" in the subject line.) |
38 | | * The document may be freely distributed, provided this |
39 | | * copyright notice is included and no money is charged for |
40 | | * the document. |
41 | | * |
42 | | * This document is provided "as is". No warranties are made as |
43 | | * to its correctness. |
44 | | */ |
45 | | |
46 | | /* Prologue {{{1 */ |
47 | | |
48 | | #include "config.h" |
49 | | |
50 | | /* langinfo.h in glibc 2.27 defines ALTMON_* only if _GNU_SOURCE is defined. */ |
51 | | #ifndef _GNU_SOURCE |
52 | | #define _GNU_SOURCE 1 |
53 | | #endif |
54 | | |
55 | | #include <math.h> |
56 | | #include <stdlib.h> |
57 | | #include <string.h> |
58 | | |
59 | | #ifdef HAVE_LANGINFO_TIME |
60 | | #include <langinfo.h> |
61 | | #endif |
62 | | |
63 | | #include "gdatetime.h" |
64 | | |
65 | | #include "gslice.h" |
66 | | #include "gatomic.h" |
67 | | #include "gcharset.h" |
68 | | #include "gconvert.h" |
69 | | #include "gfileutils.h" |
70 | | #include "ghash.h" |
71 | | #include "gmain.h" |
72 | | #include "gmappedfile.h" |
73 | | #include "gstrfuncs.h" |
74 | | #include "gtestutils.h" |
75 | | #include "gthread.h" |
76 | | #include "gtimezone.h" |
77 | | |
78 | | #include "glibintl.h" |
79 | | |
80 | | #ifndef G_OS_WIN32 |
81 | | #include <sys/time.h> |
82 | | #include <time.h> |
83 | | #else |
84 | | #if defined (_MSC_VER) && (_MSC_VER < 1800) |
85 | | /* fallback implementation for isnan() on VS2012 and earlier */ |
86 | | #define isnan _isnan |
87 | | #endif |
88 | | #endif /* !G_OS_WIN32 */ |
89 | | |
90 | | /** |
91 | | * SECTION:date-time |
92 | | * @title: GDateTime |
93 | | * @short_description: a structure representing Date and Time |
94 | | * @see_also: #GTimeZone |
95 | | * |
96 | | * #GDateTime is a structure that combines a Gregorian date and time |
97 | | * into a single structure. It provides many conversion and methods to |
98 | | * manipulate dates and times. Time precision is provided down to |
99 | | * microseconds and the time can range (proleptically) from 0001-01-01 |
100 | | * 00:00:00 to 9999-12-31 23:59:59.999999. #GDateTime follows POSIX |
101 | | * time in the sense that it is oblivious to leap seconds. |
102 | | * |
103 | | * #GDateTime is an immutable object; once it has been created it cannot |
104 | | * be modified further. All modifiers will create a new #GDateTime. |
105 | | * Nearly all such functions can fail due to the date or time going out |
106 | | * of range, in which case %NULL will be returned. |
107 | | * |
108 | | * #GDateTime is reference counted: the reference count is increased by calling |
109 | | * g_date_time_ref() and decreased by calling g_date_time_unref(). When the |
110 | | * reference count drops to 0, the resources allocated by the #GDateTime |
111 | | * structure are released. |
112 | | * |
113 | | * Many parts of the API may produce non-obvious results. As an |
114 | | * example, adding two months to January 31st will yield March 31st |
115 | | * whereas adding one month and then one month again will yield either |
116 | | * March 28th or March 29th. Also note that adding 24 hours is not |
117 | | * always the same as adding one day (since days containing daylight |
118 | | * savings time transitions are either 23 or 25 hours in length). |
119 | | * |
120 | | * #GDateTime is available since GLib 2.26. |
121 | | */ |
122 | | |
123 | | struct _GDateTime |
124 | | { |
125 | | /* Microsecond timekeeping within Day */ |
126 | | guint64 usec; |
127 | | |
128 | | /* TimeZone information */ |
129 | | GTimeZone *tz; |
130 | | gint interval; |
131 | | |
132 | | /* 1 is 0001-01-01 in Proleptic Gregorian */ |
133 | | gint32 days; |
134 | | |
135 | | gint ref_count; /* (atomic) */ |
136 | | }; |
137 | | |
138 | | /* Time conversion {{{1 */ |
139 | | |
140 | 16.2k | #define UNIX_EPOCH_START 719163 |
141 | | #define INSTANT_TO_UNIX(instant) \ |
142 | 0 | ((instant)/USEC_PER_SECOND - UNIX_EPOCH_START * SEC_PER_DAY) |
143 | | #define INSTANT_TO_UNIX_USECS(instant) \ |
144 | 0 | ((instant) - UNIX_EPOCH_START * SEC_PER_DAY * USEC_PER_SECOND) |
145 | | #define UNIX_TO_INSTANT(unix) \ |
146 | 0 | (((gint64) (unix) + UNIX_EPOCH_START * SEC_PER_DAY) * USEC_PER_SECOND) |
147 | | #define UNIX_USECS_TO_INSTANT(unix_usecs) \ |
148 | 0 | ((gint64) (unix_usecs) + UNIX_EPOCH_START * SEC_PER_DAY * USEC_PER_SECOND) |
149 | | #define UNIX_TO_INSTANT_IS_VALID(unix) \ |
150 | 0 | ((gint64) (unix) <= INSTANT_TO_UNIX (G_MAXINT64)) |
151 | | #define UNIX_USECS_TO_INSTANT_IS_VALID(unix_usecs) \ |
152 | 0 | ((gint64) (unix_usecs) <= INSTANT_TO_UNIX_USECS (G_MAXINT64)) |
153 | | |
154 | 0 | #define DAYS_IN_4YEARS 1461 /* days in 4 years */ |
155 | 0 | #define DAYS_IN_100YEARS 36524 /* days in 100 years */ |
156 | 0 | #define DAYS_IN_400YEARS 146097 /* days in 400 years */ |
157 | | |
158 | 32.5k | #define USEC_PER_SECOND (G_GINT64_CONSTANT (1000000)) |
159 | 8.13k | #define USEC_PER_MINUTE (G_GINT64_CONSTANT (60000000)) |
160 | 8.13k | #define USEC_PER_HOUR (G_GINT64_CONSTANT (3600000000)) |
161 | | #define USEC_PER_MILLISECOND (G_GINT64_CONSTANT (1000)) |
162 | 0 | #define USEC_PER_DAY (G_GINT64_CONSTANT (86400000000)) |
163 | 32.5k | #define SEC_PER_DAY (G_GINT64_CONSTANT (86400)) |
164 | | |
165 | 16.2k | #define SECS_PER_MINUTE (60) |
166 | 8.13k | #define SECS_PER_HOUR (60 * SECS_PER_MINUTE) |
167 | | #define SECS_PER_DAY (24 * SECS_PER_HOUR) |
168 | | #define SECS_PER_YEAR (365 * SECS_PER_DAY) |
169 | | #define SECS_PER_JULIAN (DAYS_PER_PERIOD * SECS_PER_DAY) |
170 | | |
171 | 44.0k | #define GREGORIAN_LEAP(y) ((((y) % 4) == 0) && (!((((y) % 100) == 0) && (((y) % 400) != 0)))) |
172 | | #define JULIAN_YEAR(d) ((d)->julian / 365.25) |
173 | | #define DAYS_PER_PERIOD (G_GINT64_CONSTANT (2914695)) |
174 | | |
175 | | static const guint16 days_in_months[2][13] = |
176 | | { |
177 | | { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
178 | | { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
179 | | }; |
180 | | |
181 | | static const guint16 days_in_year[2][13] = |
182 | | { |
183 | | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, |
184 | | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } |
185 | | }; |
186 | | |
187 | | #ifdef HAVE_LANGINFO_TIME |
188 | | |
189 | 0 | #define GET_AMPM(d) ((g_date_time_get_hour (d) < 12) ? \ |
190 | 0 | nl_langinfo (AM_STR) : \ |
191 | 0 | nl_langinfo (PM_STR)) |
192 | 0 | #define GET_AMPM_IS_LOCALE TRUE |
193 | | |
194 | 0 | #define PREFERRED_DATE_TIME_FMT nl_langinfo (D_T_FMT) |
195 | 0 | #define PREFERRED_DATE_FMT nl_langinfo (D_FMT) |
196 | 0 | #define PREFERRED_TIME_FMT nl_langinfo (T_FMT) |
197 | 0 | #define PREFERRED_12HR_TIME_FMT nl_langinfo (T_FMT_AMPM) |
198 | | |
199 | | static const gint weekday_item[2][7] = |
200 | | { |
201 | | { ABDAY_2, ABDAY_3, ABDAY_4, ABDAY_5, ABDAY_6, ABDAY_7, ABDAY_1 }, |
202 | | { DAY_2, DAY_3, DAY_4, DAY_5, DAY_6, DAY_7, DAY_1 } |
203 | | }; |
204 | | |
205 | | static const gint month_item[2][12] = |
206 | | { |
207 | | { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 }, |
208 | | { MON_1, MON_2, MON_3, MON_4, MON_5, MON_6, MON_7, MON_8, MON_9, MON_10, MON_11, MON_12 }, |
209 | | }; |
210 | | |
211 | 0 | #define WEEKDAY_ABBR(d) nl_langinfo (weekday_item[0][g_date_time_get_day_of_week (d) - 1]) |
212 | 0 | #define WEEKDAY_ABBR_IS_LOCALE TRUE |
213 | 0 | #define WEEKDAY_FULL(d) nl_langinfo (weekday_item[1][g_date_time_get_day_of_week (d) - 1]) |
214 | 0 | #define WEEKDAY_FULL_IS_LOCALE TRUE |
215 | 0 | #define MONTH_ABBR(d) nl_langinfo (month_item[0][g_date_time_get_month (d) - 1]) |
216 | 0 | #define MONTH_ABBR_IS_LOCALE TRUE |
217 | 0 | #define MONTH_FULL(d) nl_langinfo (month_item[1][g_date_time_get_month (d) - 1]) |
218 | 0 | #define MONTH_FULL_IS_LOCALE TRUE |
219 | | |
220 | | #else |
221 | | |
222 | | #define GET_AMPM(d) (get_fallback_ampm (g_date_time_get_hour (d))) |
223 | | #define GET_AMPM_IS_LOCALE FALSE |
224 | | |
225 | | /* Translators: this is the preferred format for expressing the date and the time */ |
226 | | #define PREFERRED_DATE_TIME_FMT C_("GDateTime", "%a %b %e %H:%M:%S %Y") |
227 | | |
228 | | /* Translators: this is the preferred format for expressing the date */ |
229 | | #define PREFERRED_DATE_FMT C_("GDateTime", "%m/%d/%y") |
230 | | |
231 | | /* Translators: this is the preferred format for expressing the time */ |
232 | | #define PREFERRED_TIME_FMT C_("GDateTime", "%H:%M:%S") |
233 | | |
234 | | /* Translators: this is the preferred format for expressing 12 hour time */ |
235 | | #define PREFERRED_12HR_TIME_FMT C_("GDateTime", "%I:%M:%S %p") |
236 | | |
237 | | #define WEEKDAY_ABBR(d) (get_weekday_name_abbr (g_date_time_get_day_of_week (d))) |
238 | | #define WEEKDAY_ABBR_IS_LOCALE FALSE |
239 | | #define WEEKDAY_FULL(d) (get_weekday_name (g_date_time_get_day_of_week (d))) |
240 | | #define WEEKDAY_FULL_IS_LOCALE FALSE |
241 | | /* We don't yet know if nl_langinfo (MON_n) returns standalone or complete-date |
242 | | * format forms but if nl_langinfo (ALTMON_n) is not supported then we will |
243 | | * have to use MONTH_FULL as standalone. The same if nl_langinfo () does not |
244 | | * exist at all. MONTH_ABBR is similar: if nl_langinfo (_NL_ABALTMON_n) is not |
245 | | * supported then we will use MONTH_ABBR as standalone. |
246 | | */ |
247 | | #define MONTH_ABBR(d) (get_month_name_abbr_standalone (g_date_time_get_month (d))) |
248 | | #define MONTH_ABBR_IS_LOCALE FALSE |
249 | | #define MONTH_FULL(d) (get_month_name_standalone (g_date_time_get_month (d))) |
250 | | #define MONTH_FULL_IS_LOCALE FALSE |
251 | | |
252 | | static const gchar * |
253 | | get_month_name_standalone (gint month) |
254 | | { |
255 | | switch (month) |
256 | | { |
257 | | case 1: |
258 | | /* Translators: Some languages (Baltic, Slavic, Greek, and some more) |
259 | | * need different grammatical forms of month names depending on whether |
260 | | * they are standalone or in a complete date context, with the day |
261 | | * number. Some other languages may prefer starting with uppercase when |
262 | | * they are standalone and with lowercase when they are in a complete |
263 | | * date context. Here are full month names in a form appropriate when |
264 | | * they are used standalone. If your system is Linux with the glibc |
265 | | * version 2.27 (released Feb 1, 2018) or newer or if it is from the BSD |
266 | | * family (which includes OS X) then you can refer to the date command |
267 | | * line utility and see what the command `date +%OB' produces. Also in |
268 | | * the latest Linux the command `locale alt_mon' in your native locale |
269 | | * produces a complete list of month names almost ready to copy and |
270 | | * paste here. Note that in most of the languages (western European, |
271 | | * non-European) there is no difference between the standalone and |
272 | | * complete date form. |
273 | | */ |
274 | | return C_("full month name", "January"); |
275 | | case 2: |
276 | | return C_("full month name", "February"); |
277 | | case 3: |
278 | | return C_("full month name", "March"); |
279 | | case 4: |
280 | | return C_("full month name", "April"); |
281 | | case 5: |
282 | | return C_("full month name", "May"); |
283 | | case 6: |
284 | | return C_("full month name", "June"); |
285 | | case 7: |
286 | | return C_("full month name", "July"); |
287 | | case 8: |
288 | | return C_("full month name", "August"); |
289 | | case 9: |
290 | | return C_("full month name", "September"); |
291 | | case 10: |
292 | | return C_("full month name", "October"); |
293 | | case 11: |
294 | | return C_("full month name", "November"); |
295 | | case 12: |
296 | | return C_("full month name", "December"); |
297 | | |
298 | | default: |
299 | | g_warning ("Invalid month number %d", month); |
300 | | } |
301 | | |
302 | | return NULL; |
303 | | } |
304 | | |
305 | | static const gchar * |
306 | | get_month_name_abbr_standalone (gint month) |
307 | | { |
308 | | switch (month) |
309 | | { |
310 | | case 1: |
311 | | /* Translators: Some languages need different grammatical forms of |
312 | | * month names depending on whether they are standalone or in a complete |
313 | | * date context, with the day number. Some may prefer starting with |
314 | | * uppercase when they are standalone and with lowercase when they are |
315 | | * in a full date context. However, as these names are abbreviated |
316 | | * the grammatical difference is visible probably only in Belarusian |
317 | | * and Russian. In other languages there is no difference between |
318 | | * the standalone and complete date form when they are abbreviated. |
319 | | * If your system is Linux with the glibc version 2.27 (released |
320 | | * Feb 1, 2018) or newer then you can refer to the date command line |
321 | | * utility and see what the command `date +%Ob' produces. Also in |
322 | | * the latest Linux the command `locale ab_alt_mon' in your native |
323 | | * locale produces a complete list of month names almost ready to copy |
324 | | * and paste here. Note that this feature is not yet supported by any |
325 | | * other platform. Here are abbreviated month names in a form |
326 | | * appropriate when they are used standalone. |
327 | | */ |
328 | | return C_("abbreviated month name", "Jan"); |
329 | | case 2: |
330 | | return C_("abbreviated month name", "Feb"); |
331 | | case 3: |
332 | | return C_("abbreviated month name", "Mar"); |
333 | | case 4: |
334 | | return C_("abbreviated month name", "Apr"); |
335 | | case 5: |
336 | | return C_("abbreviated month name", "May"); |
337 | | case 6: |
338 | | return C_("abbreviated month name", "Jun"); |
339 | | case 7: |
340 | | return C_("abbreviated month name", "Jul"); |
341 | | case 8: |
342 | | return C_("abbreviated month name", "Aug"); |
343 | | case 9: |
344 | | return C_("abbreviated month name", "Sep"); |
345 | | case 10: |
346 | | return C_("abbreviated month name", "Oct"); |
347 | | case 11: |
348 | | return C_("abbreviated month name", "Nov"); |
349 | | case 12: |
350 | | return C_("abbreviated month name", "Dec"); |
351 | | |
352 | | default: |
353 | | g_warning ("Invalid month number %d", month); |
354 | | } |
355 | | |
356 | | return NULL; |
357 | | } |
358 | | |
359 | | static const gchar * |
360 | | get_weekday_name (gint day) |
361 | | { |
362 | | switch (day) |
363 | | { |
364 | | case 1: |
365 | | return C_("full weekday name", "Monday"); |
366 | | case 2: |
367 | | return C_("full weekday name", "Tuesday"); |
368 | | case 3: |
369 | | return C_("full weekday name", "Wednesday"); |
370 | | case 4: |
371 | | return C_("full weekday name", "Thursday"); |
372 | | case 5: |
373 | | return C_("full weekday name", "Friday"); |
374 | | case 6: |
375 | | return C_("full weekday name", "Saturday"); |
376 | | case 7: |
377 | | return C_("full weekday name", "Sunday"); |
378 | | |
379 | | default: |
380 | | g_warning ("Invalid week day number %d", day); |
381 | | } |
382 | | |
383 | | return NULL; |
384 | | } |
385 | | |
386 | | static const gchar * |
387 | | get_weekday_name_abbr (gint day) |
388 | | { |
389 | | switch (day) |
390 | | { |
391 | | case 1: |
392 | | return C_("abbreviated weekday name", "Mon"); |
393 | | case 2: |
394 | | return C_("abbreviated weekday name", "Tue"); |
395 | | case 3: |
396 | | return C_("abbreviated weekday name", "Wed"); |
397 | | case 4: |
398 | | return C_("abbreviated weekday name", "Thu"); |
399 | | case 5: |
400 | | return C_("abbreviated weekday name", "Fri"); |
401 | | case 6: |
402 | | return C_("abbreviated weekday name", "Sat"); |
403 | | case 7: |
404 | | return C_("abbreviated weekday name", "Sun"); |
405 | | |
406 | | default: |
407 | | g_warning ("Invalid week day number %d", day); |
408 | | } |
409 | | |
410 | | return NULL; |
411 | | } |
412 | | |
413 | | #endif /* HAVE_LANGINFO_TIME */ |
414 | | |
415 | | #ifdef HAVE_LANGINFO_ALTMON |
416 | | |
417 | | /* If nl_langinfo () supports ALTMON_n then MON_n returns full date format |
418 | | * forms and ALTMON_n returns standalone forms. |
419 | | */ |
420 | | |
421 | 0 | #define MONTH_FULL_WITH_DAY(d) MONTH_FULL(d) |
422 | 0 | #define MONTH_FULL_WITH_DAY_IS_LOCALE MONTH_FULL_IS_LOCALE |
423 | | |
424 | | static const gint alt_month_item[12] = |
425 | | { |
426 | | ALTMON_1, ALTMON_2, ALTMON_3, ALTMON_4, ALTMON_5, ALTMON_6, |
427 | | ALTMON_7, ALTMON_8, ALTMON_9, ALTMON_10, ALTMON_11, ALTMON_12 |
428 | | }; |
429 | | |
430 | 0 | #define MONTH_FULL_STANDALONE(d) nl_langinfo (alt_month_item[g_date_time_get_month (d) - 1]) |
431 | 0 | #define MONTH_FULL_STANDALONE_IS_LOCALE TRUE |
432 | | |
433 | | #else |
434 | | |
435 | | /* If nl_langinfo () does not support ALTMON_n then either MON_n returns |
436 | | * standalone forms or nl_langinfo (MON_n) does not work so we have defined |
437 | | * it as standalone form. |
438 | | */ |
439 | | |
440 | | #define MONTH_FULL_STANDALONE(d) MONTH_FULL(d) |
441 | | #define MONTH_FULL_STANDALONE_IS_LOCALE MONTH_FULL_IS_LOCALE |
442 | | #define MONTH_FULL_WITH_DAY(d) (get_month_name_with_day (g_date_time_get_month (d))) |
443 | | #define MONTH_FULL_WITH_DAY_IS_LOCALE FALSE |
444 | | |
445 | | static const gchar * |
446 | | get_month_name_with_day (gint month) |
447 | | { |
448 | | switch (month) |
449 | | { |
450 | | case 1: |
451 | | /* Translators: Some languages need different grammatical forms of |
452 | | * month names depending on whether they are standalone or in a full |
453 | | * date context, with the day number. Some may prefer starting with |
454 | | * uppercase when they are standalone and with lowercase when they are |
455 | | * in a full date context. Here are full month names in a form |
456 | | * appropriate when they are used in a full date context, with the |
457 | | * day number. If your system is Linux with the glibc version 2.27 |
458 | | * (released Feb 1, 2018) or newer or if it is from the BSD family |
459 | | * (which includes OS X) then you can refer to the date command line |
460 | | * utility and see what the command `date +%B' produces. Also in |
461 | | * the latest Linux the command `locale mon' in your native locale |
462 | | * produces a complete list of month names almost ready to copy and |
463 | | * paste here. In older Linux systems due to a bug the result is |
464 | | * incorrect in some languages. Note that in most of the languages |
465 | | * (western European, non-European) there is no difference between the |
466 | | * standalone and complete date form. |
467 | | */ |
468 | | return C_("full month name with day", "January"); |
469 | | case 2: |
470 | | return C_("full month name with day", "February"); |
471 | | case 3: |
472 | | return C_("full month name with day", "March"); |
473 | | case 4: |
474 | | return C_("full month name with day", "April"); |
475 | | case 5: |
476 | | return C_("full month name with day", "May"); |
477 | | case 6: |
478 | | return C_("full month name with day", "June"); |
479 | | case 7: |
480 | | return C_("full month name with day", "July"); |
481 | | case 8: |
482 | | return C_("full month name with day", "August"); |
483 | | case 9: |
484 | | return C_("full month name with day", "September"); |
485 | | case 10: |
486 | | return C_("full month name with day", "October"); |
487 | | case 11: |
488 | | return C_("full month name with day", "November"); |
489 | | case 12: |
490 | | return C_("full month name with day", "December"); |
491 | | |
492 | | default: |
493 | | g_warning ("Invalid month number %d", month); |
494 | | } |
495 | | |
496 | | return NULL; |
497 | | } |
498 | | |
499 | | #endif /* HAVE_LANGINFO_ALTMON */ |
500 | | |
501 | | #ifdef HAVE_LANGINFO_ABALTMON |
502 | | |
503 | | /* If nl_langinfo () supports _NL_ABALTMON_n then ABMON_n returns full |
504 | | * date format forms and _NL_ABALTMON_n returns standalone forms. |
505 | | */ |
506 | | |
507 | 0 | #define MONTH_ABBR_WITH_DAY(d) MONTH_ABBR(d) |
508 | 0 | #define MONTH_ABBR_WITH_DAY_IS_LOCALE MONTH_ABBR_IS_LOCALE |
509 | | |
510 | | static const gint ab_alt_month_item[12] = |
511 | | { |
512 | | _NL_ABALTMON_1, _NL_ABALTMON_2, _NL_ABALTMON_3, _NL_ABALTMON_4, |
513 | | _NL_ABALTMON_5, _NL_ABALTMON_6, _NL_ABALTMON_7, _NL_ABALTMON_8, |
514 | | _NL_ABALTMON_9, _NL_ABALTMON_10, _NL_ABALTMON_11, _NL_ABALTMON_12 |
515 | | }; |
516 | | |
517 | 0 | #define MONTH_ABBR_STANDALONE(d) nl_langinfo (ab_alt_month_item[g_date_time_get_month (d) - 1]) |
518 | 0 | #define MONTH_ABBR_STANDALONE_IS_LOCALE TRUE |
519 | | |
520 | | #else |
521 | | |
522 | | /* If nl_langinfo () does not support _NL_ABALTMON_n then either ABMON_n |
523 | | * returns standalone forms or nl_langinfo (ABMON_n) does not work so we |
524 | | * have defined it as standalone form. Now it's time to swap. |
525 | | */ |
526 | | |
527 | | #define MONTH_ABBR_STANDALONE(d) MONTH_ABBR(d) |
528 | | #define MONTH_ABBR_STANDALONE_IS_LOCALE MONTH_ABBR_IS_LOCALE |
529 | | #define MONTH_ABBR_WITH_DAY(d) (get_month_name_abbr_with_day (g_date_time_get_month (d))) |
530 | | #define MONTH_ABBR_WITH_DAY_IS_LOCALE FALSE |
531 | | |
532 | | static const gchar * |
533 | | get_month_name_abbr_with_day (gint month) |
534 | | { |
535 | | switch (month) |
536 | | { |
537 | | case 1: |
538 | | /* Translators: Some languages need different grammatical forms of |
539 | | * month names depending on whether they are standalone or in a full |
540 | | * date context, with the day number. Some may prefer starting with |
541 | | * uppercase when they are standalone and with lowercase when they are |
542 | | * in a full date context. Here are abbreviated month names in a form |
543 | | * appropriate when they are used in a full date context, with the |
544 | | * day number. However, as these names are abbreviated the grammatical |
545 | | * difference is visible probably only in Belarusian and Russian. |
546 | | * In other languages there is no difference between the standalone |
547 | | * and complete date form when they are abbreviated. If your system |
548 | | * is Linux with the glibc version 2.27 (released Feb 1, 2018) or newer |
549 | | * then you can refer to the date command line utility and see what the |
550 | | * command `date +%b' produces. Also in the latest Linux the command |
551 | | * `locale abmon' in your native locale produces a complete list of |
552 | | * month names almost ready to copy and paste here. In other systems |
553 | | * due to a bug the result is incorrect in some languages. |
554 | | */ |
555 | | return C_("abbreviated month name with day", "Jan"); |
556 | | case 2: |
557 | | return C_("abbreviated month name with day", "Feb"); |
558 | | case 3: |
559 | | return C_("abbreviated month name with day", "Mar"); |
560 | | case 4: |
561 | | return C_("abbreviated month name with day", "Apr"); |
562 | | case 5: |
563 | | return C_("abbreviated month name with day", "May"); |
564 | | case 6: |
565 | | return C_("abbreviated month name with day", "Jun"); |
566 | | case 7: |
567 | | return C_("abbreviated month name with day", "Jul"); |
568 | | case 8: |
569 | | return C_("abbreviated month name with day", "Aug"); |
570 | | case 9: |
571 | | return C_("abbreviated month name with day", "Sep"); |
572 | | case 10: |
573 | | return C_("abbreviated month name with day", "Oct"); |
574 | | case 11: |
575 | | return C_("abbreviated month name with day", "Nov"); |
576 | | case 12: |
577 | | return C_("abbreviated month name with day", "Dec"); |
578 | | |
579 | | default: |
580 | | g_warning ("Invalid month number %d", month); |
581 | | } |
582 | | |
583 | | return NULL; |
584 | | } |
585 | | |
586 | | #endif /* HAVE_LANGINFO_ABALTMON */ |
587 | | |
588 | | /* Format AM/PM indicator if the locale does not have a localized version. */ |
589 | | static const gchar * |
590 | | get_fallback_ampm (gint hour) |
591 | 0 | { |
592 | 0 | if (hour < 12) |
593 | | /* Translators: 'before midday' indicator */ |
594 | 0 | return C_("GDateTime", "AM"); |
595 | 0 | else |
596 | | /* Translators: 'after midday' indicator */ |
597 | 0 | return C_("GDateTime", "PM"); |
598 | 0 | } |
599 | | |
600 | | static inline gint |
601 | | ymd_to_days (gint year, |
602 | | gint month, |
603 | | gint day) |
604 | 16.2k | { |
605 | 16.2k | gint64 days; |
606 | | |
607 | 16.2k | days = ((gint64) year - 1) * 365 + ((year - 1) / 4) - ((year - 1) / 100) |
608 | 16.2k | + ((year - 1) / 400); |
609 | | |
610 | 16.2k | days += days_in_year[0][month - 1]; |
611 | 16.2k | if (GREGORIAN_LEAP (year) && month > 2) |
612 | 4.90k | day++; |
613 | | |
614 | 16.2k | days += day; |
615 | | |
616 | 16.2k | return days; |
617 | 16.2k | } |
618 | | |
619 | | static void |
620 | | g_date_time_get_week_number (GDateTime *datetime, |
621 | | gint *week_number, |
622 | | gint *day_of_week, |
623 | | gint *day_of_year) |
624 | 0 | { |
625 | 0 | gint a, b, c, d, e, f, g, n, s, month, day, year; |
626 | |
|
627 | 0 | g_date_time_get_ymd (datetime, &year, &month, &day); |
628 | |
|
629 | 0 | if (month <= 2) |
630 | 0 | { |
631 | 0 | a = g_date_time_get_year (datetime) - 1; |
632 | 0 | b = (a / 4) - (a / 100) + (a / 400); |
633 | 0 | c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400); |
634 | 0 | s = b - c; |
635 | 0 | e = 0; |
636 | 0 | f = day - 1 + (31 * (month - 1)); |
637 | 0 | } |
638 | 0 | else |
639 | 0 | { |
640 | 0 | a = year; |
641 | 0 | b = (a / 4) - (a / 100) + (a / 400); |
642 | 0 | c = ((a - 1) / 4) - ((a - 1) / 100) + ((a - 1) / 400); |
643 | 0 | s = b - c; |
644 | 0 | e = s + 1; |
645 | 0 | f = day + (((153 * (month - 3)) + 2) / 5) + 58 + s; |
646 | 0 | } |
647 | |
|
648 | 0 | g = (a + b) % 7; |
649 | 0 | d = (f + g - e) % 7; |
650 | 0 | n = f + 3 - d; |
651 | |
|
652 | 0 | if (week_number) |
653 | 0 | { |
654 | 0 | if (n < 0) |
655 | 0 | *week_number = 53 - ((g - s) / 5); |
656 | 0 | else if (n > 364 + s) |
657 | 0 | *week_number = 1; |
658 | 0 | else |
659 | 0 | *week_number = (n / 7) + 1; |
660 | 0 | } |
661 | |
|
662 | 0 | if (day_of_week) |
663 | 0 | *day_of_week = d + 1; |
664 | |
|
665 | 0 | if (day_of_year) |
666 | 0 | *day_of_year = f + 1; |
667 | 0 | } |
668 | | |
669 | | /* Lifecycle {{{1 */ |
670 | | |
671 | | static GDateTime * |
672 | | g_date_time_alloc (GTimeZone *tz) |
673 | 8.13k | { |
674 | 8.13k | GDateTime *datetime; |
675 | | |
676 | 8.13k | datetime = g_slice_new0 (GDateTime); |
677 | 8.13k | datetime->tz = g_time_zone_ref (tz); |
678 | 8.13k | datetime->ref_count = 1; |
679 | | |
680 | 8.13k | return datetime; |
681 | 8.13k | } |
682 | | |
683 | | /** |
684 | | * g_date_time_ref: |
685 | | * @datetime: a #GDateTime |
686 | | * |
687 | | * Atomically increments the reference count of @datetime by one. |
688 | | * |
689 | | * Returns: the #GDateTime with the reference count increased |
690 | | * |
691 | | * Since: 2.26 |
692 | | */ |
693 | | GDateTime * |
694 | | g_date_time_ref (GDateTime *datetime) |
695 | 8.13k | { |
696 | 8.13k | g_return_val_if_fail (datetime != NULL, NULL); |
697 | 8.13k | g_return_val_if_fail (datetime->ref_count > 0, NULL); |
698 | | |
699 | 8.13k | g_atomic_int_inc (&datetime->ref_count); |
700 | | |
701 | 8.13k | return datetime; |
702 | 8.13k | } |
703 | | |
704 | | /** |
705 | | * g_date_time_unref: |
706 | | * @datetime: a #GDateTime |
707 | | * |
708 | | * Atomically decrements the reference count of @datetime by one. |
709 | | * |
710 | | * When the reference count reaches zero, the resources allocated by |
711 | | * @datetime are freed |
712 | | * |
713 | | * Since: 2.26 |
714 | | */ |
715 | | void |
716 | | g_date_time_unref (GDateTime *datetime) |
717 | 16.2k | { |
718 | 16.2k | g_return_if_fail (datetime != NULL); |
719 | 16.2k | g_return_if_fail (datetime->ref_count > 0); |
720 | | |
721 | 16.2k | if (g_atomic_int_dec_and_test (&datetime->ref_count)) |
722 | 8.13k | { |
723 | 8.13k | g_time_zone_unref (datetime->tz); |
724 | 8.13k | g_slice_free (GDateTime, datetime); |
725 | 8.13k | } |
726 | 16.2k | } |
727 | | |
728 | | /* Internal state transformers {{{1 */ |
729 | | /*< internal > |
730 | | * g_date_time_to_instant: |
731 | | * @datetime: a #GDateTime |
732 | | * |
733 | | * Convert a @datetime into an instant. |
734 | | * |
735 | | * An instant is a number that uniquely describes a particular |
736 | | * microsecond in time, taking time zone considerations into account. |
737 | | * (ie: "03:00 -0400" is the same instant as "02:00 -0500"). |
738 | | * |
739 | | * An instant is always positive but we use a signed return value to |
740 | | * avoid troubles with C. |
741 | | */ |
742 | | static gint64 |
743 | | g_date_time_to_instant (GDateTime *datetime) |
744 | 0 | { |
745 | 0 | gint64 offset; |
746 | |
|
747 | 0 | offset = g_time_zone_get_offset (datetime->tz, datetime->interval); |
748 | 0 | offset *= USEC_PER_SECOND; |
749 | |
|
750 | 0 | return datetime->days * USEC_PER_DAY + datetime->usec - offset; |
751 | 0 | } |
752 | | |
753 | | /*< internal > |
754 | | * g_date_time_from_instant: |
755 | | * @tz: a #GTimeZone |
756 | | * @instant: an instant in time |
757 | | * |
758 | | * Creates a #GDateTime from a time zone and an instant. |
759 | | * |
760 | | * This might fail if the time ends up being out of range. |
761 | | */ |
762 | | static GDateTime * |
763 | | g_date_time_from_instant (GTimeZone *tz, |
764 | | gint64 instant) |
765 | 0 | { |
766 | 0 | GDateTime *datetime; |
767 | 0 | gint64 offset; |
768 | |
|
769 | 0 | if (instant < 0 || instant > G_GINT64_CONSTANT (1000000000000000000)) |
770 | 0 | return NULL; |
771 | | |
772 | 0 | datetime = g_date_time_alloc (tz); |
773 | 0 | datetime->interval = g_time_zone_find_interval (tz, |
774 | 0 | G_TIME_TYPE_UNIVERSAL, |
775 | 0 | INSTANT_TO_UNIX (instant)); |
776 | 0 | offset = g_time_zone_get_offset (datetime->tz, datetime->interval); |
777 | 0 | offset *= USEC_PER_SECOND; |
778 | |
|
779 | 0 | instant += offset; |
780 | |
|
781 | 0 | datetime->days = instant / USEC_PER_DAY; |
782 | 0 | datetime->usec = instant % USEC_PER_DAY; |
783 | |
|
784 | 0 | if (datetime->days < 1 || 3652059 < datetime->days) |
785 | 0 | { |
786 | 0 | g_date_time_unref (datetime); |
787 | 0 | datetime = NULL; |
788 | 0 | } |
789 | |
|
790 | 0 | return datetime; |
791 | 0 | } |
792 | | |
793 | | |
794 | | /*< internal > |
795 | | * g_date_time_deal_with_date_change: |
796 | | * @datetime: a #GDateTime |
797 | | * |
798 | | * This function should be called whenever the date changes by adding |
799 | | * days, months or years. It does three things. |
800 | | * |
801 | | * First, we ensure that the date falls between 0001-01-01 and |
802 | | * 9999-12-31 and return %FALSE if it does not. |
803 | | * |
804 | | * Next we update the ->interval field. |
805 | | * |
806 | | * Finally, we ensure that the resulting date and time pair exists (by |
807 | | * ensuring that our time zone has an interval containing it) and |
808 | | * adjusting as required. For example, if we have the time 02:30:00 on |
809 | | * March 13 2010 in Toronto and we add 1 day to it, we would end up with |
810 | | * 2:30am on March 14th, which doesn't exist. In that case, we bump the |
811 | | * time up to 3:00am. |
812 | | */ |
813 | | static gboolean |
814 | | g_date_time_deal_with_date_change (GDateTime *datetime) |
815 | 0 | { |
816 | 0 | GTimeType was_dst; |
817 | 0 | gint64 full_time; |
818 | 0 | gint64 usec; |
819 | |
|
820 | 0 | if (datetime->days < 1 || datetime->days > 3652059) |
821 | 0 | return FALSE; |
822 | | |
823 | 0 | was_dst = g_time_zone_is_dst (datetime->tz, datetime->interval); |
824 | |
|
825 | 0 | full_time = datetime->days * USEC_PER_DAY + datetime->usec; |
826 | | |
827 | |
|
828 | 0 | usec = full_time % USEC_PER_SECOND; |
829 | 0 | full_time /= USEC_PER_SECOND; |
830 | 0 | full_time -= UNIX_EPOCH_START * SEC_PER_DAY; |
831 | |
|
832 | 0 | datetime->interval = g_time_zone_adjust_time (datetime->tz, |
833 | 0 | was_dst, |
834 | 0 | &full_time); |
835 | 0 | full_time += UNIX_EPOCH_START * SEC_PER_DAY; |
836 | 0 | full_time *= USEC_PER_SECOND; |
837 | 0 | full_time += usec; |
838 | |
|
839 | 0 | datetime->days = full_time / USEC_PER_DAY; |
840 | 0 | datetime->usec = full_time % USEC_PER_DAY; |
841 | | |
842 | | /* maybe daylight time caused us to shift to a different day, |
843 | | * but it definitely didn't push us into a different year */ |
844 | 0 | return TRUE; |
845 | 0 | } |
846 | | |
847 | | static GDateTime * |
848 | | g_date_time_replace_days (GDateTime *datetime, |
849 | | gint days) |
850 | 0 | { |
851 | 0 | GDateTime *new; |
852 | |
|
853 | 0 | new = g_date_time_alloc (datetime->tz); |
854 | 0 | new->interval = datetime->interval; |
855 | 0 | new->usec = datetime->usec; |
856 | 0 | new->days = days; |
857 | |
|
858 | 0 | if (!g_date_time_deal_with_date_change (new)) |
859 | 0 | { |
860 | 0 | g_date_time_unref (new); |
861 | 0 | new = NULL; |
862 | 0 | } |
863 | |
|
864 | 0 | return new; |
865 | 0 | } |
866 | | |
867 | | /* now/unix/timeval Constructors {{{1 */ |
868 | | |
869 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
870 | | /*< internal > |
871 | | * g_date_time_new_from_timeval: |
872 | | * @tz: a #GTimeZone |
873 | | * @tv: a #GTimeVal |
874 | | * |
875 | | * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the |
876 | | * given time zone @tz. |
877 | | * |
878 | | * The time contained in a #GTimeVal is always stored in the form of |
879 | | * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the |
880 | | * given time zone. |
881 | | * |
882 | | * This call can fail (returning %NULL) if @tv represents a time outside |
883 | | * of the supported range of #GDateTime. |
884 | | * |
885 | | * You should release the return value by calling g_date_time_unref() |
886 | | * when you are done with it. |
887 | | * |
888 | | * Returns: a new #GDateTime, or %NULL |
889 | | * |
890 | | * Since: 2.26 |
891 | | **/ |
892 | | static GDateTime * |
893 | | g_date_time_new_from_timeval (GTimeZone *tz, |
894 | | const GTimeVal *tv) |
895 | 0 | { |
896 | 0 | if ((gint64) tv->tv_sec > G_MAXINT64 - 1 || |
897 | 0 | !UNIX_TO_INSTANT_IS_VALID ((gint64) tv->tv_sec + 1)) |
898 | 0 | return NULL; |
899 | | |
900 | 0 | return g_date_time_from_instant (tz, tv->tv_usec + |
901 | 0 | UNIX_TO_INSTANT (tv->tv_sec)); |
902 | 0 | } |
903 | | G_GNUC_END_IGNORE_DEPRECATIONS |
904 | | |
905 | | /*< internal > |
906 | | * g_date_time_new_from_unix: |
907 | | * @tz: a #GTimeZone |
908 | | * @usecs: the Unix time, in microseconds since the epoch |
909 | | * |
910 | | * Creates a #GDateTime corresponding to the given Unix time @t_us in the |
911 | | * given time zone @tz. |
912 | | * |
913 | | * Unix time is the number of seconds that have elapsed since 1970-01-01 |
914 | | * 00:00:00 UTC, regardless of the time zone given. |
915 | | * |
916 | | * This call can fail (returning %NULL) if @t represents a time outside |
917 | | * of the supported range of #GDateTime. |
918 | | * |
919 | | * You should release the return value by calling g_date_time_unref() |
920 | | * when you are done with it. |
921 | | * |
922 | | * Returns: a new #GDateTime, or %NULL |
923 | | * |
924 | | * Since: 2.26 |
925 | | **/ |
926 | | static GDateTime * |
927 | | g_date_time_new_from_unix (GTimeZone *tz, |
928 | | gint64 usecs) |
929 | 0 | { |
930 | 0 | if (!UNIX_USECS_TO_INSTANT_IS_VALID (usecs)) |
931 | 0 | return NULL; |
932 | | |
933 | 0 | return g_date_time_from_instant (tz, UNIX_USECS_TO_INSTANT (usecs)); |
934 | 0 | } |
935 | | |
936 | | /** |
937 | | * g_date_time_new_now: (constructor) |
938 | | * @tz: a #GTimeZone |
939 | | * |
940 | | * Creates a #GDateTime corresponding to this exact instant in the given |
941 | | * time zone @tz. The time is as accurate as the system allows, to a |
942 | | * maximum accuracy of 1 microsecond. |
943 | | * |
944 | | * This function will always succeed unless GLib is still being used after the |
945 | | * year 9999. |
946 | | * |
947 | | * You should release the return value by calling g_date_time_unref() |
948 | | * when you are done with it. |
949 | | * |
950 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
951 | | * |
952 | | * Since: 2.26 |
953 | | **/ |
954 | | GDateTime * |
955 | | g_date_time_new_now (GTimeZone *tz) |
956 | 0 | { |
957 | 0 | gint64 now_us; |
958 | |
|
959 | 0 | now_us = g_get_real_time (); |
960 | |
|
961 | 0 | return g_date_time_new_from_unix (tz, now_us); |
962 | 0 | } |
963 | | |
964 | | /** |
965 | | * g_date_time_new_now_local: (constructor) |
966 | | * |
967 | | * Creates a #GDateTime corresponding to this exact instant in the local |
968 | | * time zone. |
969 | | * |
970 | | * This is equivalent to calling g_date_time_new_now() with the time |
971 | | * zone returned by g_time_zone_new_local(). |
972 | | * |
973 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
974 | | * |
975 | | * Since: 2.26 |
976 | | **/ |
977 | | GDateTime * |
978 | | g_date_time_new_now_local (void) |
979 | 0 | { |
980 | 0 | GDateTime *datetime; |
981 | 0 | GTimeZone *local; |
982 | |
|
983 | 0 | local = g_time_zone_new_local (); |
984 | 0 | datetime = g_date_time_new_now (local); |
985 | 0 | g_time_zone_unref (local); |
986 | |
|
987 | 0 | return datetime; |
988 | 0 | } |
989 | | |
990 | | /** |
991 | | * g_date_time_new_now_utc: (constructor) |
992 | | * |
993 | | * Creates a #GDateTime corresponding to this exact instant in UTC. |
994 | | * |
995 | | * This is equivalent to calling g_date_time_new_now() with the time |
996 | | * zone returned by g_time_zone_new_utc(). |
997 | | * |
998 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
999 | | * |
1000 | | * Since: 2.26 |
1001 | | **/ |
1002 | | GDateTime * |
1003 | | g_date_time_new_now_utc (void) |
1004 | 0 | { |
1005 | 0 | GDateTime *datetime; |
1006 | 0 | GTimeZone *utc; |
1007 | |
|
1008 | 0 | utc = g_time_zone_new_utc (); |
1009 | 0 | datetime = g_date_time_new_now (utc); |
1010 | 0 | g_time_zone_unref (utc); |
1011 | |
|
1012 | 0 | return datetime; |
1013 | 0 | } |
1014 | | |
1015 | | /** |
1016 | | * g_date_time_new_from_unix_local: (constructor) |
1017 | | * @t: the Unix time |
1018 | | * |
1019 | | * Creates a #GDateTime corresponding to the given Unix time @t in the |
1020 | | * local time zone. |
1021 | | * |
1022 | | * Unix time is the number of seconds that have elapsed since 1970-01-01 |
1023 | | * 00:00:00 UTC, regardless of the local time offset. |
1024 | | * |
1025 | | * This call can fail (returning %NULL) if @t represents a time outside |
1026 | | * of the supported range of #GDateTime. |
1027 | | * |
1028 | | * You should release the return value by calling g_date_time_unref() |
1029 | | * when you are done with it. |
1030 | | * |
1031 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
1032 | | * |
1033 | | * Since: 2.26 |
1034 | | **/ |
1035 | | GDateTime * |
1036 | | g_date_time_new_from_unix_local (gint64 t) |
1037 | 0 | { |
1038 | 0 | GDateTime *datetime; |
1039 | 0 | GTimeZone *local; |
1040 | |
|
1041 | 0 | if (t > G_MAXINT64 / USEC_PER_SECOND || |
1042 | 0 | t < G_MININT64 / USEC_PER_SECOND) |
1043 | 0 | return NULL; |
1044 | | |
1045 | 0 | local = g_time_zone_new_local (); |
1046 | 0 | datetime = g_date_time_new_from_unix (local, t * USEC_PER_SECOND); |
1047 | 0 | g_time_zone_unref (local); |
1048 | |
|
1049 | 0 | return datetime; |
1050 | 0 | } |
1051 | | |
1052 | | /** |
1053 | | * g_date_time_new_from_unix_utc: (constructor) |
1054 | | * @t: the Unix time |
1055 | | * |
1056 | | * Creates a #GDateTime corresponding to the given Unix time @t in UTC. |
1057 | | * |
1058 | | * Unix time is the number of seconds that have elapsed since 1970-01-01 |
1059 | | * 00:00:00 UTC. |
1060 | | * |
1061 | | * This call can fail (returning %NULL) if @t represents a time outside |
1062 | | * of the supported range of #GDateTime. |
1063 | | * |
1064 | | * You should release the return value by calling g_date_time_unref() |
1065 | | * when you are done with it. |
1066 | | * |
1067 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
1068 | | * |
1069 | | * Since: 2.26 |
1070 | | **/ |
1071 | | GDateTime * |
1072 | | g_date_time_new_from_unix_utc (gint64 t) |
1073 | 0 | { |
1074 | 0 | GDateTime *datetime; |
1075 | 0 | GTimeZone *utc; |
1076 | |
|
1077 | 0 | if (t > G_MAXINT64 / USEC_PER_SECOND || |
1078 | 0 | t < G_MININT64 / USEC_PER_SECOND) |
1079 | 0 | return NULL; |
1080 | | |
1081 | 0 | utc = g_time_zone_new_utc (); |
1082 | 0 | datetime = g_date_time_new_from_unix (utc, t * USEC_PER_SECOND); |
1083 | 0 | g_time_zone_unref (utc); |
1084 | |
|
1085 | 0 | return datetime; |
1086 | 0 | } |
1087 | | |
1088 | | /** |
1089 | | * g_date_time_new_from_timeval_local: (constructor) |
1090 | | * @tv: a #GTimeVal |
1091 | | * |
1092 | | * Creates a #GDateTime corresponding to the given #GTimeVal @tv in the |
1093 | | * local time zone. |
1094 | | * |
1095 | | * The time contained in a #GTimeVal is always stored in the form of |
1096 | | * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the |
1097 | | * local time offset. |
1098 | | * |
1099 | | * This call can fail (returning %NULL) if @tv represents a time outside |
1100 | | * of the supported range of #GDateTime. |
1101 | | * |
1102 | | * You should release the return value by calling g_date_time_unref() |
1103 | | * when you are done with it. |
1104 | | * |
1105 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
1106 | | * |
1107 | | * Since: 2.26 |
1108 | | * Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use |
1109 | | * g_date_time_new_from_unix_local() instead. |
1110 | | **/ |
1111 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
1112 | | GDateTime * |
1113 | | g_date_time_new_from_timeval_local (const GTimeVal *tv) |
1114 | 0 | { |
1115 | 0 | GDateTime *datetime; |
1116 | 0 | GTimeZone *local; |
1117 | |
|
1118 | 0 | local = g_time_zone_new_local (); |
1119 | 0 | datetime = g_date_time_new_from_timeval (local, tv); |
1120 | 0 | g_time_zone_unref (local); |
1121 | |
|
1122 | 0 | return datetime; |
1123 | 0 | } |
1124 | | G_GNUC_END_IGNORE_DEPRECATIONS |
1125 | | |
1126 | | /** |
1127 | | * g_date_time_new_from_timeval_utc: (constructor) |
1128 | | * @tv: a #GTimeVal |
1129 | | * |
1130 | | * Creates a #GDateTime corresponding to the given #GTimeVal @tv in UTC. |
1131 | | * |
1132 | | * The time contained in a #GTimeVal is always stored in the form of |
1133 | | * seconds elapsed since 1970-01-01 00:00:00 UTC. |
1134 | | * |
1135 | | * This call can fail (returning %NULL) if @tv represents a time outside |
1136 | | * of the supported range of #GDateTime. |
1137 | | * |
1138 | | * You should release the return value by calling g_date_time_unref() |
1139 | | * when you are done with it. |
1140 | | * |
1141 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
1142 | | * |
1143 | | * Since: 2.26 |
1144 | | * Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use |
1145 | | * g_date_time_new_from_unix_utc() instead. |
1146 | | **/ |
1147 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
1148 | | GDateTime * |
1149 | | g_date_time_new_from_timeval_utc (const GTimeVal *tv) |
1150 | 0 | { |
1151 | 0 | GDateTime *datetime; |
1152 | 0 | GTimeZone *utc; |
1153 | |
|
1154 | 0 | utc = g_time_zone_new_utc (); |
1155 | 0 | datetime = g_date_time_new_from_timeval (utc, tv); |
1156 | 0 | g_time_zone_unref (utc); |
1157 | |
|
1158 | 0 | return datetime; |
1159 | 0 | } |
1160 | | G_GNUC_END_IGNORE_DEPRECATIONS |
1161 | | |
1162 | | /* Parse integers in the form d (week days), dd (hours etc), ddd (ordinal days) or dddd (years) */ |
1163 | | static gboolean |
1164 | | get_iso8601_int (const gchar *text, gsize length, gint *value) |
1165 | 0 | { |
1166 | 0 | gsize i; |
1167 | 0 | guint v = 0; |
1168 | |
|
1169 | 0 | if (length < 1 || length > 4) |
1170 | 0 | return FALSE; |
1171 | | |
1172 | 0 | for (i = 0; i < length; i++) |
1173 | 0 | { |
1174 | 0 | const gchar c = text[i]; |
1175 | 0 | if (c < '0' || c > '9') |
1176 | 0 | return FALSE; |
1177 | 0 | v = v * 10 + (c - '0'); |
1178 | 0 | } |
1179 | | |
1180 | 0 | *value = v; |
1181 | 0 | return TRUE; |
1182 | 0 | } |
1183 | | |
1184 | | /* Parse seconds in the form ss or ss.sss (variable length decimal) */ |
1185 | | static gboolean |
1186 | | get_iso8601_seconds (const gchar *text, gsize length, gdouble *value) |
1187 | 0 | { |
1188 | 0 | gsize i; |
1189 | 0 | guint64 divisor = 1, v = 0; |
1190 | |
|
1191 | 0 | if (length < 2) |
1192 | 0 | return FALSE; |
1193 | | |
1194 | 0 | for (i = 0; i < 2; i++) |
1195 | 0 | { |
1196 | 0 | const gchar c = text[i]; |
1197 | 0 | if (c < '0' || c > '9') |
1198 | 0 | return FALSE; |
1199 | 0 | v = v * 10 + (c - '0'); |
1200 | 0 | } |
1201 | | |
1202 | 0 | if (length > 2 && !(text[i] == '.' || text[i] == ',')) |
1203 | 0 | return FALSE; |
1204 | | |
1205 | | /* Ignore leap seconds, see g_date_time_new_from_iso8601() */ |
1206 | 0 | if (v >= 60.0 && v <= 61.0) |
1207 | 0 | v = 59.0; |
1208 | |
|
1209 | 0 | i++; |
1210 | 0 | if (i == length) |
1211 | 0 | return FALSE; |
1212 | | |
1213 | 0 | for (; i < length; i++) |
1214 | 0 | { |
1215 | 0 | const gchar c = text[i]; |
1216 | 0 | if (c < '0' || c > '9' || |
1217 | 0 | v > (G_MAXUINT64 - (c - '0')) / 10 || |
1218 | 0 | divisor > G_MAXUINT64 / 10) |
1219 | 0 | return FALSE; |
1220 | 0 | v = v * 10 + (c - '0'); |
1221 | 0 | divisor *= 10; |
1222 | 0 | } |
1223 | | |
1224 | 0 | *value = (gdouble) v / divisor; |
1225 | 0 | return TRUE; |
1226 | 0 | } |
1227 | | |
1228 | | static GDateTime * |
1229 | | g_date_time_new_ordinal (GTimeZone *tz, gint year, gint ordinal_day, gint hour, gint minute, gdouble seconds) |
1230 | 0 | { |
1231 | 0 | GDateTime *dt; |
1232 | |
|
1233 | 0 | if (ordinal_day < 1 || ordinal_day > (GREGORIAN_LEAP (year) ? 366 : 365)) |
1234 | 0 | return NULL; |
1235 | | |
1236 | 0 | dt = g_date_time_new (tz, year, 1, 1, hour, minute, seconds); |
1237 | 0 | if (dt == NULL) |
1238 | 0 | return NULL; |
1239 | 0 | dt->days += ordinal_day - 1; |
1240 | |
|
1241 | 0 | return dt; |
1242 | 0 | } |
1243 | | |
1244 | | static GDateTime * |
1245 | | g_date_time_new_week (GTimeZone *tz, gint year, gint week, gint week_day, gint hour, gint minute, gdouble seconds) |
1246 | 0 | { |
1247 | 0 | gint64 p; |
1248 | 0 | gint max_week, jan4_week_day, ordinal_day; |
1249 | 0 | GDateTime *dt; |
1250 | |
|
1251 | 0 | p = (year * 365 + (year / 4) - (year / 100) + (year / 400)) % 7; |
1252 | 0 | max_week = p == 4 ? 53 : 52; |
1253 | |
|
1254 | 0 | if (week < 1 || week > max_week || week_day < 1 || week_day > 7) |
1255 | 0 | return NULL; |
1256 | | |
1257 | 0 | dt = g_date_time_new (tz, year, 1, 4, 0, 0, 0); |
1258 | 0 | if (dt == NULL) |
1259 | 0 | return NULL; |
1260 | 0 | g_date_time_get_week_number (dt, NULL, &jan4_week_day, NULL); |
1261 | 0 | g_date_time_unref (dt); |
1262 | |
|
1263 | 0 | ordinal_day = (week * 7) + week_day - (jan4_week_day + 3); |
1264 | 0 | if (ordinal_day < 0) |
1265 | 0 | { |
1266 | 0 | year--; |
1267 | 0 | ordinal_day += GREGORIAN_LEAP (year) ? 366 : 365; |
1268 | 0 | } |
1269 | 0 | else if (ordinal_day > (GREGORIAN_LEAP (year) ? 366 : 365)) |
1270 | 0 | { |
1271 | 0 | ordinal_day -= (GREGORIAN_LEAP (year) ? 366 : 365); |
1272 | 0 | year++; |
1273 | 0 | } |
1274 | |
|
1275 | 0 | return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds); |
1276 | 0 | } |
1277 | | |
1278 | | static GDateTime * |
1279 | | parse_iso8601_date (const gchar *text, gsize length, |
1280 | | gint hour, gint minute, gdouble seconds, GTimeZone *tz) |
1281 | 0 | { |
1282 | | /* YYYY-MM-DD */ |
1283 | 0 | if (length == 10 && text[4] == '-' && text[7] == '-') |
1284 | 0 | { |
1285 | 0 | int year, month, day; |
1286 | 0 | if (!get_iso8601_int (text, 4, &year) || |
1287 | 0 | !get_iso8601_int (text + 5, 2, &month) || |
1288 | 0 | !get_iso8601_int (text + 8, 2, &day)) |
1289 | 0 | return NULL; |
1290 | 0 | return g_date_time_new (tz, year, month, day, hour, minute, seconds); |
1291 | 0 | } |
1292 | | /* YYYY-DDD */ |
1293 | 0 | else if (length == 8 && text[4] == '-') |
1294 | 0 | { |
1295 | 0 | gint year, ordinal_day; |
1296 | 0 | if (!get_iso8601_int (text, 4, &year) || |
1297 | 0 | !get_iso8601_int (text + 5, 3, &ordinal_day)) |
1298 | 0 | return NULL; |
1299 | 0 | return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds); |
1300 | 0 | } |
1301 | | /* YYYY-Www-D */ |
1302 | 0 | else if (length == 10 && text[4] == '-' && text[5] == 'W' && text[8] == '-') |
1303 | 0 | { |
1304 | 0 | gint year, week, week_day; |
1305 | 0 | if (!get_iso8601_int (text, 4, &year) || |
1306 | 0 | !get_iso8601_int (text + 6, 2, &week) || |
1307 | 0 | !get_iso8601_int (text + 9, 1, &week_day)) |
1308 | 0 | return NULL; |
1309 | 0 | return g_date_time_new_week (tz, year, week, week_day, hour, minute, seconds); |
1310 | 0 | } |
1311 | | /* YYYYWwwD */ |
1312 | 0 | else if (length == 8 && text[4] == 'W') |
1313 | 0 | { |
1314 | 0 | gint year, week, week_day; |
1315 | 0 | if (!get_iso8601_int (text, 4, &year) || |
1316 | 0 | !get_iso8601_int (text + 5, 2, &week) || |
1317 | 0 | !get_iso8601_int (text + 7, 1, &week_day)) |
1318 | 0 | return NULL; |
1319 | 0 | return g_date_time_new_week (tz, year, week, week_day, hour, minute, seconds); |
1320 | 0 | } |
1321 | | /* YYYYMMDD */ |
1322 | 0 | else if (length == 8) |
1323 | 0 | { |
1324 | 0 | int year, month, day; |
1325 | 0 | if (!get_iso8601_int (text, 4, &year) || |
1326 | 0 | !get_iso8601_int (text + 4, 2, &month) || |
1327 | 0 | !get_iso8601_int (text + 6, 2, &day)) |
1328 | 0 | return NULL; |
1329 | 0 | return g_date_time_new (tz, year, month, day, hour, minute, seconds); |
1330 | 0 | } |
1331 | | /* YYYYDDD */ |
1332 | 0 | else if (length == 7) |
1333 | 0 | { |
1334 | 0 | gint year, ordinal_day; |
1335 | 0 | if (!get_iso8601_int (text, 4, &year) || |
1336 | 0 | !get_iso8601_int (text + 4, 3, &ordinal_day)) |
1337 | 0 | return NULL; |
1338 | 0 | return g_date_time_new_ordinal (tz, year, ordinal_day, hour, minute, seconds); |
1339 | 0 | } |
1340 | 0 | else |
1341 | 0 | return FALSE; |
1342 | 0 | } |
1343 | | |
1344 | | static GTimeZone * |
1345 | | parse_iso8601_timezone (const gchar *text, gsize length, gssize *tz_offset) |
1346 | 0 | { |
1347 | 0 | gint i, tz_length, offset_hours, offset_minutes; |
1348 | 0 | gint offset_sign = 1; |
1349 | 0 | GTimeZone *tz; |
1350 | | |
1351 | | /* UTC uses Z suffix */ |
1352 | 0 | if (length > 0 && text[length - 1] == 'Z') |
1353 | 0 | { |
1354 | 0 | *tz_offset = length - 1; |
1355 | 0 | return g_time_zone_new_utc (); |
1356 | 0 | } |
1357 | | |
1358 | | /* Look for '+' or '-' of offset */ |
1359 | 0 | for (i = length - 1; i >= 0; i--) |
1360 | 0 | if (text[i] == '+' || text[i] == '-') |
1361 | 0 | { |
1362 | 0 | offset_sign = text[i] == '-' ? -1 : 1; |
1363 | 0 | break; |
1364 | 0 | } |
1365 | 0 | if (i < 0) |
1366 | 0 | return NULL; |
1367 | 0 | tz_length = length - i; |
1368 | | |
1369 | | /* +hh:mm or -hh:mm */ |
1370 | 0 | if (tz_length == 6 && text[i+3] == ':') |
1371 | 0 | { |
1372 | 0 | if (!get_iso8601_int (text + i + 1, 2, &offset_hours) || |
1373 | 0 | !get_iso8601_int (text + i + 4, 2, &offset_minutes)) |
1374 | 0 | return NULL; |
1375 | 0 | } |
1376 | | /* +hhmm or -hhmm */ |
1377 | 0 | else if (tz_length == 5) |
1378 | 0 | { |
1379 | 0 | if (!get_iso8601_int (text + i + 1, 2, &offset_hours) || |
1380 | 0 | !get_iso8601_int (text + i + 3, 2, &offset_minutes)) |
1381 | 0 | return NULL; |
1382 | 0 | } |
1383 | | /* +hh or -hh */ |
1384 | 0 | else if (tz_length == 3) |
1385 | 0 | { |
1386 | 0 | if (!get_iso8601_int (text + i + 1, 2, &offset_hours)) |
1387 | 0 | return NULL; |
1388 | 0 | offset_minutes = 0; |
1389 | 0 | } |
1390 | 0 | else |
1391 | 0 | return NULL; |
1392 | | |
1393 | 0 | *tz_offset = i; |
1394 | 0 | tz = g_time_zone_new_identifier (text + i); |
1395 | | |
1396 | | /* Double-check that the GTimeZone matches our interpretation of the timezone. |
1397 | | * This can fail because our interpretation is less strict than (for example) |
1398 | | * parse_time() in gtimezone.c, which restricts the range of the parsed |
1399 | | * integers. */ |
1400 | 0 | if (tz == NULL || g_time_zone_get_offset (tz, 0) != offset_sign * (offset_hours * 3600 + offset_minutes * 60)) |
1401 | 0 | { |
1402 | 0 | g_clear_pointer (&tz, g_time_zone_unref); |
1403 | 0 | return NULL; |
1404 | 0 | } |
1405 | | |
1406 | 0 | return tz; |
1407 | 0 | } |
1408 | | |
1409 | | static gboolean |
1410 | | parse_iso8601_time (const gchar *text, gsize length, |
1411 | | gint *hour, gint *minute, gdouble *seconds, GTimeZone **tz) |
1412 | 0 | { |
1413 | 0 | gssize tz_offset = -1; |
1414 | | |
1415 | | /* Check for timezone suffix */ |
1416 | 0 | *tz = parse_iso8601_timezone (text, length, &tz_offset); |
1417 | 0 | if (tz_offset >= 0) |
1418 | 0 | length = tz_offset; |
1419 | | |
1420 | | /* hh:mm:ss(.sss) */ |
1421 | 0 | if (length >= 8 && text[2] == ':' && text[5] == ':') |
1422 | 0 | { |
1423 | 0 | return get_iso8601_int (text, 2, hour) && |
1424 | 0 | get_iso8601_int (text + 3, 2, minute) && |
1425 | 0 | get_iso8601_seconds (text + 6, length - 6, seconds); |
1426 | 0 | } |
1427 | | /* hhmmss(.sss) */ |
1428 | 0 | else if (length >= 6) |
1429 | 0 | { |
1430 | 0 | return get_iso8601_int (text, 2, hour) && |
1431 | 0 | get_iso8601_int (text + 2, 2, minute) && |
1432 | 0 | get_iso8601_seconds (text + 4, length - 4, seconds); |
1433 | 0 | } |
1434 | 0 | else |
1435 | 0 | return FALSE; |
1436 | 0 | } |
1437 | | |
1438 | | /** |
1439 | | * g_date_time_new_from_iso8601: (constructor) |
1440 | | * @text: an ISO 8601 formatted time string. |
1441 | | * @default_tz: (nullable): a #GTimeZone to use if the text doesn't contain a |
1442 | | * timezone, or %NULL. |
1443 | | * |
1444 | | * Creates a #GDateTime corresponding to the given |
1445 | | * [ISO 8601 formatted string](https://en.wikipedia.org/wiki/ISO_8601) |
1446 | | * @text. ISO 8601 strings of the form <date><sep><time><tz> are supported, with |
1447 | | * some extensions from [RFC 3339](https://tools.ietf.org/html/rfc3339) as |
1448 | | * mentioned below. |
1449 | | * |
1450 | | * Note that as #GDateTime "is oblivious to leap seconds", leap seconds information |
1451 | | * in an ISO-8601 string will be ignored, so a `23:59:60` time would be parsed as |
1452 | | * `23:59:59`. |
1453 | | * |
1454 | | * <sep> is the separator and can be either 'T', 't' or ' '. The latter two |
1455 | | * separators are an extension from |
1456 | | * [RFC 3339](https://tools.ietf.org/html/rfc3339#section-5.6). |
1457 | | * |
1458 | | * <date> is in the form: |
1459 | | * |
1460 | | * - `YYYY-MM-DD` - Year/month/day, e.g. 2016-08-24. |
1461 | | * - `YYYYMMDD` - Same as above without dividers. |
1462 | | * - `YYYY-DDD` - Ordinal day where DDD is from 001 to 366, e.g. 2016-237. |
1463 | | * - `YYYYDDD` - Same as above without dividers. |
1464 | | * - `YYYY-Www-D` - Week day where ww is from 01 to 52 and D from 1-7, |
1465 | | * e.g. 2016-W34-3. |
1466 | | * - `YYYYWwwD` - Same as above without dividers. |
1467 | | * |
1468 | | * <time> is in the form: |
1469 | | * |
1470 | | * - `hh:mm:ss(.sss)` - Hours, minutes, seconds (subseconds), e.g. 22:10:42.123. |
1471 | | * - `hhmmss(.sss)` - Same as above without dividers. |
1472 | | * |
1473 | | * <tz> is an optional timezone suffix of the form: |
1474 | | * |
1475 | | * - `Z` - UTC. |
1476 | | * - `+hh:mm` or `-hh:mm` - Offset from UTC in hours and minutes, e.g. +12:00. |
1477 | | * - `+hh` or `-hh` - Offset from UTC in hours, e.g. +12. |
1478 | | * |
1479 | | * If the timezone is not provided in @text it must be provided in @default_tz |
1480 | | * (this field is otherwise ignored). |
1481 | | * |
1482 | | * This call can fail (returning %NULL) if @text is not a valid ISO 8601 |
1483 | | * formatted string. |
1484 | | * |
1485 | | * You should release the return value by calling g_date_time_unref() |
1486 | | * when you are done with it. |
1487 | | * |
1488 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
1489 | | * |
1490 | | * Since: 2.56 |
1491 | | */ |
1492 | | GDateTime * |
1493 | | g_date_time_new_from_iso8601 (const gchar *text, GTimeZone *default_tz) |
1494 | 0 | { |
1495 | 0 | gint length, date_length = -1; |
1496 | 0 | gint hour = 0, minute = 0; |
1497 | 0 | gdouble seconds = 0.0; |
1498 | 0 | GTimeZone *tz = NULL; |
1499 | 0 | GDateTime *datetime = NULL; |
1500 | |
|
1501 | 0 | g_return_val_if_fail (text != NULL, NULL); |
1502 | | |
1503 | | /* Count length of string and find date / time separator ('T', 't', or ' ') */ |
1504 | 0 | for (length = 0; text[length] != '\0'; length++) |
1505 | 0 | { |
1506 | 0 | if (date_length < 0 && (text[length] == 'T' || text[length] == 't' || text[length] == ' ')) |
1507 | 0 | date_length = length; |
1508 | 0 | } |
1509 | |
|
1510 | 0 | if (date_length < 0) |
1511 | 0 | return NULL; |
1512 | | |
1513 | 0 | if (!parse_iso8601_time (text + date_length + 1, length - (date_length + 1), |
1514 | 0 | &hour, &minute, &seconds, &tz)) |
1515 | 0 | goto out; |
1516 | 0 | if (tz == NULL && default_tz == NULL) |
1517 | 0 | return NULL; |
1518 | | |
1519 | 0 | datetime = parse_iso8601_date (text, date_length, hour, minute, seconds, tz ? tz : default_tz); |
1520 | |
|
1521 | 0 | out: |
1522 | 0 | if (tz != NULL) |
1523 | 0 | g_time_zone_unref (tz); |
1524 | 0 | return datetime; |
1525 | 0 | } |
1526 | | |
1527 | | /* full new functions {{{1 */ |
1528 | | |
1529 | | /** |
1530 | | * g_date_time_new: (constructor) |
1531 | | * @tz: a #GTimeZone |
1532 | | * @year: the year component of the date |
1533 | | * @month: the month component of the date |
1534 | | * @day: the day component of the date |
1535 | | * @hour: the hour component of the date |
1536 | | * @minute: the minute component of the date |
1537 | | * @seconds: the number of seconds past the minute |
1538 | | * |
1539 | | * Creates a new #GDateTime corresponding to the given date and time in |
1540 | | * the time zone @tz. |
1541 | | * |
1542 | | * The @year must be between 1 and 9999, @month between 1 and 12 and @day |
1543 | | * between 1 and 28, 29, 30 or 31 depending on the month and the year. |
1544 | | * |
1545 | | * @hour must be between 0 and 23 and @minute must be between 0 and 59. |
1546 | | * |
1547 | | * @seconds must be at least 0.0 and must be strictly less than 60.0. |
1548 | | * It will be rounded down to the nearest microsecond. |
1549 | | * |
1550 | | * If the given time is not representable in the given time zone (for |
1551 | | * example, 02:30 on March 14th 2010 in Toronto, due to daylight savings |
1552 | | * time) then the time will be rounded up to the nearest existing time |
1553 | | * (in this case, 03:00). If this matters to you then you should verify |
1554 | | * the return value for containing the same as the numbers you gave. |
1555 | | * |
1556 | | * In the case that the given time is ambiguous in the given time zone |
1557 | | * (for example, 01:30 on November 7th 2010 in Toronto, due to daylight |
1558 | | * savings time) then the time falling within standard (ie: |
1559 | | * non-daylight) time is taken. |
1560 | | * |
1561 | | * It not considered a programmer error for the values to this function |
1562 | | * to be out of range, but in the case that they are, the function will |
1563 | | * return %NULL. |
1564 | | * |
1565 | | * You should release the return value by calling g_date_time_unref() |
1566 | | * when you are done with it. |
1567 | | * |
1568 | | * Returns: (transfer full) (nullable): a new #GDateTime, or %NULL |
1569 | | * |
1570 | | * Since: 2.26 |
1571 | | **/ |
1572 | | GDateTime * |
1573 | | g_date_time_new (GTimeZone *tz, |
1574 | | gint year, |
1575 | | gint month, |
1576 | | gint day, |
1577 | | gint hour, |
1578 | | gint minute, |
1579 | | gdouble seconds) |
1580 | 14.0k | { |
1581 | 14.0k | GDateTime *datetime; |
1582 | 14.0k | gint64 full_time; |
1583 | | /* keep these variables as volatile. We do not want them ending up in |
1584 | | * registers - them doing so may cause us to hit precision problems on i386. |
1585 | | * See: https://bugzilla.gnome.org/show_bug.cgi?id=792410 */ |
1586 | 14.0k | volatile gint64 usec; |
1587 | 14.0k | volatile gdouble usecd; |
1588 | | |
1589 | 14.0k | g_return_val_if_fail (tz != NULL, NULL); |
1590 | | |
1591 | 14.0k | if (year < 1 || year > 9999 || |
1592 | 14.0k | month < 1 || month > 12 || |
1593 | 12.2k | day < 1 || day > days_in_months[GREGORIAN_LEAP (year)][month] || |
1594 | 9.44k | hour < 0 || hour > 23 || |
1595 | 8.67k | minute < 0 || minute > 59 || |
1596 | 14.0k | isnan (seconds) || |
1597 | 8.44k | seconds < 0.0 || seconds >= 60.0) |
1598 | 5.94k | return NULL; |
1599 | | |
1600 | 8.13k | datetime = g_date_time_alloc (tz); |
1601 | 8.13k | datetime->days = ymd_to_days (year, month, day); |
1602 | 8.13k | datetime->usec = (hour * USEC_PER_HOUR) |
1603 | 8.13k | + (minute * USEC_PER_MINUTE) |
1604 | 8.13k | + (gint64) (seconds * USEC_PER_SECOND); |
1605 | | |
1606 | 8.13k | full_time = SEC_PER_DAY * |
1607 | 8.13k | (ymd_to_days (year, month, day) - UNIX_EPOCH_START) + |
1608 | 8.13k | SECS_PER_HOUR * hour + |
1609 | 8.13k | SECS_PER_MINUTE * minute + |
1610 | 8.13k | (int) seconds; |
1611 | | |
1612 | 8.13k | datetime->interval = g_time_zone_adjust_time (datetime->tz, |
1613 | 8.13k | G_TIME_TYPE_STANDARD, |
1614 | 8.13k | &full_time); |
1615 | | |
1616 | | /* This is the correct way to convert a scaled FP value to integer. |
1617 | | * If this surprises you, please observe that (int)(1.000001 * 1e6) |
1618 | | * is 1000000. This is not a problem with precision, it's just how |
1619 | | * FP numbers work. |
1620 | | * See https://bugzilla.gnome.org/show_bug.cgi?id=697715. */ |
1621 | 8.13k | usec = seconds * USEC_PER_SECOND; |
1622 | 8.13k | usecd = (usec + 1) * 1e-6; |
1623 | 8.13k | if (usecd <= seconds) { |
1624 | 0 | usec++; |
1625 | 0 | } |
1626 | | |
1627 | 8.13k | full_time += UNIX_EPOCH_START * SEC_PER_DAY; |
1628 | 8.13k | datetime->days = full_time / SEC_PER_DAY; |
1629 | 8.13k | datetime->usec = (full_time % SEC_PER_DAY) * USEC_PER_SECOND; |
1630 | 8.13k | datetime->usec += usec % USEC_PER_SECOND; |
1631 | | |
1632 | 8.13k | return datetime; |
1633 | 14.0k | } |
1634 | | |
1635 | | /** |
1636 | | * g_date_time_new_local: (constructor) |
1637 | | * @year: the year component of the date |
1638 | | * @month: the month component of the date |
1639 | | * @day: the day component of the date |
1640 | | * @hour: the hour component of the date |
1641 | | * @minute: the minute component of the date |
1642 | | * @seconds: the number of seconds past the minute |
1643 | | * |
1644 | | * Creates a new #GDateTime corresponding to the given date and time in |
1645 | | * the local time zone. |
1646 | | * |
1647 | | * This call is equivalent to calling g_date_time_new() with the time |
1648 | | * zone returned by g_time_zone_new_local(). |
1649 | | * |
1650 | | * Returns: (transfer full) (nullable): a #GDateTime, or %NULL |
1651 | | * |
1652 | | * Since: 2.26 |
1653 | | **/ |
1654 | | GDateTime * |
1655 | | g_date_time_new_local (gint year, |
1656 | | gint month, |
1657 | | gint day, |
1658 | | gint hour, |
1659 | | gint minute, |
1660 | | gdouble seconds) |
1661 | 0 | { |
1662 | 0 | GDateTime *datetime; |
1663 | 0 | GTimeZone *local; |
1664 | |
|
1665 | 0 | local = g_time_zone_new_local (); |
1666 | 0 | datetime = g_date_time_new (local, year, month, day, hour, minute, seconds); |
1667 | 0 | g_time_zone_unref (local); |
1668 | |
|
1669 | 0 | return datetime; |
1670 | 0 | } |
1671 | | |
1672 | | /** |
1673 | | * g_date_time_new_utc: (constructor) |
1674 | | * @year: the year component of the date |
1675 | | * @month: the month component of the date |
1676 | | * @day: the day component of the date |
1677 | | * @hour: the hour component of the date |
1678 | | * @minute: the minute component of the date |
1679 | | * @seconds: the number of seconds past the minute |
1680 | | * |
1681 | | * Creates a new #GDateTime corresponding to the given date and time in |
1682 | | * UTC. |
1683 | | * |
1684 | | * This call is equivalent to calling g_date_time_new() with the time |
1685 | | * zone returned by g_time_zone_new_utc(). |
1686 | | * |
1687 | | * Returns: (transfer full) (nullable): a #GDateTime, or %NULL |
1688 | | * |
1689 | | * Since: 2.26 |
1690 | | **/ |
1691 | | GDateTime * |
1692 | | g_date_time_new_utc (gint year, |
1693 | | gint month, |
1694 | | gint day, |
1695 | | gint hour, |
1696 | | gint minute, |
1697 | | gdouble seconds) |
1698 | 0 | { |
1699 | 0 | GDateTime *datetime; |
1700 | 0 | GTimeZone *utc; |
1701 | |
|
1702 | 0 | utc = g_time_zone_new_utc (); |
1703 | 0 | datetime = g_date_time_new (utc, year, month, day, hour, minute, seconds); |
1704 | 0 | g_time_zone_unref (utc); |
1705 | |
|
1706 | 0 | return datetime; |
1707 | 0 | } |
1708 | | |
1709 | | /* Adders {{{1 */ |
1710 | | |
1711 | | /** |
1712 | | * g_date_time_add: |
1713 | | * @datetime: a #GDateTime |
1714 | | * @timespan: a #GTimeSpan |
1715 | | * |
1716 | | * Creates a copy of @datetime and adds the specified timespan to the copy. |
1717 | | * |
1718 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1719 | | * should be freed with g_date_time_unref(), or %NULL |
1720 | | * |
1721 | | * Since: 2.26 |
1722 | | */ |
1723 | | GDateTime* |
1724 | | g_date_time_add (GDateTime *datetime, |
1725 | | GTimeSpan timespan) |
1726 | 0 | { |
1727 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
1728 | | |
1729 | 0 | return g_date_time_from_instant (datetime->tz, timespan + |
1730 | 0 | g_date_time_to_instant (datetime)); |
1731 | 0 | } |
1732 | | |
1733 | | /** |
1734 | | * g_date_time_add_years: |
1735 | | * @datetime: a #GDateTime |
1736 | | * @years: the number of years |
1737 | | * |
1738 | | * Creates a copy of @datetime and adds the specified number of years to the |
1739 | | * copy. Add negative values to subtract years. |
1740 | | * |
1741 | | * As with g_date_time_add_months(), if the resulting date would be 29th |
1742 | | * February on a non-leap year, the day will be clamped to 28th February. |
1743 | | * |
1744 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1745 | | * should be freed with g_date_time_unref(), or %NULL |
1746 | | * |
1747 | | * Since: 2.26 |
1748 | | */ |
1749 | | GDateTime * |
1750 | | g_date_time_add_years (GDateTime *datetime, |
1751 | | gint years) |
1752 | 0 | { |
1753 | 0 | gint year, month, day; |
1754 | |
|
1755 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
1756 | | |
1757 | 0 | if (years < -10000 || years > 10000) |
1758 | 0 | return NULL; |
1759 | | |
1760 | 0 | g_date_time_get_ymd (datetime, &year, &month, &day); |
1761 | 0 | year += years; |
1762 | | |
1763 | | /* only possible issue is if we've entered a year with no February 29 |
1764 | | */ |
1765 | 0 | if (month == 2 && day == 29 && !GREGORIAN_LEAP (year)) |
1766 | 0 | day = 28; |
1767 | |
|
1768 | 0 | return g_date_time_replace_days (datetime, ymd_to_days (year, month, day)); |
1769 | 0 | } |
1770 | | |
1771 | | /** |
1772 | | * g_date_time_add_months: |
1773 | | * @datetime: a #GDateTime |
1774 | | * @months: the number of months |
1775 | | * |
1776 | | * Creates a copy of @datetime and adds the specified number of months to the |
1777 | | * copy. Add negative values to subtract months. |
1778 | | * |
1779 | | * The day of the month of the resulting #GDateTime is clamped to the number |
1780 | | * of days in the updated calendar month. For example, if adding 1 month to |
1781 | | * 31st January 2018, the result would be 28th February 2018. In 2020 (a leap |
1782 | | * year), the result would be 29th February. |
1783 | | * |
1784 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1785 | | * should be freed with g_date_time_unref(), or %NULL |
1786 | | * |
1787 | | * Since: 2.26 |
1788 | | */ |
1789 | | GDateTime* |
1790 | | g_date_time_add_months (GDateTime *datetime, |
1791 | | gint months) |
1792 | 0 | { |
1793 | 0 | gint year, month, day; |
1794 | |
|
1795 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
1796 | 0 | g_date_time_get_ymd (datetime, &year, &month, &day); |
1797 | |
|
1798 | 0 | if (months < -120000 || months > 120000) |
1799 | 0 | return NULL; |
1800 | | |
1801 | 0 | year += months / 12; |
1802 | 0 | month += months % 12; |
1803 | 0 | if (month < 1) |
1804 | 0 | { |
1805 | 0 | month += 12; |
1806 | 0 | year--; |
1807 | 0 | } |
1808 | 0 | else if (month > 12) |
1809 | 0 | { |
1810 | 0 | month -= 12; |
1811 | 0 | year++; |
1812 | 0 | } |
1813 | |
|
1814 | 0 | day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]); |
1815 | |
|
1816 | 0 | return g_date_time_replace_days (datetime, ymd_to_days (year, month, day)); |
1817 | 0 | } |
1818 | | |
1819 | | /** |
1820 | | * g_date_time_add_weeks: |
1821 | | * @datetime: a #GDateTime |
1822 | | * @weeks: the number of weeks |
1823 | | * |
1824 | | * Creates a copy of @datetime and adds the specified number of weeks to the |
1825 | | * copy. Add negative values to subtract weeks. |
1826 | | * |
1827 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1828 | | * should be freed with g_date_time_unref(), or %NULL |
1829 | | * |
1830 | | * Since: 2.26 |
1831 | | */ |
1832 | | GDateTime* |
1833 | | g_date_time_add_weeks (GDateTime *datetime, |
1834 | | gint weeks) |
1835 | 0 | { |
1836 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
1837 | | |
1838 | 0 | return g_date_time_add_days (datetime, weeks * 7); |
1839 | 0 | } |
1840 | | |
1841 | | /** |
1842 | | * g_date_time_add_days: |
1843 | | * @datetime: a #GDateTime |
1844 | | * @days: the number of days |
1845 | | * |
1846 | | * Creates a copy of @datetime and adds the specified number of days to the |
1847 | | * copy. Add negative values to subtract days. |
1848 | | * |
1849 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1850 | | * should be freed with g_date_time_unref(), or %NULL |
1851 | | * |
1852 | | * Since: 2.26 |
1853 | | */ |
1854 | | GDateTime* |
1855 | | g_date_time_add_days (GDateTime *datetime, |
1856 | | gint days) |
1857 | 0 | { |
1858 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
1859 | | |
1860 | 0 | if (days < -3660000 || days > 3660000) |
1861 | 0 | return NULL; |
1862 | | |
1863 | 0 | return g_date_time_replace_days (datetime, datetime->days + days); |
1864 | 0 | } |
1865 | | |
1866 | | /** |
1867 | | * g_date_time_add_hours: |
1868 | | * @datetime: a #GDateTime |
1869 | | * @hours: the number of hours to add |
1870 | | * |
1871 | | * Creates a copy of @datetime and adds the specified number of hours. |
1872 | | * Add negative values to subtract hours. |
1873 | | * |
1874 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1875 | | * should be freed with g_date_time_unref(), or %NULL |
1876 | | * |
1877 | | * Since: 2.26 |
1878 | | */ |
1879 | | GDateTime* |
1880 | | g_date_time_add_hours (GDateTime *datetime, |
1881 | | gint hours) |
1882 | 0 | { |
1883 | 0 | return g_date_time_add (datetime, hours * USEC_PER_HOUR); |
1884 | 0 | } |
1885 | | |
1886 | | /** |
1887 | | * g_date_time_add_minutes: |
1888 | | * @datetime: a #GDateTime |
1889 | | * @minutes: the number of minutes to add |
1890 | | * |
1891 | | * Creates a copy of @datetime adding the specified number of minutes. |
1892 | | * Add negative values to subtract minutes. |
1893 | | * |
1894 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1895 | | * should be freed with g_date_time_unref(), or %NULL |
1896 | | * |
1897 | | * Since: 2.26 |
1898 | | */ |
1899 | | GDateTime* |
1900 | | g_date_time_add_minutes (GDateTime *datetime, |
1901 | | gint minutes) |
1902 | 0 | { |
1903 | 0 | return g_date_time_add (datetime, minutes * USEC_PER_MINUTE); |
1904 | 0 | } |
1905 | | |
1906 | | |
1907 | | /** |
1908 | | * g_date_time_add_seconds: |
1909 | | * @datetime: a #GDateTime |
1910 | | * @seconds: the number of seconds to add |
1911 | | * |
1912 | | * Creates a copy of @datetime and adds the specified number of seconds. |
1913 | | * Add negative values to subtract seconds. |
1914 | | * |
1915 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1916 | | * should be freed with g_date_time_unref(), or %NULL |
1917 | | * |
1918 | | * Since: 2.26 |
1919 | | */ |
1920 | | GDateTime* |
1921 | | g_date_time_add_seconds (GDateTime *datetime, |
1922 | | gdouble seconds) |
1923 | 0 | { |
1924 | 0 | return g_date_time_add (datetime, seconds * USEC_PER_SECOND); |
1925 | 0 | } |
1926 | | |
1927 | | /** |
1928 | | * g_date_time_add_full: |
1929 | | * @datetime: a #GDateTime |
1930 | | * @years: the number of years to add |
1931 | | * @months: the number of months to add |
1932 | | * @days: the number of days to add |
1933 | | * @hours: the number of hours to add |
1934 | | * @minutes: the number of minutes to add |
1935 | | * @seconds: the number of seconds to add |
1936 | | * |
1937 | | * Creates a new #GDateTime adding the specified values to the current date and |
1938 | | * time in @datetime. Add negative values to subtract. |
1939 | | * |
1940 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
1941 | | * should be freed with g_date_time_unref(), or %NULL |
1942 | | * |
1943 | | * Since: 2.26 |
1944 | | */ |
1945 | | GDateTime * |
1946 | | g_date_time_add_full (GDateTime *datetime, |
1947 | | gint years, |
1948 | | gint months, |
1949 | | gint days, |
1950 | | gint hours, |
1951 | | gint minutes, |
1952 | | gdouble seconds) |
1953 | 0 | { |
1954 | 0 | gint year, month, day; |
1955 | 0 | gint64 full_time; |
1956 | 0 | GDateTime *new; |
1957 | 0 | gint interval; |
1958 | |
|
1959 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
1960 | 0 | g_date_time_get_ymd (datetime, &year, &month, &day); |
1961 | |
|
1962 | 0 | months += years * 12; |
1963 | |
|
1964 | 0 | if (months < -120000 || months > 120000) |
1965 | 0 | return NULL; |
1966 | | |
1967 | 0 | if (days < -3660000 || days > 3660000) |
1968 | 0 | return NULL; |
1969 | | |
1970 | 0 | year += months / 12; |
1971 | 0 | month += months % 12; |
1972 | 0 | if (month < 1) |
1973 | 0 | { |
1974 | 0 | month += 12; |
1975 | 0 | year--; |
1976 | 0 | } |
1977 | 0 | else if (month > 12) |
1978 | 0 | { |
1979 | 0 | month -= 12; |
1980 | 0 | year++; |
1981 | 0 | } |
1982 | |
|
1983 | 0 | day = MIN (day, days_in_months[GREGORIAN_LEAP (year)][month]); |
1984 | | |
1985 | | /* full_time is now in unix (local) time */ |
1986 | 0 | full_time = datetime->usec / USEC_PER_SECOND + SEC_PER_DAY * |
1987 | 0 | (ymd_to_days (year, month, day) + days - UNIX_EPOCH_START); |
1988 | |
|
1989 | 0 | interval = g_time_zone_adjust_time (datetime->tz, |
1990 | 0 | g_time_zone_is_dst (datetime->tz, |
1991 | 0 | datetime->interval), |
1992 | 0 | &full_time); |
1993 | | |
1994 | | /* move to UTC unix time */ |
1995 | 0 | full_time -= g_time_zone_get_offset (datetime->tz, interval); |
1996 | | |
1997 | | /* convert back to an instant, add back fractional seconds */ |
1998 | 0 | full_time += UNIX_EPOCH_START * SEC_PER_DAY; |
1999 | 0 | full_time = full_time * USEC_PER_SECOND + |
2000 | 0 | datetime->usec % USEC_PER_SECOND; |
2001 | | |
2002 | | /* do the actual addition now */ |
2003 | 0 | full_time += (hours * USEC_PER_HOUR) + |
2004 | 0 | (minutes * USEC_PER_MINUTE) + |
2005 | 0 | (gint64) (seconds * USEC_PER_SECOND); |
2006 | | |
2007 | | /* find the new interval */ |
2008 | 0 | interval = g_time_zone_find_interval (datetime->tz, |
2009 | 0 | G_TIME_TYPE_UNIVERSAL, |
2010 | 0 | INSTANT_TO_UNIX (full_time)); |
2011 | | |
2012 | | /* convert back into local time */ |
2013 | 0 | full_time += USEC_PER_SECOND * |
2014 | 0 | g_time_zone_get_offset (datetime->tz, interval); |
2015 | | |
2016 | | /* split into days and usec of a new datetime */ |
2017 | 0 | new = g_date_time_alloc (datetime->tz); |
2018 | 0 | new->interval = interval; |
2019 | 0 | new->days = full_time / USEC_PER_DAY; |
2020 | 0 | new->usec = full_time % USEC_PER_DAY; |
2021 | | |
2022 | | /* XXX validate */ |
2023 | |
|
2024 | 0 | return new; |
2025 | 0 | } |
2026 | | |
2027 | | /* Compare, difference, hash, equal {{{1 */ |
2028 | | /** |
2029 | | * g_date_time_compare: |
2030 | | * @dt1: (type GDateTime) (not nullable): first #GDateTime to compare |
2031 | | * @dt2: (type GDateTime) (not nullable): second #GDateTime to compare |
2032 | | * |
2033 | | * A comparison function for #GDateTimes that is suitable |
2034 | | * as a #GCompareFunc. Both #GDateTimes must be non-%NULL. |
2035 | | * |
2036 | | * Returns: -1, 0 or 1 if @dt1 is less than, equal to or greater |
2037 | | * than @dt2. |
2038 | | * |
2039 | | * Since: 2.26 |
2040 | | */ |
2041 | | gint |
2042 | | g_date_time_compare (gconstpointer dt1, |
2043 | | gconstpointer dt2) |
2044 | 0 | { |
2045 | 0 | gint64 difference; |
2046 | |
|
2047 | 0 | difference = g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2); |
2048 | |
|
2049 | 0 | if (difference < 0) |
2050 | 0 | return -1; |
2051 | | |
2052 | 0 | else if (difference > 0) |
2053 | 0 | return 1; |
2054 | | |
2055 | 0 | else |
2056 | 0 | return 0; |
2057 | 0 | } |
2058 | | |
2059 | | /** |
2060 | | * g_date_time_difference: |
2061 | | * @end: a #GDateTime |
2062 | | * @begin: a #GDateTime |
2063 | | * |
2064 | | * Calculates the difference in time between @end and @begin. The |
2065 | | * #GTimeSpan that is returned is effectively @end - @begin (ie: |
2066 | | * positive if the first parameter is larger). |
2067 | | * |
2068 | | * Returns: the difference between the two #GDateTime, as a time |
2069 | | * span expressed in microseconds. |
2070 | | * |
2071 | | * Since: 2.26 |
2072 | | */ |
2073 | | GTimeSpan |
2074 | | g_date_time_difference (GDateTime *end, |
2075 | | GDateTime *begin) |
2076 | 0 | { |
2077 | 0 | g_return_val_if_fail (begin != NULL, 0); |
2078 | 0 | g_return_val_if_fail (end != NULL, 0); |
2079 | | |
2080 | 0 | return g_date_time_to_instant (end) - |
2081 | 0 | g_date_time_to_instant (begin); |
2082 | 0 | } |
2083 | | |
2084 | | /** |
2085 | | * g_date_time_hash: |
2086 | | * @datetime: (type GDateTime) (not nullable): a #GDateTime |
2087 | | * |
2088 | | * Hashes @datetime into a #guint, suitable for use within #GHashTable. |
2089 | | * |
2090 | | * Returns: a #guint containing the hash |
2091 | | * |
2092 | | * Since: 2.26 |
2093 | | */ |
2094 | | guint |
2095 | | g_date_time_hash (gconstpointer datetime) |
2096 | 0 | { |
2097 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2098 | | |
2099 | 0 | return g_date_time_to_instant ((GDateTime *) datetime); |
2100 | 0 | } |
2101 | | |
2102 | | /** |
2103 | | * g_date_time_equal: |
2104 | | * @dt1: (type GDateTime) (not nullable): a #GDateTime |
2105 | | * @dt2: (type GDateTime) (not nullable): a #GDateTime |
2106 | | * |
2107 | | * Checks to see if @dt1 and @dt2 are equal. |
2108 | | * |
2109 | | * Equal here means that they represent the same moment after converting |
2110 | | * them to the same time zone. |
2111 | | * |
2112 | | * Returns: %TRUE if @dt1 and @dt2 are equal |
2113 | | * |
2114 | | * Since: 2.26 |
2115 | | */ |
2116 | | gboolean |
2117 | | g_date_time_equal (gconstpointer dt1, |
2118 | | gconstpointer dt2) |
2119 | 0 | { |
2120 | 0 | return g_date_time_difference ((GDateTime *) dt1, (GDateTime *) dt2) == 0; |
2121 | 0 | } |
2122 | | |
2123 | | /* Year, Month, Day Getters {{{1 */ |
2124 | | /** |
2125 | | * g_date_time_get_ymd: |
2126 | | * @datetime: a #GDateTime. |
2127 | | * @year: (out) (optional): the return location for the gregorian year, or %NULL. |
2128 | | * @month: (out) (optional): the return location for the month of the year, or %NULL. |
2129 | | * @day: (out) (optional): the return location for the day of the month, or %NULL. |
2130 | | * |
2131 | | * Retrieves the Gregorian day, month, and year of a given #GDateTime. |
2132 | | * |
2133 | | * Since: 2.26 |
2134 | | **/ |
2135 | | void |
2136 | | g_date_time_get_ymd (GDateTime *datetime, |
2137 | | gint *year, |
2138 | | gint *month, |
2139 | | gint *day) |
2140 | 0 | { |
2141 | 0 | gint the_year; |
2142 | 0 | gint the_month; |
2143 | 0 | gint the_day; |
2144 | 0 | gint remaining_days; |
2145 | 0 | gint y100_cycles; |
2146 | 0 | gint y4_cycles; |
2147 | 0 | gint y1_cycles; |
2148 | 0 | gint preceding; |
2149 | 0 | gboolean leap; |
2150 | |
|
2151 | 0 | g_return_if_fail (datetime != NULL); |
2152 | | |
2153 | 0 | remaining_days = datetime->days; |
2154 | | |
2155 | | /* |
2156 | | * We need to convert an offset in days to its year/month/day representation. |
2157 | | * Leap years makes this a little trickier than it should be, so we use |
2158 | | * 400, 100 and 4 years cycles here to get to the correct year. |
2159 | | */ |
2160 | | |
2161 | | /* Our days offset starts sets 0001-01-01 as day 1, if it was day 0 our |
2162 | | * math would be simpler, so let's do it */ |
2163 | 0 | remaining_days--; |
2164 | |
|
2165 | 0 | the_year = (remaining_days / DAYS_IN_400YEARS) * 400 + 1; |
2166 | 0 | remaining_days = remaining_days % DAYS_IN_400YEARS; |
2167 | |
|
2168 | 0 | y100_cycles = remaining_days / DAYS_IN_100YEARS; |
2169 | 0 | remaining_days = remaining_days % DAYS_IN_100YEARS; |
2170 | 0 | the_year += y100_cycles * 100; |
2171 | |
|
2172 | 0 | y4_cycles = remaining_days / DAYS_IN_4YEARS; |
2173 | 0 | remaining_days = remaining_days % DAYS_IN_4YEARS; |
2174 | 0 | the_year += y4_cycles * 4; |
2175 | |
|
2176 | 0 | y1_cycles = remaining_days / 365; |
2177 | 0 | the_year += y1_cycles; |
2178 | 0 | remaining_days = remaining_days % 365; |
2179 | |
|
2180 | 0 | if (y1_cycles == 4 || y100_cycles == 4) { |
2181 | 0 | g_assert (remaining_days == 0); |
2182 | | |
2183 | | /* special case that indicates that the date is actually one year before, |
2184 | | * in the 31th of December */ |
2185 | 0 | the_year--; |
2186 | 0 | the_month = 12; |
2187 | 0 | the_day = 31; |
2188 | 0 | goto end; |
2189 | 0 | } |
2190 | | |
2191 | | /* now get the month and the day */ |
2192 | 0 | leap = y1_cycles == 3 && (y4_cycles != 24 || y100_cycles == 3); |
2193 | |
|
2194 | 0 | g_assert (leap == GREGORIAN_LEAP(the_year)); |
2195 | | |
2196 | 0 | the_month = (remaining_days + 50) >> 5; |
2197 | 0 | preceding = (days_in_year[0][the_month - 1] + (the_month > 2 && leap)); |
2198 | 0 | if (preceding > remaining_days) |
2199 | 0 | { |
2200 | | /* estimate is too large */ |
2201 | 0 | the_month -= 1; |
2202 | 0 | preceding -= leap ? days_in_months[1][the_month] |
2203 | 0 | : days_in_months[0][the_month]; |
2204 | 0 | } |
2205 | |
|
2206 | 0 | remaining_days -= preceding; |
2207 | 0 | g_assert(0 <= remaining_days); |
2208 | | |
2209 | 0 | the_day = remaining_days + 1; |
2210 | |
|
2211 | 0 | end: |
2212 | 0 | if (year) |
2213 | 0 | *year = the_year; |
2214 | 0 | if (month) |
2215 | 0 | *month = the_month; |
2216 | 0 | if (day) |
2217 | 0 | *day = the_day; |
2218 | 0 | } |
2219 | | |
2220 | | /** |
2221 | | * g_date_time_get_year: |
2222 | | * @datetime: A #GDateTime |
2223 | | * |
2224 | | * Retrieves the year represented by @datetime in the Gregorian calendar. |
2225 | | * |
2226 | | * Returns: the year represented by @datetime |
2227 | | * |
2228 | | * Since: 2.26 |
2229 | | */ |
2230 | | gint |
2231 | | g_date_time_get_year (GDateTime *datetime) |
2232 | 0 | { |
2233 | 0 | gint year; |
2234 | |
|
2235 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2236 | | |
2237 | 0 | g_date_time_get_ymd (datetime, &year, NULL, NULL); |
2238 | |
|
2239 | 0 | return year; |
2240 | 0 | } |
2241 | | |
2242 | | /** |
2243 | | * g_date_time_get_month: |
2244 | | * @datetime: a #GDateTime |
2245 | | * |
2246 | | * Retrieves the month of the year represented by @datetime in the Gregorian |
2247 | | * calendar. |
2248 | | * |
2249 | | * Returns: the month represented by @datetime |
2250 | | * |
2251 | | * Since: 2.26 |
2252 | | */ |
2253 | | gint |
2254 | | g_date_time_get_month (GDateTime *datetime) |
2255 | 0 | { |
2256 | 0 | gint month; |
2257 | |
|
2258 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2259 | | |
2260 | 0 | g_date_time_get_ymd (datetime, NULL, &month, NULL); |
2261 | |
|
2262 | 0 | return month; |
2263 | 0 | } |
2264 | | |
2265 | | /** |
2266 | | * g_date_time_get_day_of_month: |
2267 | | * @datetime: a #GDateTime |
2268 | | * |
2269 | | * Retrieves the day of the month represented by @datetime in the gregorian |
2270 | | * calendar. |
2271 | | * |
2272 | | * Returns: the day of the month |
2273 | | * |
2274 | | * Since: 2.26 |
2275 | | */ |
2276 | | gint |
2277 | | g_date_time_get_day_of_month (GDateTime *datetime) |
2278 | 0 | { |
2279 | 0 | gint day_of_year, |
2280 | 0 | i; |
2281 | 0 | const guint16 *days; |
2282 | 0 | guint16 last = 0; |
2283 | |
|
2284 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2285 | | |
2286 | 0 | days = days_in_year[GREGORIAN_LEAP (g_date_time_get_year (datetime)) ? 1 : 0]; |
2287 | 0 | g_date_time_get_week_number (datetime, NULL, NULL, &day_of_year); |
2288 | |
|
2289 | 0 | for (i = 1; i <= 12; i++) |
2290 | 0 | { |
2291 | 0 | if (days [i] >= day_of_year) |
2292 | 0 | return day_of_year - last; |
2293 | 0 | last = days [i]; |
2294 | 0 | } |
2295 | | |
2296 | 0 | g_warn_if_reached (); |
2297 | 0 | return 0; |
2298 | 0 | } |
2299 | | |
2300 | | /* Week of year / day of week getters {{{1 */ |
2301 | | /** |
2302 | | * g_date_time_get_week_numbering_year: |
2303 | | * @datetime: a #GDateTime |
2304 | | * |
2305 | | * Returns the ISO 8601 week-numbering year in which the week containing |
2306 | | * @datetime falls. |
2307 | | * |
2308 | | * This function, taken together with g_date_time_get_week_of_year() and |
2309 | | * g_date_time_get_day_of_week() can be used to determine the full ISO |
2310 | | * week date on which @datetime falls. |
2311 | | * |
2312 | | * This is usually equal to the normal Gregorian year (as returned by |
2313 | | * g_date_time_get_year()), except as detailed below: |
2314 | | * |
2315 | | * For Thursday, the week-numbering year is always equal to the usual |
2316 | | * calendar year. For other days, the number is such that every day |
2317 | | * within a complete week (Monday to Sunday) is contained within the |
2318 | | * same week-numbering year. |
2319 | | * |
2320 | | * For Monday, Tuesday and Wednesday occurring near the end of the year, |
2321 | | * this may mean that the week-numbering year is one greater than the |
2322 | | * calendar year (so that these days have the same week-numbering year |
2323 | | * as the Thursday occurring early in the next year). |
2324 | | * |
2325 | | * For Friday, Saturday and Sunday occurring near the start of the year, |
2326 | | * this may mean that the week-numbering year is one less than the |
2327 | | * calendar year (so that these days have the same week-numbering year |
2328 | | * as the Thursday occurring late in the previous year). |
2329 | | * |
2330 | | * An equivalent description is that the week-numbering year is equal to |
2331 | | * the calendar year containing the majority of the days in the current |
2332 | | * week (Monday to Sunday). |
2333 | | * |
2334 | | * Note that January 1 0001 in the proleptic Gregorian calendar is a |
2335 | | * Monday, so this function never returns 0. |
2336 | | * |
2337 | | * Returns: the ISO 8601 week-numbering year for @datetime |
2338 | | * |
2339 | | * Since: 2.26 |
2340 | | **/ |
2341 | | gint |
2342 | | g_date_time_get_week_numbering_year (GDateTime *datetime) |
2343 | 0 | { |
2344 | 0 | gint year, month, day, weekday; |
2345 | |
|
2346 | 0 | g_date_time_get_ymd (datetime, &year, &month, &day); |
2347 | 0 | weekday = g_date_time_get_day_of_week (datetime); |
2348 | | |
2349 | | /* January 1, 2, 3 might be in the previous year if they occur after |
2350 | | * Thursday. |
2351 | | * |
2352 | | * Jan 1: Friday, Saturday, Sunday => day 1: weekday 5, 6, 7 |
2353 | | * Jan 2: Saturday, Sunday => day 2: weekday 6, 7 |
2354 | | * Jan 3: Sunday => day 3: weekday 7 |
2355 | | * |
2356 | | * So we have a special case if (day - weekday) <= -4 |
2357 | | */ |
2358 | 0 | if (month == 1 && (day - weekday) <= -4) |
2359 | 0 | return year - 1; |
2360 | | |
2361 | | /* December 29, 30, 31 might be in the next year if they occur before |
2362 | | * Thursday. |
2363 | | * |
2364 | | * Dec 31: Monday, Tuesday, Wednesday => day 31: weekday 1, 2, 3 |
2365 | | * Dec 30: Monday, Tuesday => day 30: weekday 1, 2 |
2366 | | * Dec 29: Monday => day 29: weekday 1 |
2367 | | * |
2368 | | * So we have a special case if (day - weekday) >= 28 |
2369 | | */ |
2370 | 0 | else if (month == 12 && (day - weekday) >= 28) |
2371 | 0 | return year + 1; |
2372 | | |
2373 | 0 | else |
2374 | 0 | return year; |
2375 | 0 | } |
2376 | | |
2377 | | /** |
2378 | | * g_date_time_get_week_of_year: |
2379 | | * @datetime: a #GDateTime |
2380 | | * |
2381 | | * Returns the ISO 8601 week number for the week containing @datetime. |
2382 | | * The ISO 8601 week number is the same for every day of the week (from |
2383 | | * Moday through Sunday). That can produce some unusual results |
2384 | | * (described below). |
2385 | | * |
2386 | | * The first week of the year is week 1. This is the week that contains |
2387 | | * the first Thursday of the year. Equivalently, this is the first week |
2388 | | * that has more than 4 of its days falling within the calendar year. |
2389 | | * |
2390 | | * The value 0 is never returned by this function. Days contained |
2391 | | * within a year but occurring before the first ISO 8601 week of that |
2392 | | * year are considered as being contained in the last week of the |
2393 | | * previous year. Similarly, the final days of a calendar year may be |
2394 | | * considered as being part of the first ISO 8601 week of the next year |
2395 | | * if 4 or more days of that week are contained within the new year. |
2396 | | * |
2397 | | * Returns: the ISO 8601 week number for @datetime. |
2398 | | * |
2399 | | * Since: 2.26 |
2400 | | */ |
2401 | | gint |
2402 | | g_date_time_get_week_of_year (GDateTime *datetime) |
2403 | 0 | { |
2404 | 0 | gint weeknum; |
2405 | |
|
2406 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2407 | | |
2408 | 0 | g_date_time_get_week_number (datetime, &weeknum, NULL, NULL); |
2409 | |
|
2410 | 0 | return weeknum; |
2411 | 0 | } |
2412 | | |
2413 | | /** |
2414 | | * g_date_time_get_day_of_week: |
2415 | | * @datetime: a #GDateTime |
2416 | | * |
2417 | | * Retrieves the ISO 8601 day of the week on which @datetime falls (1 is |
2418 | | * Monday, 2 is Tuesday... 7 is Sunday). |
2419 | | * |
2420 | | * Returns: the day of the week |
2421 | | * |
2422 | | * Since: 2.26 |
2423 | | */ |
2424 | | gint |
2425 | | g_date_time_get_day_of_week (GDateTime *datetime) |
2426 | 0 | { |
2427 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2428 | | |
2429 | 0 | return (datetime->days - 1) % 7 + 1; |
2430 | 0 | } |
2431 | | |
2432 | | /* Day of year getter {{{1 */ |
2433 | | /** |
2434 | | * g_date_time_get_day_of_year: |
2435 | | * @datetime: a #GDateTime |
2436 | | * |
2437 | | * Retrieves the day of the year represented by @datetime in the Gregorian |
2438 | | * calendar. |
2439 | | * |
2440 | | * Returns: the day of the year |
2441 | | * |
2442 | | * Since: 2.26 |
2443 | | */ |
2444 | | gint |
2445 | | g_date_time_get_day_of_year (GDateTime *datetime) |
2446 | 0 | { |
2447 | 0 | gint doy = 0; |
2448 | |
|
2449 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2450 | | |
2451 | 0 | g_date_time_get_week_number (datetime, NULL, NULL, &doy); |
2452 | 0 | return doy; |
2453 | 0 | } |
2454 | | |
2455 | | /* Time component getters {{{1 */ |
2456 | | |
2457 | | /** |
2458 | | * g_date_time_get_hour: |
2459 | | * @datetime: a #GDateTime |
2460 | | * |
2461 | | * Retrieves the hour of the day represented by @datetime |
2462 | | * |
2463 | | * Returns: the hour of the day |
2464 | | * |
2465 | | * Since: 2.26 |
2466 | | */ |
2467 | | gint |
2468 | | g_date_time_get_hour (GDateTime *datetime) |
2469 | 0 | { |
2470 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2471 | | |
2472 | 0 | return (datetime->usec / USEC_PER_HOUR); |
2473 | 0 | } |
2474 | | |
2475 | | /** |
2476 | | * g_date_time_get_minute: |
2477 | | * @datetime: a #GDateTime |
2478 | | * |
2479 | | * Retrieves the minute of the hour represented by @datetime |
2480 | | * |
2481 | | * Returns: the minute of the hour |
2482 | | * |
2483 | | * Since: 2.26 |
2484 | | */ |
2485 | | gint |
2486 | | g_date_time_get_minute (GDateTime *datetime) |
2487 | 0 | { |
2488 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2489 | | |
2490 | 0 | return (datetime->usec % USEC_PER_HOUR) / USEC_PER_MINUTE; |
2491 | 0 | } |
2492 | | |
2493 | | /** |
2494 | | * g_date_time_get_second: |
2495 | | * @datetime: a #GDateTime |
2496 | | * |
2497 | | * Retrieves the second of the minute represented by @datetime |
2498 | | * |
2499 | | * Returns: the second represented by @datetime |
2500 | | * |
2501 | | * Since: 2.26 |
2502 | | */ |
2503 | | gint |
2504 | | g_date_time_get_second (GDateTime *datetime) |
2505 | 0 | { |
2506 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2507 | | |
2508 | 0 | return (datetime->usec % USEC_PER_MINUTE) / USEC_PER_SECOND; |
2509 | 0 | } |
2510 | | |
2511 | | /** |
2512 | | * g_date_time_get_microsecond: |
2513 | | * @datetime: a #GDateTime |
2514 | | * |
2515 | | * Retrieves the microsecond of the date represented by @datetime |
2516 | | * |
2517 | | * Returns: the microsecond of the second |
2518 | | * |
2519 | | * Since: 2.26 |
2520 | | */ |
2521 | | gint |
2522 | | g_date_time_get_microsecond (GDateTime *datetime) |
2523 | 0 | { |
2524 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2525 | | |
2526 | 0 | return (datetime->usec % USEC_PER_SECOND); |
2527 | 0 | } |
2528 | | |
2529 | | /** |
2530 | | * g_date_time_get_seconds: |
2531 | | * @datetime: a #GDateTime |
2532 | | * |
2533 | | * Retrieves the number of seconds since the start of the last minute, |
2534 | | * including the fractional part. |
2535 | | * |
2536 | | * Returns: the number of seconds |
2537 | | * |
2538 | | * Since: 2.26 |
2539 | | **/ |
2540 | | gdouble |
2541 | | g_date_time_get_seconds (GDateTime *datetime) |
2542 | 0 | { |
2543 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2544 | | |
2545 | 0 | return (datetime->usec % USEC_PER_MINUTE) / 1000000.0; |
2546 | 0 | } |
2547 | | |
2548 | | /* Exporters {{{1 */ |
2549 | | /** |
2550 | | * g_date_time_to_unix: |
2551 | | * @datetime: a #GDateTime |
2552 | | * |
2553 | | * Gives the Unix time corresponding to @datetime, rounding down to the |
2554 | | * nearest second. |
2555 | | * |
2556 | | * Unix time is the number of seconds that have elapsed since 1970-01-01 |
2557 | | * 00:00:00 UTC, regardless of the time zone associated with @datetime. |
2558 | | * |
2559 | | * Returns: the Unix time corresponding to @datetime |
2560 | | * |
2561 | | * Since: 2.26 |
2562 | | **/ |
2563 | | gint64 |
2564 | | g_date_time_to_unix (GDateTime *datetime) |
2565 | 0 | { |
2566 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2567 | | |
2568 | 0 | return INSTANT_TO_UNIX (g_date_time_to_instant (datetime)); |
2569 | 0 | } |
2570 | | |
2571 | | /** |
2572 | | * g_date_time_to_timeval: |
2573 | | * @datetime: a #GDateTime |
2574 | | * @tv: a #GTimeVal to modify |
2575 | | * |
2576 | | * Stores the instant in time that @datetime represents into @tv. |
2577 | | * |
2578 | | * The time contained in a #GTimeVal is always stored in the form of |
2579 | | * seconds elapsed since 1970-01-01 00:00:00 UTC, regardless of the time |
2580 | | * zone associated with @datetime. |
2581 | | * |
2582 | | * On systems where 'long' is 32bit (ie: all 32bit systems and all |
2583 | | * Windows systems), a #GTimeVal is incapable of storing the entire |
2584 | | * range of values that #GDateTime is capable of expressing. On those |
2585 | | * systems, this function returns %FALSE to indicate that the time is |
2586 | | * out of range. |
2587 | | * |
2588 | | * On systems where 'long' is 64bit, this function never fails. |
2589 | | * |
2590 | | * Returns: %TRUE if successful, else %FALSE |
2591 | | * |
2592 | | * Since: 2.26 |
2593 | | * Deprecated: 2.62: #GTimeVal is not year-2038-safe. Use |
2594 | | * g_date_time_to_unix() instead. |
2595 | | **/ |
2596 | | G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
2597 | | gboolean |
2598 | | g_date_time_to_timeval (GDateTime *datetime, |
2599 | | GTimeVal *tv) |
2600 | 0 | { |
2601 | 0 | g_return_val_if_fail (datetime != NULL, FALSE); |
2602 | | |
2603 | 0 | tv->tv_sec = INSTANT_TO_UNIX (g_date_time_to_instant (datetime)); |
2604 | 0 | tv->tv_usec = datetime->usec % USEC_PER_SECOND; |
2605 | |
|
2606 | 0 | return TRUE; |
2607 | 0 | } |
2608 | | G_GNUC_END_IGNORE_DEPRECATIONS |
2609 | | |
2610 | | /* Timezone queries {{{1 */ |
2611 | | /** |
2612 | | * g_date_time_get_utc_offset: |
2613 | | * @datetime: a #GDateTime |
2614 | | * |
2615 | | * Determines the offset to UTC in effect at the time and in the time |
2616 | | * zone of @datetime. |
2617 | | * |
2618 | | * The offset is the number of microseconds that you add to UTC time to |
2619 | | * arrive at local time for the time zone (ie: negative numbers for time |
2620 | | * zones west of GMT, positive numbers for east). |
2621 | | * |
2622 | | * If @datetime represents UTC time, then the offset is always zero. |
2623 | | * |
2624 | | * Returns: the number of microseconds that should be added to UTC to |
2625 | | * get the local time |
2626 | | * |
2627 | | * Since: 2.26 |
2628 | | **/ |
2629 | | GTimeSpan |
2630 | | g_date_time_get_utc_offset (GDateTime *datetime) |
2631 | 0 | { |
2632 | 0 | gint offset; |
2633 | |
|
2634 | 0 | g_return_val_if_fail (datetime != NULL, 0); |
2635 | | |
2636 | 0 | offset = g_time_zone_get_offset (datetime->tz, datetime->interval); |
2637 | |
|
2638 | 0 | return (gint64) offset * USEC_PER_SECOND; |
2639 | 0 | } |
2640 | | |
2641 | | /** |
2642 | | * g_date_time_get_timezone: |
2643 | | * @datetime: a #GDateTime |
2644 | | * |
2645 | | * Get the time zone for this @datetime. |
2646 | | * |
2647 | | * Returns: (transfer none): the time zone |
2648 | | * Since: 2.58 |
2649 | | */ |
2650 | | GTimeZone * |
2651 | | g_date_time_get_timezone (GDateTime *datetime) |
2652 | 0 | { |
2653 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
2654 | | |
2655 | 0 | g_assert (datetime->tz != NULL); |
2656 | 0 | return datetime->tz; |
2657 | 0 | } |
2658 | | |
2659 | | /** |
2660 | | * g_date_time_get_timezone_abbreviation: |
2661 | | * @datetime: a #GDateTime |
2662 | | * |
2663 | | * Determines the time zone abbreviation to be used at the time and in |
2664 | | * the time zone of @datetime. |
2665 | | * |
2666 | | * For example, in Toronto this is currently "EST" during the winter |
2667 | | * months and "EDT" during the summer months when daylight savings |
2668 | | * time is in effect. |
2669 | | * |
2670 | | * Returns: (transfer none): the time zone abbreviation. The returned |
2671 | | * string is owned by the #GDateTime and it should not be |
2672 | | * modified or freed |
2673 | | * |
2674 | | * Since: 2.26 |
2675 | | **/ |
2676 | | const gchar * |
2677 | | g_date_time_get_timezone_abbreviation (GDateTime *datetime) |
2678 | 0 | { |
2679 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
2680 | | |
2681 | 0 | return g_time_zone_get_abbreviation (datetime->tz, datetime->interval); |
2682 | 0 | } |
2683 | | |
2684 | | /** |
2685 | | * g_date_time_is_daylight_savings: |
2686 | | * @datetime: a #GDateTime |
2687 | | * |
2688 | | * Determines if daylight savings time is in effect at the time and in |
2689 | | * the time zone of @datetime. |
2690 | | * |
2691 | | * Returns: %TRUE if daylight savings time is in effect |
2692 | | * |
2693 | | * Since: 2.26 |
2694 | | **/ |
2695 | | gboolean |
2696 | | g_date_time_is_daylight_savings (GDateTime *datetime) |
2697 | 0 | { |
2698 | 0 | g_return_val_if_fail (datetime != NULL, FALSE); |
2699 | | |
2700 | 0 | return g_time_zone_is_dst (datetime->tz, datetime->interval); |
2701 | 0 | } |
2702 | | |
2703 | | /* Timezone convert {{{1 */ |
2704 | | /** |
2705 | | * g_date_time_to_timezone: |
2706 | | * @datetime: a #GDateTime |
2707 | | * @tz: the new #GTimeZone |
2708 | | * |
2709 | | * Create a new #GDateTime corresponding to the same instant in time as |
2710 | | * @datetime, but in the time zone @tz. |
2711 | | * |
2712 | | * This call can fail in the case that the time goes out of bounds. For |
2713 | | * example, converting 0001-01-01 00:00:00 UTC to a time zone west of |
2714 | | * Greenwich will fail (due to the year 0 being out of range). |
2715 | | * |
2716 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
2717 | | * should be freed with g_date_time_unref(), or %NULL |
2718 | | * |
2719 | | * Since: 2.26 |
2720 | | **/ |
2721 | | GDateTime * |
2722 | | g_date_time_to_timezone (GDateTime *datetime, |
2723 | | GTimeZone *tz) |
2724 | 0 | { |
2725 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
2726 | 0 | g_return_val_if_fail (tz != NULL, NULL); |
2727 | | |
2728 | 0 | return g_date_time_from_instant (tz, g_date_time_to_instant (datetime)); |
2729 | 0 | } |
2730 | | |
2731 | | /** |
2732 | | * g_date_time_to_local: |
2733 | | * @datetime: a #GDateTime |
2734 | | * |
2735 | | * Creates a new #GDateTime corresponding to the same instant in time as |
2736 | | * @datetime, but in the local time zone. |
2737 | | * |
2738 | | * This call is equivalent to calling g_date_time_to_timezone() with the |
2739 | | * time zone returned by g_time_zone_new_local(). |
2740 | | * |
2741 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
2742 | | * should be freed with g_date_time_unref(), or %NULL |
2743 | | * |
2744 | | * Since: 2.26 |
2745 | | **/ |
2746 | | GDateTime * |
2747 | | g_date_time_to_local (GDateTime *datetime) |
2748 | 0 | { |
2749 | 0 | GDateTime *new; |
2750 | 0 | GTimeZone *local; |
2751 | |
|
2752 | 0 | local = g_time_zone_new_local (); |
2753 | 0 | new = g_date_time_to_timezone (datetime, local); |
2754 | 0 | g_time_zone_unref (local); |
2755 | |
|
2756 | 0 | return new; |
2757 | 0 | } |
2758 | | |
2759 | | /** |
2760 | | * g_date_time_to_utc: |
2761 | | * @datetime: a #GDateTime |
2762 | | * |
2763 | | * Creates a new #GDateTime corresponding to the same instant in time as |
2764 | | * @datetime, but in UTC. |
2765 | | * |
2766 | | * This call is equivalent to calling g_date_time_to_timezone() with the |
2767 | | * time zone returned by g_time_zone_new_utc(). |
2768 | | * |
2769 | | * Returns: (transfer full) (nullable): the newly created #GDateTime which |
2770 | | * should be freed with g_date_time_unref(), or %NULL |
2771 | | * |
2772 | | * Since: 2.26 |
2773 | | **/ |
2774 | | GDateTime * |
2775 | | g_date_time_to_utc (GDateTime *datetime) |
2776 | 0 | { |
2777 | 0 | GDateTime *new; |
2778 | 0 | GTimeZone *utc; |
2779 | |
|
2780 | 0 | utc = g_time_zone_new_utc (); |
2781 | 0 | new = g_date_time_to_timezone (datetime, utc); |
2782 | 0 | g_time_zone_unref (utc); |
2783 | |
|
2784 | 0 | return new; |
2785 | 0 | } |
2786 | | |
2787 | | /* Format {{{1 */ |
2788 | | |
2789 | | static gboolean |
2790 | | format_z (GString *outstr, |
2791 | | gint offset, |
2792 | | guint colons) |
2793 | 0 | { |
2794 | 0 | gint hours; |
2795 | 0 | gint minutes; |
2796 | 0 | gint seconds; |
2797 | 0 | gchar sign = offset >= 0 ? '+' : '-'; |
2798 | |
|
2799 | 0 | offset = ABS (offset); |
2800 | 0 | hours = offset / 3600; |
2801 | 0 | minutes = offset / 60 % 60; |
2802 | 0 | seconds = offset % 60; |
2803 | |
|
2804 | 0 | switch (colons) |
2805 | 0 | { |
2806 | 0 | case 0: |
2807 | 0 | g_string_append_printf (outstr, "%c%02d%02d", |
2808 | 0 | sign, |
2809 | 0 | hours, |
2810 | 0 | minutes); |
2811 | 0 | break; |
2812 | | |
2813 | 0 | case 1: |
2814 | 0 | g_string_append_printf (outstr, "%c%02d:%02d", |
2815 | 0 | sign, |
2816 | 0 | hours, |
2817 | 0 | minutes); |
2818 | 0 | break; |
2819 | | |
2820 | 0 | case 2: |
2821 | 0 | g_string_append_printf (outstr, "%c%02d:%02d:%02d", |
2822 | 0 | sign, |
2823 | 0 | hours, |
2824 | 0 | minutes, |
2825 | 0 | seconds); |
2826 | 0 | break; |
2827 | | |
2828 | 0 | case 3: |
2829 | 0 | g_string_append_printf (outstr, "%c%02d", sign, hours); |
2830 | |
|
2831 | 0 | if (minutes != 0 || seconds != 0) |
2832 | 0 | { |
2833 | 0 | g_string_append_printf (outstr, ":%02d", minutes); |
2834 | |
|
2835 | 0 | if (seconds != 0) |
2836 | 0 | g_string_append_printf (outstr, ":%02d", seconds); |
2837 | 0 | } |
2838 | 0 | break; |
2839 | | |
2840 | 0 | default: |
2841 | 0 | return FALSE; |
2842 | 0 | } |
2843 | | |
2844 | 0 | return TRUE; |
2845 | 0 | } |
2846 | | |
2847 | | #ifdef HAVE_LANGINFO_OUTDIGIT |
2848 | | /* Initializes the array with UTF-8 encoded alternate digits suitable for use |
2849 | | * in current locale. Returns NULL when current locale does not use alternate |
2850 | | * digits or there was an error converting them to UTF-8. |
2851 | | */ |
2852 | | static const gchar * const * |
2853 | | initialize_alt_digits (void) |
2854 | 0 | { |
2855 | 0 | guint i; |
2856 | 0 | gsize digit_len; |
2857 | 0 | gchar *digit; |
2858 | 0 | const gchar *locale_digit; |
2859 | 0 | #define N_DIGITS 10 |
2860 | 0 | #define MAX_UTF8_ENCODING_LEN 4 |
2861 | 0 | static gchar buffer[N_DIGITS * (MAX_UTF8_ENCODING_LEN + 1 /* null separator */)]; |
2862 | 0 | #undef N_DIGITS |
2863 | 0 | #undef MAX_UTF8_ENCODING_LEN |
2864 | 0 | gchar *buffer_end = buffer; |
2865 | 0 | static const gchar *alt_digits[10]; |
2866 | |
|
2867 | 0 | for (i = 0; i != 10; ++i) |
2868 | 0 | { |
2869 | 0 | locale_digit = nl_langinfo (_NL_CTYPE_OUTDIGIT0_MB + i); |
2870 | |
|
2871 | 0 | if (g_strcmp0 (locale_digit, "") == 0) |
2872 | 0 | return NULL; |
2873 | | |
2874 | 0 | digit = g_locale_to_utf8 (locale_digit, -1, NULL, &digit_len, NULL); |
2875 | 0 | if (digit == NULL) |
2876 | 0 | return NULL; |
2877 | | |
2878 | 0 | g_assert (digit_len < (gsize) (buffer + sizeof (buffer) - buffer_end)); |
2879 | | |
2880 | 0 | alt_digits[i] = buffer_end; |
2881 | 0 | buffer_end = g_stpcpy (buffer_end, digit); |
2882 | | /* skip trailing null byte */ |
2883 | 0 | buffer_end += 1; |
2884 | |
|
2885 | 0 | g_free (digit); |
2886 | 0 | } |
2887 | | |
2888 | 0 | return alt_digits; |
2889 | 0 | } |
2890 | | #endif /* HAVE_LANGINFO_OUTDIGIT */ |
2891 | | |
2892 | | static void |
2893 | | format_number (GString *str, |
2894 | | gboolean use_alt_digits, |
2895 | | const gchar *pad, |
2896 | | gint width, |
2897 | | guint32 number) |
2898 | 0 | { |
2899 | 0 | const gchar *ascii_digits[10] = { |
2900 | 0 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" |
2901 | 0 | }; |
2902 | 0 | const gchar * const *digits = ascii_digits; |
2903 | 0 | const gchar *tmp[10]; |
2904 | 0 | gint i = 0; |
2905 | |
|
2906 | 0 | g_return_if_fail (width <= 10); |
2907 | | |
2908 | 0 | #ifdef HAVE_LANGINFO_OUTDIGIT |
2909 | 0 | if (use_alt_digits) |
2910 | 0 | { |
2911 | 0 | static const gchar * const *alt_digits = NULL; |
2912 | 0 | static gsize initialised; |
2913 | |
|
2914 | 0 | if G_UNLIKELY (g_once_init_enter (&initialised)) |
2915 | 0 | { |
2916 | 0 | alt_digits = initialize_alt_digits (); |
2917 | |
|
2918 | 0 | if (alt_digits == NULL) |
2919 | 0 | alt_digits = ascii_digits; |
2920 | |
|
2921 | 0 | g_once_init_leave (&initialised, TRUE); |
2922 | 0 | } |
2923 | |
|
2924 | 0 | digits = alt_digits; |
2925 | 0 | } |
2926 | 0 | #endif /* HAVE_LANGINFO_OUTDIGIT */ |
2927 | |
|
2928 | 0 | do |
2929 | 0 | { |
2930 | 0 | tmp[i++] = digits[number % 10]; |
2931 | 0 | number /= 10; |
2932 | 0 | } |
2933 | 0 | while (number); |
2934 | |
|
2935 | 0 | while (pad && i < width) |
2936 | 0 | tmp[i++] = *pad == '0' ? digits[0] : pad; |
2937 | | |
2938 | | /* should really be impossible */ |
2939 | 0 | g_assert (i <= 10); |
2940 | | |
2941 | 0 | while (i) |
2942 | 0 | g_string_append (str, tmp[--i]); |
2943 | 0 | } |
2944 | | |
2945 | | static gboolean |
2946 | | format_ampm (GDateTime *datetime, |
2947 | | GString *outstr, |
2948 | | gboolean locale_is_utf8, |
2949 | | gboolean uppercase) |
2950 | 0 | { |
2951 | 0 | const gchar *ampm; |
2952 | 0 | gchar *tmp = NULL, *ampm_dup; |
2953 | |
|
2954 | 0 | ampm = GET_AMPM (datetime); |
2955 | |
|
2956 | 0 | if (!ampm || ampm[0] == '\0') |
2957 | 0 | ampm = get_fallback_ampm (g_date_time_get_hour (datetime)); |
2958 | |
|
2959 | 0 | if (!locale_is_utf8 && GET_AMPM_IS_LOCALE) |
2960 | 0 | { |
2961 | | /* This assumes that locale encoding can't have embedded NULs */ |
2962 | 0 | ampm = tmp = g_locale_to_utf8 (ampm, -1, NULL, NULL, NULL); |
2963 | 0 | if (tmp == NULL) |
2964 | 0 | return FALSE; |
2965 | 0 | } |
2966 | 0 | if (uppercase) |
2967 | 0 | ampm_dup = g_utf8_strup (ampm, -1); |
2968 | 0 | else |
2969 | 0 | ampm_dup = g_utf8_strdown (ampm, -1); |
2970 | 0 | g_free (tmp); |
2971 | |
|
2972 | 0 | g_string_append (outstr, ampm_dup); |
2973 | 0 | g_free (ampm_dup); |
2974 | |
|
2975 | 0 | return TRUE; |
2976 | 0 | } |
2977 | | |
2978 | | static gboolean g_date_time_format_utf8 (GDateTime *datetime, |
2979 | | const gchar *format, |
2980 | | GString *outstr, |
2981 | | gboolean locale_is_utf8); |
2982 | | |
2983 | | /* g_date_time_format() subroutine that takes a locale-encoded format |
2984 | | * string and produces a UTF-8 encoded date/time string. |
2985 | | */ |
2986 | | static gboolean |
2987 | | g_date_time_format_locale (GDateTime *datetime, |
2988 | | const gchar *locale_format, |
2989 | | GString *outstr, |
2990 | | gboolean locale_is_utf8) |
2991 | 0 | { |
2992 | 0 | gchar *utf8_format; |
2993 | 0 | gboolean success; |
2994 | |
|
2995 | 0 | if (locale_is_utf8) |
2996 | 0 | return g_date_time_format_utf8 (datetime, locale_format, outstr, locale_is_utf8); |
2997 | | |
2998 | 0 | utf8_format = g_locale_to_utf8 (locale_format, -1, NULL, NULL, NULL); |
2999 | 0 | if (utf8_format == NULL) |
3000 | 0 | return FALSE; |
3001 | | |
3002 | 0 | success = g_date_time_format_utf8 (datetime, utf8_format, outstr, |
3003 | 0 | locale_is_utf8); |
3004 | 0 | g_free (utf8_format); |
3005 | 0 | return success; |
3006 | 0 | } |
3007 | | |
3008 | | static inline gboolean |
3009 | | string_append (GString *string, |
3010 | | const gchar *s, |
3011 | | gboolean s_is_utf8) |
3012 | 0 | { |
3013 | 0 | gchar *utf8; |
3014 | 0 | gsize utf8_len; |
3015 | |
|
3016 | 0 | if (s_is_utf8) |
3017 | 0 | { |
3018 | 0 | g_string_append (string, s); |
3019 | 0 | } |
3020 | 0 | else |
3021 | 0 | { |
3022 | 0 | utf8 = g_locale_to_utf8 (s, -1, NULL, &utf8_len, NULL); |
3023 | 0 | if (utf8 == NULL) |
3024 | 0 | return FALSE; |
3025 | 0 | g_string_append_len (string, utf8, utf8_len); |
3026 | 0 | g_free (utf8); |
3027 | 0 | } |
3028 | | |
3029 | 0 | return TRUE; |
3030 | 0 | } |
3031 | | |
3032 | | /* g_date_time_format() subroutine that takes a UTF-8 encoded format |
3033 | | * string and produces a UTF-8 encoded date/time string. |
3034 | | */ |
3035 | | static gboolean |
3036 | | g_date_time_format_utf8 (GDateTime *datetime, |
3037 | | const gchar *utf8_format, |
3038 | | GString *outstr, |
3039 | | gboolean locale_is_utf8) |
3040 | 0 | { |
3041 | 0 | guint len; |
3042 | 0 | guint colons; |
3043 | 0 | gunichar c; |
3044 | 0 | gboolean alt_digits = FALSE; |
3045 | 0 | gboolean pad_set = FALSE; |
3046 | 0 | gboolean name_is_utf8; |
3047 | 0 | const gchar *pad = ""; |
3048 | 0 | const gchar *name; |
3049 | 0 | const gchar *tz; |
3050 | |
|
3051 | 0 | while (*utf8_format) |
3052 | 0 | { |
3053 | 0 | len = strcspn (utf8_format, "%"); |
3054 | 0 | if (len) |
3055 | 0 | g_string_append_len (outstr, utf8_format, len); |
3056 | |
|
3057 | 0 | utf8_format += len; |
3058 | 0 | if (!*utf8_format) |
3059 | 0 | break; |
3060 | | |
3061 | 0 | g_assert (*utf8_format == '%'); |
3062 | 0 | utf8_format++; |
3063 | 0 | if (!*utf8_format) |
3064 | 0 | break; |
3065 | | |
3066 | 0 | colons = 0; |
3067 | 0 | alt_digits = FALSE; |
3068 | 0 | pad_set = FALSE; |
3069 | |
|
3070 | 0 | next_mod: |
3071 | 0 | c = g_utf8_get_char (utf8_format); |
3072 | 0 | utf8_format = g_utf8_next_char (utf8_format); |
3073 | 0 | switch (c) |
3074 | 0 | { |
3075 | 0 | case 'a': |
3076 | 0 | name = WEEKDAY_ABBR (datetime); |
3077 | 0 | if (g_strcmp0 (name, "") == 0) |
3078 | 0 | return FALSE; |
3079 | | |
3080 | 0 | name_is_utf8 = locale_is_utf8 || !WEEKDAY_ABBR_IS_LOCALE; |
3081 | |
|
3082 | 0 | if (!string_append (outstr, name, name_is_utf8)) |
3083 | 0 | return FALSE; |
3084 | | |
3085 | 0 | break; |
3086 | 0 | case 'A': |
3087 | 0 | name = WEEKDAY_FULL (datetime); |
3088 | 0 | if (g_strcmp0 (name, "") == 0) |
3089 | 0 | return FALSE; |
3090 | | |
3091 | 0 | name_is_utf8 = locale_is_utf8 || !WEEKDAY_FULL_IS_LOCALE; |
3092 | |
|
3093 | 0 | if (!string_append (outstr, name, name_is_utf8)) |
3094 | 0 | return FALSE; |
3095 | | |
3096 | 0 | break; |
3097 | 0 | case 'b': |
3098 | 0 | name = alt_digits ? MONTH_ABBR_STANDALONE (datetime) |
3099 | 0 | : MONTH_ABBR_WITH_DAY (datetime); |
3100 | 0 | if (g_strcmp0 (name, "") == 0) |
3101 | 0 | return FALSE; |
3102 | | |
3103 | 0 | name_is_utf8 = locale_is_utf8 || |
3104 | 0 | ((alt_digits && !MONTH_ABBR_STANDALONE_IS_LOCALE) || |
3105 | 0 | (!alt_digits && !MONTH_ABBR_WITH_DAY_IS_LOCALE)); |
3106 | |
|
3107 | 0 | if (!string_append (outstr, name, name_is_utf8)) |
3108 | 0 | return FALSE; |
3109 | | |
3110 | 0 | break; |
3111 | 0 | case 'B': |
3112 | 0 | name = alt_digits ? MONTH_FULL_STANDALONE (datetime) |
3113 | 0 | : MONTH_FULL_WITH_DAY (datetime); |
3114 | 0 | if (g_strcmp0 (name, "") == 0) |
3115 | 0 | return FALSE; |
3116 | | |
3117 | 0 | name_is_utf8 = locale_is_utf8 || |
3118 | 0 | ((alt_digits && !MONTH_FULL_STANDALONE_IS_LOCALE) || |
3119 | 0 | (!alt_digits && !MONTH_FULL_WITH_DAY_IS_LOCALE)); |
3120 | |
|
3121 | 0 | if (!string_append (outstr, name, name_is_utf8)) |
3122 | 0 | return FALSE; |
3123 | | |
3124 | 0 | break; |
3125 | 0 | case 'c': |
3126 | 0 | { |
3127 | 0 | if (g_strcmp0 (PREFERRED_DATE_TIME_FMT, "") == 0) |
3128 | 0 | return FALSE; |
3129 | 0 | if (!g_date_time_format_locale (datetime, PREFERRED_DATE_TIME_FMT, |
3130 | 0 | outstr, locale_is_utf8)) |
3131 | 0 | return FALSE; |
3132 | 0 | } |
3133 | 0 | break; |
3134 | 0 | case 'C': |
3135 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3136 | 0 | g_date_time_get_year (datetime) / 100); |
3137 | 0 | break; |
3138 | 0 | case 'd': |
3139 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3140 | 0 | g_date_time_get_day_of_month (datetime)); |
3141 | 0 | break; |
3142 | 0 | case 'e': |
3143 | 0 | format_number (outstr, alt_digits, pad_set ? pad : " ", 2, |
3144 | 0 | g_date_time_get_day_of_month (datetime)); |
3145 | 0 | break; |
3146 | 0 | case 'f': |
3147 | 0 | g_string_append_printf (outstr, "%06" G_GUINT64_FORMAT, |
3148 | 0 | datetime->usec % G_TIME_SPAN_SECOND); |
3149 | 0 | break; |
3150 | 0 | case 'F': |
3151 | 0 | g_string_append_printf (outstr, "%d-%02d-%02d", |
3152 | 0 | g_date_time_get_year (datetime), |
3153 | 0 | g_date_time_get_month (datetime), |
3154 | 0 | g_date_time_get_day_of_month (datetime)); |
3155 | 0 | break; |
3156 | 0 | case 'g': |
3157 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3158 | 0 | g_date_time_get_week_numbering_year (datetime) % 100); |
3159 | 0 | break; |
3160 | 0 | case 'G': |
3161 | 0 | format_number (outstr, alt_digits, pad_set ? pad : 0, 0, |
3162 | 0 | g_date_time_get_week_numbering_year (datetime)); |
3163 | 0 | break; |
3164 | 0 | case 'h': |
3165 | 0 | name = alt_digits ? MONTH_ABBR_STANDALONE (datetime) |
3166 | 0 | : MONTH_ABBR_WITH_DAY (datetime); |
3167 | 0 | if (g_strcmp0 (name, "") == 0) |
3168 | 0 | return FALSE; |
3169 | | |
3170 | 0 | name_is_utf8 = locale_is_utf8 || |
3171 | 0 | ((alt_digits && !MONTH_ABBR_STANDALONE_IS_LOCALE) || |
3172 | 0 | (!alt_digits && !MONTH_ABBR_WITH_DAY_IS_LOCALE)); |
3173 | |
|
3174 | 0 | if (!string_append (outstr, name, name_is_utf8)) |
3175 | 0 | return FALSE; |
3176 | | |
3177 | 0 | break; |
3178 | 0 | case 'H': |
3179 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3180 | 0 | g_date_time_get_hour (datetime)); |
3181 | 0 | break; |
3182 | 0 | case 'I': |
3183 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3184 | 0 | (g_date_time_get_hour (datetime) + 11) % 12 + 1); |
3185 | 0 | break; |
3186 | 0 | case 'j': |
3187 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 3, |
3188 | 0 | g_date_time_get_day_of_year (datetime)); |
3189 | 0 | break; |
3190 | 0 | case 'k': |
3191 | 0 | format_number (outstr, alt_digits, pad_set ? pad : " ", 2, |
3192 | 0 | g_date_time_get_hour (datetime)); |
3193 | 0 | break; |
3194 | 0 | case 'l': |
3195 | 0 | format_number (outstr, alt_digits, pad_set ? pad : " ", 2, |
3196 | 0 | (g_date_time_get_hour (datetime) + 11) % 12 + 1); |
3197 | 0 | break; |
3198 | 0 | case 'm': |
3199 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3200 | 0 | g_date_time_get_month (datetime)); |
3201 | 0 | break; |
3202 | 0 | case 'M': |
3203 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3204 | 0 | g_date_time_get_minute (datetime)); |
3205 | 0 | break; |
3206 | 0 | case 'n': |
3207 | 0 | g_string_append_c (outstr, '\n'); |
3208 | 0 | break; |
3209 | 0 | case 'O': |
3210 | 0 | alt_digits = TRUE; |
3211 | 0 | goto next_mod; |
3212 | 0 | case 'p': |
3213 | 0 | if (!format_ampm (datetime, outstr, locale_is_utf8, TRUE)) |
3214 | 0 | return FALSE; |
3215 | 0 | break; |
3216 | 0 | case 'P': |
3217 | 0 | if (!format_ampm (datetime, outstr, locale_is_utf8, FALSE)) |
3218 | 0 | return FALSE; |
3219 | 0 | break; |
3220 | 0 | case 'r': |
3221 | 0 | { |
3222 | 0 | if (g_strcmp0 (PREFERRED_12HR_TIME_FMT, "") == 0) |
3223 | 0 | return FALSE; |
3224 | 0 | if (!g_date_time_format_locale (datetime, PREFERRED_12HR_TIME_FMT, |
3225 | 0 | outstr, locale_is_utf8)) |
3226 | 0 | return FALSE; |
3227 | 0 | } |
3228 | 0 | break; |
3229 | 0 | case 'R': |
3230 | 0 | g_string_append_printf (outstr, "%02d:%02d", |
3231 | 0 | g_date_time_get_hour (datetime), |
3232 | 0 | g_date_time_get_minute (datetime)); |
3233 | 0 | break; |
3234 | 0 | case 's': |
3235 | 0 | g_string_append_printf (outstr, "%" G_GINT64_FORMAT, g_date_time_to_unix (datetime)); |
3236 | 0 | break; |
3237 | 0 | case 'S': |
3238 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3239 | 0 | g_date_time_get_second (datetime)); |
3240 | 0 | break; |
3241 | 0 | case 't': |
3242 | 0 | g_string_append_c (outstr, '\t'); |
3243 | 0 | break; |
3244 | 0 | case 'T': |
3245 | 0 | g_string_append_printf (outstr, "%02d:%02d:%02d", |
3246 | 0 | g_date_time_get_hour (datetime), |
3247 | 0 | g_date_time_get_minute (datetime), |
3248 | 0 | g_date_time_get_second (datetime)); |
3249 | 0 | break; |
3250 | 0 | case 'u': |
3251 | 0 | format_number (outstr, alt_digits, 0, 0, |
3252 | 0 | g_date_time_get_day_of_week (datetime)); |
3253 | 0 | break; |
3254 | 0 | case 'V': |
3255 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3256 | 0 | g_date_time_get_week_of_year (datetime)); |
3257 | 0 | break; |
3258 | 0 | case 'w': |
3259 | 0 | format_number (outstr, alt_digits, 0, 0, |
3260 | 0 | g_date_time_get_day_of_week (datetime) % 7); |
3261 | 0 | break; |
3262 | 0 | case 'x': |
3263 | 0 | { |
3264 | 0 | if (g_strcmp0 (PREFERRED_DATE_FMT, "") == 0) |
3265 | 0 | return FALSE; |
3266 | 0 | if (!g_date_time_format_locale (datetime, PREFERRED_DATE_FMT, |
3267 | 0 | outstr, locale_is_utf8)) |
3268 | 0 | return FALSE; |
3269 | 0 | } |
3270 | 0 | break; |
3271 | 0 | case 'X': |
3272 | 0 | { |
3273 | 0 | if (g_strcmp0 (PREFERRED_TIME_FMT, "") == 0) |
3274 | 0 | return FALSE; |
3275 | 0 | if (!g_date_time_format_locale (datetime, PREFERRED_TIME_FMT, |
3276 | 0 | outstr, locale_is_utf8)) |
3277 | 0 | return FALSE; |
3278 | 0 | } |
3279 | 0 | break; |
3280 | 0 | case 'y': |
3281 | 0 | format_number (outstr, alt_digits, pad_set ? pad : "0", 2, |
3282 | 0 | g_date_time_get_year (datetime) % 100); |
3283 | 0 | break; |
3284 | 0 | case 'Y': |
3285 | 0 | format_number (outstr, alt_digits, 0, 0, |
3286 | 0 | g_date_time_get_year (datetime)); |
3287 | 0 | break; |
3288 | 0 | case 'z': |
3289 | 0 | { |
3290 | 0 | gint64 offset; |
3291 | 0 | offset = g_date_time_get_utc_offset (datetime) / USEC_PER_SECOND; |
3292 | 0 | if (!format_z (outstr, (int) offset, colons)) |
3293 | 0 | return FALSE; |
3294 | 0 | } |
3295 | 0 | break; |
3296 | 0 | case 'Z': |
3297 | 0 | tz = g_date_time_get_timezone_abbreviation (datetime); |
3298 | 0 | g_string_append (outstr, tz); |
3299 | 0 | break; |
3300 | 0 | case '%': |
3301 | 0 | g_string_append_c (outstr, '%'); |
3302 | 0 | break; |
3303 | 0 | case '-': |
3304 | 0 | pad_set = TRUE; |
3305 | 0 | pad = ""; |
3306 | 0 | goto next_mod; |
3307 | 0 | case '_': |
3308 | 0 | pad_set = TRUE; |
3309 | 0 | pad = " "; |
3310 | 0 | goto next_mod; |
3311 | 0 | case '0': |
3312 | 0 | pad_set = TRUE; |
3313 | 0 | pad = "0"; |
3314 | 0 | goto next_mod; |
3315 | 0 | case ':': |
3316 | | /* Colons are only allowed before 'z' */ |
3317 | 0 | if (*utf8_format && *utf8_format != 'z' && *utf8_format != ':') |
3318 | 0 | return FALSE; |
3319 | 0 | colons++; |
3320 | 0 | goto next_mod; |
3321 | 0 | default: |
3322 | 0 | return FALSE; |
3323 | 0 | } |
3324 | 0 | } |
3325 | | |
3326 | 0 | return TRUE; |
3327 | 0 | } |
3328 | | |
3329 | | /** |
3330 | | * g_date_time_format: |
3331 | | * @datetime: A #GDateTime |
3332 | | * @format: a valid UTF-8 string, containing the format for the |
3333 | | * #GDateTime |
3334 | | * |
3335 | | * Creates a newly allocated string representing the requested @format. |
3336 | | * |
3337 | | * The format strings understood by this function are a subset of the |
3338 | | * strftime() format language as specified by C99. The \%D, \%U and \%W |
3339 | | * conversions are not supported, nor is the 'E' modifier. The GNU |
3340 | | * extensions \%k, \%l, \%s and \%P are supported, however, as are the |
3341 | | * '0', '_' and '-' modifiers. The Python extension \%f is also supported. |
3342 | | * |
3343 | | * In contrast to strftime(), this function always produces a UTF-8 |
3344 | | * string, regardless of the current locale. Note that the rendering of |
3345 | | * many formats is locale-dependent and may not match the strftime() |
3346 | | * output exactly. |
3347 | | * |
3348 | | * The following format specifiers are supported: |
3349 | | * |
3350 | | * - \%a: the abbreviated weekday name according to the current locale |
3351 | | * - \%A: the full weekday name according to the current locale |
3352 | | * - \%b: the abbreviated month name according to the current locale |
3353 | | * - \%B: the full month name according to the current locale |
3354 | | * - \%c: the preferred date and time representation for the current locale |
3355 | | * - \%C: the century number (year/100) as a 2-digit integer (00-99) |
3356 | | * - \%d: the day of the month as a decimal number (range 01 to 31) |
3357 | | * - \%e: the day of the month as a decimal number (range 1 to 31) |
3358 | | * - \%F: equivalent to `%Y-%m-%d` (the ISO 8601 date format) |
3359 | | * - \%g: the last two digits of the ISO 8601 week-based year as a |
3360 | | * decimal number (00-99). This works well with \%V and \%u. |
3361 | | * - \%G: the ISO 8601 week-based year as a decimal number. This works |
3362 | | * well with \%V and \%u. |
3363 | | * - \%h: equivalent to \%b |
3364 | | * - \%H: the hour as a decimal number using a 24-hour clock (range 00 to 23) |
3365 | | * - \%I: the hour as a decimal number using a 12-hour clock (range 01 to 12) |
3366 | | * - \%j: the day of the year as a decimal number (range 001 to 366) |
3367 | | * - \%k: the hour (24-hour clock) as a decimal number (range 0 to 23); |
3368 | | * single digits are preceded by a blank |
3369 | | * - \%l: the hour (12-hour clock) as a decimal number (range 1 to 12); |
3370 | | * single digits are preceded by a blank |
3371 | | * - \%m: the month as a decimal number (range 01 to 12) |
3372 | | * - \%M: the minute as a decimal number (range 00 to 59) |
3373 | | * - \%f: the microsecond as a decimal number (range 000000 to 999999) |
3374 | | * - \%p: either "AM" or "PM" according to the given time value, or the |
3375 | | * corresponding strings for the current locale. Noon is treated as |
3376 | | * "PM" and midnight as "AM". Use of this format specifier is discouraged, as |
3377 | | * many locales have no concept of AM/PM formatting. Use \%c or \%X instead. |
3378 | | * - \%P: like \%p but lowercase: "am" or "pm" or a corresponding string for |
3379 | | * the current locale. Use of this format specifier is discouraged, as |
3380 | | * many locales have no concept of AM/PM formatting. Use \%c or \%X instead. |
3381 | | * - \%r: the time in a.m. or p.m. notation. Use of this format specifier is |
3382 | | * discouraged, as many locales have no concept of AM/PM formatting. Use \%c |
3383 | | * or \%X instead. |
3384 | | * - \%R: the time in 24-hour notation (\%H:\%M) |
3385 | | * - \%s: the number of seconds since the Epoch, that is, since 1970-01-01 |
3386 | | * 00:00:00 UTC |
3387 | | * - \%S: the second as a decimal number (range 00 to 60) |
3388 | | * - \%t: a tab character |
3389 | | * - \%T: the time in 24-hour notation with seconds (\%H:\%M:\%S) |
3390 | | * - \%u: the ISO 8601 standard day of the week as a decimal, range 1 to 7, |
3391 | | * Monday being 1. This works well with \%G and \%V. |
3392 | | * - \%V: the ISO 8601 standard week number of the current year as a decimal |
3393 | | * number, range 01 to 53, where week 1 is the first week that has at |
3394 | | * least 4 days in the new year. See g_date_time_get_week_of_year(). |
3395 | | * This works well with \%G and \%u. |
3396 | | * - \%w: the day of the week as a decimal, range 0 to 6, Sunday being 0. |
3397 | | * This is not the ISO 8601 standard format -- use \%u instead. |
3398 | | * - \%x: the preferred date representation for the current locale without |
3399 | | * the time |
3400 | | * - \%X: the preferred time representation for the current locale without |
3401 | | * the date |
3402 | | * - \%y: the year as a decimal number without the century |
3403 | | * - \%Y: the year as a decimal number including the century |
3404 | | * - \%z: the time zone as an offset from UTC (+hhmm) |
3405 | | * - \%:z: the time zone as an offset from UTC (+hh:mm). |
3406 | | * This is a gnulib strftime() extension. Since: 2.38 |
3407 | | * - \%::z: the time zone as an offset from UTC (+hh:mm:ss). This is a |
3408 | | * gnulib strftime() extension. Since: 2.38 |
3409 | | * - \%:::z: the time zone as an offset from UTC, with : to necessary |
3410 | | * precision (e.g., -04, +05:30). This is a gnulib strftime() extension. Since: 2.38 |
3411 | | * - \%Z: the time zone or name or abbreviation |
3412 | | * - \%\%: a literal \% character |
3413 | | * |
3414 | | * Some conversion specifications can be modified by preceding the |
3415 | | * conversion specifier by one or more modifier characters. The |
3416 | | * following modifiers are supported for many of the numeric |
3417 | | * conversions: |
3418 | | * |
3419 | | * - O: Use alternative numeric symbols, if the current locale supports those. |
3420 | | * - _: Pad a numeric result with spaces. This overrides the default padding |
3421 | | * for the specifier. |
3422 | | * - -: Do not pad a numeric result. This overrides the default padding |
3423 | | * for the specifier. |
3424 | | * - 0: Pad a numeric result with zeros. This overrides the default padding |
3425 | | * for the specifier. |
3426 | | * |
3427 | | * Additionally, when O is used with B, b, or h, it produces the alternative |
3428 | | * form of a month name. The alternative form should be used when the month |
3429 | | * name is used without a day number (e.g., standalone). It is required in |
3430 | | * some languages (Baltic, Slavic, Greek, and more) due to their grammatical |
3431 | | * rules. For other languages there is no difference. \%OB is a GNU and BSD |
3432 | | * strftime() extension expected to be added to the future POSIX specification, |
3433 | | * \%Ob and \%Oh are GNU strftime() extensions. Since: 2.56 |
3434 | | * |
3435 | | * Returns: (transfer full) (nullable): a newly allocated string formatted to |
3436 | | * the requested format or %NULL in the case that there was an error (such |
3437 | | * as a format specifier not being supported in the current locale). The |
3438 | | * string should be freed with g_free(). |
3439 | | * |
3440 | | * Since: 2.26 |
3441 | | */ |
3442 | | gchar * |
3443 | | g_date_time_format (GDateTime *datetime, |
3444 | | const gchar *format) |
3445 | 0 | { |
3446 | 0 | GString *outstr; |
3447 | 0 | const gchar *charset; |
3448 | | /* Avoid conversions from locale charset to UTF-8 if charset is compatible |
3449 | | * with UTF-8 already. Check for UTF-8 and synonymous canonical names of |
3450 | | * ASCII. */ |
3451 | 0 | gboolean locale_is_utf8_compatible = g_get_charset (&charset) || |
3452 | 0 | g_strcmp0 ("ASCII", charset) == 0 || |
3453 | 0 | g_strcmp0 ("ANSI_X3.4-1968", charset) == 0; |
3454 | |
|
3455 | 0 | g_return_val_if_fail (datetime != NULL, NULL); |
3456 | 0 | g_return_val_if_fail (format != NULL, NULL); |
3457 | 0 | g_return_val_if_fail (g_utf8_validate (format, -1, NULL), NULL); |
3458 | | |
3459 | 0 | outstr = g_string_sized_new (strlen (format) * 2); |
3460 | |
|
3461 | 0 | if (!g_date_time_format_utf8 (datetime, format, outstr, |
3462 | 0 | locale_is_utf8_compatible)) |
3463 | 0 | { |
3464 | 0 | g_string_free (outstr, TRUE); |
3465 | 0 | return NULL; |
3466 | 0 | } |
3467 | | |
3468 | 0 | return g_string_free (outstr, FALSE); |
3469 | 0 | } |
3470 | | |
3471 | | /** |
3472 | | * g_date_time_format_iso8601: |
3473 | | * @datetime: A #GDateTime |
3474 | | * |
3475 | | * Format @datetime in [ISO 8601 format](https://en.wikipedia.org/wiki/ISO_8601), |
3476 | | * including the date, time and time zone, and return that as a UTF-8 encoded |
3477 | | * string. |
3478 | | * |
3479 | | * Since GLib 2.66, this will output to sub-second precision if needed. |
3480 | | * |
3481 | | * Returns: (transfer full) (nullable): a newly allocated string formatted in |
3482 | | * ISO 8601 format or %NULL in the case that there was an error. The string |
3483 | | * should be freed with g_free(). |
3484 | | * |
3485 | | * Since: 2.62 |
3486 | | */ |
3487 | | gchar * |
3488 | | g_date_time_format_iso8601 (GDateTime *datetime) |
3489 | 0 | { |
3490 | 0 | GString *outstr = NULL; |
3491 | 0 | gchar *main_date = NULL; |
3492 | 0 | gint64 offset; |
3493 | 0 | gchar *format = "%Y-%m-%dT%H:%M:%S"; |
3494 | | |
3495 | | /* if datetime has sub-second non-zero values below the second precision we |
3496 | | * should print them as well */ |
3497 | 0 | if (datetime->usec % G_TIME_SPAN_SECOND != 0) |
3498 | 0 | format = "%Y-%m-%dT%H:%M:%S.%f"; |
3499 | | |
3500 | | /* Main date and time. */ |
3501 | 0 | main_date = g_date_time_format (datetime, format); |
3502 | 0 | outstr = g_string_new (main_date); |
3503 | 0 | g_free (main_date); |
3504 | | |
3505 | | /* Timezone. Format it as `%:::z` unless the offset is zero, in which case |
3506 | | * we can simply use `Z`. */ |
3507 | 0 | offset = g_date_time_get_utc_offset (datetime); |
3508 | |
|
3509 | 0 | if (offset == 0) |
3510 | 0 | { |
3511 | 0 | g_string_append_c (outstr, 'Z'); |
3512 | 0 | } |
3513 | 0 | else |
3514 | 0 | { |
3515 | 0 | gchar *time_zone = g_date_time_format (datetime, "%:::z"); |
3516 | 0 | g_string_append (outstr, time_zone); |
3517 | 0 | g_free (time_zone); |
3518 | 0 | } |
3519 | |
|
3520 | 0 | return g_string_free (outstr, FALSE); |
3521 | 0 | } |
3522 | | |
3523 | | |
3524 | | /* Epilogue {{{1 */ |
3525 | | /* vim:set foldmethod=marker: */ |