QOF: test-date.c Source File

QOF 0.7.5

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

test-date.c
1 /***************************************************************************
2 * test-date.c
3 * Rewritten from scratch for QOF 0.7.0
4 *
5 * Copyright (C) 2002, 2004, 2005, 2006
6 * Free Software Foundation, Inc.
7 *
8 ****************************************************************************/
9 /*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
23 */
24
25 #include "config.h"
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <glib.h>
29 #include <time.h>
30 #include "qof.h"
31 #include "test-stuff.h"
32
33 static gboolean test_data_is_init = FALSE;
34 static GList *test_data = NULL;
35 static QofLogModule log_module = QOF_MOD_DATE;
36
37 typedef struct
38 {
39 /* time value of the date below */
40 QofTime *time;
41 /* date value of the above time. */
42 QofDate *date;
43 /* GList of the date in each QofDateFormat */
44 GList *string_list;
45 /* unique ID for this date for error reports. */
46 const gchar *id;
47 }QTestDate;
48
49 static void
50 check_date_cycles (gpointer data,
51 gpointer user_data __attribute__ ((unused)))
52 {
53 QofDateFormat df;
54 gchar *str, *cmp;
55 QTestDate *d;
56 gint i;
57
58 d = (QTestDate*)data;
59 df = qof_date_format_get_current ();
60 /* test qof_date_to_qtime and qof_date_from_qtime */
61 {
62 do_test ((0 == qof_time_cmp (qof_date_to_qtime (d->date),
63 d->time)), d->id);
64 do_test ((0 == qof_date_compare (qof_date_from_qtime (d->time),
65 d->date)), d->id);
66 }
67 /* don't test locale-sensitive formats, yet. */
68 for (i = 1; i <= 6; i++)
69 {
70 str = qof_date_print (d->date, i);
71 cmp = (gchar*)g_list_nth_data (d->string_list, (i - 1));
72 if (0 != safe_strcasecmp (str, cmp))
73 DEBUG (" str=%s cmp=%s", str, cmp);
74 do_test ((0 == safe_strcasecmp (str, cmp)), d->id);
75 /* now test qofstrptime */
76 {
77 gint result;
78 QofDate *h, *j;
79 const gchar * t;
80 struct tm base;
81
82 base.tm_year = base.tm_mday = base.tm_mon = 0;
83 base.tm_min = base.tm_hour = base.tm_gmtoff = 0;
84 base.tm_sec = base.tm_isdst = 0;
85 result = 0;
87 if ((d->date->qd_year > 0) &&
88 (d->date->qd_year < 9999))
89 {
90 h = qof_date_parse (cmp, i);
91 do_test ((h != NULL), "no date could be parsed");
92 if (!h)
93 PERR (" h failed for str=%s, "
94 "cmp=%s, %d\n", str, cmp, i);
95 t = qof_date_format_get_format (i);
96 strptime (cmp, t, &base);
97 j = qof_date_from_struct_tm (&base);
98 do_test ((j != NULL), "no value from struct tm");
99 if (h)
100 {
101 j->qd_nanosecs = h->qd_nanosecs;
102 result = qof_date_compare (h, j);
103 do_test ((0 == qof_date_compare (h, j)),
104 "compare with strptime");
105 }
106 if (h)
107 qof_date_free (h);
108 }
109 }
110 }
111 qof_date_format_set_current (df);
112 }
113
114 static void
115 test_date_init (void)
116 {
117 test_data_is_init = TRUE;
118 test_data = NULL;
119 /* A selection of current, old and future dates - all in UTC only. */
120 {
121 QTestDate *td = g_new0 (QTestDate, 1);
122 td->time = qof_time_new ();
123 qof_time_set_secs (td->time, 1147621550);
124 qof_time_set_nanosecs (td->time, G_GINT64_CONSTANT(1000));
125 td->date = qof_date_new ();
126 td->date->qd_year = 2006;
127 td->date->qd_mon = 5;
128 td->date->qd_mday = 14;
129 td->date->qd_hour = 15;
130 td->date->qd_min = 45;
131 td->date->qd_sec = G_GINT64_CONSTANT(50);
132 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
133 qof_date_valid (td->date);
134 td->string_list = NULL;
135 td->string_list = g_list_append (td->string_list, "05/14/2006");
136 td->string_list = g_list_append (td->string_list, "14/05/2006");
137 td->string_list = g_list_append (td->string_list, "14.05.2006");
138 td->string_list = g_list_append (td->string_list, "2006-05-14");
139 td->string_list = g_list_append (td->string_list, "2006-05-14T15:45:50Z");
140 td->string_list = g_list_append (td->string_list,
141 "2006-05-14 15:45:50.000001000 +0000");
142 td->id = "a current time";
143 test_data = g_list_prepend (test_data, td);
144 }
145 {
146 QTestDate *td = g_new0 (QTestDate, 1);
147 td->time = qof_time_new ();
148 qof_time_set_secs (td->time, 1147132800);
149 qof_time_set_nanosecs (td->time, 2);
150 td->date = qof_date_new ();
151 td->date->qd_year = 2006;
152 td->date->qd_mon = 5;
153 td->date->qd_mday = 9;
154 td->date->qd_hour = 0;
155 td->date->qd_min = 0;
156 td->date->qd_sec = G_GINT64_CONSTANT(0);
157 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
158 qof_date_valid (td->date);
159 td->string_list = NULL;
160 td->string_list = g_list_prepend (td->string_list, "05/09/2006");
161 td->string_list = g_list_prepend (td->string_list, "09/05/2006");
162 td->string_list = g_list_prepend (td->string_list, "09.05.2006");
163 td->string_list = g_list_prepend (td->string_list, "2006-05-09");
164 td->string_list = g_list_prepend (td->string_list, "2006-05-09T00:00:00Z");
165 td->string_list = g_list_prepend (td->string_list,
166 "2006-05-09 00:00:00.000000002 +0000");
167 td->string_list = g_list_reverse (td->string_list);
168 td->id = "a recent time";
169 test_data = g_list_prepend (test_data, td);
170 }
171 {
172 QTestDate *td = g_new0 (QTestDate, 1);
173 td->time = qof_time_new ();
174 qof_time_set_secs (td->time, 1147186144);
175 qof_time_set_nanosecs (td->time, 100);
176 td->date = qof_date_new ();
177 td->date->qd_year = 2006;
178 td->date->qd_mon = 5;
179 td->date->qd_mday = 9;
180 td->date->qd_hour = 14;
181 td->date->qd_min = 49;
182 td->date->qd_sec = G_GINT64_CONSTANT(4);
183 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
184 qof_date_valid (td->date);
185 td->string_list = NULL;
186 td->string_list = g_list_prepend (td->string_list, "05/09/2006");
187 td->string_list = g_list_prepend (td->string_list, "09/05/2006");
188 td->string_list = g_list_prepend (td->string_list, "09.05.2006");
189 td->string_list = g_list_prepend (td->string_list, "2006-05-09");
190 td->string_list = g_list_prepend (td->string_list, "2006-05-09T14:49:04Z");
191 td->string_list = g_list_prepend (td->string_list,
192 "2006-05-09 14:49:04.000000100 +0000");
193 td->string_list = g_list_reverse (td->string_list);
194 td->id = "second recent time";
195 test_data = g_list_prepend (test_data, td);
196 }
197 {
198 QTestDate *td = g_new0 (QTestDate, 1);
199 td->time = qof_time_new ();
200 qof_time_set_secs (td->time, 63039600);
201 qof_time_set_nanosecs (td->time, 4);
202 td->date = qof_date_new ();
203 td->date->qd_year = 1971;
204 td->date->qd_mon = 12;
205 td->date->qd_mday = 31;
206 td->date->qd_hour = 15;
207 td->date->qd_min = 0;
208 td->date->qd_sec = G_GINT64_CONSTANT(0);
209 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
210 qof_date_valid (td->date);
211 td->string_list = NULL;
212 td->string_list = g_list_prepend (td->string_list, "12/31/1971");
213 td->string_list = g_list_prepend (td->string_list, "31/12/1971");
214 td->string_list = g_list_prepend (td->string_list, "31.12.1971");
215 td->string_list = g_list_prepend (td->string_list, "1971-12-31");
216 td->string_list = g_list_prepend (td->string_list, "1971-12-31T15:00:00Z");
217 td->string_list = g_list_prepend (td->string_list,
218 "1971-12-31 15:00:00.000000004 +0000");
219 td->string_list = g_list_reverse (td->string_list);
220 td->id = "New Year's Eve 1971";
221 test_data = g_list_prepend (test_data, td);
222 }
223 {
224 QTestDate *td = g_new0 (QTestDate, 1);
225 td->time = qof_time_new ();
226 qof_time_set_secs (td->time, 315532800);
227 qof_time_set_nanosecs (td->time, 123456789);
228 td->date = qof_date_new ();
229 td->date->qd_year = 1980;
230 td->date->qd_mon = 1;
231 td->date->qd_mday = 1;
232 td->date->qd_hour = 0;
233 td->date->qd_min = 0;
234 td->date->qd_sec = G_GINT64_CONSTANT(0);
235 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
236 qof_date_valid (td->date);
237 td->string_list = NULL;
238 td->string_list = g_list_prepend (td->string_list, "01/01/1980");
239 td->string_list = g_list_prepend (td->string_list, "01/01/1980");
240 td->string_list = g_list_prepend (td->string_list, "01.01.1980");
241 td->string_list = g_list_prepend (td->string_list, "1980-01-01");
242 td->string_list = g_list_prepend (td->string_list, "1980-01-01T00:00:00Z");
243 td->string_list = g_list_prepend (td->string_list,
244 "1980-01-01 00:00:00.123456789 +0000");
245 td->string_list = g_list_reverse (td->string_list);
246 td->id = "New Year's Day 1980";
247 test_data = g_list_prepend (test_data, td);
248 }
249 {
250 QTestDate *td = g_new0 (QTestDate, 1);
251 td->time = qof_time_new ();
252 qof_time_set_secs (td->time, 946684799);
253 qof_time_set_nanosecs (td->time, 987654321);
254 td->date = qof_date_new ();
255 td->date->qd_year = 1999;
256 td->date->qd_mon = 12;
257 td->date->qd_mday = 31;
258 td->date->qd_hour = 23;
259 td->date->qd_min = 59;
260 td->date->qd_sec = G_GINT64_CONSTANT(59);
261 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
262 qof_date_valid (td->date);
263 td->string_list = NULL;
264 td->string_list = g_list_prepend (td->string_list, "12/31/1999");
265 td->string_list = g_list_prepend (td->string_list, "31/12/1999");
266 td->string_list = g_list_prepend (td->string_list, "31.12.1999");
267 td->string_list = g_list_prepend (td->string_list, "1999-12-31");
268 td->string_list = g_list_prepend (td->string_list, "1999-12-31T23:59:59Z");
269 td->string_list = g_list_prepend (td->string_list,
270 "1999-12-31 23:59:59.987654321 +0000");
271 td->string_list = g_list_reverse (td->string_list);
272 td->id = "Millenium Eve";
273 test_data = g_list_prepend (test_data, td);
274 }
275 {
276 QTestDate *td = g_new0 (QTestDate, 1);
277 td->time = qof_time_new ();
278 qof_time_set_secs (td->time, 699378323);
279 qof_time_set_nanosecs (td->time, 90000);
280 td->date = qof_date_new ();
281 td->date->qd_year = 1992;
282 td->date->qd_mon = 2;
283 td->date->qd_mday = 29;
284 td->date->qd_hour = 15;
285 td->date->qd_min = 45;
286 td->date->qd_sec = G_GINT64_CONSTANT(23);
287 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
288 qof_date_valid (td->date);
289 td->string_list = NULL;
290 td->string_list = g_list_prepend (td->string_list, "02/29/1992");
291 td->string_list = g_list_prepend (td->string_list, "29/02/1992");
292 td->string_list = g_list_prepend (td->string_list, "29.02.1992");
293 td->string_list = g_list_prepend (td->string_list, "1992-02-29");
294 td->string_list = g_list_prepend (td->string_list, "1992-02-29T15:45:23Z");
295 td->string_list = g_list_prepend (td->string_list,
296 "1992-02-29 15:45:23.000090000 +0000");
297 td->string_list = g_list_reverse (td->string_list);
298 td->id = "29th February 1992";
299 test_data = g_list_prepend (test_data, td);
300 }
301 {
302
303 QTestDate *td = g_new0 (QTestDate, 1);
304 td->time = qof_time_new ();
305 qof_time_set_secs (td->time, -1);
306 qof_time_set_nanosecs (td->time, 9);
307 td->date = qof_date_new ();
308 td->date->qd_year = 1969;
309 td->date->qd_mon = 12;
310 td->date->qd_mday = 31;
311 td->date->qd_hour = 23;
312 td->date->qd_min = 59;
313 td->date->qd_sec = G_GINT64_CONSTANT(59);
314 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
315 qof_date_valid (td->date);
316 td->string_list = NULL;
317 td->string_list = g_list_prepend (td->string_list, "12/31/1969");
318 td->string_list = g_list_prepend (td->string_list, "31/12/1969");
319 td->string_list = g_list_prepend (td->string_list, "31.12.1969");
320 td->string_list = g_list_prepend (td->string_list, "1969-12-31");
321 td->string_list = g_list_prepend (td->string_list, "1969-12-31T23:59:59Z");
322 td->string_list = g_list_prepend (td->string_list,
323 "1969-12-31 23:59:59.000000009 +0000");
324 td->string_list = g_list_reverse (td->string_list);
325 td->id = "epoch eve";
326 test_data = g_list_prepend (test_data, td);
327 }
328 {
329 QTestDate *td = g_new0 (QTestDate, 1);
330 td->time = qof_time_new ();
331 qof_time_set_secs (td->time, -192776400);
332 qof_time_set_nanosecs (td->time, 818818818);
333 td->date = qof_date_new ();
334 td->date->qd_year = 1963;
335 td->date->qd_mon = 11;
336 td->date->qd_mday = 22;
337 td->date->qd_hour = 19;
338 td->date->qd_min = 0;
339 td->date->qd_sec = G_GINT64_CONSTANT(0);
340 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
341 qof_date_valid (td->date);
342 td->string_list = NULL;
343 td->string_list = g_list_prepend (td->string_list, "11/22/1963");
344 td->string_list = g_list_prepend (td->string_list, "22/11/1963");
345 td->string_list = g_list_prepend (td->string_list, "22.11.1963");
346 td->string_list = g_list_prepend (td->string_list, "1963-11-22");
347 td->string_list = g_list_prepend (td->string_list, "1963-11-22T19:00:00Z");
348 td->string_list = g_list_prepend (td->string_list,
349 "1963-11-22 19:00:00.818818818 +0000");
350 td->string_list = g_list_reverse (td->string_list);
351 td->id = "approx JFK, 1963";
352 test_data = g_list_prepend (test_data, td);
353 }
354 {
355 QTestDate *td = g_new0 (QTestDate, 1);
356 td->time = qof_time_new ();
357 qof_time_set_secs (td->time, -767311080);
358 qof_time_set_nanosecs (td->time, 0);
359 td->date = qof_date_new ();
360 td->date->qd_year = 1945;
361 td->date->qd_mon = 9;
362 td->date->qd_mday = 8;
363 td->date->qd_hour = 2;
364 td->date->qd_min = 2;
365 td->date->qd_sec = G_GINT64_CONSTANT(0);
366 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
367 qof_date_valid (td->date);
368 td->string_list = NULL;
369 td->string_list = g_list_prepend (td->string_list, "09/08/1945");
370 td->string_list = g_list_prepend (td->string_list, "08/09/1945");
371 td->string_list = g_list_prepend (td->string_list, "08.09.1945");
372 td->string_list = g_list_prepend (td->string_list, "1945-09-08");
373 td->string_list = g_list_prepend (td->string_list, "1945-09-08T02:02:00Z");
374 td->string_list = g_list_prepend (td->string_list,
375 "1945-09-08 02:02:00.000000000 +0000");
376 td->string_list = g_list_reverse (td->string_list);
377 td->id = "Nagasaki, 1945";
378 test_data = g_list_prepend (test_data, td);
379 }
380 {
381 QTestDate *td = g_new0 (QTestDate, 1);
382 td->time = qof_time_new ();
383 qof_time_set_secs (td->time, -1613826000);
384 qof_time_set_nanosecs (td->time, 0);
385 td->date = qof_date_new ();
386 td->date->qd_year = 1918;
387 td->date->qd_mon = 11;
388 td->date->qd_mday = 11;
389 td->date->qd_hour = 11;
390 td->date->qd_min = 0;
391 td->date->qd_sec = G_GINT64_CONSTANT(0);
392 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
393 qof_date_valid (td->date);
394 td->string_list = NULL;
395 td->string_list = g_list_prepend (td->string_list, "11/11/1918");
396 td->string_list = g_list_prepend (td->string_list, "11/11/1918");
397 td->string_list = g_list_prepend (td->string_list, "11.11.1918");
398 td->string_list = g_list_prepend (td->string_list, "1918-11-11");
399 td->string_list = g_list_prepend (td->string_list, "1918-11-11T11:00:00Z");
400 td->string_list = g_list_prepend (td->string_list,
401 "1918-11-11 11:00:00.000000000 +0000");
402 td->string_list = g_list_reverse (td->string_list);
403 td->id = "Armistice 1918";
404 test_data = g_list_prepend (test_data, td);
405 }
406 {
407 QTestDate *td = g_new0 (QTestDate, 1);
408 td->time = qof_time_new ();
409 qof_time_set_secs (td->time,
410 G_GINT64_CONSTANT(-2208988801));
411 qof_time_set_nanosecs (td->time, 0);
412 td->date = qof_date_new ();
413 td->date->qd_year = 1899;
414 td->date->qd_mon = 12;
415 td->date->qd_mday = 31;
416 td->date->qd_hour = 23;
417 td->date->qd_min = 59;
418 td->date->qd_sec = G_GINT64_CONSTANT(59);
419 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
420 qof_date_valid (td->date);
421 td->string_list = NULL;
422 td->string_list = g_list_prepend (td->string_list, "12/31/1899");
423 td->string_list = g_list_prepend (td->string_list, "31/12/1899");
424 td->string_list = g_list_prepend (td->string_list, "31.12.1899");
425 td->string_list = g_list_prepend (td->string_list, "1899-12-31");
426 td->string_list = g_list_prepend (td->string_list, "1899-12-31T23:59:59Z");
427 td->string_list = g_list_prepend (td->string_list,
428 "1899-12-31 23:59:59.000000000 +0000");
429 td->string_list = g_list_reverse (td->string_list);
430 td->id = "19th century Millenium Eve";
431 test_data = g_list_prepend (test_data, td);
432 }
433 {
434 QTestDate *td = g_new0 (QTestDate, 1);
435 td->time = qof_time_new ();
436 qof_time_set_secs (td->time,
437 G_GINT64_CONSTANT(-13311993599));
438 qof_time_set_nanosecs (td->time, 0);
439 td->date = qof_date_new ();
440 td->date->qd_year = 1548;
441 td->date->qd_mon = 2;
442 td->date->qd_mday = 29;
443 td->date->qd_hour = 0;
444 td->date->qd_min = 0;
445 td->date->qd_sec = G_GINT64_CONSTANT(1);
446 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
447 qof_date_valid (td->date);
448 td->string_list = NULL;
449 td->string_list = g_list_prepend (td->string_list, "02/29/1548");
450 td->string_list = g_list_prepend (td->string_list, "29/02/1548");
451 td->string_list = g_list_prepend (td->string_list, "29.02.1548");
452 td->string_list = g_list_prepend (td->string_list, "1548-02-29");
453 td->string_list = g_list_prepend (td->string_list, "1548-02-29T00:00:01Z");
454 td->string_list = g_list_prepend (td->string_list,
455 "1548-02-29 00:00:01.000000000 +0000");
456 td->string_list = g_list_reverse (td->string_list);
457 td->id = "16th century leap day";
458 test_data = g_list_prepend (test_data, td);
459 }
460 {
461 QTestDate *td = g_new0 (QTestDate, 1);
462 td->time = qof_time_new ();
463 qof_time_set_secs (td->time, G_GINT64_CONSTANT(-28502726400));
464 qof_time_set_nanosecs (td->time, 0);
465 td->date = qof_date_new ();
466 td->date->qd_year = 1066;
467 td->date->qd_mon = 10;
468 td->date->qd_mday = 14;
469 td->date->qd_hour = 8;
470 td->date->qd_min = 0;
471 td->date->qd_sec = G_GINT64_CONSTANT(0);
472 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
473 qof_date_valid (td->date);
474 td->string_list = NULL;
475 td->string_list = g_list_prepend (td->string_list, "10/14/1066");
476 td->string_list = g_list_prepend (td->string_list, "14/10/1066");
477 td->string_list = g_list_prepend (td->string_list, "14.10.1066");
478 td->string_list = g_list_prepend (td->string_list, "1066-10-14");
479 td->string_list = g_list_prepend (td->string_list, "1066-10-14T08:00:00Z");
480 td->string_list = g_list_prepend (td->string_list,
481 "1066-10-14 08:00:00.000000000 +0000");
482 td->string_list = g_list_reverse (td->string_list);
483 td->id = "Battle of Hastings, 1066";
484 test_data = g_list_prepend (test_data, td);
485 }
486 {
487 QTestDate *td = g_new0 (QTestDate, 1);
488 td->time = qof_time_new ();
489 qof_time_set_secs (td->time,
490 G_GINT64_CONSTANT(-36417340799));
491 qof_time_set_nanosecs (td->time, 0);
492 td->date = qof_date_new ();
493 td->date->qd_year = 815;
494 td->date->qd_mon = 12;
495 td->date->qd_mday = 25;
496 td->date->qd_hour = 0;
497 td->date->qd_min = 0;
498 td->date->qd_sec = G_GINT64_CONSTANT(1);
499 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
500 qof_date_valid (td->date);
501 td->string_list = NULL;
502 td->string_list = g_list_prepend (td->string_list, "12/25/0815");
503 td->string_list = g_list_prepend (td->string_list, "25/12/0815");
504 td->string_list = g_list_prepend (td->string_list, "25.12.0815");
505 td->string_list = g_list_prepend (td->string_list, "0815-12-25");
506 td->string_list = g_list_prepend (td->string_list, "0815-12-25T00:00:01Z");
507 td->string_list = g_list_prepend (td->string_list,
508 "0815-12-25 00:00:01.000000000 +0000");
509 td->string_list = g_list_reverse (td->string_list);
510 td->id = "9th century Christmas Day";
511 test_data = g_list_prepend (test_data, td);
512 }
513 {
514 QTestDate *td = g_new0 (QTestDate, 1);
515 td->time = qof_time_new ();
516 qof_time_set_secs (td->time, G_GINT64_CONSTANT(-60798160800));
517 qof_time_set_nanosecs (td->time, 0);
518 td->date = qof_date_new ();
519 td->date->qd_year = 43;
520 td->date->qd_mon = 5;
521 td->date->qd_mday = 20;
522 td->date->qd_hour = 14;
523 td->date->qd_min = 0;
524 td->date->qd_sec = G_GINT64_CONSTANT(0);
525 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
526 qof_date_valid (td->date);
527 td->string_list = NULL;
528 td->string_list = g_list_prepend (td->string_list, "05/20/0043");
529 td->string_list = g_list_prepend (td->string_list, "20/05/0043");
530 td->string_list = g_list_prepend (td->string_list, "20.05.0043");
531 td->string_list = g_list_prepend (td->string_list, "0043-05-20");
532 td->string_list = g_list_prepend (td->string_list, "0043-05-20T14:00:00Z");
533 td->string_list = g_list_prepend (td->string_list,
534 "0043-05-20 14:00:00.000000000 +0000");
535 td->string_list = g_list_reverse (td->string_list);
536 td->id = "approx Roman invasion, 43AD";
537 test_data = g_list_prepend (test_data, td);
538 }
543 {
544 QTestDate *td = g_new0 (QTestDate, 1);
545 td->time = qof_time_new ();
546 qof_time_set_secs (td->time, G_GINT64_CONSTANT(-62167824001));
547 qof_time_set_nanosecs (td->time, 0);
548 td->date = qof_date_new ();
549 td->date->qd_year = -1;
550 td->date->qd_mon = 12;
551 td->date->qd_mday = 24;
552 td->date->qd_hour = 23;
553 td->date->qd_min = 59;
554 td->date->qd_sec = G_GINT64_CONSTANT(59);
555 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
556 qof_date_valid (td->date);
557 td->string_list = NULL;
558 td->string_list = g_list_prepend (td->string_list, "12/24/-001");
559 td->string_list = g_list_prepend (td->string_list, "24/12/-001");
560 td->string_list = g_list_prepend (td->string_list, "24.12.-001");
561 td->string_list = g_list_prepend (td->string_list, "-001-12-24");
562 td->string_list = g_list_prepend (td->string_list, "-001-12-24T23:59:59Z");
563 td->string_list = g_list_prepend (td->string_list,
564 "-001-12-24 23:59:59.000000000 +0000");
565 td->string_list = g_list_reverse (td->string_list);
566 td->id = "Xmas eve, 1BC";
567 test_data = g_list_prepend (test_data, td);
568 }
569 {
570 QTestDate *td = g_new0 (QTestDate, 1);
571 td->time = qof_time_new ();
572 qof_time_set_secs (td->time,
573 G_GINT64_CONSTANT(-204110409601));
574 qof_time_set_nanosecs (td->time, 0);
575 td->date = qof_date_new ();
576 td->date->qd_year = -4499;
577 td->date->qd_mon = 12;
578 td->date->qd_mday = 31;
579 td->date->qd_hour = 23;
580 td->date->qd_min = 59;
581 td->date->qd_sec = G_GINT64_CONSTANT(59);
582 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
583 qof_date_valid (td->date);
584 td->string_list = NULL;
585 td->string_list = g_list_prepend (td->string_list, "12/31/-4499");
586 td->string_list = g_list_prepend (td->string_list, "31/12/-4499");
587 td->string_list = g_list_prepend (td->string_list, "31.12.-4499");
588 td->string_list = g_list_prepend (td->string_list, "-4499-12-31");
589 td->string_list = g_list_prepend (td->string_list, "-4499-12-31T23:59:59Z");
590 td->string_list = g_list_prepend (td->string_list,
591 "-4499-12-31 23:59:59.000000000 +0000");
592 td->string_list = g_list_reverse (td->string_list);
593 td->id = "far past.";
594 test_data = g_list_prepend (test_data, td);
595 }
596 {
597 QTestDate *td = g_new0 (QTestDate, 1);
598 td->time = qof_time_new ();
599 qof_time_set_secs (td->time,
600 G_GINT64_CONSTANT(-2097527529601));
601 qof_time_set_nanosecs (td->time, 0);
602 td->date = qof_date_new ();
603 td->date->qd_year = -64499;
604 td->date->qd_mon = 12;
605 td->date->qd_mday = 31;
606 td->date->qd_hour = 23;
607 td->date->qd_min = 59;
608 td->date->qd_sec = G_GINT64_CONSTANT(59);
609 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
610 qof_date_valid (td->date);
611 td->string_list = NULL;
612 td->string_list = g_list_prepend (td->string_list, "12/31/-64499");
613 td->string_list = g_list_prepend (td->string_list, "31/12/-64499");
614 td->string_list = g_list_prepend (td->string_list, "31.12.-64499");
615 td->string_list = g_list_prepend (td->string_list, "-64499-12-31");
616 td->string_list = g_list_prepend (td->string_list, "-64499-12-31T23:59:59Z");
617 td->string_list = g_list_prepend (td->string_list,
618 "-64499-12-31 23:59:59.000000000 +0000");
619 td->string_list = g_list_reverse (td->string_list);
620 td->id = "far, far past.";
621 test_data = g_list_prepend (test_data, td);
622 }
623 /* now test far future dates */
624 {
625 QTestDate *td = g_new0 (QTestDate, 1);
626 td->time = qof_time_new ();
627 qof_time_set_secs (td->time, G_GINT64_CONSTANT(32679095666));
628 qof_time_set_nanosecs (td->time, 800);
629 td->date = qof_date_new ();
630 td->date->qd_year = 3005;
631 td->date->qd_mon = 7;
632 td->date->qd_mday = 24;
633 td->date->qd_hour = 6;
634 td->date->qd_min = 34;
635 td->date->qd_sec = G_GINT64_CONSTANT(26);
636 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
637 qof_date_valid (td->date);
638 td->string_list = NULL;
639 td->string_list = g_list_prepend (td->string_list, "07/24/3005");
640 td->string_list = g_list_prepend (td->string_list, "24/07/3005");
641 td->string_list = g_list_prepend (td->string_list, "24.07.3005");
642 td->string_list = g_list_prepend (td->string_list, "3005-07-24");
643 td->string_list = g_list_prepend (td->string_list, "3005-07-24T06:34:26Z");
644 td->string_list = g_list_prepend (td->string_list,
645 "3005-07-24 06:34:26.000000800 +0000");
646 td->string_list = g_list_reverse (td->string_list);
647 td->id = "24th July 3005";
648 test_data = g_list_prepend (test_data, td);
649 }
650 {
651 QTestDate *td = g_new0 (QTestDate, 1);
652 td->time = qof_time_new ();
653 qof_time_set_secs (td->time,
654 G_GINT64_CONSTANT(79839129599));
655 qof_time_set_nanosecs (td->time, 50000);
656 td->date = qof_date_new ();
657 td->date->qd_year = 4499;
658 td->date->qd_mon = 12;
659 td->date->qd_mday = 31;
660 td->date->qd_hour = 23;
661 td->date->qd_min = 59;
662 td->date->qd_sec = G_GINT64_CONSTANT(59);
663 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
664 qof_date_valid (td->date);
665 td->string_list = NULL;
666 td->string_list = g_list_prepend (td->string_list, "12/31/4499");
667 td->string_list = g_list_prepend (td->string_list, "31/12/4499");
668 td->string_list = g_list_prepend (td->string_list, "31.12.4499");
669 td->string_list = g_list_prepend (td->string_list, "4499-12-31");
670 td->string_list = g_list_prepend (td->string_list, "4499-12-31T23:59:59Z");
671 td->string_list = g_list_prepend (td->string_list,
672 "4499-12-31 23:59:59.000050000 +0000");
673 td->string_list = g_list_reverse (td->string_list);
674 td->id = "44th century Millenium Eve";
675 test_data = g_list_prepend (test_data, td);
676 }
677 {
678 QTestDate *td = g_new0 (QTestDate, 1);
679 td->time = qof_time_new ();
680 qof_time_set_secs (td->time,
681 G_GINT64_CONSTANT(395408649599));
682 qof_time_set_nanosecs (td->time, 7000000);
683 td->date = qof_date_new ();
684 td->date->qd_year = 14499;
685 td->date->qd_mon = 12;
686 td->date->qd_mday = 31;
687 td->date->qd_hour = 23;
688 td->date->qd_min = 59;
689 td->date->qd_sec = G_GINT64_CONSTANT(59);
690 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
691 qof_date_valid (td->date);
692 td->string_list = NULL;
693 td->string_list = g_list_prepend (td->string_list, "12/31/14499");
694 td->string_list = g_list_prepend (td->string_list, "31/12/14499");
695 td->string_list = g_list_prepend (td->string_list, "31.12.14499");
696 td->string_list = g_list_prepend (td->string_list, "14499-12-31");
697 td->string_list = g_list_prepend (td->string_list, "14499-12-31T23:59:59Z");
698 td->string_list = g_list_prepend (td->string_list,
699 "14499-12-31 23:59:59.007000000 +0000");
700 td->string_list = g_list_reverse (td->string_list);
701 td->id = "144th century Millenium Eve";
702 test_data = g_list_prepend (test_data, td);
703 }
704 {
705 QTestDate *td = g_new0 (QTestDate, 1);
706 td->time = qof_time_new ();
707 qof_time_set_secs (td->time,
708 G_GINT64_CONSTANT(74869815369599));
709 qof_time_set_nanosecs (td->time, 45321545);
710 td->date = qof_date_new ();
711 td->date->qd_year = 2374499;
712 td->date->qd_mon = 12;
713 td->date->qd_mday = 31;
714 td->date->qd_hour = 23;
715 td->date->qd_min = 59;
716 td->date->qd_sec = G_GINT64_CONSTANT(59);
717 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
718 qof_date_valid (td->date);
719 td->string_list = NULL;
720 td->string_list = g_list_prepend (td->string_list, "12/31/2374499");
721 td->string_list = g_list_prepend (td->string_list, "31/12/2374499");
722 td->string_list = g_list_prepend (td->string_list, "31.12.2374499");
723 td->string_list = g_list_prepend (td->string_list, "2374499-12-31");
724 td->string_list = g_list_prepend (td->string_list, "2374499-12-31T23:59:59Z");
725 td->string_list = g_list_prepend (td->string_list,
726 "2374499-12-31 23:59:59.045321545 +0000");
727 td->string_list = g_list_reverse (td->string_list);
728 td->id = "far, far future";
729 test_data = g_list_prepend (test_data, td);
730 }
731 }
732
733 static void
734 free_test_data (gpointer data, gpointer user_data __attribute__ ((unused)))
735 {
736 QTestDate *td;
737
738 td = (QTestDate*)data;
739 qof_date_free (td->date);
740 qof_time_free (td->time);
741 td->string_list = NULL;
742 g_free (td);
743 td = NULL;
744 }
745
746 static void
747 test_date_close (void)
748 {
749 g_list_foreach (test_data, free_test_data, NULL);
750 }
751
752 static void
753 scan_and_stamp (const gchar * str, QofDateFormat df, QofTimeSecs check)
754 {
755 QofTime *scan;
756 QofDate *qd;
757 gchar *stamp;
758
759 qd = qof_date_parse (str, df);
760 scan = qof_date_to_qtime (qd);
761 do_test ((scan != NULL), "scan failed");
762 if (scan == NULL)
763 return;
764 do_test ((0 == safe_strcasecmp(qd->qd_zone, "GMT")),
765 " timezone reset incorrect");
766 do_test ((qof_time_get_secs (scan) == check),
767 "wrong time value");
768 if (qof_time_get_secs (scan) != check)
769 PERR (" wrong time value %"
770 G_GINT64_FORMAT " %" G_GINT64_FORMAT " diff=%"
771 G_GINT64_FORMAT " df=%d str=%s cmp=%s",
772 qof_time_get_secs (scan), check,
773 qof_time_get_secs (scan) - check, df, str,
774 qof_date_print(qd, QOF_DATE_FORMAT_UTC));
775 stamp = qof_date_print (qof_date_from_qtime(scan), df);
776 do_test ((stamp != NULL), "stamp failed");
777 /* timezone tests mean stamp cannot be compared to str */
778 qof_time_free (scan);
779 }
780
781 static void
782 stamp_and_scan (QofTimeSecs start, glong nanosecs,
783 QofDateFormat df)
784 {
785 gchar *str1, *str2;
786 QofDate *check;
787 QofTime *time, *scan;
788
789 time = qof_time_new ();
790 qof_time_set_secs (time, start);
791 qof_time_set_nanosecs (time, nanosecs);
792 str1 = qof_date_print (qof_date_from_qtime(time), df);
793 do_test ((str1 != NULL), "stamp failed");
794 if (!str1)
795 return;
796 check = qof_date_parse (str1, df);
797 do_test ((check != NULL), "parse failed");
798 if (!check)
799 PERR (" tried to parse %s\n", str1);
800 scan = qof_date_to_qtime (check);
801 qof_date_free (check);
802 do_test ((scan != NULL), "scan failed");
803 if (!scan)
804 return;
805 /* depending on the format, data may be lost here
806 as some formats do not encode the time. So only
807 test that the returned stamp is the same. */
808 check = qof_date_from_qtime (scan);
809 str2 = qof_date_print (check, df);
810 qof_date_free (check);
811 /* 2 digit year errors with format 6 */
812 do_test ((str2 != NULL), "printed string is null");
813 do_test ((qof_date_from_qtime (scan) != NULL),
814 "from_qtime failed");
815 do_test ((0 == safe_strcasecmp (str1, str2)),
816 "stamp different to scan");
817 if (0 != safe_strcasecmp (str1, str2))
818 PERR (" df=%d str=%s scan=%s", df, str1, str2);
819 qof_time_free (scan);
820 qof_time_free (time);
821 g_free (str1);
822 g_free (str2);
823 }
824
825 static void
826 run_print_scan_tests (void)
827 {
828 gint i;
829 gint64 secs;
830 QofDateFormat df;
831
832 /* Set a secs value at the limit of the range of time_t
833 on 32bit systems. */
834 secs = G_MAXINT32;
835 /* add ten days */
836 secs += SECS_PER_DAY * 10;
837 for (i = 1; i <= QOF_DATE_FORMAT_ISO8601; i++)
838 {
839 stamp_and_scan (796179600, 0, i);
840 stamp_and_scan (796179500, 72000, i);
841 stamp_and_scan (152098136, 0, i);
842 stamp_and_scan (1964049931, 0, i);
843 stamp_and_scan (1162088421, 12548000, i);
844 stamp_and_scan (325659000 - 6500, 0, i);
845 stamp_and_scan (1143943200, 0, i);
846 stamp_and_scan (1603591171, 595311000, i);
847 stamp_and_scan (1738909365, 204102000, i);
848 stamp_and_scan (1603591171, 595311000, i);
849 stamp_and_scan (1143943200 - 1, 0, i);
850 stamp_and_scan (1143943200, 0, i);
851 stamp_and_scan (1143943200 + (7 * 60 * 60), 0, i);
852 stamp_and_scan (1143943200 + (8 * 60 * 60), 0, i);
853 stamp_and_scan (1841443200, 0, i);
854
855 /* work with early dates */
856 stamp_and_scan (G_GINT64_CONSTANT (-796179600), 253, i);
857 stamp_and_scan (G_GINT64_CONSTANT (-152098136), 865, i);
858 stamp_and_scan (G_GINT64_CONSTANT (-1143943200), 67, i);
859 stamp_and_scan (G_GINT64_CONSTANT (-1964049931), 53, i);
860 stamp_and_scan (G_GINT64_CONSTANT (-2463880447), 48, i);
861 stamp_and_scan (G_GINT64_CONSTANT (-22905158401), 9, i);
862 stamp_and_scan (G_GINT64_CONSTANT (-28502726400), 1, i);
863 stamp_and_scan (G_GINT64_CONSTANT (-60798211200), 0, i);
864 stamp_and_scan (G_GINT64_CONSTANT (-32727638740), 0, i);
865 /* stamp_and_scan (G_GINT64_CONSTANT (-86956848000), 0, i);*/
866 stamp_and_scan (secs, 0, i);
867 /* Wed 29 Jan 2048 03:14:07 UTC */
868 stamp_and_scan (G_GINT64_CONSTANT (2463880447), 0, i);
869 /* Sat 29 Jan 2050 03:14:07 UTC */
870 stamp_and_scan (G_GINT64_CONSTANT (2527038847), 0, i);
871 /* work with far future dates */
872 /* 32,727,638,740 Fri Feb 6 02:45:40 UTC 3007 */
873 stamp_and_scan (G_GINT64_CONSTANT (32727638740), 0, i);
874 /* 88,313,632,867 Fri Jul 19 12:41:07 UTC 4768 */
875 stamp_and_scan (G_GINT64_CONSTANT (88313632867), 0, i);
876 /* 189,216,632,865 Fri Jan 14 07:47:45 UTC 7966 */
877 stamp_and_scan (G_GINT64_CONSTANT (189216632865), 0, i);
878 /* 378,432,632,864 Sat Jan 20 07:47:44 UTC 13,962 */
879 stamp_and_scan (G_GINT64_CONSTANT (378432632864), 0, i);
880 /* 3165071328567 Wed Feb 13 00:09:27 UTC 102,267 */
881 stamp_and_scan (G_GINT64_CONSTANT (3165071328567), 0, i);
882 }
883 /* far, far future dates delay the test */
884 #ifdef TEST_DEBUG
885 /* 43165071328567 Wed Aug 28 23:16:07 UTC 1,369,816 */
886 stamp_and_scan (G_GINT64_CONSTANT (3165071328567),
887 0, QOF_DATE_FORMAT_UTC);
888 /* 843165071328567 Tue Jun 19 05:29:27 UTC 26,720,807 */
889 stamp_and_scan (G_GINT64_CONSTANT(843165071328567),
890 0, QOF_DATE_FORMAT_UTC);
892 /* 9843165071328567 Mon Jan 9 21:29:27 UTC 311,919,454 */
893 /* stamp_and_scan (G_GINT64_CONSTANT(9843165071328567),
894 0, QOF_DATE_FORMAT_UTC);
895 stamp_and_scan (G_GINT64_CONSTANT(9843165071328567),
896 354758450, QOF_DATE_FORMAT_ISO8601);*/
897 #endif
898 scan_and_stamp ("05/09/2006", QOF_DATE_FORMAT_US,
899 1147132800);
900 scan_and_stamp ("09/05/2006", QOF_DATE_FORMAT_UK,
901 1147132800);
902 scan_and_stamp ("09.05.2006", QOF_DATE_FORMAT_CE,
903 1147132800);
904 scan_and_stamp ("2006-05-09", QOF_DATE_FORMAT_ISO,
905 1147132800);
906 scan_and_stamp ("2006-05-09T00:00:00Z", QOF_DATE_FORMAT_UTC,
907 1147132800);
908 scan_and_stamp ("2006-05-09T14:49:04Z", QOF_DATE_FORMAT_UTC,
909 1147186144);
910
911 /* test a custom format */
912 df = 0;
913 do_test ((qof_date_format_add ("%Y-%m-%d %H:%M:%S %z",
914 &df) == TRUE), "failed to add scan suitable format");
915 /* test timezone settings */
916 /* 1972-01-01T00:00:00Z */
917 scan_and_stamp ("1971-12-31 15:00:00 -0900", df, 63072000);
918 /* 1980-01-01T00:00:00Z */
919 scan_and_stamp ("1979-12-31 15:00:00 -0900", df, 315532800);
920 scan_and_stamp ("1980-01-01 00:00:00 -0000", df, 315532800);
921 scan_and_stamp ("1980-01-01 00:00:00 +0000", df, 315532800);
922 scan_and_stamp ("1980-01-01 09:00:00 +0900", df, 315532800);
923 scan_and_stamp ("1980-01-01 08:30:00 +0830", df, 315532800);
924 /* pre-1970 dates */
925 scan_and_stamp ("1963-11-22 14:00:00 -0500", df, -192776400);
926 scan_and_stamp ("1945-09-08 11:02:00 +0900", df, -767311080);
927 scan_and_stamp ("1918-11-11 11:00:00 +0000", df, -1613826000);
928 /* work with really early dates */
929 /* 14th October 1066 (time is just a guess) */
930 scan_and_stamp ("1066-10-14 08:00:00 +0000", df,
931 G_GINT64_CONSTANT (-28502726400));
932 /* May 43AD Roman invasion (day and time guessed) */
933 scan_and_stamp ("0043-05-20 14:00:00 +0000", df,
934 G_GINT64_CONSTANT (-60798160800));
935 {
936 QofDate *qd;
937 QofTime *qt;
938 gint64 secs;
939
940 qt = qof_time_new ();
941 /* Tue May 9 14:50:10 UTC 2006 */
942 qof_time_set_secs (qt, 1147186210);
943 qd = qof_date_from_qtime (qt);
944 do_test ((qof_date_adddays (qd, 45) == TRUE),
945 "add_days failed");
946 secs = 1147186210 + (45 * SECS_PER_DAY);
947 qof_time_free (qt);
948 qt = qof_date_to_qtime (qd);
949 qof_date_free (qd);
950 do_test ((secs == qof_time_get_secs (qt)),
951 "add_days gave incorrect result.");
952 if (secs != qof_time_get_secs (qt))
953 PERR (" secs=%" G_GINT64_FORMAT "cmp=%"
954 G_GINT64_FORMAT, secs, qof_time_get_secs (qt));
955 qof_time_set_secs (qt, 1147186210);
956 qd = qof_date_from_qtime (qt);
957 do_test ((qof_date_addmonths (qd, 50, TRUE) == TRUE),
958 "add_months failed");
959 qof_time_free (qt);
960 qt = qof_date_to_qtime (qd);
961 do_test ((1278687010 == qof_time_get_secs (qt)),
962 "add_months gave incorrect result.");
963 qof_time_free (qt);
964 qof_date_free (qd);
965 }
966 }
967
968 static void
969 run_qofdate_test (void)
970 {
971 QofDateFormat df, test;
972
973 df = qof_date_format_get_current ();
974 /* default date format tests */
975 {
976 qof_date_format_set_current (QOF_DATE_FORMAT_UK);
977 test = qof_date_format_get_current ();
978 do_test ((test == QOF_DATE_FORMAT_UK),
979 "setting current format as UK");
980 do_test ((safe_strcasecmp (qof_date_format_to_name (test),
981 "uk") == 0), "getting the shorthand name");
982 do_test ((FALSE ==
983 qof_date_format_set_name ("foo", QOF_DATE_FORMAT_UK)),
984 "default name should not be overridden");
985 do_test ((QOF_DATE_FORMAT_UK ==
986 qof_date_format_from_name ("uk")),
987 "getting date format from shorthand name");
988 do_test (('/' == qof_date_format_get_date_separator (test)),
989 "getting date format separator from date format");
990 do_test ((FALSE == qof_date_format_set_date_separator (':',
991 test)), "default separator should not be overridden");
992 }
993 /* custom date format tests */
994 {
995 QofDateFormat df, df2;
996 do_test ((qof_date_format_add ("%T", &df) == TRUE),
997 "adding a valid format");
998 do_test ((qof_date_format_add ("%a%A%b%B%c%C%d%D%e%F%f%r",
999 &df2) == FALSE), "adding an invalid format");
1000 qof_date_format_set_current (df);
1001 test = qof_date_format_get_current ();
1002 do_test ((test == df), "setting current format");
1003 do_test ((safe_strcasecmp (qof_date_format_to_name (test),
1004 "%T") == 0), "getting the shorthand name");
1005 do_test ((TRUE == qof_date_format_set_name ("foo", test)),
1006 "custom name should be overridden");
1007 do_test ((test == qof_date_format_from_name ("foo")),
1008 "getting date format from shorthand name");
1009 do_test (('\0' == qof_date_format_get_date_separator (test)),
1010 "getting date format separator from date format");
1011 do_test ((TRUE == qof_date_format_set_date_separator (':', test)),
1012 "custom separator should be overridden");
1013 do_test ((':' == qof_date_format_get_date_separator (test)),
1014 "getting modified date format separator from date format");
1015 }
1016 qof_date_format_set_current (df);
1017 /* run tests on date_normalise */
1018 {
1019 QofDate *date;
1020
1021 date = qof_date_new ();
1022 /* Mon Jan 3 00:00:30 UTC 2000 */
1023 date->qd_sec = SECS_PER_DAY * 2 + 30;
1024 date->qd_year = 2000;
1025 do_test ((qof_date_valid (date) == TRUE), "date 1 was invalid");
1026 do_test ((date->qd_sec == 30), "normalised seconds incorrect - 1");
1027 do_test ((date->qd_mday == 2), "normalised day incorrect - 1");
1028 date->qd_mday = 54;
1029 do_test ((qof_date_valid (date) == TRUE), "date 2 was invalid");
1030 do_test ((date->qd_sec == 30), "normalised seconds incorrect - 2");
1031 do_test ((date->qd_mday == 23), "normalised day incorrect - 2");
1032 date->qd_hour = 34;
1033 qof_date_valid (date);
1034 do_test ((date->qd_hour == 10), "normalised hour incorrect");
1035 do_test ((date->qd_mday == 24), "normalised day incorrect - 3");
1036 date->qd_mon = 17;
1037 qof_date_valid (date);
1038 do_test ((date->qd_mon == 5), "normalised month incorrect");
1039 do_test ((date->qd_year == 2001), "normalised year incorrect");
1040 date->qd_hour = 27;
1041 qof_date_valid (date);
1042 do_test ((date->qd_hour == 3), "normalised hour incorrect - 1");
1043 do_test ((date->qd_mday == 25), "normalised day incorrect - 4");
1044 date->qd_hour = -53;
1045 qof_date_valid (date);
1046 do_test ((date->qd_hour == 19), "normalised hour incorrect - 2");
1047 do_test ((date->qd_mday == 22), "normalised day incorrect - 5");
1048 date->qd_min = -225;
1049 qof_date_valid (date);
1050 do_test ((date->qd_min == 15), "normalised min incorrect");
1051 do_test ((date->qd_hour == 15), "normalised hour incorrect - 3");
1052 date->qd_min = 5;
1053 date->qd_sec = 68;
1054 qof_date_valid (date);
1055 do_test ((date->qd_sec == 8), "doxygen sec example incorrect - 1");
1056 do_test ((date->qd_min == 6), "doxygen min example incorrect - 1");
1057 date->qd_min = 5;
1058 date->qd_sec = -64;
1059 qof_date_valid (date);
1060 do_test ((date->qd_sec == 56), "doxygen sec example incorrect - 2");
1061 do_test ((date->qd_min == 3), "doxygen min example incorrect - 2");
1062 }
1063 /* run tests against QofDate<->QofTime conversions. */
1064 /* opposite direction compared to the tests above. */
1065 {
1066 QofDate *qd;
1067 QofTime *qt;
1068 gchar *str1;
1069
1070 qd = qof_date_new ();
1071 qd->qd_year = 2006;
1072 qd->qd_mon = 5;
1073 qd->qd_mday = 30;
1074 qd->qd_hour = 18;
1075 qd->qd_min = 24;
1076 qd->qd_sec = 17;
1077 qd->qd_nanosecs = 123456789;
1078 do_test ((qof_date_valid (qd)), "date not valid");
1079 qt = qof_date_to_qtime (qd);
1080 str1 = qof_date_print (qd, QOF_DATE_FORMAT_UTC);
1081 do_test ((0 == safe_strcasecmp (str1,
1082 "2006-05-30T18:24:17Z")), "with nanosecs");
1083 do_test ((qof_time_get_nanosecs (qt) ==
1084 123456789L), "nanosecs mismatched.");
1085 do_test ((0 == safe_strcasecmp ("05/30/2006",
1086 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1087 "strftime:US:first");
1088 do_test ((0 == safe_strcasecmp ("30/05/2006",
1089 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1090 "strftime:UK:first");
1091 do_test ((0 == safe_strcasecmp ("30.05.2006",
1092 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1093 "strftime:CE:first");
1094 do_test ((0 == safe_strcasecmp ("2006-05-30",
1095 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1096 "strftime:ISO:first");
1097 do_test ((0 == safe_strcasecmp ("2006-05-30T18:24:17Z",
1098 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1099 "strftime:UTC:first");
1100 do_test ((0 != safe_strcasecmp (NULL,
1101 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1102 "strftime:LOCALE:first:inrange");
1103 do_test ((0 != safe_strcasecmp (NULL,
1104 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1105 "strftime:CUSTOM:first:inrange");
1106 qof_date_free (qd);
1107 qof_time_free (qt);
1108 qd = qof_date_new ();
1109 qd->qd_year = 3005;
1110 qd->qd_mon = 07;
1111 qd->qd_mday = 24;
1112 qd->qd_hour = 06;
1113 qd->qd_min = 34;
1114 qd->qd_sec = 26;
1115 do_test ((qof_date_valid (qd)), "date not valid");
1116 qt = qof_date_to_qtime (qd);
1117 do_test ((0 == safe_strcasecmp ("07/24/3005",
1118 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1119 "strftime:US:forward");
1120 do_test ((0 == safe_strcasecmp ("24/07/3005",
1121 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1122 "strftime:UK:forward");
1123 do_test ((0 == safe_strcasecmp ("24.07.3005",
1124 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1125 "strftime:CE:forward");
1126 do_test ((0 == safe_strcasecmp ("3005-07-24",
1127 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1128 "strftime:ISO:forward");
1129 do_test ((0 == safe_strcasecmp ("3005-07-24T06:34:26Z",
1130 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1131 "strftime:UTC:forward");
1132 do_test ((0 != safe_strcasecmp (NULL,
1133 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1134 "strftime:LOCALE:forward:inrange");
1135 do_test ((0 != safe_strcasecmp (NULL,
1136 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1137 "strftime:CUSTOM:forward:inrange");
1138 qof_date_free (qd);
1139 qd = qof_date_new ();
1140 qd->qd_year = 4500;
1141 qd->qd_mon = 07;
1142 qd->qd_mday = 24;
1143 qd->qd_hour = 06;
1144 qd->qd_min = 34;
1145 qd->qd_sec = 26;
1146 do_test ((qof_date_valid (qd)), "date not valid");
1147 qt = qof_date_to_qtime (qd);
1148 do_test ((0 == safe_strcasecmp ("07/24/4500",
1149 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1150 "strftime:US:fifth");
1151 do_test ((0 == safe_strcasecmp ("24/07/4500",
1152 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1153 "strftime:UK:fifth");
1154 do_test ((0 == safe_strcasecmp ("24.07.4500",
1155 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1156 "strftime:CE:fifth");
1157 do_test ((0 == safe_strcasecmp ("4500-07-24",
1158 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1159 "strftime:ISO:fifth");
1160 do_test ((0 == safe_strcasecmp ("4500-07-24T06:34:26Z",
1161 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1162 "strftime:UTC:fifth");
1163 do_test ((0 != safe_strcasecmp (NULL,
1164 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1165 "strftime:LOCALE:fifth:inrange");
1166 do_test ((0 != safe_strcasecmp (NULL,
1167 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1168 "strftime:CUSTOM:fifth:inrange");
1169 qof_date_free (qd);
1170 qd = qof_date_new ();
1171 qd->qd_year = 45000;
1172 qd->qd_mon = 07;
1173 qd->qd_mday = 24;
1174 qd->qd_hour = 06;
1175 qd->qd_min = 34;
1176 qd->qd_sec = 26;
1177 do_test ((qof_date_valid (qd)), "date not valid");
1178 qt = qof_date_to_qtime (qd);
1179 do_test ((0 == safe_strcasecmp ("07/24/45000",
1180 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1181 "strftime:US:forward2");
1182 do_test ((0 == safe_strcasecmp ("24/07/45000",
1183 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1184 "strftime:UK:forward2");
1185 do_test ((0 == safe_strcasecmp ("24.07.45000",
1186 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1187 "strftime:CE:forward2");
1188 do_test ((0 == safe_strcasecmp ("45000-07-24",
1189 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1190 "strftime:ISO:forward2");
1191 do_test ((0 == safe_strcasecmp ("45000-07-24T06:34:26Z",
1192 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1193 "strftime:UTC:forward2");
1194 do_test ((0 != safe_strcasecmp (NULL,
1195 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1196 "strftime:LOCALE:forward2:outofrange");
1197 do_test ((0 != safe_strcasecmp (NULL,
1198 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1199 "strftime:CUSTOM:forward2:outofrange");
1200 qof_date_free (qd);
1201 g_free (str1);
1202 qd = qof_date_new ();
1203 qd->qd_year = 1914;
1204 qd->qd_mon = 11;
1205 qd->qd_mday = 11;
1206 qd->qd_hour = 11;
1207 do_test ((qof_date_valid (qd)), "date not valid");
1208 qt = qof_date_to_qtime (qd);
1209 str1 = qof_date_print (qd, QOF_DATE_FORMAT_UTC);
1210 do_test ((0 == safe_strcasecmp (str1,
1211 "1914-11-11T11:00:00Z")), "armistice day");
1212 do_test ((0 == safe_strcasecmp ("11/11/1914",
1213 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1214 "strftime:US:second");
1215 do_test ((0 == safe_strcasecmp ("11/11/1914",
1216 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1217 "strftime:UK:second");
1218 do_test ((0 == safe_strcasecmp ("11.11.1914",
1219 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1220 "strftime:CE:second");
1221 do_test ((0 == safe_strcasecmp ("1914-11-11",
1222 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1223 "strftime:ISO:second");
1224 do_test ((0 == safe_strcasecmp ("1914-11-11T11:00:00Z",
1225 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1226 "strftime:UTC:second");
1227 do_test ((0 != safe_strcasecmp (NULL,
1228 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1229 "strftime:LOCALE:second:inrange");
1230 do_test ((0 != safe_strcasecmp (NULL,
1231 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1232 "strftime:CUSTOM:second:inrange");
1233 qof_date_free (qd);
1234 qof_time_free (qt);
1235 qd = qof_date_new ();
1236 qd->qd_year = 1812;
1237 qd->qd_mon = 07;
1238 qd->qd_mday = 24;
1239 qd->qd_hour = 06;
1240 qd->qd_min = 34;
1241 qd->qd_sec = 26;
1242 do_test ((qof_date_valid (qd)), "date not valid");
1243 qt = qof_date_to_qtime (qd);
1244 do_test ((0 == safe_strcasecmp ("07/24/1812",
1245 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1246 "strftime:US:third");
1247 do_test ((0 == safe_strcasecmp ("24/07/1812",
1248 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1249 "strftime:UK:third");
1250 do_test ((0 == safe_strcasecmp ("24.07.1812",
1251 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1252 "strftime:CE:third");
1253 do_test ((0 == safe_strcasecmp ("1812-07-24",
1254 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1255 "strftime:ISO:third");
1256 do_test ((0 == safe_strcasecmp ("1812-07-24T06:34:26Z",
1257 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1258 "strftime:UTC:third");
1259 do_test ((0 == safe_strcasecmp (NULL,
1260 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1261 "strftime:LOCALE:third:outofrange");
1262 do_test ((0 == safe_strcasecmp (NULL,
1263 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1264 "strftime:CUSTOM:third:outofrange");
1265 qof_date_free (qd);
1266 qd = qof_date_new ();
1267 qd->qd_year = 1066;
1268 qd->qd_mon = 07;
1269 qd->qd_mday = 24;
1270 qd->qd_hour = 06;
1271 qd->qd_min = 34;
1272 qd->qd_sec = 26;
1273 do_test ((qof_date_valid (qd)), "date not valid");
1274 qt = qof_date_to_qtime (qd);
1275 do_test ((0 == safe_strcasecmp ("07/24/1066",
1276 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1277 "strftime:US:fourth");
1278 do_test ((0 == safe_strcasecmp ("24/07/1066",
1279 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1280 "strftime:UK:fourth");
1281 do_test ((0 == safe_strcasecmp ("24.07.1066",
1282 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1283 "strftime:CE:fourth");
1284 do_test ((0 == safe_strcasecmp ("1066-07-24",
1285 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1286 "strftime:ISO:fourth");
1287 do_test ((0 == safe_strcasecmp ("1066-07-24T06:34:26Z",
1288 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1289 "strftime:UTC:fourth");
1290 do_test ((0 == safe_strcasecmp (NULL,
1291 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1292 "strftime:LOCALE:fourth:outofrange");
1293 do_test ((0 == safe_strcasecmp (NULL,
1294 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1295 "strftime:CUSTOM:fourth:outofrange");
1296 qof_date_free (qd);
1297 qd = qof_date_new ();
1298 qd->qd_year = -45;
1299 qd->qd_mon = 07;
1300 qd->qd_mday = 24;
1301 qd->qd_hour = 06;
1302 qd->qd_min = 34;
1303 qd->qd_sec = 26;
1304 do_test ((qof_date_valid (qd)), "date not valid");
1305 qt = qof_date_to_qtime (qd);
1306 do_test ((0 == safe_strcasecmp ("07/24/-045",
1307 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1308 "strftime:US:fifth");
1309 do_test ((0 == safe_strcasecmp ("24/07/-045",
1310 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1311 "strftime:UK:fifth");
1312 do_test ((0 == safe_strcasecmp ("24.07.-045",
1313 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1314 "strftime:CE:fifth");
1315 do_test ((0 == safe_strcasecmp ("-045-07-24",
1316 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1317 "strftime:ISO:fifth");
1318 do_test ((0 == safe_strcasecmp ("-045-07-24T06:34:26Z",
1319 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1320 "strftime:UTC:fifth");
1321 do_test ((0 == safe_strcasecmp (NULL,
1322 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1323 "strftime:LOCALE:fifth:outofrange");
1324 do_test ((0 == safe_strcasecmp (NULL,
1325 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1326 "strftime:CUSTOM:fifth:outofrange");
1327 qof_date_free (qd);
1328 qd = qof_date_new ();
1329 qd->qd_year = -4500;
1330 qd->qd_mon = 07;
1331 qd->qd_mday = 24;
1332 qd->qd_hour = 06;
1333 qd->qd_min = 34;
1334 qd->qd_sec = 26;
1335 do_test ((qof_date_valid (qd)), "date not valid");
1336 qt = qof_date_to_qtime (qd);
1337 do_test ((0 == safe_strcasecmp ("07/24/-4500",
1338 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1339 "strftime:US:sixth");
1340 do_test ((0 == safe_strcasecmp ("24/07/-4500",
1341 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1342 "strftime:UK:sixth");
1343 do_test ((0 == safe_strcasecmp ("24.07.-4500",
1344 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1345 "strftime:CE:sixth");
1346 do_test ((0 == safe_strcasecmp ("-4500-07-24",
1347 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1348 "strftime:ISO:sixth");
1349 do_test ((0 == safe_strcasecmp ("-4500-07-24T06:34:26Z",
1350 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1351 "strftime:UTC:sixth");
1352 do_test ((0 == safe_strcasecmp (NULL,
1353 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1354 "strftime:LOCALE:sixth:outofrange");
1355 do_test ((0 == safe_strcasecmp (NULL,
1356 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1357 "strftime:CUSTOM:sixth:outofrange");
1358 qof_date_free (qd);
1359 qd = qof_date_new ();
1360 qd->qd_year = -4500000;
1361 qd->qd_mon = 07;
1362 qd->qd_mday = 24;
1363 qd->qd_hour = 06;
1364 qd->qd_min = 34;
1365 qd->qd_sec = 26;
1366 do_test ((qof_date_valid (qd)), "date not valid");
1367 qt = qof_date_to_qtime (qd);
1368 do_test ((0 == safe_strcasecmp ("07/24/-4500000",
1369 qof_date_print (qd, QOF_DATE_FORMAT_US))),
1370 "strftime:US:seventh");
1371 do_test ((0 == safe_strcasecmp ("24/07/-4500000",
1372 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
1373 "strftime:UK:seventh");
1374 do_test ((0 == safe_strcasecmp ("24.07.-4500000",
1375 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
1376 "strftime:CE:seventh");
1377 do_test ((0 == safe_strcasecmp ("-4500000-07-24",
1378 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
1379 "strftime:ISO:seventh");
1380 do_test ((0 == safe_strcasecmp ("-4500000-07-24T06:34:26Z",
1381 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
1382 "strftime:UTC:seventh");
1383 do_test ((0 == safe_strcasecmp (NULL,
1384 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
1385 "strftime:LOCALE:seventh:outofrange");
1386 do_test ((0 == safe_strcasecmp (NULL,
1387 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
1388 "strftime:CUSTOM:seventh:outofrange");
1389 qof_date_free (qd);
1390 }
1391 }
1392
1393 static void
1394 run_qoftime_test (void)
1395 {
1396 QofTime *time, *cmp, *diff;
1397
1398 time = qof_time_new ();
1399 do_test ((time != NULL), "failed to initialise QofTime.");
1400 /* basic tests */
1401 {
1402 qof_time_set_secs (time, 796179600);
1403 do_test ((qof_time_get_secs (time) > 0), "failed to set secs");
1404 do_test ((time != NULL), "error found");
1405 qof_time_set_secs (time, 0);
1406 qof_time_set_nanosecs (time, 2041020040);
1407 do_test ((qof_time_get_secs (time) > 0), "failed to normalise.");
1408 do_test ((time != NULL), "error found");
1409 do_test ((qof_time_get_nanosecs (time) > 0),
1410 "failed to set nanosecs");
1411 }
1412 /* calculation and comparison tests */
1413 {
1414 qof_time_add_secs (time, -1143943200);
1415 do_test ((qof_time_get_secs (time) < 0), "failed to subtract.");
1416 do_test ((time != NULL), "error found");
1417 cmp = qof_time_new ();
1418 qof_time_set_secs (cmp, 0);
1419 do_test ((qof_time_equal (time, cmp) == FALSE), "test equal failed.");
1420 do_test ((qof_time_cmp (time, cmp) == -1), "compare cmp test");
1421 diff = qof_time_new ();
1422 qof_time_set_secs (diff, qof_time_get_secs (cmp));
1423 do_test ((qof_time_cmp (diff, cmp) == 0), "compare diff test");
1424 qof_time_free (diff);
1425 diff = qof_time_diff (time, cmp);
1426 do_test ((qof_time_get_secs (diff) < 0), "diff of negative value");
1427 qof_time_free (diff);
1428 diff = qof_time_diff (cmp, time);
1429 do_test ((qof_time_get_secs (diff) > 0), "diff of negative value");
1430 time = qof_time_abs (time);
1431 do_test ((qof_time_get_nanosecs (time) != 0), "abs failed");
1432 qof_time_set_secs (cmp, qof_time_get_secs (time));
1433 qof_time_set_nanosecs (cmp, qof_time_get_nanosecs (time));
1434 do_test ((qof_time_equal (cmp, time) == TRUE),
1435 "equality test failed");
1436 qof_time_free (cmp);
1437 qof_time_free (time);
1438 }
1439 /* gdate basic tests */
1440 {
1441 GDate *date, *test;
1442 GTimeVal gtv;
1443
1444 date = g_date_new_dmy (15, 5, 2006);
1445 time = qof_time_from_gdate (date);
1446 test = qof_time_to_gdate (time);
1447 do_test ((g_date_get_day (test) == 15), "gdate day fail");
1448 do_test ((g_date_get_month (test) == 5), "gdate month fail");
1449 do_test ((g_date_get_year (test) == 2006), "gdate year fail");
1450 do_test ((qof_time_to_gtimeval (time, &gtv)), "gtimeval fail");
1451 {
1452 QofTime *copy, *diff;
1453
1454 qof_time_add_secs (time, 26451);
1455 copy = qof_time_add_secs_copy (time, -26451);
1456 diff = qof_time_diff (time, copy);
1457 do_test ((qof_time_get_secs (diff) == 26451),
1458 "add seconds failed");
1459 qof_time_free (copy);
1460 qof_time_free (diff);
1461 }
1462 g_date_free (date);
1463 g_date_free (test);
1464 qof_time_free (time);
1465 }
1466 /* gdate comparison tests */
1467 {
1468 GDate *date, *cmp;
1469 gint64 time_secs, diff;
1470 glong time_nano;
1471
1472 date = g_date_new_dmy (15, 5, 2006);
1473 time = qof_time_from_gdate (date);
1474 cmp = qof_time_to_gdate (time);
1475 do_test ((g_date_compare (date, cmp) == 0),
1476 "convert to and from gdate failed");
1477 g_date_free (cmp);
1478 time_secs = qof_time_get_secs (time);
1479 time_nano = qof_time_get_nanosecs (time);
1480 qof_time_add_secs (time, 7252);
1481 qof_time_set_nanosecs (time, 123456);
1482 do_test ((qof_time_set_day_start (time)), "set_day_start failed");
1483 do_test ((qof_time_get_secs (time) == time_secs),
1484 "start of day incorrect");
1485 do_test ((qof_time_get_nanosecs (time) == 0),
1486 "set nano at day start incorrect");
1487 do_test ((qof_time_set_day_middle (time) == TRUE),
1488 "set_day_middle failed");
1489 diff = qof_time_get_secs (time) - time_secs;
1490 do_test (diff == (SECS_PER_DAY / 2), "middle of day incorrect");
1491 /* convert middle of day back to date */
1492 cmp = qof_time_to_gdate (time);
1493 do_test ((g_date_compare (date, cmp) == 0),
1494 "middle of day not the same as original day");
1495 g_date_free (cmp);
1496
1497 do_test ((qof_time_set_day_end (time)), "set_day_end failed");
1498 do_test ((qof_time_get_secs (time) - time_secs)
1499 == (SECS_PER_DAY - 1), "end of day incorrect");
1500
1501 /* convert end of day back to date */
1502 cmp = qof_time_to_gdate (time);
1503
1504 do_test ((g_date_compare (date, cmp) == 0),
1505 "end of day not the same as original day");
1506 qof_time_free (time);
1507 g_date_free (cmp);
1508 g_date_free (date);
1509 }
1510 /* QofTime today tests */
1511 {
1512 GTimeVal *current;
1513 gint64 time_secs;
1514 QofTime *diff;
1515
1516 current = qof_time_get_current_start ();
1517 diff = qof_time_new ();
1518 qof_time_from_gtimeval (diff, current);
1519 time_secs = qof_time_get_secs (diff);
1520 time = qof_time_get_today_start ();
1521 do_test ((qof_time_get_secs (time) == time_secs),
1522 "start of day incorrect");
1523 do_test ((qof_time_get_nanosecs (time) == 0),
1524 "today start nanosecs non zero");
1525 qof_time_free (time);
1526 time = qof_time_get_today_end ();
1527 time_secs += SECS_PER_DAY - 1;
1528 do_test ((qof_time_get_secs (time) == time_secs),
1529 "start of today incorrect");
1530 do_test ((qof_time_get_nanosecs (time) == 0),
1531 "today start nanosecs non zero");
1532 qof_time_free (time);
1533 }
1534 /* last mday test */
1535 {
1536 GDate *date;
1537 guint8 mday;
1538
1539 date = g_date_new_dmy (15, 5, 2006);
1540 time = qof_time_from_gdate (date);
1541 mday = qof_time_last_mday (time);
1542 do_test ((mday == 31), " wrong last day of May");
1543 }
1544 /* qof_date_get_mday and qof_date_get_yday tests */
1545 {
1546 QofDate *d;
1547 /* Wed 3rd Sep 2003 = 246. */
1548 do_test ((246 == qof_date_get_yday (3, 9, 2003)),
1549 "get year day test, September");
1550 d = qof_date_new ();
1551 d->qd_mday = 3;
1552 d->qd_mon = 9;
1553 d->qd_year = 2003;
1554 do_test ((TRUE == qof_date_valid (d)),
1555 "3/9/2003 not valid");
1556 do_test ((3 == d->qd_wday), "not Wednesday");
1557 qof_date_free (d);
1558 /* Fri 3rd Sep 2004 = 247. */
1559 do_test ((247 == qof_date_get_yday (3, 9, 2004)),
1560 "get year day test, leap year");
1561 d = qof_date_new ();
1562 d->qd_mday = 3;
1563 d->qd_mon = 9;
1564 d->qd_year = 2004;
1565 do_test ((TRUE == qof_date_valid (d)),
1566 "3/9/2003 not valid");
1567 do_test ((5 == d->qd_wday), "not Friday");
1568 qof_date_free (d);
1569 /* Sun 19th May 2002 = 139. */
1570 do_test ((139 == qof_date_get_yday (19, 5, 2002)),
1571 "get year day test, May");
1572 d = qof_date_new ();
1573 d->qd_mday = 19;
1574 d->qd_mon = 5;
1575 d->qd_year = 2002;
1576 do_test ((TRUE == qof_date_valid (d)),
1577 "3/9/2003 not valid");
1578 do_test ((0 == d->qd_wday), "not Sunday");
1579 qof_date_free (d);
1580 /* Wed 19th May 2004 = 140. */
1581 do_test ((140 == qof_date_get_yday (19, 5, 2004)),
1582 "get year day test, May");
1583 d = qof_date_new ();
1584 d->qd_mday = 19;
1585 d->qd_mon = 5;
1586 d->qd_year = 2004;
1587 do_test ((TRUE == qof_date_valid (d)),
1588 "3/9/2003 not valid");
1589 do_test ((3 == d->qd_wday), "not Wednesday, May");
1590 qof_date_free (d);
1591 /* Nov 2003 = 30 */
1592 do_test ((30 == qof_date_get_mday (11, 2003)),
1593 "get days in month, non-leap");
1594 /* Feb 2004 = 29 */
1595 do_test ((29 == qof_date_get_mday (2, 2004)),
1596 "get days in month, leap year");
1597 }
1598 /* time to dmy test */
1599 {
1600 gboolean success;
1601 guint8 day, month;
1602 guint16 year;
1603 GDate *date;
1604 QofTime *reverse;
1605
1606 date = g_date_new_dmy (15, 5, 2006);
1607 time = qof_time_from_gdate (date);
1608 success = qof_time_to_dmy (time, &day, &month, &year);
1609 do_test ((success == TRUE), "time to dmy failed");
1610 do_test ((day == 15), "wrong day calculated");
1611 do_test ((month == 5), "wrong month calculated");
1612 do_test ((year == 2006), "wrong year calculated");
1613 reverse = qof_time_dmy_to_time (day, month, year);
1614 do_test ((qof_time_cmp (time, reverse) == 0), "dmy to time failed");
1615 g_date_free (date);
1616 qof_time_free (time);
1617 qof_time_free (reverse);
1618 }
1619 {
1620 /* day, month, year wrapping */
1621 QofTime *wrap_t;
1622 QofDate *wrap_d;
1623 glong day, month, year;
1624
1625 /* Thu Jul 13 18:20:27 UTC 2006 */
1626 wrap_t = qof_time_set (1152814827, 345634);
1627 wrap_d = qof_date_from_qtime (wrap_t);
1628 PINFO (" base date for section = %s",
1629 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
1630 day = wrap_d->qd_mday;
1631 month = wrap_d->qd_mon;
1632 year = wrap_d->qd_year;
1633 qof_date_free (wrap_d);
1634 /* one day ago */
1635 wrap_d = qof_date_from_qtime (wrap_t);
1636 wrap_d->qd_mday -= 1;
1637 qof_date_valid (wrap_d);
1638 do_test ((wrap_d->qd_mon == month &&
1639 wrap_d->qd_year == year &&
1640 wrap_d->qd_mday == (day - 1)),
1641 " this time on previous day");
1642 PINFO (" same time previous day = %s",
1643 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
1644 qof_date_free (wrap_d);
1645 /* this time last month */
1646 wrap_d = qof_date_from_qtime (wrap_t);
1647 wrap_d->qd_mon -= 1;
1648 qof_date_valid (wrap_d);
1649 do_test ((wrap_d->qd_mon == (month - 1) &&
1650 wrap_d->qd_year == year &&
1651 wrap_d->qd_mday == day),
1652 " this time last month");
1653 PINFO (" same time previous month = %s",
1654 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
1655 qof_date_free (wrap_d);
1656 /* this time last year */
1657 wrap_d = qof_date_from_qtime (wrap_t);
1658 wrap_d->qd_year -= 1;
1659 qof_date_valid (wrap_d);
1660 do_test ((wrap_d->qd_mon == month &&
1661 wrap_d->qd_year == (year - 1) &&
1662 wrap_d->qd_mday == day),
1663 " this time last year");
1664 PINFO (" same time previous year = %s",
1665 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
1666 qof_time_free (wrap_t);
1667 qof_date_free (wrap_d);
1668 }
1669 }
1670
1671 int
1672 main (void)
1673 {
1674 qof_init ();
1675 #ifdef TEST_DEBUG
1676 qof_log_init_filename ("test-date.trace");
1677 qof_log_set_default (QOF_LOG_DETAIL);
1678 ENTER (" ");
1679 #endif
1680 test_date_init ();
1681 run_qoftime_test ();
1682 run_qofdate_test ();
1683 run_print_scan_tests ();
1684 g_list_foreach (test_data, check_date_cycles, NULL);
1685 print_test_results ();
1686 test_date_close ();
1687 #ifdef TEST_DEBUG
1688 LEAVE (" ");
1689 qof_log_shutdown ();
1690 #endif
1691 qof_close ();
1692 exit (get_rv ());
1693 }
QofDate_s::qd_year
gint64 qd_year
Extended version to cope with full range of dates.
Definition: qofdate.h:181
QOF_DATE_FORMAT_UTC
#define QOF_DATE_FORMAT_UTC
QOF UTC format, xsd:date compatible. QOF_UTC_DATE_FORMAT "%Y-%m-%dT%H:%M:%SZ".
Definition: qofdate.h:277
PERR
#define PERR(format, args...)
Definition: qoflog.h:183
qof_date_format_set_current
gboolean qof_date_format_set_current(QofDateFormat df)
Selects one registered date format as the current default.
Definition: qofdate.c:294
qof_time_from_gdate
QofTime * qof_time_from_gdate(GDate *date)
Convert a GDate to a QofTime.
Definition: qoftime.c:312
QOF_DATE_FORMAT_ISO
#define QOF_DATE_FORMAT_ISO
Short ISO form. "%F".
Definition: qofdate.h:268
qof_log_init_filename
void qof_log_init_filename(const gchar *logfilename)
Definition: qoflog.c:144
PINFO
#define PINFO(format, args...)
Definition: qoflog.h:199
qof_date_adddays
gboolean qof_date_adddays(QofDate *qd, gint days)
Add a number of days to a QofDate and normalise.
Definition: qofdate.c:966
qof_date_free
void qof_date_free(QofDate *date)
Definition: qofdate.c:608
qof_date_from_qtime
QofDate * qof_date_from_qtime(const QofTime *qt)
Definition: qofdate.c:850
qof_time_set_day_end
gboolean qof_time_set_day_end(QofTime *qt)
set the given QofTime to the last second of that day.
Definition: qoftime.c:327
qof_date_format_set_name
gboolean qof_date_format_set_name(const gchar *name, QofDateFormat format)
Set a shorthand name for a custom date format.
Definition: qofdate.c:269
qof_date_format_from_name
QofDateFormat qof_date_format_from_name(const gchar *name)
Returns the default date format for a known shorthand name.
Definition: qofdate.c:385
QofDate_s::qd_zone
const gchar * qd_zone
Calculated value based on struct tm.tm_zone.
Definition: qofdate.h:208
qof_time_add_secs_copy
QofTime * qof_time_add_secs_copy(QofTime *qt, QofTimeSecs secs)
Create a new QofTime, secs different to an original.
Definition: qoftime.c:73
qof_date_get_mday
guint8 qof_date_get_mday(gint month, gint64 year)
Definition: qofdate.c:183
qof_time_abs
QofTime * qof_time_abs(QofTime *qt)
Definition: qoftime.c:196
qof_time_get_secs
QofTimeSecs qof_time_get_secs(const QofTime *qt)
Get the number of seconds.
Definition: qoftime.c:133
qof_time_get_today_end
QofTime * qof_time_get_today_end(void)
Definition: qoftime.c:413
qof_time_to_dmy
gboolean qof_time_to_dmy(QofTime *qt, guint8 *day, guint8 *month, guint16 *year)
Definition: qoftime.c:439
safe_strcasecmp
gint safe_strcasecmp(const gchar *da, const gchar *db)
Definition: qofutil.c:92
qof_date_print
gchar * qof_date_print(const QofDate *date, QofDateFormat df)
Convert a QofDate to a timestamp according to the specified date format.
Definition: qofdate.c:547
qof_date_format_set_date_separator
gboolean qof_date_format_set_date_separator(const gchar sep, QofDateFormat df)
Set a locale-specific separator.
Definition: qofdate.c:340
QOF_MOD_DATE
#define QOF_MOD_DATE
Definition: qofdate.h:98
qof_time_set_secs
void qof_time_set_secs(QofTime *qt, QofTimeSecs secs)
Set the number of seconds.
Definition: qoftime.c:117
qof_time_new
QofTime * qof_time_new(void)
create an empty QofTime
Definition: qoftime.c:46
qof_date_format_get_current
QofDateFormat qof_date_format_get_current(void)
returns the current date format.
Definition: qofdate.c:288
qof_log_shutdown
void qof_log_shutdown(void)
Definition: qoflog.c:159
qof_time_to_gdate
GDate * qof_time_to_gdate(QofTime *qt)
Convert QofTime to GDate.
Definition: qoftime.c:299
qof_time_set_day_start
gboolean qof_time_set_day_start(QofTime *qt)
set the given QofTime to the first second of that day.
Definition: qoftime.c:374
qof_time_dmy_to_time
QofTime * qof_time_dmy_to_time(guint8 day, guint8 month, guint16 year)
Definition: qoftime.c:457
qof_time_get_nanosecs
glong qof_time_get_nanosecs(const QofTime *qt)
Get the number of seconds.
Definition: qoftime.c:141
qof_date_format_get_format
const gchar * qof_date_format_get_format(QofDateFormat df)
Retrieve the strftime format string for a registered date format.
Definition: qofdate.c:310
LEAVE
#define LEAVE(format, args...)
Definition: qoflog.h:227
qof_date_to_qtime
QofTime * qof_date_to_qtime(const QofDate *qd)
Definition: qofdate.c:892
QofDate_s
Full range replacement for struct tm.
Definition: qofdate.h:138
qof_date_format_get_date_separator
gchar qof_date_format_get_date_separator(QofDateFormat df)
Return the field separator for the current date format.
Definition: qofdate.c:325
qof_time_set_day_middle
gboolean qof_time_set_day_middle(QofTime *qt)
set the given QofTime to midday on the same day.
Definition: qoftime.c:336
qof_date_parse
QofDate * qof_date_parse(const gchar *str, QofDateFormat df)
Convert a timestamp to a QofTime.
Definition: qofdate.c:525
QOF_DATE_FORMAT_CE
#define QOF_DATE_FORMAT_CE
Contintental European default. "%d.%m.%Y".
Definition: qofdate.h:263
QofDate_s::qd_hour
glong qd_hour
Signed replacement of struct tm.tm_hour.
Definition: qofdate.h:157
qof_time_free
void qof_time_free(QofTime *qt)
Free a QofTime when no longer required.
Definition: qoftime.c:56
QofDate_s::qd_min
glong qd_min
Signed replacement of struct tm.tm_min.
Definition: qofdate.h:150
qof_time_cmp
gint qof_time_cmp(const QofTime *ta, const QofTime *tb)
Definition: qoftime.c:165
QofDate_s::qd_mon
glong qd_mon
Signed replacement of struct tm.tm_mon.
Definition: qofdate.h:171
qof_date_get_yday
guint16 qof_date_get_yday(gint mday, gint month, gint64 year)
Definition: qofdate.c:167
qof_time_add_secs
void qof_time_add_secs(QofTime *qt, QofTimeSecs secs)
Add (or subtract) seconds from a QofTime.
Definition: qoftime.c:65
DEBUG
#define DEBUG(format, args...)
Definition: qoflog.h:208
qof_time_get_today_start
QofTime * qof_time_get_today_start(void)
Definition: qoftime.c:402
qof_time_get_current_start
GTimeVal * qof_time_get_current_start(void)
Definition: qoftime.c:345
QOF_DATE_FORMAT_ISO8601
#define QOF_DATE_FORMAT_ISO8601
Definition: qofdate.h:287
QOF_DATE_FORMAT_UK
#define QOF_DATE_FORMAT_UK
United Kingdom default. "%d/%m/%Y".
Definition: qofdate.h:258
qof_time_equal
gboolean qof_time_equal(const QofTime *ta, const QofTime *tb)
Definition: qoftime.c:148
QOF_DATE_FORMAT_LOCALE
#define QOF_DATE_FORMAT_LOCALE
GNU locale default. "%x".
Definition: qofdate.h:303
qof_time_set_nanosecs
void qof_time_set_nanosecs(QofTime *qt, glong nano)
Set the number of seconds.
Definition: qoftime.c:125
qof_time_last_mday
guint8 qof_time_last_mday(QofTime *qt)
Definition: qoftime.c:423
QofDate_s::qd_sec
gint64 qd_sec
Definition: qofdate.h:143
QofDateFormat
gint QofDateFormat
Definition: qofdate.h:335
qof_time_set
QofTime * qof_time_set(QofTimeSecs t, glong nanosecs)
Definition: qoftime.c:210
qof_date_addmonths
gboolean qof_date_addmonths(QofDate *qd, gint months, gboolean track_last_day)
Definition: qofdate.c:975
qof_log_set_default
void qof_log_set_default(QofLogLevel log_level)
Definition: qoflog.c:232
qof_date_format_to_name
const gchar * qof_date_format_to_name(QofDateFormat format)
Retrieve the shorthand name for the selected date format.
Definition: qofdate.c:254
QofDate_s::qd_nanosecs
glong qd_nanosecs
Definition: qofdate.h:141
SECS_PER_DAY
#define SECS_PER_DAY
Definition: qofdate.h:94
qof_close
void qof_close(void)
Safely close down the Query Object Framework.
Definition: qofutil.c:844
QofTimeSecs
gint64 QofTimeSecs
Replacement for time_t.
Definition: qoftime.h:129
QOF_DATE_FORMAT_CUSTOM
#define QOF_DATE_FORMAT_CUSTOM
Date and time for the current locale "%c".
Definition: qofdate.h:319
QofTime
struct QofTime64 QofTime
Use a 64-bit signed int QofTime.
Definition: qoftime.h:120
qof_date_new
QofDate * qof_date_new(void)
Definition: qofdate.c:573
qof_date_compare
gint qof_date_compare(const QofDate *d1, const QofDate *d2)
Definition: qofdate.c:637
QofDate_s::qd_wday
gshort qd_wday
Definition: qofdate.h:185
qof_time_from_gtimeval
void qof_time_from_gtimeval(QofTime *qt, GTimeVal *gtv)
Convert a QofTime to a GTimeVal.
Definition: qoftime.c:290
QofDate_s::qd_mday
glong qd_mday
Signed replacement of struct tm.tm_mday.
Definition: qofdate.h:164
qof_time_to_gtimeval
gboolean qof_time_to_gtimeval(QofTime *qt, GTimeVal *gtv)
Convert a QofTime to a GTimeVal.
Definition: qoftime.c:272
qof_init
void qof_init(void)
Initialise the Query Object Framework.
Definition: qofutil.c:833
ENTER
#define ENTER(format, args...)
Definition: qoflog.h:217
qof_date_format_add
gboolean qof_date_format_add(const gchar *str, QofDateFormat *identifier)
Add a specific strftime compatible string as a new QofDateFormat.
Definition: qofdate.c:202
QOF_DATE_FORMAT_US
#define QOF_DATE_FORMAT_US
Continental US default. "%m/%d/%Y".
Definition: qofdate.h:253
qof_date_valid
gboolean qof_date_valid(QofDate *date)
Validate a QofDate.
Definition: qofdate.c:616
qof_time_diff
QofTime * qof_time_diff(const QofTime *ta, const QofTime *tb)
difference between two QofTimes.
Definition: qoftime.c:182
QofLogModule
const gchar * QofLogModule
Definition: qofid.h:85
qof_date_from_struct_tm
QofDate * qof_date_from_struct_tm(const struct tm *stm)
Convert a struct tm to a QofDate.
Definition: qofdate.c:679