source: trunk/third/glib2/glib/gdate.h @ 18159

Revision 18159, 10.0 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18158, which included commits to RCS files with non-trunk default branches.
Line 
1/* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20/*
21 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22 * file for a list of people on the GLib Team.  See the ChangeLog
23 * files for a list of changes.  These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27#ifndef __G_DATE_H__
28#define __G_DATE_H__
29
30#include <glib/gquark.h>
31
32G_BEGIN_DECLS
33
34/* GDate
35 *
36 * Date calculations (not time for now, to be resolved). These are a
37 * mutant combination of Steffen Beyer's DateCalc routines
38 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
39 * date routines (written for in-house software).  Written by Havoc
40 * Pennington <hp@pobox.com>
41 */
42
43typedef gint32  GTime;
44typedef guint16 GDateYear;
45typedef guint8  GDateDay;   /* day of the month */
46typedef struct _GDate GDate;
47/* make struct tm known without having to include time.h */
48struct tm;
49
50/* enum used to specify order of appearance in parsed date strings */
51typedef enum
52{
53  G_DATE_DAY   = 0,
54  G_DATE_MONTH = 1,
55  G_DATE_YEAR  = 2
56} GDateDMY;
57
58/* actual week and month values */
59typedef enum
60{
61  G_DATE_BAD_WEEKDAY  = 0,
62  G_DATE_MONDAY       = 1,
63  G_DATE_TUESDAY      = 2,
64  G_DATE_WEDNESDAY    = 3,
65  G_DATE_THURSDAY     = 4,
66  G_DATE_FRIDAY       = 5,
67  G_DATE_SATURDAY     = 6,
68  G_DATE_SUNDAY       = 7
69} GDateWeekday;
70typedef enum
71{
72  G_DATE_BAD_MONTH = 0,
73  G_DATE_JANUARY   = 1,
74  G_DATE_FEBRUARY  = 2,
75  G_DATE_MARCH     = 3,
76  G_DATE_APRIL     = 4,
77  G_DATE_MAY       = 5,
78  G_DATE_JUNE      = 6,
79  G_DATE_JULY      = 7,
80  G_DATE_AUGUST    = 8,
81  G_DATE_SEPTEMBER = 9,
82  G_DATE_OCTOBER   = 10,
83  G_DATE_NOVEMBER  = 11,
84  G_DATE_DECEMBER  = 12
85} GDateMonth;
86
87#define G_DATE_BAD_JULIAN 0U
88#define G_DATE_BAD_DAY    0U
89#define G_DATE_BAD_YEAR   0U
90
91/* Note: directly manipulating structs is generally a bad idea, but
92 * in this case it's an *incredibly* bad idea, because all or part
93 * of this struct can be invalid at any given time. Use the functions,
94 * or you will get hosed, I promise.
95 */
96struct _GDate
97{
98  guint julian_days : 32; /* julian days representation - we use a
99                           *  bitfield hoping that 64 bit platforms
100                           *  will pack this whole struct in one big
101                           *  int
102                           */
103
104  guint julian : 1;    /* julian is valid */
105  guint dmy    : 1;    /* dmy is valid */
106
107  /* DMY representation */
108  guint day    : 6;
109  guint month  : 4;
110  guint year   : 16;
111};
112
113/* g_date_new() returns an invalid date, you then have to _set() stuff
114 * to get a usable object. You can also allocate a GDate statically,
115 * then call g_date_clear() to initialize.
116 */
117GDate*       g_date_new                   (void);
118GDate*       g_date_new_dmy               (GDateDay     day,
119                                           GDateMonth   month,
120                                           GDateYear    year);
121GDate*       g_date_new_julian            (guint32      julian_day);
122void         g_date_free                  (GDate       *date);
123
124/* check g_date_valid() after doing an operation that might fail, like
125 * _parse.  Almost all g_date operations are undefined on invalid
126 * dates (the exceptions are the mutators, since you need those to
127 * return to validity).
128 */
129gboolean     g_date_valid                 (const GDate *date);
130gboolean     g_date_valid_day             (GDateDay     day) G_GNUC_CONST;
131gboolean     g_date_valid_month           (GDateMonth month) G_GNUC_CONST;
132gboolean     g_date_valid_year            (GDateYear  year) G_GNUC_CONST;
133gboolean     g_date_valid_weekday         (GDateWeekday weekday) G_GNUC_CONST;
134gboolean     g_date_valid_julian          (guint32 julian_date) G_GNUC_CONST;
135gboolean     g_date_valid_dmy             (GDateDay     day,
136                                           GDateMonth   month,
137                                           GDateYear    year) G_GNUC_CONST;
138
139GDateWeekday g_date_get_weekday           (const GDate *date);
140GDateMonth   g_date_get_month             (const GDate *date);
141GDateYear    g_date_get_year              (const GDate *date);
142GDateDay     g_date_get_day               (const GDate *date);
143guint32      g_date_get_julian            (const GDate *date);
144guint        g_date_get_day_of_year       (const GDate *date);
145/* First monday/sunday is the start of week 1; if we haven't reached
146 * that day, return 0. These are not ISO weeks of the year; that
147 * routine needs to be added.
148 * these functions return the number of weeks, starting on the
149 * corrsponding day
150 */
151guint        g_date_get_monday_week_of_year (const GDate *date);
152guint        g_date_get_sunday_week_of_year (const GDate *date);
153
154/* If you create a static date struct you need to clear it to get it
155 * in a sane state before use. You can clear a whole array at
156 * once with the ndates argument.
157 */
158void         g_date_clear                 (GDate       *date,
159                                           guint        n_dates);
160
161/* The parse routine is meant for dates typed in by a user, so it
162 * permits many formats but tries to catch common typos. If your data
163 * needs to be strictly validated, it is not an appropriate function.
164 */
165void         g_date_set_parse             (GDate       *date,
166                                           const gchar *str);
167void         g_date_set_time              (GDate       *date,
168                                           GTime        time_);
169void         g_date_set_month             (GDate       *date,
170                                           GDateMonth   month);
171void         g_date_set_day               (GDate       *date,
172                                           GDateDay     day);
173void         g_date_set_year              (GDate       *date,
174                                           GDateYear    year);
175void         g_date_set_dmy               (GDate       *date,
176                                           GDateDay     day,
177                                           GDateMonth   month,
178                                           GDateYear    y);
179void         g_date_set_julian            (GDate       *date,
180                                           guint32      julian_date);
181gboolean     g_date_is_first_of_month     (const GDate *date);
182gboolean     g_date_is_last_of_month      (const GDate *date);
183
184/* To go forward by some number of weeks just go forward weeks*7 days */
185void         g_date_add_days              (GDate       *date,
186                                           guint        n_days);
187void         g_date_subtract_days         (GDate       *date,
188                                           guint        n_days);
189
190/* If you add/sub months while day > 28, the day might change */
191void         g_date_add_months            (GDate       *date,
192                                           guint        n_months);
193void         g_date_subtract_months       (GDate       *date,
194                                           guint        n_months);
195
196/* If it's feb 29, changing years can move you to the 28th */
197void         g_date_add_years             (GDate       *date,
198                                           guint        n_years);
199void         g_date_subtract_years        (GDate       *date,
200                                           guint        n_years);
201gboolean     g_date_is_leap_year          (GDateYear    year) G_GNUC_CONST;
202guint8       g_date_get_days_in_month     (GDateMonth   month,
203                                           GDateYear    year) G_GNUC_CONST;
204guint8       g_date_get_monday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
205guint8       g_date_get_sunday_weeks_in_year  (GDateYear    year) G_GNUC_CONST;
206
207/* Returns the number of days between the two dates.  If date2 comes
208   before date1, a negative value is return. */
209gint         g_date_days_between          (const GDate *date1,
210                                           const GDate *date2);
211
212/* qsort-friendly (with a cast...) */
213gint         g_date_compare               (const GDate *lhs,
214                                           const GDate *rhs);
215void         g_date_to_struct_tm          (const GDate *date,
216                                           struct tm   *tm);
217
218void         g_date_clamp                 (GDate *date,
219                                           const GDate *min_date,
220                                           const GDate *max_date);
221
222/* Swap date1 and date2's values if date1 > date2. */
223void         g_date_order                 (GDate *date1, GDate *date2);
224
225/* Just like strftime() except you can only use date-related formats.
226 *   Using a time format is undefined.
227 */
228gsize        g_date_strftime              (gchar       *s,
229                                           gsize        slen,
230                                           const gchar *format,
231                                           const GDate *date);
232
233#ifndef G_DISABLE_DEPRECATED
234
235#define g_date_weekday                  g_date_get_weekday
236#define g_date_month                    g_date_get_month
237#define g_date_year                     g_date_get_year
238#define g_date_day                      g_date_get_day
239#define g_date_julian                   g_date_get_julian
240#define g_date_day_of_year              g_date_get_day_of_year
241#define g_date_monday_week_of_year      g_date_get_monday_week_of_year
242#define g_date_sunday_week_of_year      g_date_get_sunday_week_of_year
243#define g_date_days_in_month            g_date_get_days_in_month
244#define g_date_monday_weeks_in_year     g_date_get_monday_weeks_in_year
245#define g_date_sunday_weeks_in_year     g_date_get_sunday_weeks_in_year
246
247#endif /* G_DISABLE_DEPRECATED */
248
249G_END_DECLS
250
251#endif /* __G_DATE_H__ */
252
Note: See TracBrowser for help on using the repository browser.