ab83317beff2ddbce39c3449d4c7fcf6697e38d0
[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 <!-- ##### SECTION Stability_Level ##### -->
67
68
69 <!-- ##### MACRO G_USEC_PER_SEC ##### -->
70 <para>
71 Number of microseconds in one second (1 million). This macro is provided for
72 code readability.
73 </para>
74
75
76
77 <!-- ##### STRUCT GTimeVal ##### -->
78 <para>
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.
82 </para>
83
84 @tv_sec: seconds.
85 @tv_usec: microseconds.
86
87 <!-- ##### FUNCTION g_get_current_time ##### -->
88 <para>
89 </para>
90
91 @result: 
92
93
94 <!-- ##### FUNCTION g_usleep ##### -->
95 <para>
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
100 length of the sleep.
101 </para>
102
103 @microseconds: number of microseconds to pause.
104
105
106 <!-- ##### FUNCTION g_time_val_add ##### -->
107 <para>
108
109 </para>
110
111 @time_: 
112 @microseconds: 
113
114
115 <!-- ##### STRUCT GDate ##### -->
116 <para>
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,
125 month, and year.
126 </para>
127
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
136
137 <!-- ##### TYPEDEF GTime ##### -->
138 <para>
139 Simply a replacement for <type>time_t</type>. Unrelated to #GTimer.
140 </para>
141
142
143 <!-- ##### ENUM GDateDMY ##### -->
144 <para>
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.
147 </para>
148
149 @G_DATE_DAY: a day
150 @G_DATE_MONTH: a month
151 @G_DATE_YEAR: a year
152
153 <!-- ##### TYPEDEF GDateDay ##### -->
154 <para>
155 Integer representing a day of the month; between 1 and
156 31. #G_DATE_BAD_DAY represents an invalid day of the month.
157 </para>
158
159
160 <!-- ##### ENUM GDateMonth ##### -->
161 <para>
162 Enumeration representing a month; values are #G_DATE_JANUARY,
163 #G_DATE_FEBRUARY, etc. #G_DATE_BAD_MONTH is the invalid value.
164 </para>
165
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.
171 @G_DATE_MAY: May.
172 @G_DATE_JUNE: June.
173 @G_DATE_JULY: July.
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.
179
180 <!-- ##### TYPEDEF GDateYear ##### -->
181 <para>
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.
185 </para>
186
187
188 <!-- ##### ENUM GDateWeekday ##### -->
189 <para>
190 Enumeration representing a day of the week; #G_DATE_MONDAY,
191 #G_DATE_TUESDAY, etc. #G_DATE_BAD_WEEKDAY is an invalid weekday.
192 </para>
193
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.
202
203 <!-- ##### MACRO G_DATE_BAD_DAY ##### -->
204 <para>
205 Represents an invalid #GDateDay.
206 </para>
207
208
209
210 <!-- ##### MACRO G_DATE_BAD_JULIAN ##### -->
211 <para>
212 Represents an invalid Julian day number.
213 </para>
214
215
216
217 <!-- ##### MACRO G_DATE_BAD_YEAR ##### -->
218 <para>
219 Represents an invalid year.
220 </para>
221
222
223
224 <!-- ##### FUNCTION g_date_new ##### -->
225 <para>
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().
229 </para>
230
231 @Returns: a newly-allocated #GDate.
232
233
234 <!-- ##### FUNCTION g_date_new_dmy ##### -->
235 <para>
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.
239 </para>
240
241 @day: day of the month.
242 @month: month of the year.
243 @year: year
244 @Returns: a newly-allocated #GDate initialized with @day, @month, and @year.
245
246
247 <!-- ##### FUNCTION g_date_new_julian ##### -->
248 <para>
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.
252 </para>
253
254 @julian_day: days since January 1, Year 1.
255 @Returns: a newly-allocated #GDate initialized with @julian_day.
256
257
258 <!-- ##### FUNCTION g_date_clear ##### -->
259 <para>
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().
264 </para>
265
266 @date: pointer to one or more dates to clear.
267 @n_dates: number of dates to clear.
268
269
270 <!-- ##### FUNCTION g_date_free ##### -->
271 <para>
272 Frees a #GDate returned from g_date_new().
273 </para>
274
275 @date: a #GDate.
276
277
278 <!-- ##### FUNCTION g_date_set_day ##### -->
279 <para>
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.
282 </para>
283
284 @date: a #GDate.
285 @day: day to set.
286
287
288 <!-- ##### FUNCTION g_date_set_month ##### -->
289 <para>
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.
292 </para>
293
294 @date: a #GDate.
295 @month: month to set.
296
297
298 <!-- ##### FUNCTION g_date_set_year ##### -->
299 <para>
300 Sets the year for a #GDate. If the resulting day-month-year triplet is
301 invalid, the date will be invalid.
302 </para>
303
304 @date: a #GDate.
305 @year: year to set.
306
307
308 <!-- ##### FUNCTION g_date_set_dmy ##### -->
309 <para>
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.
313 </para>
314
315 @date: a #GDate.
316 @day: day.
317 @month: month.
318 @y: year.
319
320
321 <!-- ##### FUNCTION g_date_set_julian ##### -->
322 <para>
323 Sets the value of a #GDate from a Julian day number.
324 </para>
325
326 @date: a #GDate.
327 @julian_date: Julian day number (days since January 1, Year 1).
328
329
330 <!-- ##### FUNCTION g_date_set_time ##### -->
331 <para>
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>
337 </para>
338
339 @date: a #GDate.
340 @time_: #GTime value to set.
341
342
343 <!-- ##### FUNCTION g_date_set_parse ##### -->
344 <para>
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.
350 </para>
351
352 <para>
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
357 capacity).
358 </para>
359
360 @date: a #GDate to fill in.
361 @str: string to parse.
362
363
364 <!-- ##### FUNCTION g_date_add_days ##### -->
365 <para>
366 Increments a date some number of days. To move forward by weeks, add
367 weeks*7 days. The date must be valid.
368 </para>
369
370 @date: a #GDate to increment.
371 @n_days: number of days to move the date forward.
372
373
374 <!-- ##### FUNCTION g_date_subtract_days ##### -->
375 <para>
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.
378 </para>
379
380 @date: a #GDate to decrement.
381 @n_days: number of days to move.
382
383
384 <!-- ##### FUNCTION g_date_add_months ##### -->
385 <para>
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
389 must be valid.
390 </para>
391
392 @date: a #GDate to increment.
393 @n_months: number of months to move forward.
394
395
396 <!-- ##### FUNCTION g_date_subtract_months ##### -->
397 <para>
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.
401 </para>
402
403 @date: a #GDate to decrement.
404 @n_months: number of months to move.
405
406
407 <!-- ##### FUNCTION g_date_add_years ##### -->
408 <para>
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.
412 </para>
413
414 @date: a #GDate to increment.
415 @n_years: number of years to move forward.
416
417
418 <!-- ##### FUNCTION g_date_subtract_years ##### -->
419 <para>
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
423 must be valid.
424 </para>
425
426 @date: a #GDate to decrement.
427 @n_years: number of years to move.
428
429
430 <!-- ##### FUNCTION g_date_days_between ##### -->
431 <para>
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.
435 </para>
436
437 @date1: the first date.
438 @date2: the second date.
439 @Returns: the number of days between @date1 and @date2.
440
441
442 <!-- ##### FUNCTION g_date_compare ##### -->
443 <para>
444 <function>qsort()</function>-style comparsion function for dates. Both
445 dates must be valid.
446 </para>
447
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.
452
453
454 <!-- ##### FUNCTION g_date_clamp ##### -->
455 <para>
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
459 must be valid.
460 </para>
461
462 @date: a #GDate to clamp.
463 @min_date: minimum accepted value for @date.
464 @max_date: maximum accepted value for @date.
465
466
467 <!-- ##### FUNCTION g_date_order ##### -->
468 <para>
469 Checks if @date1 is less than or equal to @date2,
470 and swap the values if this is not the case.
471 </para>
472
473 @date1: the first date.
474 @date2: the second date.
475
476
477 <!-- ##### FUNCTION g_date_get_day ##### -->
478 <para>
479 Returns the day of the month. The date must be valid.
480 </para>
481
482 @date: a #GDate to extract the day of the month from.
483 @Returns: day of the month.
484
485
486 <!-- ##### FUNCTION g_date_get_month ##### -->
487 <para>
488 Returns the month of the year. The date must be valid.
489 </para>
490
491 @date: a #GDate to get the month from.
492 @Returns: month of the year as a #GDateMonth.
493
494
495 <!-- ##### FUNCTION g_date_get_year ##### -->
496 <para>
497 Returns the year of a #GDate. The date must be valid.
498 </para>
499
500 @date: a #GDate.
501 @Returns: year in which the date falls.
502
503
504 <!-- ##### FUNCTION g_date_get_julian ##### -->
505 <para>
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.
510 </para>
511
512 @date: a #GDate to extract the Julian day from.
513 @Returns: Julian day.
514
515
516 <!-- ##### FUNCTION g_date_get_weekday ##### -->
517 <para>
518 Returns the day of the week for a #GDate. The date must be valid.
519 </para>
520
521 @date: a #GDate.
522 @Returns: day of the week as a #GDateWeekday.
523
524
525 <!-- ##### FUNCTION g_date_get_day_of_year ##### -->
526 <para>
527 Returns the day of the year, where Jan 1 is the first day of the
528 year. The date must be valid.
529 </para>
530
531 @date: a #GDate to extract day of year from.
532 @Returns: day of the year.
533
534
535 <!-- ##### FUNCTION g_date_get_days_in_month ##### -->
536 <para>
537 Returns the number of days in a month, taking leap years into account.
538 </para>
539
540 @month: month.
541 @year: year.
542 @Returns: number of days in @month during the @year.
543
544
545 <!-- ##### FUNCTION g_date_is_first_of_month ##### -->
546 <para>
547 Returns %TRUE if the date is on the first of a month. The date must be valid.
548 </para>
549
550 @date: a #GDate to check.
551 @Returns: %TRUE if the date is the first of the month.
552
553
554 <!-- ##### FUNCTION g_date_is_last_of_month ##### -->
555 <para>
556 Returns %TRUE if the date is the last day of the month. The date must be valid.
557 </para>
558
559 @date: a #GDate to check.
560 @Returns: %TRUE if the date is the last day of the month.
561
562
563 <!-- ##### FUNCTION g_date_is_leap_year ##### -->
564 <para>
565 Returns %TRUE if the year is a leap year.
566 </para>
567
568 @year: year to check.
569 @Returns: %TRUE if the year is a leap year.
570
571
572 <!-- ##### FUNCTION g_date_get_monday_week_of_year ##### -->
573 <para>
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.
577 </para>
578
579 @date: a #GDate.
580 @Returns: week of the year.
581
582
583 <!-- ##### FUNCTION g_date_get_monday_weeks_in_year ##### -->
584 <para>
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
590 to be a Monday.)
591 </para>
592
593 @year: a year.
594 @Returns: number of Mondays in the year.
595
596
597 <!-- ##### FUNCTION g_date_get_sunday_week_of_year ##### -->
598 <para>
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.
602 </para>
603
604 @date: a #GDate.
605 @Returns: week number.
606
607
608 <!-- ##### FUNCTION g_date_get_sunday_weeks_in_year ##### -->
609 <para>
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
615 to be a Sunday.)
616 </para>
617
618 @year: year to count weeks in.
619 @Returns: number of weeks.
620
621
622 <!-- ##### FUNCTION g_date_get_iso8601_week_of_year ##### -->
623 <para>
624
625 </para>
626
627 @date: 
628 @Returns: 
629
630
631 <!-- ##### FUNCTION g_date_strftime ##### -->
632 <para>
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.
637 </para>
638
639 @s: destination buffer.
640 @slen: buffer size.
641 @format: format string.
642 @date: valid #GDate.
643 @Returns: number of characters written to the buffer, or 0 the buffer was too small.
644
645
646 <!-- ##### FUNCTION g_date_to_struct_tm ##### -->
647 <para>
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.
651 </para>
652
653 @date: a #GDate to set the <structname>struct tm</structname> from.
654 @tm: <structname>struct tm</structname> to fill.
655
656
657 <!-- ##### FUNCTION g_date_valid ##### -->
658 <para>
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.
662 </para>
663
664 @date: a #GDate to check.
665 @Returns: Whether the date is valid.
666
667
668 <!-- ##### FUNCTION g_date_valid_day ##### -->
669 <para>
670 Returns %TRUE if the day of the month is valid (a day is valid if it's
671 between 1 and 31 inclusive).
672 </para>
673
674 @day: day to check.
675 @Returns: %TRUE if the day is valid.
676
677
678 <!-- ##### FUNCTION g_date_valid_month ##### -->
679 <para>
680 Returns %TRUE if the month value is valid. The 12 #GDateMonth
681 enumeration values are the only valid months.
682 </para>
683
684 @month: month.
685 @Returns: %TRUE if the month is valid.
686
687
688 <!-- ##### FUNCTION g_date_valid_year ##### -->
689 <para>
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.
692 </para>
693
694 @year: year.
695 @Returns: %TRUE if the year is valid.
696
697
698 <!-- ##### FUNCTION g_date_valid_dmy ##### -->
699 <para>
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).
703 </para>
704
705 @day: day.
706 @month: month.
707 @year: year.
708 @Returns: %TRUE if the date is a valid one.
709
710
711 <!-- ##### FUNCTION g_date_valid_julian ##### -->
712 <para>
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.
715 </para>
716
717 @julian_date: Julian day to check.
718 @Returns: %TRUE if the Julian day is valid.
719
720
721 <!-- ##### FUNCTION g_date_valid_weekday ##### -->
722 <para>
723 Returns %TRUE if the weekday is valid. The 7 #GDateWeekday enumeration
724 values are the only valid weekdays.
725 </para>
726
727 @weekday: weekday.
728 @Returns: %TRUE if the weekday is valid.
729
730