1 <!-- ##### SECTION Title ##### -->
2 Date and Time Functions
4 <!-- ##### SECTION Short_Description ##### -->
5 calendrical calculations and miscellaneous time stuff
7 <!-- ##### SECTION Long_Description ##### -->
9 The #GDate data structure represents a day between January 1, Year 1,
10 and sometime a few thousand years in the future (right now it will go
11 to the year 65535 or so, but g_date_set_parse() only parses up to the
12 year 8000 or so - just count on "a few thousand"). #GDate is meant to
13 represent everyday dates, not astronomical dates or historical dates
14 or ISO timestamps or the like. It extrapolates the current Gregorian
15 calendar forward and backward in time; there is no attempt to change
16 the calendar to match time periods or locations. #GDate does not store
17 time information; it represents a <emphasis>day</emphasis>.
21 The #GDate implementation has several nice features; it is only a
22 64-bit struct, so storing large numbers of dates is very efficient. It
23 can keep both a Julian and day-month-year representation of the date,
24 since some calculations are much easier with one representation or the
25 other. A Julian representation is simply a count of days since some
26 fixed day in the past; for #GDate the fixed day is January 1, 1 AD.
27 ("Julian" dates in the #GDate API aren't really Julian dates in the
28 technical sense; technically, Julian dates count from the start of the
29 Julian period, Jan 1, 4713 BC).
33 #GDate is simple to use. First you need a "blank" date; you can get a
34 dynamically allocated date from g_date_new(), or you can declare an
35 automatic variable or array and initialize it to a sane state by
36 calling g_date_clear(). A cleared date is sane; it's safe to call
37 g_date_set_dmy() and the other mutator functions to initialize the
38 value of a cleared date. However, a cleared date is initially
39 <emphasis>invalid</emphasis>, meaning that it doesn't represent a day
40 that exists. It is undefined to call any of the date calculation
41 routines on an invalid date. If you obtain a date from a user or other
42 unpredictable source, you should check its validity with the
43 g_date_valid() predicate. g_date_valid() is also used to check for
44 errors with g_date_set_parse() and other functions that can
45 fail. Dates can be invalidated by calling g_date_clear() again.
49 <emphasis>It is very important to use the API to access the #GDate
50 struct.</emphasis> Often only the day-month-year or only the Julian
51 representation is valid. Sometimes neither is valid. Use the API.
55 GLib also features #GDateTime which represents a precise time.
58 <!-- ##### SECTION See_Also ##### -->
63 <!-- ##### SECTION Stability_Level ##### -->
66 <!-- ##### SECTION Image ##### -->
69 <!-- ##### MACRO G_USEC_PER_SEC ##### -->
71 Number of microseconds in one second (1 million). This macro is provided for
77 <!-- ##### STRUCT GTimeVal ##### -->
79 Represents a precise time, with seconds and microseconds.
80 Similar to the <structname>struct timeval</structname> returned by
81 the <function>gettimeofday()</function> UNIX call.
85 GLib is attempting to unify around the use of 64bit integers to
86 represent microsecond-precision time. As such, this type will be
87 removed from a future version of GLib.
91 @tv_usec: microseconds
93 <!-- ##### FUNCTION g_get_current_time ##### -->
100 <!-- ##### FUNCTION g_usleep ##### -->
102 Pauses the current thread for the given number of microseconds. There
103 are 1 million microseconds per second (represented by the
104 #G_USEC_PER_SEC macro). g_usleep() may have limited precision,
105 depending on hardware and operating system; don't rely on the exact
109 @microseconds: number of microseconds to pause
112 <!-- ##### FUNCTION g_time_val_add ##### -->
121 <!-- ##### FUNCTION g_time_val_from_iso8601 ##### -->
131 <!-- ##### FUNCTION g_time_val_to_iso8601 ##### -->
140 <!-- ##### STRUCT GTimeSpec ##### -->
148 <!-- ##### STRUCT GDate ##### -->
150 Represents a day between January 1, Year 1 and a few thousand years in
151 the future. None of its members should be accessed directly. If the
152 <structname>GDate</structname> is obtained from g_date_new(), it will
153 be safe to mutate but invalid and thus not safe for calendrical computations.
154 If it's declared on the stack, it will contain garbage so must be
155 initialized with g_date_clear(). g_date_clear() makes the date invalid
156 but sane. An invalid date doesn't represent a day, it's "empty." A
157 date becomes valid after you set it to a Julian day or you set a day,
161 @julian_days: the Julian representation of the date
162 @julian: this bit is set if @julian_days is valid
163 @dmy: this is set if @day, @month and @year are valid
164 @day: the day of the day-month-year representation of the date, as
165 a number between 1 and 31
166 @month: the day of the day-month-year representation of the date, as
167 a number between 1 and 12
168 @year: the day of the day-month-year representation of the date
170 <!-- ##### TYPEDEF GTime ##### -->
172 Simply a replacement for <type>time_t</type>. It has been deprecated
173 since it is <emphasis>not</emphasis> equivalent to <type>time_t</type>
174 on 64-bit platforms with a 64-bit <type>time_t</type>.
175 Unrelated to #GTimer.
180 Note that <type>GTime</type> is defined to always be a 32bit integer,
181 unlike <type>time_t</type> which may be 64bit on some systems.
182 Therefore, <type>GTime</type> will overflow in the year 2038, and
183 you cannot use the address of a <type>GTime</type> variable as argument
184 to the UNIX time() function. Instead, do the following:
191 gtime = (GTime)ttime;
197 <!-- ##### ENUM GDateDMY ##### -->
199 This enumeration isn't used in the API, but may be useful if you need
200 to mark a number as a day, month, or year.
204 @G_DATE_MONTH: a month
207 <!-- ##### TYPEDEF GDateDay ##### -->
209 Integer representing a day of the month; between 1 and
210 31. #G_DATE_BAD_DAY represents an invalid day of the month.
214 <!-- ##### ENUM GDateMonth ##### -->
216 Enumeration representing a month; values are #G_DATE_JANUARY,
217 #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
220 @G_DATE_BAD_MONTH: invalid value
221 @G_DATE_JANUARY: January
222 @G_DATE_FEBRUARY: February
228 @G_DATE_AUGUST: August
229 @G_DATE_SEPTEMBER: September
230 @G_DATE_OCTOBER: October
231 @G_DATE_NOVEMBER: November
232 @G_DATE_DECEMBER: December
234 <!-- ##### TYPEDEF GDateYear ##### -->
236 Integer representing a year; #G_DATE_BAD_YEAR is the invalid
237 value. The year must be 1 or higher; negative (BC) years are not
238 allowed. The year is represented with four digits.
242 <!-- ##### ENUM GDateWeekday ##### -->
244 Enumeration representing a day of the week; #G_DATE_MONDAY,
245 #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
248 @G_DATE_BAD_WEEKDAY: invalid value
249 @G_DATE_MONDAY: Monday
250 @G_DATE_TUESDAY: Tuesday
251 @G_DATE_WEDNESDAY: Wednesday
252 @G_DATE_THURSDAY: Thursday
253 @G_DATE_FRIDAY: Friday
254 @G_DATE_SATURDAY: Saturday
255 @G_DATE_SUNDAY: Sunday
257 <!-- ##### MACRO G_DATE_BAD_DAY ##### -->
259 Represents an invalid #GDateDay.
264 <!-- ##### MACRO G_DATE_BAD_JULIAN ##### -->
266 Represents an invalid Julian day number.
271 <!-- ##### MACRO G_DATE_BAD_YEAR ##### -->
273 Represents an invalid year.
278 <!-- ##### FUNCTION g_date_new ##### -->
280 Allocates a #GDate and initializes it to a sane state. The new date will
281 be cleared (as if you'd called g_date_clear()) but invalid (it won't
282 represent an existing day). Free the return value with g_date_free().
286 @Returns: a newly-allocated #GDate
289 <!-- ##### FUNCTION g_date_new_dmy ##### -->
291 Like g_date_new(), but also sets the value of the date. Assuming the
292 day-month-year triplet you pass in represents an existing day, the
293 returned date will be valid.
296 @day: day of the month
297 @month: month of the year
299 @Returns: a newly-allocated #GDate initialized with @day, @month, and @year
302 <!-- ##### FUNCTION g_date_new_julian ##### -->
304 Like g_date_new(), but also sets the value of the date. Assuming the
305 Julian day number you pass in is valid (greater than 0, less than an
306 unreasonably large number), the returned date will be valid.
309 @julian_day: days since January 1, Year 1
310 @Returns: a newly-allocated #GDate initialized with @julian_day
313 <!-- ##### FUNCTION g_date_clear ##### -->
315 Initializes one or more #GDate structs to a sane but invalid
316 state. The cleared dates will not represent an existing date, but will
317 not contain garbage. Useful to init a date declared on the stack.
318 Validity can be tested with g_date_valid().
321 @date: pointer to one or more dates to clear
322 @n_dates: number of dates to clear
325 <!-- ##### FUNCTION g_date_free ##### -->
327 Frees a #GDate returned from g_date_new().
333 <!-- ##### FUNCTION g_date_set_day ##### -->
335 Sets the day of the month for a #GDate. If the resulting day-month-year
336 triplet is invalid, the date will be invalid.
343 <!-- ##### FUNCTION g_date_set_month ##### -->
345 Sets the month of the year for a #GDate. If the resulting
346 day-month-year triplet is invalid, the date will be invalid.
353 <!-- ##### FUNCTION g_date_set_year ##### -->
355 Sets the year for a #GDate. If the resulting day-month-year triplet is
356 invalid, the date will be invalid.
363 <!-- ##### FUNCTION g_date_set_dmy ##### -->
365 Sets the value of a #GDate from a day, month, and year. The day-month-year
366 triplet must be valid; if you aren't sure it is, call g_date_valid_dmy() to
367 check before you set it.
376 <!-- ##### FUNCTION g_date_set_julian ##### -->
378 Sets the value of a #GDate from a Julian day number.
382 @julian_date: Julian day number (days since January 1, Year 1)
385 <!-- ##### FUNCTION g_date_set_time ##### -->
394 <!-- ##### FUNCTION g_date_set_time_t ##### -->
403 <!-- ##### FUNCTION g_date_set_time_val ##### -->
412 <!-- ##### FUNCTION g_date_set_parse ##### -->
414 Parses a user-inputted string @str, and try to figure out what date it
415 represents, taking the <link linkend="setlocale">current locale</link>
416 into account. If the string is successfully parsed, the date will be
417 valid after the call. Otherwise, it will be invalid. You should check
418 using g_date_valid() to see whether the parsing succeeded.
422 This function is not appropriate for file formats and the like; it
423 isn't very precise, and its exact behavior varies with the
424 locale. It's intended to be a heuristic routine that guesses what the
425 user means by a given string (and it does work pretty well in that
429 @date: a #GDate to fill in
430 @str: string to parse
433 <!-- ##### FUNCTION g_date_add_days ##### -->
435 Increments a date some number of days. To move forward by weeks, add
436 weeks*7 days. The date must be valid.
439 @date: a #GDate to increment
440 @n_days: number of days to move the date forward
443 <!-- ##### FUNCTION g_date_subtract_days ##### -->
445 Moves a date some number of days into the past. To move by weeks, just
446 move by weeks*7 days. The date must be valid.
449 @date: a #GDate to decrement
450 @n_days: number of days to move
453 <!-- ##### FUNCTION g_date_add_months ##### -->
455 Increments a date by some number of months. If the day of the month is
456 greater than 28, this routine may change the day of the month (because
457 the destination month may not have the current day in it). The date
461 @date: a #GDate to increment
462 @n_months: number of months to move forward
465 <!-- ##### FUNCTION g_date_subtract_months ##### -->
467 Moves a date some number of months into the past. If the current day of
468 the month doesn't exist in the destination month, the day of the month
469 may change. The date must be valid.
472 @date: a #GDate to decrement
473 @n_months: number of months to move
476 <!-- ##### FUNCTION g_date_add_years ##### -->
478 Increments a date by some number of years. If the date is February 29,
479 and the destination year is not a leap year, the date will be changed
480 to February 28. The date must be valid.
483 @date: a #GDate to increment
484 @n_years: number of years to move forward
487 <!-- ##### FUNCTION g_date_subtract_years ##### -->
489 Moves a date some number of years into the past. If the current day
490 doesn't exist in the destination year (i.e. it's February 29 and you
491 move to a non-leap-year) then the day is changed to February 29. The date
495 @date: a #GDate to decrement
496 @n_years: number of years to move
499 <!-- ##### FUNCTION g_date_days_between ##### -->
501 Computes the number of days between two dates.
502 If @date2 is prior to @date1, the returned value is negative.
503 Both dates must be valid.
506 @date1: the first date
507 @date2: the second date
508 @Returns: the number of days between @date1 and @date2
511 <!-- ##### FUNCTION g_date_compare ##### -->
513 qsort()-style comparsion function for dates. Both
517 @lhs: first date to compare
518 @rhs: second date to compare
519 @Returns: 0 for equal, less than zero if @lhs is less than @rhs,
520 greater than zero if @lhs is greater than @rhs
523 <!-- ##### FUNCTION g_date_clamp ##### -->
525 If @date is prior to @min_date, sets @date equal to @min_date.
526 If @date falls after @max_date, sets @date equal to @max_date.
527 Otherwise, @date is unchanged.
528 Either of @min_date and @max_date may be %NULL. All non-%NULL dates
532 @date: a #GDate to clamp
533 @min_date: minimum accepted value for @date
534 @max_date: maximum accepted value for @date
537 <!-- ##### FUNCTION g_date_order ##### -->
539 Checks if @date1 is less than or equal to @date2,
540 and swap the values if this is not the case.
543 @date1: the first date
544 @date2: the second date
547 <!-- ##### FUNCTION g_date_get_day ##### -->
549 Returns the day of the month. The date must be valid.
552 @date: a #GDate to extract the day of the month from
553 @Returns: day of the month
556 <!-- ##### FUNCTION g_date_get_month ##### -->
558 Returns the month of the year. The date must be valid.
561 @date: a #GDate to get the month from
562 @Returns: month of the year as a #GDateMonth
565 <!-- ##### FUNCTION g_date_get_year ##### -->
567 Returns the year of a #GDate. The date must be valid.
571 @Returns: year in which the date falls
574 <!-- ##### FUNCTION g_date_get_julian ##### -->
576 Returns the Julian day or "serial number" of the #GDate. The
577 Julian day is simply the number of days since January 1, Year 1; i.e.,
578 January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
579 etc. The date must be valid.
582 @date: a #GDate to extract the Julian day from
586 <!-- ##### FUNCTION g_date_get_weekday ##### -->
588 Returns the day of the week for a #GDate. The date must be valid.
592 @Returns: day of the week as a #GDateWeekday.
595 <!-- ##### FUNCTION g_date_get_day_of_year ##### -->
597 Returns the day of the year, where Jan 1 is the first day of the
598 year. The date must be valid.
601 @date: a #GDate to extract day of year from
602 @Returns: day of the year
605 <!-- ##### FUNCTION g_date_get_days_in_month ##### -->
607 Returns the number of days in a month, taking leap years into account.
612 @Returns: number of days in @month during the @year
615 <!-- ##### FUNCTION g_date_is_first_of_month ##### -->
617 Returns %TRUE if the date is on the first of a month. The date must be valid.
620 @date: a #GDate to check
621 @Returns: %TRUE if the date is the first of the month
624 <!-- ##### FUNCTION g_date_is_last_of_month ##### -->
626 Returns %TRUE if the date is the last day of the month. The date must be valid.
629 @date: a #GDate to check
630 @Returns: %TRUE if the date is the last day of the month
633 <!-- ##### FUNCTION g_date_is_leap_year ##### -->
635 Returns %TRUE if the year is a leap year.<footnote><para>
636 For the purposes of this function, leap year is every year divisible by
637 4 unless that year is divisible by 100. If it is divisible by 100 it would
638 be a leap year only if that year is also divisible by 400.</para></footnote>
642 @Returns: %TRUE if the year is a leap year
645 <!-- ##### FUNCTION g_date_get_monday_week_of_year ##### -->
647 Returns the week of the year, where weeks are understood to start on
648 Monday. If the date is before the first Monday of the year, return
649 0. The date must be valid.
653 @Returns: week of the year
656 <!-- ##### FUNCTION g_date_get_monday_weeks_in_year ##### -->
658 Returns the number of weeks in the year, where weeks are taken to start
659 on Monday. Will be 52 or 53. The date must be valid. (Years always have 52
660 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
661 year. This function is basically telling you how many Mondays are in
662 the year, i.e. there are 53 Mondays if one of the extra days happens
667 @Returns: number of Mondays in the year
670 <!-- ##### FUNCTION g_date_get_sunday_week_of_year ##### -->
672 Returns the week of the year during which this date falls, if weeks
673 are understood to being on Sunday. The date must be valid. Can return 0 if
674 the day is before the first Sunday of the year.
678 @Returns: week number
681 <!-- ##### FUNCTION g_date_get_sunday_weeks_in_year ##### -->
683 Returns the number of weeks in the year, where weeks are taken to start
684 on Sunday. Will be 52 or 53. The date must be valid. (Years always have 52
685 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
686 year. This function is basically telling you how many Sundays are in
687 the year, i.e. there are 53 Sundays if one of the extra days happens
691 @year: year to count weeks in
692 @Returns: number of weeks
695 <!-- ##### FUNCTION g_date_get_iso8601_week_of_year ##### -->
704 <!-- ##### FUNCTION g_date_strftime ##### -->
706 Generates a printed representation of the date, in a
707 <link linkend="setlocale">locale</link>-specific way. Works just like
708 the platform's C library strftime() function, but only accepts date-related
709 formats; time-related formats give undefined results. Date must be valid.
710 Unlike strftime() (which uses the locale encoding), works on a UTF-8 format
711 string and stores a UTF-8 result.
715 This function does not provide any conversion specifiers in addition
716 to those implemented by the platform's C library. For example, don't
717 expect that using g_date_strftime() would make the \%F provided by the C99
718 strftime() work on Windows where the C library only complies to C89.
721 @s: destination buffer
723 @format: format string
725 @Returns: number of characters written to the buffer, or 0 the buffer was too small
728 <!-- ##### FUNCTION g_date_to_struct_tm ##### -->
730 Fills in the date-related bits of a <structname>struct tm</structname>
731 using the @date value. Initializes the non-date parts with something
732 sane but meaningless.
735 @date: a #GDate to set the <structname>struct tm</structname> from.
736 @tm: <structname>struct tm</structname> to fill.
739 <!-- ##### FUNCTION g_date_valid ##### -->
741 Returns %TRUE if the #GDate represents an existing day. The date must not
742 contain garbage; it should have been initialized with g_date_clear()
743 if it wasn't allocated by one of the g_date_new() variants.
746 @date: a #GDate to check
747 @Returns: Whether the date is valid
750 <!-- ##### FUNCTION g_date_valid_day ##### -->
752 Returns %TRUE if the day of the month is valid (a day is valid if it's
753 between 1 and 31 inclusive).
757 @Returns: %TRUE if the day is valid
760 <!-- ##### FUNCTION g_date_valid_month ##### -->
762 Returns %TRUE if the month value is valid. The 12 #GDateMonth
763 enumeration values are the only valid months.
767 @Returns: %TRUE if the month is valid
770 <!-- ##### FUNCTION g_date_valid_year ##### -->
772 Returns %TRUE if the year is valid. Any year greater than 0 is valid,
773 though there is a 16-bit limit to what #GDate will understand.
777 @Returns: %TRUE if the year is valid
780 <!-- ##### FUNCTION g_date_valid_dmy ##### -->
782 Returns %TRUE if the day-month-year triplet forms a valid, existing day
783 in the range of days #GDate understands (Year 1 or later, no more than
784 a few thousand years in the future).
790 @Returns: %TRUE if the date is a valid one
793 <!-- ##### FUNCTION g_date_valid_julian ##### -->
795 Returns %TRUE if the Julian day is valid. Anything greater than zero
796 is basically a valid Julian, though there is a 32-bit limit.
799 @julian_date: Julian day to check
800 @Returns: %TRUE if the Julian day is valid
803 <!-- ##### FUNCTION g_date_valid_weekday ##### -->
805 Returns %TRUE if the weekday is valid. The seven #GDateWeekday enumeration
806 values are the only valid weekdays.
810 @Returns: %TRUE if the weekday is valid