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 which is equivalent to <type>time_t</type>, and a #GTimeVal
57 struct which represents a more precise time (with microseconds). You
58 can request the current time as 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. Same as the
80 <structname>struct timeval</structname> returned by the
81 <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 <!-- ##### STRUCT GDate ##### -->
117 Represents a day between January 1, Year 1 and a few thousand years in
118 the future. None of its members should be accessed directly. If the
119 <structname>GDate</structname> is obtained from g_date_new(), it will
120 be safe to mutate but invalid and thus not safe for calendrical computations.
121 If it's declared on the stack, it will contain garbage so must be
122 initialized with g_date_clear(). g_date_clear() makes the date invalid
123 but sane. An invalid date doesn't represent a day, it's "empty." A
124 date becomes valid after you set it to a Julian day or you set a day,
128 @julian_days: the Julian representation of the date
129 @julian: this bit is set if @julian_days is valid
130 @dmy: this is set if @day, @month and @year are valid
131 @day: the day of the day-month-year representation of the date, as
132 a number between 1 and 31
133 @month: the day of the day-month-year representation of the date, as
134 a number between 1 and 12
135 @year: the day of the day-month-year representation of the date
137 <!-- ##### TYPEDEF GTime ##### -->
139 Simply a replacement for <type>time_t</type>. Unrelated to #GTimer.
143 <!-- ##### ENUM GDateDMY ##### -->
145 This enumeration isn't used in the API, but may be useful if you need
146 to mark a number as a day, month, or year.
150 @G_DATE_MONTH: a month
153 <!-- ##### TYPEDEF GDateDay ##### -->
155 Integer representing a day of the month; between 1 and
156 31. #G_DATE_BAD_DAY represents an invalid day of the month.
160 <!-- ##### ENUM GDateMonth ##### -->
162 Enumeration representing a month; values are #G_DATE_JANUARY,
163 #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
166 @G_DATE_BAD_MONTH: invalid value.
167 @G_DATE_JANUARY: January.
168 @G_DATE_FEBRUARY: February.
169 @G_DATE_MARCH: March.
170 @G_DATE_APRIL: April.
174 @G_DATE_AUGUST: August.
175 @G_DATE_SEPTEMBER: September.
176 @G_DATE_OCTOBER: October.
177 @G_DATE_NOVEMBER: November.
178 @G_DATE_DECEMBER: December.
180 <!-- ##### TYPEDEF GDateYear ##### -->
182 Integer representing a year; #G_DATE_BAD_YEAR is the invalid
183 value. The year must be 1 or higher; negative (BC) years are not
184 allowed. The year is represented with four digits.
188 <!-- ##### ENUM GDateWeekday ##### -->
190 Enumeration representing a day of the week; #G_DATE_MONDAY,
191 #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
194 @G_DATE_BAD_WEEKDAY: invalid value.
195 @G_DATE_MONDAY: Monday.
196 @G_DATE_TUESDAY: Tuesday.
197 @G_DATE_WEDNESDAY: Wednesday.
198 @G_DATE_THURSDAY: Thursday.
199 @G_DATE_FRIDAY: Friday.
200 @G_DATE_SATURDAY: Saturday.
201 @G_DATE_SUNDAY: Sunday.
203 <!-- ##### MACRO G_DATE_BAD_DAY ##### -->
205 Represents an invalid #GDateDay.
210 <!-- ##### MACRO G_DATE_BAD_JULIAN ##### -->
212 Represents an invalid Julian day number.
217 <!-- ##### MACRO G_DATE_BAD_YEAR ##### -->
219 Represents an invalid year.
224 <!-- ##### FUNCTION g_date_new ##### -->
226 Allocates a #GDate and initializes it to a sane state. The new date will
227 be cleared (as if you'd called g_date_clear()) but invalid (it won't
228 represent an existing day). Free the return value with g_date_free().
231 @Returns: a newly-allocated #GDate.
234 <!-- ##### FUNCTION g_date_new_dmy ##### -->
236 Like g_date_new(), but also sets the value of the date. Assuming the
237 day-month-year triplet you pass in represents an existing day, the
238 returned date will be valid.
241 @day: day of the month.
242 @month: month of the year.
244 @Returns: a newly-allocated #GDate initialized with @day, @month, and @year.
247 <!-- ##### FUNCTION g_date_new_julian ##### -->
249 Like g_date_new(), but also sets the value of the date. Assuming the
250 Julian day number you pass in is valid (greater than 0, less than an
251 unreasonably large number), the returned date will be valid.
254 @julian_day: days since January 1, Year 1.
255 @Returns: a newly-allocated #GDate initialized with @julian_day.
258 <!-- ##### FUNCTION g_date_clear ##### -->
260 Initializes one or more #GDate structs to a sane but invalid
261 state. The cleared dates will not represent an existing date, but will
262 not contain garbage. Useful to init a date declared on the stack.
263 Validity can be tested with g_date_valid().
266 @date: pointer to one or more dates to clear.
267 @n_dates: number of dates to clear.
270 <!-- ##### FUNCTION g_date_free ##### -->
272 Frees a #GDate returned from g_date_new().
278 <!-- ##### FUNCTION g_date_set_day ##### -->
280 Sets the day of the month for a #GDate. If the resulting day-month-year
281 triplet is invalid, the date will be invalid.
288 <!-- ##### FUNCTION g_date_set_month ##### -->
290 Sets the month of the year for a #GDate. If the resulting
291 day-month-year triplet is invalid, the date will be invalid.
295 @month: month to set.
298 <!-- ##### FUNCTION g_date_set_year ##### -->
300 Sets the year for a #GDate. If the resulting day-month-year triplet is
301 invalid, the date will be invalid.
308 <!-- ##### FUNCTION g_date_set_dmy ##### -->
310 Sets the value of a #GDate from a day, month, and year. The day-month-year
311 triplet must be valid; if you aren't sure it is, call g_date_valid_dmy() to
312 check before you set it.
321 <!-- ##### FUNCTION g_date_set_julian ##### -->
323 Sets the value of a #GDate from a Julian day number.
327 @julian_date: Julian day number (days since January 1, Year 1).
330 <!-- ##### FUNCTION g_date_set_time ##### -->
332 Sets the value of a date from a #GTime (<type>time_t</type>) value.
333 To set the value of a date to the current day, you could write:
334 <informalexample><programlisting>
335 g_date_set_time (date, time (NULL));
336 </programlisting></informalexample>
340 @time_: #GTime value to set.
343 <!-- ##### FUNCTION g_date_set_parse ##### -->
345 Parses a user-inputted string @str, and try to figure out what date it
346 represents, taking the current locale into account. If the string is
347 successfully parsed, the date will be valid after the call. Otherwise,
348 it will be invalid. You should check using g_date_valid() to see
349 whether the parsing succeeded.
353 This function is not appropriate for file formats and the like; it
354 isn't very precise, and its exact behavior varies with the
355 locale. It's intended to be a heuristic routine that guesses what the
356 user means by a given string (and it does work pretty well in that
360 @date: a #GDate to fill in.
361 @str: string to parse.
364 <!-- ##### FUNCTION g_date_add_days ##### -->
366 Increments a date some number of days. To move forward by weeks, add
367 weeks*7 days. The date must be valid.
370 @date: a #GDate to increment.
371 @n_days: number of days to move the date forward.
374 <!-- ##### FUNCTION g_date_subtract_days ##### -->
376 Moves a date some number of days into the past. To move by weeks, just
377 move by weeks*7 days. The date must be valid.
380 @date: a #GDate to decrement.
381 @n_days: number of days to move.
384 <!-- ##### FUNCTION g_date_add_months ##### -->
386 Increments a date by some number of months. If the day of the month is
387 greater than 28, this routine may change the day of the month (because
388 the destination month may not have the current day in it). The date
392 @date: a #GDate to increment.
393 @n_months: number of months to move forward.
396 <!-- ##### FUNCTION g_date_subtract_months ##### -->
398 Moves a date some number of months into the past. If the current day of
399 the month doesn't exist in the destination month, the day of the month
400 may change. The date must be valid.
403 @date: a #GDate to decrement.
404 @n_months: number of months to move.
407 <!-- ##### FUNCTION g_date_add_years ##### -->
409 Increments a date by some number of years. If the date is February 29,
410 and the destination year is not a leap year, the date will be changed
411 to February 28. The date must be valid.
414 @date: a #GDate to increment.
415 @n_years: number of years to move forward.
418 <!-- ##### FUNCTION g_date_subtract_years ##### -->
420 Moves a date some number of years into the past. If the current day
421 doesn't exist in the destination year (i.e. it's February 29 and you
422 move to a non-leap-year) then the day is changed to February 29. The date
426 @date: a #GDate to decrement.
427 @n_years: number of years to move.
430 <!-- ##### FUNCTION g_date_days_between ##### -->
432 Computes the number of days between two dates.
433 If @date2 is prior to @date1, the returned value is negative.
434 Both dates must be valid.
437 @date1: the first date.
438 @date2: the second date.
439 @Returns: the number of days between @date1 and @date2.
442 <!-- ##### FUNCTION g_date_compare ##### -->
444 <function>qsort()</function>-style comparsion function for dates. Both
448 @lhs: first date to compare.
449 @rhs: second date to compare.
450 @Returns: 0 for equal, less than zero if @lhs is less than @rhs,
451 greater than zero if @lhs is greater than @rhs.
454 <!-- ##### FUNCTION g_date_clamp ##### -->
456 If @date is prior to @min_date, sets @date equal to @min_date.
457 If @date falls after @max_date, sets @date equal to @max_date.
458 Either @min_date and @max_date may be %NULL. All non-%NULL dates
462 @date: a #GDate to clamp.
463 @min_date: minimum accepted value for @date.
464 @max_date: maximum accepted value for @date.
467 <!-- ##### FUNCTION g_date_order ##### -->
469 Checks if @date1 is less than or equal to @date2,
470 and swap the values if this is not the case.
473 @date1: the first date.
474 @date2: the second date.
477 <!-- ##### FUNCTION g_date_get_day ##### -->
479 Returns the day of the month. The date must be valid.
482 @date: a #GDate to extract the day of the month from.
483 @Returns: day of the month.
486 <!-- ##### FUNCTION g_date_get_month ##### -->
488 Returns the month of the year. The date must be valid.
491 @date: a #GDate to get the month from.
492 @Returns: month of the year as a #GDateMonth.
495 <!-- ##### FUNCTION g_date_get_year ##### -->
497 Returns the year of a #GDate. The date must be valid.
501 @Returns: year in which the date falls.
504 <!-- ##### FUNCTION g_date_get_julian ##### -->
506 Returns the Julian day or "serial number" of the #GDate. The
507 Julian day is simply the number of days since January 1, Year 1; i.e.,
508 January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
509 etc. The date must be valid.
512 @date: a #GDate to extract the Julian day from.
513 @Returns: Julian day.
516 <!-- ##### FUNCTION g_date_get_weekday ##### -->
518 Returns the day of the week for a #GDate. The date must be valid.
522 @Returns: day of the week as a #GDateWeekday.
525 <!-- ##### FUNCTION g_date_get_day_of_year ##### -->
527 Returns the day of the year, where Jan 1 is the first day of the
528 year. The date must be valid.
531 @date: a #GDate to extract day of year from.
532 @Returns: day of the year.
535 <!-- ##### FUNCTION g_date_get_days_in_month ##### -->
537 Returns the number of days in a month, taking leap years into account.
542 @Returns: number of days in @month during the @year.
545 <!-- ##### FUNCTION g_date_is_first_of_month ##### -->
547 Returns %TRUE if the date is on the first of a month. The date must be valid.
550 @date: a #GDate to check.
551 @Returns: %TRUE if the date is the first of the month.
554 <!-- ##### FUNCTION g_date_is_last_of_month ##### -->
556 Returns %TRUE if the date is the last day of the month. The date must be valid.
559 @date: a #GDate to check.
560 @Returns: %TRUE if the date is the last day of the month.
563 <!-- ##### FUNCTION g_date_is_leap_year ##### -->
565 Returns %TRUE if the year is a leap year.
568 @year: year to check.
569 @Returns: %TRUE if the year is a leap year.
572 <!-- ##### FUNCTION g_date_get_monday_week_of_year ##### -->
574 Returns the week of the year, where weeks are understood to start on
575 Monday. If the date is before the first Monday of the year, return
576 0. The date must be valid.
580 @Returns: week of the year.
583 <!-- ##### FUNCTION g_date_get_monday_weeks_in_year ##### -->
585 Returns the number of weeks in the year, where weeks are taken to start
586 on Monday. Will be 52 or 53. The date must be valid. (Years always have 52
587 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
588 year. This function is basically telling you how many Mondays are in
589 the year, i.e. there are 53 Mondays if one of the extra days happens
594 @Returns: number of Mondays in the year.
597 <!-- ##### FUNCTION g_date_get_sunday_week_of_year ##### -->
599 Returns the week of the year during which this date falls, if weeks
600 are understood to being on Sunday. The date must be valid. Can return 0 if
601 the day is before the first Sunday of the year.
605 @Returns: week number.
608 <!-- ##### FUNCTION g_date_get_sunday_weeks_in_year ##### -->
610 Returns the number of weeks in the year, where weeks are taken to start
611 on Sunday. Will be 52 or 53. The date must be valid. (Years always have 52
612 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
613 year. This function is basically telling you how many Sundays are in
614 the year, i.e. there are 53 Sundays if one of the extra days happens
618 @year: year to count weeks in.
619 @Returns: number of weeks.
622 <!-- ##### FUNCTION g_date_get_iso8601_week_of_year ##### -->
631 <!-- ##### FUNCTION g_date_strftime ##### -->
633 Generates a printed representation of the date, in a locale-specific
634 way. Works just like the standard C <function>strftime()</function>
635 function, but only accepts date-related formats; time-related formats
636 give undefined results. Date must be valid.
639 @s: destination buffer.
641 @format: format string.
643 @Returns: number of characters written to the buffer, or 0 the buffer was too small.
646 <!-- ##### FUNCTION g_date_to_struct_tm ##### -->
648 Fills in the date-related bits of a <structname>struct tm</structname>
649 using the @date value. Initializes the non-date parts with something
650 sane but meaningless.
653 @date: a #GDate to set the <structname>struct tm</structname> from.
654 @tm: <structname>struct tm</structname> to fill.
657 <!-- ##### FUNCTION g_date_valid ##### -->
659 Returns %TRUE if the #GDate represents an existing day. The date must not
660 contain garbage; it should have been initialized with g_date_clear()
661 if it wasn't allocated by one of the g_date_new() variants.
664 @date: a #GDate to check.
665 @Returns: Whether the date is valid.
668 <!-- ##### FUNCTION g_date_valid_day ##### -->
670 Returns %TRUE if the day of the month is valid (a day is valid if it's
671 between 1 and 31 inclusive).
675 @Returns: %TRUE if the day is valid.
678 <!-- ##### FUNCTION g_date_valid_month ##### -->
680 Returns %TRUE if the month value is valid. The 12 #GDateMonth
681 enumeration values are the only valid months.
685 @Returns: %TRUE if the month is valid.
688 <!-- ##### FUNCTION g_date_valid_year ##### -->
690 Returns %TRUE if the year is valid. Any year greater than 0 is valid,
691 though there is a 16-bit limit to what #GDate will understand.
695 @Returns: %TRUE if the year is valid.
698 <!-- ##### FUNCTION g_date_valid_dmy ##### -->
700 Returns %TRUE if the day-month-year triplet forms a valid, existing day
701 in the range of days #GDate understands (Year 1 or later, no more than
702 a few thousand years in the future).
708 @Returns: %TRUE if the date is a valid one.
711 <!-- ##### FUNCTION g_date_valid_julian ##### -->
713 Returns %TRUE if the Julian day is valid. Anything greater than zero is basically a
714 valid Julian, though there is a 32-bit limit.
717 @julian_date: Julian day to check.
718 @Returns: %TRUE if the Julian day is valid.
721 <!-- ##### FUNCTION g_date_valid_weekday ##### -->
723 Returns %TRUE if the weekday is valid. The 7 #GDateWeekday enumeration
724 values are the only valid weekdays.
728 @Returns: %TRUE if the weekday is valid.