Patch from Havoc Pennington to add functions for setting and getting a
[platform/upstream/glib.git] / docs / reference / glib / tmpl / date.sgml
1 <!-- ##### SECTION Title ##### -->
2 Date and Time Functions
3
4 <!-- ##### SECTION Short_Description ##### -->
5 calendrical calculations and miscellaneous time stuff.
6
7 <!-- ##### SECTION Long_Description ##### -->
8 <para>
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>.
18 </para>
19
20 <para>
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).
30 </para>
31
32 <para>
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.
46 </para>
47
48 <para>
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.
52 </para>
53
54 <para>
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().
59 </para>
60
61 <!-- ##### SECTION See_Also ##### -->
62 <para>
63
64 </para>
65
66 <!-- ##### MACRO G_USEC_PER_SEC ##### -->
67 <para>
68 Number of microseconds in one second (1 million). This macro is provided for
69 code readability.
70 </para>
71
72
73
74 <!-- ##### STRUCT GTimeVal ##### -->
75 <para>
76 Represents a precise time, with seconds and microseconds. Same as the
77 <structname>struct timeval</structname> returned by the
78 <function>gettimeofday()</function> UNIX call.
79 </para>
80
81 @tv_sec: seconds.
82 @tv_usec: microseconds.
83
84 <!-- ##### FUNCTION g_get_current_time ##### -->
85 <para>
86 Equivalent to <function>gettimeofday()</function>, but also works on
87 Win32. Returns the current time.
88 </para>
89
90 @result: 
91
92
93 <!-- ##### FUNCTION g_usleep ##### -->
94 <para>
95 Pauses the current thread for the given number of microseconds. There
96 are 1 million microseconds per second (represented by the
97 #G_USEC_PER_SEC macro). g_usleep() may have limited precision,
98 depending on hardware and operating system; don't rely on the exact
99 length of the sleep.
100 </para>
101
102 @microseconds: number of microseconds to pause.
103
104
105 <!-- ##### FUNCTION g_time_val_add ##### -->
106 <para>
107
108 </para>
109
110 @time: 
111 @microseconds: 
112
113
114 <!-- ##### STRUCT GDate ##### -->
115 <para>
116 Represents a day between January 1, Year 1 and a few thousand years in
117 the future. None of its members should be accessed directly. If the
118 <structname>GDate</structname> is obtained from g_date_new(), it will
119 be safe to mutate but invalid and thus not safe for calendrical computations.
120 If it's declared on the stack, it will contain garbage so must be
121 initialized with g_date_clear(). g_date_clear() makes the date invalid
122 but sane. An invalid date doesn't represent a day, it's "empty." A
123 date becomes valid after you set it to a Julian day or you set a day,
124 month, and year.
125 </para>
126
127 @julian_days: 
128 @julian: 
129 @dmy: 
130 @day: 
131 @month: 
132 @year: 
133
134 <!-- ##### TYPEDEF GTime ##### -->
135 <para>
136 Simply a replacement for <type>time_t</type>. Unrelated to #GTimer.
137 </para>
138
139
140 <!-- ##### ENUM GDateDMY ##### -->
141 <para>
142 This enumeration isn't used in the API, but may be useful if you need
143 to mark a number as a day, month, or year.
144 </para>
145
146 @G_DATE_DAY: 
147 @G_DATE_MONTH: 
148 @G_DATE_YEAR: 
149
150 <!-- ##### TYPEDEF GDateDay ##### -->
151 <para>
152 Integer representing a day of the month; between 1 and
153 31. #G_DATE_BAD_DAY represents an invalid day of the month.
154 </para>
155
156
157 <!-- ##### ENUM GDateMonth ##### -->
158 <para>
159 Enumeration representing a month; values are #G_DATE_JANUARY,
160 #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
161 </para>
162
163 @G_DATE_BAD_MONTH: invalid value.
164 @G_DATE_JANUARY: January.
165 @G_DATE_FEBRUARY: February.
166 @G_DATE_MARCH: March.
167 @G_DATE_APRIL: April.
168 @G_DATE_MAY: May.
169 @G_DATE_JUNE: June.
170 @G_DATE_JULY: July.
171 @G_DATE_AUGUST: August.
172 @G_DATE_SEPTEMBER: September.
173 @G_DATE_OCTOBER: October.
174 @G_DATE_NOVEMBER: November.
175 @G_DATE_DECEMBER: December.
176
177 <!-- ##### TYPEDEF GDateYear ##### -->
178 <para>
179 Integer representing a year; #G_DATE_BAD_YEAR is the invalid
180 value. The year must be 1 or higher; negative (BC) years are not
181 allowed. The year is represented with four digits.
182 </para>
183
184
185 <!-- ##### ENUM GDateWeekday ##### -->
186 <para>
187 Enumeration representing a day of the week; #G_DATE_MONDAY,
188 #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
189 </para>
190
191 @G_DATE_BAD_WEEKDAY: invalid value.
192 @G_DATE_MONDAY: Monday.
193 @G_DATE_TUESDAY: Tuesday.
194 @G_DATE_WEDNESDAY: Wednesday.
195 @G_DATE_THURSDAY: Thursday.
196 @G_DATE_FRIDAY: Friday.
197 @G_DATE_SATURDAY: Saturday.
198 @G_DATE_SUNDAY: Sunday.
199
200 <!-- ##### MACRO G_DATE_BAD_DAY ##### -->
201 <para>
202 Represents an invalid #GDateDay.
203 </para>
204
205
206
207 <!-- ##### MACRO G_DATE_BAD_JULIAN ##### -->
208 <para>
209 Represents an invalid Julian day number.
210 </para>
211
212
213
214 <!-- ##### MACRO G_DATE_BAD_YEAR ##### -->
215 <para>
216 Represents an invalid year.
217 </para>
218
219
220
221 <!-- ##### FUNCTION g_date_new ##### -->
222 <para>
223 Allocates a #GDate and initializes it to a sane state. The new date will
224 be cleared (as if you'd called g_date_clear()) but invalid (it won't
225 represent an existing day). Free the return value with g_date_free().
226 </para>
227
228 @Returns: a newly-allocated #GDate.
229
230
231 <!-- ##### FUNCTION g_date_new_dmy ##### -->
232 <para>
233 Like g_date_new(), but also sets the value of the date. Assuming the
234 day-month-year triplet you pass in represents an existing day, the
235 returned date will be valid.
236 </para>
237
238 @day: day of the month.
239 @month: month of the year.
240 @year: year
241 @Returns: a newly-allocated #GDate initialized with @day, @month, and @year.
242
243
244 <!-- ##### FUNCTION g_date_new_julian ##### -->
245 <para>
246 Like g_date_new(), but also sets the value of the date. Assuming the
247 Julian day number you pass in is valid (greater than 0, less than an
248 unreasonably large number), the returned date will be valid.
249 </para>
250
251 @julian_day: days since January 1, Year 1.
252 @Returns: a newly-allocated #GDate initialized with @julian_day.
253
254
255 <!-- ##### FUNCTION g_date_clear ##### -->
256 <para>
257 Initializes one or more #GDate structs to a sane but invalid
258 state. The cleared dates will not represent an existing date, but will
259 not contain garbage. Useful to init a date declared on the stack.
260 Validity can be tested with g_date_valid().
261 </para>
262
263 @date: pointer to one or more dates to clear.
264 @n_dates: number of dates to clear.
265
266
267 <!-- ##### FUNCTION g_date_free ##### -->
268 <para>
269 Frees a #GDate returned from g_date_new().
270 </para>
271
272 @date: a #GDate.
273
274
275 <!-- ##### FUNCTION g_date_set_day ##### -->
276 <para>
277 Sets the day of the month for a #GDate. If the resulting day-month-year
278 triplet is invalid, the date will be invalid.
279 </para>
280
281 @date: a #GDate.
282 @day: day to set.
283
284
285 <!-- ##### FUNCTION g_date_set_month ##### -->
286 <para>
287 Sets the month of the year for a #GDate.  If the resulting
288 day-month-year triplet is invalid, the date will be invalid.
289 </para>
290
291 @date: a #GDate.
292 @month: month to set.
293
294
295 <!-- ##### FUNCTION g_date_set_year ##### -->
296 <para>
297 Sets the year for a #GDate. If the resulting day-month-year triplet is
298 invalid, the date will be invalid.
299 </para>
300
301 @date: a #GDate.
302 @year: year to set.
303
304
305 <!-- ##### FUNCTION g_date_set_dmy ##### -->
306 <para>
307 Sets the value of a #GDate from a day, month, and year. The day-month-year 
308 triplet must be valid; if you aren't sure it is, call g_date_valid_dmy() to
309 check before you set it.
310 </para>
311
312 @date: a #GDate.
313 @day: day.
314 @month: month.
315 @y: year.
316
317
318 <!-- ##### FUNCTION g_date_set_julian ##### -->
319 <para>
320 Sets the value of a #GDate from a Julian day number.
321 </para>
322
323 @date: a #GDate.
324 @julian_date: Julian day number (days since January 1, Year 1).
325
326
327 <!-- ##### FUNCTION g_date_set_time ##### -->
328 <para>
329 Sets the value of a date from a #GTime (<type>time_t</type>) value. 
330 To set the value of a date to the current day, you could write:
331 <informalexample><programlisting>
332 g_date_set_time (date, time (NULL));
333 </programlisting></informalexample>
334 </para>
335
336 @date: a #GDate.
337 @time: #GTime value to set.
338
339
340 <!-- ##### FUNCTION g_date_set_parse ##### -->
341 <para>
342 Parses a user-inputted string @str, and try to figure out what date it
343 represents, taking the current locale into account. If the string is
344 successfully parsed, the date will be valid after the call. Otherwise,
345 it will be invalid. You should check using g_date_valid() to see
346 whether the parsing succeeded.
347 </para>
348
349 <para>
350 This function is not appropriate for file formats and the like; it
351 isn't very precise, and its exact behavior varies with the
352 locale. It's intended to be a heuristic routine that guesses what the
353 user means by a given string (and it does work pretty well in that
354 capacity).
355 </para>
356
357 @date: a #GDate to fill in.
358 @str: string to parse.
359
360
361 <!-- ##### FUNCTION g_date_add_days ##### -->
362 <para>
363 Increments a date some number of days. To move forward by weeks, add
364 weeks*7 days. The date must be valid.
365 </para>
366
367 @date: a #GDate to increment.
368 @n_days: number of days to move the date forward.
369
370
371 <!-- ##### FUNCTION g_date_subtract_days ##### -->
372 <para>
373 Moves a date some number of days into the past. To move by weeks, just
374 move by weeks*7 days. The date must be valid.
375 </para>
376
377 @date: a #GDate to decrement.
378 @n_days: number of days to move.
379
380
381 <!-- ##### FUNCTION g_date_add_months ##### -->
382 <para>
383 Increments a date by some number of months. If the day of the month is
384 greater than 28, this routine may change the day of the month (because
385 the destination month may not have the current day in it). The date
386 must be valid.
387 </para>
388
389 @date: a #GDate to increment.
390 @n_months: number of months to move forward.
391
392
393 <!-- ##### FUNCTION g_date_subtract_months ##### -->
394 <para>
395 Moves a date some number of months into the past. If the current day of
396 the month doesn't exist in the destination month, the day of the month
397 may change. The date must be valid.
398 </para>
399
400 @date: a #GDate to decrement.
401 @n_months: number of months to move.
402
403
404 <!-- ##### FUNCTION g_date_add_years ##### -->
405 <para>
406 Increments a date by some number of years. If the date is February 29,
407 and the destination year is not a leap year, the date will be changed
408 to February 28. The date must be valid.
409 </para>
410
411 @date: a #GDate to increment.
412 @n_years: number of years to move forward.
413
414
415 <!-- ##### FUNCTION g_date_subtract_years ##### -->
416 <para>
417 Moves a date some number of years into the past. If the current day
418 doesn't exist in the destination year (i.e. it's February 29 and you
419 move to a non-leap-year) then the day is changed to February 29. The date
420 must be valid.
421 </para>
422
423 @date: a #GDate to decrement.
424 @n_years: number of years to move.
425
426
427 <!-- ##### FUNCTION g_date_days_between ##### -->
428 <para>
429 Computes the number of days between two dates.
430 If @date2 is prior to @date1, the returned value is negative.
431 Both dates must be valid.
432 </para>
433
434 @date1: the first date.
435 @date2: the second date.
436 @Returns: the number of days between @date1 and @date2.
437
438
439 <!-- ##### FUNCTION g_date_compare ##### -->
440 <para>
441 <function>qsort()</function>-style comparsion function for dates. Both
442 dates must be valid.
443 </para>
444
445 @lhs: first date to compare.
446 @rhs: second date to compare.
447 @Returns: 0 for equal, less than zero if @lhs is less than @rhs,
448 greater than zero if @lhs is greater than @rhs.
449
450
451 <!-- ##### FUNCTION g_date_clamp ##### -->
452 <para>
453 If @date is prior to @min_date, sets @date equal to @min_date.
454 If @date falls after @max_date, sets @date equal to @max_date.
455 Either @min_date and @max_date may be %NULL.  All non-%NULL dates
456 must be valid.
457 </para>
458
459 @date: a #GDate to clamp.
460 @min_date: minimum accepted value for @date.
461 @max_date: maximum accepted value for @date.
462
463
464 <!-- ##### FUNCTION g_date_order ##### -->
465 <para>
466 Checks if @date1 is less than or equal to @date2,
467 and swap the values if this is not the case.
468 </para>
469
470 @date1: the first date.
471 @date2: the second date.
472
473
474 <!-- ##### FUNCTION g_date_get_day ##### -->
475 <para>
476 Returns the day of the month. The date must be valid.
477 </para>
478
479 @date: a #GDate to extract the day of the month from.
480 @Returns: day of the month.
481
482
483 <!-- ##### FUNCTION g_date_get_month ##### -->
484 <para>
485 Returns the month of the year. The date must be valid.
486 </para>
487
488 @date: a #GDate to get the month from.
489 @Returns: month of the year as a #GDateMonth.
490
491
492 <!-- ##### FUNCTION g_date_get_year ##### -->
493 <para>
494 Returns the year of a #GDate. The date must be valid.
495 </para>
496
497 @date: a #GDate.
498 @Returns: year in which the date falls.
499
500
501 <!-- ##### FUNCTION g_date_get_julian ##### -->
502 <para>
503 Returns the Julian day or "serial number" of the #GDate. The
504 Julian day is simply the number of days since January 1, Year 1; i.e.,
505 January 1, Year 1 is Julian day 1; January 2, Year 1 is Julian day 2,
506 etc. The date must be valid.
507 </para>
508
509 @date: a #GDate to extract the Julian day from.
510 @Returns: Julian day.
511
512
513 <!-- ##### FUNCTION g_date_get_weekday ##### -->
514 <para>
515 Returns the day of the week for a #GDate. The date must be valid.
516 </para>
517
518 @date: a #GDate.
519 @Returns: day of the week as a #GDateWeekday.
520
521
522 <!-- ##### FUNCTION g_date_get_day_of_year ##### -->
523 <para>
524 Returns the day of the year, where Jan 1 is the first day of the
525 year. The date must be valid.
526 </para>
527
528 @date: a #GDate to extract day of year from.
529 @Returns: day of the year.
530
531
532 <!-- ##### FUNCTION g_date_get_days_in_month ##### -->
533 <para>
534 Returns the number of days in a month, taking leap years into account.
535 </para>
536
537 @month: month.
538 @year: year.
539 @Returns: number of days in @month during the @year.
540
541
542 <!-- ##### FUNCTION g_date_is_first_of_month ##### -->
543 <para>
544 Returns %TRUE if the date is on the first of a month. The date must be valid.
545 </para>
546
547 @date: a #GDate to check.
548 @Returns: %TRUE if the date is the first of the month.
549
550
551 <!-- ##### FUNCTION g_date_is_last_of_month ##### -->
552 <para>
553 Returns %TRUE if the date is the last day of the month. The date must be valid.
554 </para>
555
556 @date: a #GDate to check.
557 @Returns: %TRUE if the date is the last day of the month.
558
559
560 <!-- ##### FUNCTION g_date_is_leap_year ##### -->
561 <para>
562 Returns %TRUE if the year is a leap year.
563 </para>
564
565 @year: year to check.
566 @Returns: %TRUE if the year is a leap year.
567
568
569 <!-- ##### FUNCTION g_date_get_monday_week_of_year ##### -->
570 <para>
571 Returns the week of the year, where weeks are understood to start on
572 Monday. If the date is before the first Monday of the year, return
573 0. The date must be valid.
574 </para>
575
576 @date: a #GDate.
577 @Returns: week of the year.
578
579
580 <!-- ##### FUNCTION g_date_get_monday_weeks_in_year ##### -->
581 <para>
582 Returns the number of weeks in the year, where weeks are taken to start
583 on Monday. Will be 52 or 53. The date must be valid. (Years always have 52
584 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
585 year. This function is basically telling you how many Mondays are in
586 the year, i.e. there are 53 Mondays if one of the extra days happens
587 to be a Monday.)
588 </para>
589
590 @year: a year.
591 @Returns: number of Mondays in the year.
592
593
594 <!-- ##### FUNCTION g_date_get_sunday_week_of_year ##### -->
595 <para>
596 Returns the week of the year during which this date falls, if weeks 
597 are understood to being on Sunday. The date must be valid. Can return 0 if 
598 the day is before the first Sunday of the year.
599 </para>
600
601 @date: a #GDate.
602 @Returns: week number.
603
604
605 <!-- ##### FUNCTION g_date_get_sunday_weeks_in_year ##### -->
606 <para>
607 Returns the number of weeks in the year, where weeks are taken to start
608 on Sunday. Will be 52 or 53. The date must be valid. (Years always have 52
609 7-day periods, plus 1 or 2 extra days depending on whether it's a leap
610 year. This function is basically telling you how many Sundays are in
611 the year, i.e. there are 53 Sundays if one of the extra days happens
612 to be a Sunday.)
613 </para>
614
615 @year: year to count weeks in.
616 @Returns: number of weeks.
617
618
619 <!-- ##### FUNCTION g_date_strftime ##### -->
620 <para>
621 Generates a printed representation of the date, in a locale-specific
622 way. Works just like the standard C <function>strftime()</function>
623 function, but only accepts date-related formats; time-related formats
624 give undefined results. Date must be valid.
625 </para>
626
627 @s: destination buffer.
628 @slen: buffer size.
629 @format: format string.
630 @date: valid #GDate.
631 @Returns: number of characters written to the buffer, or 0 the buffer was too small.
632
633
634 <!-- ##### FUNCTION g_date_to_struct_tm ##### -->
635 <para>
636 Fills in the date-related bits of a <structname>struct tm</structname>
637 using the @date value. Initializes the non-date parts with something
638 sane but meaningless.
639 </para>
640
641 @date: a #GDate to set the <structname>struct tm</structname> from.
642 @tm: <structname>struct tm</structname> to fill.
643
644
645 <!-- ##### FUNCTION g_date_valid ##### -->
646 <para>
647 Returns %TRUE if the #GDate represents an existing day. The date must not
648 contain garbage; it should have been initialized with g_date_clear()
649 if it wasn't allocated by one of the g_date_new() variants.
650 </para>
651
652 @date: a #GDate to check.
653 @Returns: Whether the date is valid.
654
655
656 <!-- ##### FUNCTION g_date_valid_day ##### -->
657 <para>
658 Returns %TRUE if the day of the month is valid (a day is valid if it's
659 between 1 and 31 inclusive).
660 </para>
661
662 @day: day to check.
663 @Returns: %TRUE if the day is valid.
664
665
666 <!-- ##### FUNCTION g_date_valid_month ##### -->
667 <para>
668 Returns %TRUE if the month value is valid. The 12 #GDateMonth
669 enumeration values are the only valid months.
670 </para>
671
672 @month: month.
673 @Returns: %TRUE if the month is valid.
674
675
676 <!-- ##### FUNCTION g_date_valid_year ##### -->
677 <para>
678 Returns %TRUE if the year is valid. Any year greater than 0 is valid,
679 though there is a 16-bit limit to what #GDate will understand.
680 </para>
681
682 @year: year.
683 @Returns: %TRUE if the year is valid.
684
685
686 <!-- ##### FUNCTION g_date_valid_dmy ##### -->
687 <para>
688 Returns %TRUE if the day-month-year triplet forms a valid, existing day
689 in the range of days #GDate understands (Year 1 or later, no more than
690 a few thousand years in the future).
691 </para>
692
693 @day: day.
694 @month: month.
695 @year: year.
696 @Returns: %TRUE if the date is a valid one.
697
698
699 <!-- ##### FUNCTION g_date_valid_julian ##### -->
700 <para>
701 Returns %TRUE if the Julian day is valid. Anything greater than zero is basically a
702 valid Julian, though there is a 32-bit limit.
703 </para>
704
705 @julian_date: Julian day to check.
706 @Returns: %TRUE if the Julian day is valid.
707
708
709 <!-- ##### FUNCTION g_date_valid_weekday ##### -->
710 <para>
711 Returns %TRUE if the weekday is valid. The 7 #GDateWeekday enumeration
712 values are the only valid weekdays.
713 </para>
714
715 @weekday: weekday.
716 @Returns: %TRUE if the weekday is valid.
717
718