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 doesn't contain any time-manipulation functions; however, there
56 is a #GTime typedef and a #GTimeVal struct which represents a more
57 precise time (with microseconds). You can request the current time as
58 a #GTimeVal with g_get_current_time().
61 <!-- ##### SECTION See_Also ##### -->
66 <!-- ##### SECTION Stability_Level ##### -->
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 @tv_usec: microseconds.
87 <!-- ##### FUNCTION g_get_current_time ##### -->
94 <!-- ##### FUNCTION g_usleep ##### -->
96 Pauses the current thread for the given number of microseconds. There
97 are 1 million microseconds per second (represented by the
98 #G_USEC_PER_SEC macro). g_usleep() may have limited precision,
99 depending on hardware and operating system; don't rely on the exact
103 @microseconds: number of microseconds to pause.
106 <!-- ##### FUNCTION g_time_val_add ##### -->
115 <!-- ##### FUNCTION g_time_val_from_iso8601 ##### -->
125 <!-- ##### FUNCTION g_time_val_to_iso8601 ##### -->
134 <!-- ##### STRUCT GDate ##### -->
136 Represents a day between January 1, Year 1 and a few thousand years in
137 the future. None of its members should be accessed directly. If the
138 <structname>GDate</structname> is obtained from g_date_new(), it will
139 be safe to mutate but invalid and thus not safe for calendrical computations.
140 If it's declared on the stack, it will contain garbage so must be
141 initialized with g_date_clear(). g_date_clear() makes the date invalid
142 but sane. An invalid date doesn't represent a day, it's "empty." A
143 date becomes valid after you set it to a Julian day or you set a day,
147 @julian_days: the Julian representation of the date
148 @julian: this bit is set if @julian_days is valid
149 @dmy: this is set if @day, @month and @year are valid
150 @day: the day of the day-month-year representation of the date, as
151 a number between 1 and 31
152 @month: the day of the day-month-year representation of the date, as
153 a number between 1 and 12
154 @year: the day of the day-month-year representation of the date
156 <!-- ##### TYPEDEF GTime ##### -->
158 Simply a replacement for <type>time_t</type>. It has been deprected
159 since it is <emphasis>not</emphasis> equivalent to <type>time_t</type>
160 on 64-bit platforms with a 64-bit <type>time_t</type>.
161 Unrelated to #GTimer.
166 Note that <type>GTime</type> is defined to always be a 32bit integer,
167 unlike <type>time_t</type> which may be 64bit on some systems.
168 Therefore, <type>GTime</type> will overflow in the year 2038, and
169 you cannot use the address of a <type>GTime</type> variable as argument
170 to the UNIX time() function. Instead, do the following:
177 gtime = (GTime)ttime;
183 <!-- ##### ENUM GDateDMY ##### -->
185 This enumeration isn't used in the API, but may be useful if you need
186 to mark a number as a day, month, or year.
190 @G_DATE_MONTH: a month
193 <!-- ##### TYPEDEF GDateDay ##### -->
195 Integer representing a day of the month; between 1 and
196 31. #G_DATE_BAD_DAY represents an invalid day of the month.
200 <!-- ##### ENUM GDateMonth ##### -->
202 Enumeration representing a month; values are #G_DATE_JANUARY,
203 #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
206 @G_DATE_BAD_MONTH: invalid value.
207 @G_DATE_JANUARY: January.
208 @G_DATE_FEBRUARY: February.
209 @G_DATE_MARCH: March.
210 @G_DATE_APRIL: April.
214 @G_DATE_AUGUST: August.
215 @G_DATE_SEPTEMBER: September.
216 @G_DATE_OCTOBER: October.
217 @G_DATE_NOVEMBER: November.
218 @G_DATE_DECEMBER: December.
220 <!-- ##### TYPEDEF GDateYear ##### -->
222 Integer representing a year; #G_DATE_BAD_YEAR is the invalid
223 value. The year must be 1 or higher; negative (BC) years are not
224 allowed. The year is represented with four digits.
228 <!-- ##### ENUM GDateWeekday ##### -->
230 Enumeration representing a day of the week; #G_DATE_MONDAY,
231 #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
234 @G_DATE_BAD_WEEKDAY: invalid value.
235 @G_DATE_MONDAY: Monday.
236 @G_DATE_TUESDAY: Tuesday.
237 @G_DATE_WEDNESDAY: Wednesday.
238 @G_DATE_THURSDAY: Thursday.
239 @G_DATE_FRIDAY: Friday.
240 @G_DATE_SATURDAY: Saturday.
241 @G_DATE_SUNDAY: Sunday.
243 <!-- ##### MACRO G_DATE_BAD_DAY ##### -->
245 Represents an invalid #GDateDay.
250 <!-- ##### MACRO G_DATE_BAD_JULIAN ##### -->
252 Represents an invalid Julian day number.
257 <!-- ##### MACRO G_DATE_BAD_YEAR ##### -->
259 Represents an invalid year.
264 <!-- ##### FUNCTION g_date_new ##### -->
266 Allocates a #GDate and initializes it to a sane state. The new date will
267 be cleared (as if you'd called g_date_clear()) but invalid (it won't
268 represent an existing day). Free the return value with g_date_free().
271 @Returns: a newly-allocated #GDate.
274 <!-- ##### FUNCTION g_date_new_dmy ##### -->
276 Like g_date_new(), but also sets the value of the date. Assuming the
277 day-month-year triplet you pass in represents an existing day, the
278 returned date will be valid.
281 @day: day of the month.
282 @month: month of the year.
284 @Returns: a newly-allocated #GDate initialized with @day, @month, and @year.
287 <!-- ##### FUNCTION g_date_new_julian ##### -->
289 Like g_date_new(), but also sets the value of the date. Assuming the
290 Julian day number you pass in is valid (greater than 0, less than an
291 unreasonably large number), the returned date will be valid.
294 @julian_day: days since January 1, Year 1.
295 @Returns: a newly-allocated #GDate initialized with @julian_day.
298 <!-- ##### FUNCTION g_date_clear ##### -->
300 Initializes one or more #GDate structs to a sane but invalid
301 state. The cleared dates will not represent an existing date, but will
302 not contain garbage. Useful to init a date declared on the stack.
303 Validity can be tested with g_date_valid().
306 @date: pointer to one or more dates to clear.
307 @n_dates: number of dates to clear.
310 <!-- ##### FUNCTION g_date_free ##### -->
312 Frees a #GDate returned from g_date_new().
318 <!-- ##### FUNCTION g_date_set_day ##### -->
320 Sets the day of the month for a #GDate. If the resulting day-month-year
321 triplet is invalid, the date will be invalid.
328 <!-- ##### FUNCTION g_date_set_month ##### -->
330 Sets the month of the year for a #GDate. If the resulting
331 day-month-year triplet is invalid, the date will be invalid.
335 @month: month to set.
338 <!-- ##### FUNCTION g_date_set_year ##### -->
340 Sets the year for a #GDate. If the resulting day-month-year triplet is
341 invalid, the date will be invalid.
348 <!-- ##### FUNCTION g_date_set_dmy ##### -->
350 Sets the value of a #GDate from a day, month, and year. The day-month-year
351 triplet must be valid; if you aren't sure it is, call g_date_valid_dmy() to
352 check before you set it.
361 <!-- ##### FUNCTION g_date_set_julian ##### -->
363 Sets the value of a #GDate from a Julian day number.
367 @julian_date: Julian day number (days since January 1, Year 1).
370 <!-- ##### FUNCTION g_date_set_time ##### -->
379 <!-- ##### FUNCTION g_date_set_time_t ##### -->
388 <!-- ##### FUNCTION g_date_set_time_val ##### -->
397 <!-- ##### FUNCTION g_date_set_parse ##### -->
399 Parses a user-inputted string @str, and try to figure out what date it
400 represents, taking the <link linkend="setlocale">current locale</link>
401 into account. If the string is successfully parsed, the date will be
402 valid after the call. Otherwise, it will be invalid. You should check
403 using g_date_valid() to see whether the parsing succeeded.
407 This function is not appropriate for file formats and the like; it
408 isn't very precise, and its exact behavior varies with the
409 locale. It's intended to be a heuristic routine that guesses what the
410 user means by a given string (and it does work pretty well in that
414 @date: a #GDate to fill in.
415 @str: string to parse.
418 <!-- ##### FUNCTION g_date_add_days ##### -->
420 Increments a date some number of days. To move forward by weeks, add
421 weeks*7 days. The date must be valid.
424 @date: a #GDate to increment.
425 @n_days: number of days to move the date forward.
428 <!-- ##### FUNCTION g_date_subtract_days ##### -->
430 Moves a date some number of days into the past. To move by weeks, just
431 move by weeks*7 days. The date must be valid.
434 @date: a #GDate to decrement.
435 @n_days: number of days to move.
438 <!-- ##### FUNCTION g_date_add_months ##### -->
440 Increments a date by some number of months. If the day of the month is
441 greater than 28, this routine may change the day of the month (because
442 the destination month may not have the current day in it). The date
446 @date: a #GDate to increment.
447 @n_months: number of months to move forward.
450 <!-- ##### FUNCTION g_date_subtract_months ##### -->
452 Moves a date some number of months into the past. If the current day of
453 the month doesn't exist in the destination month, the day of the month
454 may change. The date must be valid.
457 @date: a #GDate to decrement.
458 @n_months: number of months to move.
461 <!-- ##### FUNCTION g_date_add_years ##### -->
463 Increments a date by some number of years. If the date is February 29,
464 and the destination year is not a leap year, the date will be changed
465 to February 28. The date must be valid.
468 @date: a #GDate to increment.
469 @n_years: number of years to move forward.
472 <!-- ##### FUNCTION g_date_subtract_years ##### -->
474 Moves a date some number of years into the past. If the current day
475 doesn't exist in the destination year (i.e. it's February 29 and you
476 move to a non-leap-year) then the day is changed to February 29. The date
480 @date: a #GDate to decrement.
481 @n_years: number of years to move.
484 <!-- ##### FUNCTION g_date_days_between ##### -->
486 Computes the number of days between two dates.
487 If @date2 is prior to @date1, the returned value is negative.
488 Both dates must be valid.
491 @date1: the first date.
492 @date2: the second date.
493 @Returns: the number of days between @date1 and @date2.
496 <!-- ##### FUNCTION g_date_compare ##### -->
498 <function>qsort()</function>-style comparsion function for dates. Both
502 @lhs: first date to compare.
503 @rhs: second date to compare.
504 @Returns: 0 for equal, less than zero if @lhs is less than @rhs,
505 greater than zero if @lhs is greater than @rhs.
508 <!-- ##### FUNCTION g_date_clamp ##### -->
510 If @date is prior to @min_date, sets @date equal to @min_date.
511 If @date falls after @max_date, sets @date equal to @max_date.
512 Either @min_date and @max_date may be %NULL. All non-%NULL dates
516 @date: a #GDate to clamp.
517 @min_date: minimum accepted value for @date.
518 @max_date: maximum accepted value for @date.
521 <!-- ##### FUNCTION g_date_order ##### -->
523 Checks if @date1 is less than or equal to @date2,
524 and swap the values if this is not the case.
527 @date1: the first date.
528 @date2: the second date.
531 <!-- ##### FUNCTION g_date_get_day ##### -->
533 Returns the day of the month. The date must be valid.
536 @date: a #GDate to extract the day of the month from.
537 @Returns: day of the month.
540 <!-- ##### FUNCTION g_date_get_month ##### -->
542 Returns the month of the year. The date must be valid.
545 @date: a #GDate to get the month from.
546 @Returns: month of the year as a #GDateMonth.
549 <!-- ##### FUNCTION g_date_get_year ##### -->
551 Returns the year of a #GDate. The date must be valid.
555 @Returns: year in which the date falls.
558 <!-- ##### FUNCTION g_date_get_julian ##### -->
560 Returns the Julian day or "serial number" of the #GDate. The
561 Julian day is simply the number of days since January 1, Year 1; i.e.,
562 January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
563 etc. The date must be valid.
566 @date: a #GDate to extract the Julian day from.
567 @Returns: Julian day.
570 <!-- ##### FUNCTION g_date_get_weekday ##### -->
572 Returns the day of the week for a #GDate. The date must be valid.
576 @Returns: day of the week as a #GDateWeekday.
579 <!-- ##### FUNCTION g_date_get_day_of_year ##### -->
581 Returns the day of the year, where Jan 1 is the first day of the
582 year. The date must be valid.
585 @date: a #GDate to extract day of year from.
586 @Returns: day of the year.
589 <!-- ##### FUNCTION g_date_get_days_in_month ##### -->
591 Returns the number of days in a month, taking leap years into account.
596 @Returns: number of days in @month during the @year.
599 <!-- ##### FUNCTION g_date_is_first_of_month ##### -->
601 Returns %TRUE if the date is on the first of a month. The date must be valid.
604 @date: a #GDate to check.
605 @Returns: %TRUE if the date is the first of the month.
608 <!-- ##### FUNCTION g_date_is_last_of_month ##### -->
610 Returns %TRUE if the date is the last day of the month. The date must be valid.
613 @date: a #GDate to check.
614 @Returns: %TRUE if the date is the last day of the month.
617 <!-- ##### FUNCTION g_date_is_leap_year ##### -->
619 Returns %TRUE if the year is a leap year.
622 @year: year to check.
623 @Returns: %TRUE if the year is a leap year.
626 <!-- ##### FUNCTION g_date_get_monday_week_of_year ##### -->
628 Returns the week of the year, where weeks are understood to start on
629 Monday. If the date is before the first Monday of the year, return
630 0. The date must be valid.
634 @Returns: week of the year.
637 <!-- ##### FUNCTION g_date_get_monday_weeks_in_year ##### -->
639 Returns the number of weeks in the year, where weeks are taken to start
640 on Monday. Will be 52 or 53. The date must be valid. (Years always have 52
641 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
642 year. This function is basically telling you how many Mondays are in
643 the year, i.e. there are 53 Mondays if one of the extra days happens
648 @Returns: number of Mondays in the year.
651 <!-- ##### FUNCTION g_date_get_sunday_week_of_year ##### -->
653 Returns the week of the year during which this date falls, if weeks
654 are understood to being on Sunday. The date must be valid. Can return 0 if
655 the day is before the first Sunday of the year.
659 @Returns: week number.
662 <!-- ##### FUNCTION g_date_get_sunday_weeks_in_year ##### -->
664 Returns the number of weeks in the year, where weeks are taken to start
665 on Sunday. Will be 52 or 53. The date must be valid. (Years always have 52
666 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
667 year. This function is basically telling you how many Sundays are in
668 the year, i.e. there are 53 Sundays if one of the extra days happens
672 @year: year to count weeks in.
673 @Returns: number of weeks.
676 <!-- ##### FUNCTION g_date_get_iso8601_week_of_year ##### -->
685 <!-- ##### FUNCTION g_date_strftime ##### -->
687 Generates a printed representation of the date, in a
688 <link linkend="setlocale">locale</link>-specific way. Works just like
689 the platform's C library strftime() function, but only accepts date-related
690 formats; time-related formats give undefined results. Date must be valid.
691 Unlike strftime() (which uses the locale encoding), works on a UTF-8 format
692 string and stores a UTF-8 result.
696 This function does not provide any conversion specifiers in addition
697 to those implemented by the platform's C library. For example, don't
698 expect that using g_date_strftime() would make the %F provided by the C99
699 strftime() work on Windows where the C library only complies to C89.
702 @s: destination buffer.
704 @format: format string.
706 @Returns: number of characters written to the buffer, or 0 the buffer was too small.
709 <!-- ##### FUNCTION g_date_to_struct_tm ##### -->
711 Fills in the date-related bits of a <structname>struct tm</structname>
712 using the @date value. Initializes the non-date parts with something
713 sane but meaningless.
716 @date: a #GDate to set the <structname>struct tm</structname> from.
717 @tm: <structname>struct tm</structname> to fill.
720 <!-- ##### FUNCTION g_date_valid ##### -->
722 Returns %TRUE if the #GDate represents an existing day. The date must not
723 contain garbage; it should have been initialized with g_date_clear()
724 if it wasn't allocated by one of the g_date_new() variants.
727 @date: a #GDate to check.
728 @Returns: Whether the date is valid.
731 <!-- ##### FUNCTION g_date_valid_day ##### -->
733 Returns %TRUE if the day of the month is valid (a day is valid if it's
734 between 1 and 31 inclusive).
738 @Returns: %TRUE if the day is valid.
741 <!-- ##### FUNCTION g_date_valid_month ##### -->
743 Returns %TRUE if the month value is valid. The 12 #GDateMonth
744 enumeration values are the only valid months.
748 @Returns: %TRUE if the month is valid.
751 <!-- ##### FUNCTION g_date_valid_year ##### -->
753 Returns %TRUE if the year is valid. Any year greater than 0 is valid,
754 though there is a 16-bit limit to what #GDate will understand.
758 @Returns: %TRUE if the year is valid.
761 <!-- ##### FUNCTION g_date_valid_dmy ##### -->
763 Returns %TRUE if the day-month-year triplet forms a valid, existing day
764 in the range of days #GDate understands (Year 1 or later, no more than
765 a few thousand years in the future).
771 @Returns: %TRUE if the date is a valid one.
774 <!-- ##### FUNCTION g_date_valid_julian ##### -->
776 Returns %TRUE if the Julian day is valid. Anything greater than zero is basically a
777 valid Julian, though there is a 32-bit limit.
780 @julian_date: Julian day to check.
781 @Returns: %TRUE if the Julian day is valid.
784 <!-- ##### FUNCTION g_date_valid_weekday ##### -->
786 Returns %TRUE if the weekday is valid. The 7 #GDateWeekday enumeration
787 values are the only valid weekdays.
791 @Returns: %TRUE if the weekday is valid.