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