00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "config.h"
00026 #include <stdio.h>
00027 #include <ctype.h>
00028 #include <glib.h>
00029 #include <time.h>
00030 #include "qof.h"
00031 #include "test-stuff.h"
00032
00033 static gboolean test_data_is_init = FALSE;
00034 static GList *test_data = NULL;
00035 static QofLogModule log_module = QOF_MOD_DATE;
00036
00037 typedef struct
00038 {
00039
00040 QofTime *time;
00041
00042 QofDate *date;
00043
00044 GList *string_list;
00045
00046 const gchar *id;
00047 }QTestDate;
00048
00049 static void
00050 check_date_cycles (gpointer data,
00051 gpointer user_data __attribute__ ((unused)))
00052 {
00053 QofDateFormat df;
00054 gchar *str, *cmp;
00055 QTestDate *d;
00056 gint i;
00057
00058 d = (QTestDate*)data;
00059 df = qof_date_format_get_current ();
00060
00061 {
00062 do_test ((0 == qof_time_cmp (qof_date_to_qtime (d->date),
00063 d->time)), d->id);
00064 do_test ((0 == qof_date_compare (qof_date_from_qtime (d->time),
00065 d->date)), d->id);
00066 }
00067
00068 for (i = 1; i <= 6; i++)
00069 {
00070 str = qof_date_print (d->date, i);
00071 cmp = (gchar*)g_list_nth_data (d->string_list, (i - 1));
00072 if (0 != safe_strcasecmp (str, cmp))
00073 DEBUG (" str=%s cmp=%s", str, cmp);
00074 do_test ((0 == safe_strcasecmp (str, cmp)), d->id);
00075
00076 {
00077 gint result;
00078 QofDate *h, *j;
00079 const gchar * t;
00080 struct tm base;
00081
00082 base.tm_year = base.tm_mday = base.tm_mon = 0;
00083 base.tm_min = base.tm_hour = base.tm_gmtoff = 0;
00084 base.tm_sec = base.tm_isdst = 0;
00085 result = 0;
00087 if ((d->date->qd_year > 0) &&
00088 (d->date->qd_year < 9999))
00089 {
00090 h = qof_date_parse (cmp, i);
00091 do_test ((h != NULL), "no date could be parsed");
00092 if (!h)
00093 PERR (" h failed for str=%s, "
00094 "cmp=%s, %d\n", str, cmp, i);
00095 t = qof_date_format_get_format (i);
00096 strptime (cmp, t, &base);
00097 j = qof_date_from_struct_tm (&base);
00098 do_test ((j != NULL), "no value from struct tm");
00099 if (h)
00100 {
00101 j->qd_nanosecs = h->qd_nanosecs;
00102 result = qof_date_compare (h, j);
00103 do_test ((0 == qof_date_compare (h, j)),
00104 "compare with strptime");
00105 }
00106 if (h)
00107 qof_date_free (h);
00108 }
00109 }
00110 }
00111 qof_date_format_set_current (df);
00112 }
00113
00114 static void
00115 test_date_init (void)
00116 {
00117 test_data_is_init = TRUE;
00118 test_data = NULL;
00119
00120 {
00121 QTestDate *td = g_new0 (QTestDate, 1);
00122 td->time = qof_time_new ();
00123 qof_time_set_secs (td->time, 1147621550);
00124 qof_time_set_nanosecs (td->time, G_GINT64_CONSTANT(1000));
00125 td->date = qof_date_new ();
00126 td->date->qd_year = 2006;
00127 td->date->qd_mon = 5;
00128 td->date->qd_mday = 14;
00129 td->date->qd_hour = 15;
00130 td->date->qd_min = 45;
00131 td->date->qd_sec = G_GINT64_CONSTANT(50);
00132 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00133 qof_date_valid (td->date);
00134 td->string_list = NULL;
00135 td->string_list = g_list_append (td->string_list, "05/14/2006");
00136 td->string_list = g_list_append (td->string_list, "14/05/2006");
00137 td->string_list = g_list_append (td->string_list, "14.05.2006");
00138 td->string_list = g_list_append (td->string_list, "2006-05-14");
00139 td->string_list = g_list_append (td->string_list, "2006-05-14T15:45:50Z");
00140 td->string_list = g_list_append (td->string_list,
00141 "2006-05-14 15:45:50.000001000 +0000");
00142 td->id = "a current time";
00143 test_data = g_list_prepend (test_data, td);
00144 }
00145 {
00146 QTestDate *td = g_new0 (QTestDate, 1);
00147 td->time = qof_time_new ();
00148 qof_time_set_secs (td->time, 1147132800);
00149 qof_time_set_nanosecs (td->time, 2);
00150 td->date = qof_date_new ();
00151 td->date->qd_year = 2006;
00152 td->date->qd_mon = 5;
00153 td->date->qd_mday = 9;
00154 td->date->qd_hour = 0;
00155 td->date->qd_min = 0;
00156 td->date->qd_sec = G_GINT64_CONSTANT(0);
00157 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00158 qof_date_valid (td->date);
00159 td->string_list = NULL;
00160 td->string_list = g_list_prepend (td->string_list, "05/09/2006");
00161 td->string_list = g_list_prepend (td->string_list, "09/05/2006");
00162 td->string_list = g_list_prepend (td->string_list, "09.05.2006");
00163 td->string_list = g_list_prepend (td->string_list, "2006-05-09");
00164 td->string_list = g_list_prepend (td->string_list, "2006-05-09T00:00:00Z");
00165 td->string_list = g_list_prepend (td->string_list,
00166 "2006-05-09 00:00:00.000000002 +0000");
00167 td->string_list = g_list_reverse (td->string_list);
00168 td->id = "a recent time";
00169 test_data = g_list_prepend (test_data, td);
00170 }
00171 {
00172 QTestDate *td = g_new0 (QTestDate, 1);
00173 td->time = qof_time_new ();
00174 qof_time_set_secs (td->time, 1147186144);
00175 qof_time_set_nanosecs (td->time, 100);
00176 td->date = qof_date_new ();
00177 td->date->qd_year = 2006;
00178 td->date->qd_mon = 5;
00179 td->date->qd_mday = 9;
00180 td->date->qd_hour = 14;
00181 td->date->qd_min = 49;
00182 td->date->qd_sec = G_GINT64_CONSTANT(4);
00183 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00184 qof_date_valid (td->date);
00185 td->string_list = NULL;
00186 td->string_list = g_list_prepend (td->string_list, "05/09/2006");
00187 td->string_list = g_list_prepend (td->string_list, "09/05/2006");
00188 td->string_list = g_list_prepend (td->string_list, "09.05.2006");
00189 td->string_list = g_list_prepend (td->string_list, "2006-05-09");
00190 td->string_list = g_list_prepend (td->string_list, "2006-05-09T14:49:04Z");
00191 td->string_list = g_list_prepend (td->string_list,
00192 "2006-05-09 14:49:04.000000100 +0000");
00193 td->string_list = g_list_reverse (td->string_list);
00194 td->id = "second recent time";
00195 test_data = g_list_prepend (test_data, td);
00196 }
00197 {
00198 QTestDate *td = g_new0 (QTestDate, 1);
00199 td->time = qof_time_new ();
00200 qof_time_set_secs (td->time, 63039600);
00201 qof_time_set_nanosecs (td->time, 4);
00202 td->date = qof_date_new ();
00203 td->date->qd_year = 1971;
00204 td->date->qd_mon = 12;
00205 td->date->qd_mday = 31;
00206 td->date->qd_hour = 15;
00207 td->date->qd_min = 0;
00208 td->date->qd_sec = G_GINT64_CONSTANT(0);
00209 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00210 qof_date_valid (td->date);
00211 td->string_list = NULL;
00212 td->string_list = g_list_prepend (td->string_list, "12/31/1971");
00213 td->string_list = g_list_prepend (td->string_list, "31/12/1971");
00214 td->string_list = g_list_prepend (td->string_list, "31.12.1971");
00215 td->string_list = g_list_prepend (td->string_list, "1971-12-31");
00216 td->string_list = g_list_prepend (td->string_list, "1971-12-31T15:00:00Z");
00217 td->string_list = g_list_prepend (td->string_list,
00218 "1971-12-31 15:00:00.000000004 +0000");
00219 td->string_list = g_list_reverse (td->string_list);
00220 td->id = "New Year's Eve 1971";
00221 test_data = g_list_prepend (test_data, td);
00222 }
00223 {
00224 QTestDate *td = g_new0 (QTestDate, 1);
00225 td->time = qof_time_new ();
00226 qof_time_set_secs (td->time, 315532800);
00227 qof_time_set_nanosecs (td->time, 123456789);
00228 td->date = qof_date_new ();
00229 td->date->qd_year = 1980;
00230 td->date->qd_mon = 1;
00231 td->date->qd_mday = 1;
00232 td->date->qd_hour = 0;
00233 td->date->qd_min = 0;
00234 td->date->qd_sec = G_GINT64_CONSTANT(0);
00235 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00236 qof_date_valid (td->date);
00237 td->string_list = NULL;
00238 td->string_list = g_list_prepend (td->string_list, "01/01/1980");
00239 td->string_list = g_list_prepend (td->string_list, "01/01/1980");
00240 td->string_list = g_list_prepend (td->string_list, "01.01.1980");
00241 td->string_list = g_list_prepend (td->string_list, "1980-01-01");
00242 td->string_list = g_list_prepend (td->string_list, "1980-01-01T00:00:00Z");
00243 td->string_list = g_list_prepend (td->string_list,
00244 "1980-01-01 00:00:00.123456789 +0000");
00245 td->string_list = g_list_reverse (td->string_list);
00246 td->id = "New Year's Day 1980";
00247 test_data = g_list_prepend (test_data, td);
00248 }
00249 {
00250 QTestDate *td = g_new0 (QTestDate, 1);
00251 td->time = qof_time_new ();
00252 qof_time_set_secs (td->time, 946684799);
00253 qof_time_set_nanosecs (td->time, 987654321);
00254 td->date = qof_date_new ();
00255 td->date->qd_year = 1999;
00256 td->date->qd_mon = 12;
00257 td->date->qd_mday = 31;
00258 td->date->qd_hour = 23;
00259 td->date->qd_min = 59;
00260 td->date->qd_sec = G_GINT64_CONSTANT(59);
00261 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00262 qof_date_valid (td->date);
00263 td->string_list = NULL;
00264 td->string_list = g_list_prepend (td->string_list, "12/31/1999");
00265 td->string_list = g_list_prepend (td->string_list, "31/12/1999");
00266 td->string_list = g_list_prepend (td->string_list, "31.12.1999");
00267 td->string_list = g_list_prepend (td->string_list, "1999-12-31");
00268 td->string_list = g_list_prepend (td->string_list, "1999-12-31T23:59:59Z");
00269 td->string_list = g_list_prepend (td->string_list,
00270 "1999-12-31 23:59:59.987654321 +0000");
00271 td->string_list = g_list_reverse (td->string_list);
00272 td->id = "Millenium Eve";
00273 test_data = g_list_prepend (test_data, td);
00274 }
00275 {
00276 QTestDate *td = g_new0 (QTestDate, 1);
00277 td->time = qof_time_new ();
00278 qof_time_set_secs (td->time, 699378323);
00279 qof_time_set_nanosecs (td->time, 90000);
00280 td->date = qof_date_new ();
00281 td->date->qd_year = 1992;
00282 td->date->qd_mon = 2;
00283 td->date->qd_mday = 29;
00284 td->date->qd_hour = 15;
00285 td->date->qd_min = 45;
00286 td->date->qd_sec = G_GINT64_CONSTANT(23);
00287 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00288 qof_date_valid (td->date);
00289 td->string_list = NULL;
00290 td->string_list = g_list_prepend (td->string_list, "02/29/1992");
00291 td->string_list = g_list_prepend (td->string_list, "29/02/1992");
00292 td->string_list = g_list_prepend (td->string_list, "29.02.1992");
00293 td->string_list = g_list_prepend (td->string_list, "1992-02-29");
00294 td->string_list = g_list_prepend (td->string_list, "1992-02-29T15:45:23Z");
00295 td->string_list = g_list_prepend (td->string_list,
00296 "1992-02-29 15:45:23.000090000 +0000");
00297 td->string_list = g_list_reverse (td->string_list);
00298 td->id = "29th February 1992";
00299 test_data = g_list_prepend (test_data, td);
00300 }
00301 {
00302
00303 QTestDate *td = g_new0 (QTestDate, 1);
00304 td->time = qof_time_new ();
00305 qof_time_set_secs (td->time, -1);
00306 qof_time_set_nanosecs (td->time, 9);
00307 td->date = qof_date_new ();
00308 td->date->qd_year = 1969;
00309 td->date->qd_mon = 12;
00310 td->date->qd_mday = 31;
00311 td->date->qd_hour = 23;
00312 td->date->qd_min = 59;
00313 td->date->qd_sec = G_GINT64_CONSTANT(59);
00314 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00315 qof_date_valid (td->date);
00316 td->string_list = NULL;
00317 td->string_list = g_list_prepend (td->string_list, "12/31/1969");
00318 td->string_list = g_list_prepend (td->string_list, "31/12/1969");
00319 td->string_list = g_list_prepend (td->string_list, "31.12.1969");
00320 td->string_list = g_list_prepend (td->string_list, "1969-12-31");
00321 td->string_list = g_list_prepend (td->string_list, "1969-12-31T23:59:59Z");
00322 td->string_list = g_list_prepend (td->string_list,
00323 "1969-12-31 23:59:59.000000009 +0000");
00324 td->string_list = g_list_reverse (td->string_list);
00325 td->id = "epoch eve";
00326 test_data = g_list_prepend (test_data, td);
00327 }
00328 {
00329 QTestDate *td = g_new0 (QTestDate, 1);
00330 td->time = qof_time_new ();
00331 qof_time_set_secs (td->time, -192776400);
00332 qof_time_set_nanosecs (td->time, 818818818);
00333 td->date = qof_date_new ();
00334 td->date->qd_year = 1963;
00335 td->date->qd_mon = 11;
00336 td->date->qd_mday = 22;
00337 td->date->qd_hour = 19;
00338 td->date->qd_min = 0;
00339 td->date->qd_sec = G_GINT64_CONSTANT(0);
00340 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00341 qof_date_valid (td->date);
00342 td->string_list = NULL;
00343 td->string_list = g_list_prepend (td->string_list, "11/22/1963");
00344 td->string_list = g_list_prepend (td->string_list, "22/11/1963");
00345 td->string_list = g_list_prepend (td->string_list, "22.11.1963");
00346 td->string_list = g_list_prepend (td->string_list, "1963-11-22");
00347 td->string_list = g_list_prepend (td->string_list, "1963-11-22T19:00:00Z");
00348 td->string_list = g_list_prepend (td->string_list,
00349 "1963-11-22 19:00:00.818818818 +0000");
00350 td->string_list = g_list_reverse (td->string_list);
00351 td->id = "approx JFK, 1963";
00352 test_data = g_list_prepend (test_data, td);
00353 }
00354 {
00355 QTestDate *td = g_new0 (QTestDate, 1);
00356 td->time = qof_time_new ();
00357 qof_time_set_secs (td->time, -767311080);
00358 qof_time_set_nanosecs (td->time, 0);
00359 td->date = qof_date_new ();
00360 td->date->qd_year = 1945;
00361 td->date->qd_mon = 9;
00362 td->date->qd_mday = 8;
00363 td->date->qd_hour = 2;
00364 td->date->qd_min = 2;
00365 td->date->qd_sec = G_GINT64_CONSTANT(0);
00366 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00367 qof_date_valid (td->date);
00368 td->string_list = NULL;
00369 td->string_list = g_list_prepend (td->string_list, "09/08/1945");
00370 td->string_list = g_list_prepend (td->string_list, "08/09/1945");
00371 td->string_list = g_list_prepend (td->string_list, "08.09.1945");
00372 td->string_list = g_list_prepend (td->string_list, "1945-09-08");
00373 td->string_list = g_list_prepend (td->string_list, "1945-09-08T02:02:00Z");
00374 td->string_list = g_list_prepend (td->string_list,
00375 "1945-09-08 02:02:00.000000000 +0000");
00376 td->string_list = g_list_reverse (td->string_list);
00377 td->id = "Nagasaki, 1945";
00378 test_data = g_list_prepend (test_data, td);
00379 }
00380 {
00381 QTestDate *td = g_new0 (QTestDate, 1);
00382 td->time = qof_time_new ();
00383 qof_time_set_secs (td->time, -1613826000);
00384 qof_time_set_nanosecs (td->time, 0);
00385 td->date = qof_date_new ();
00386 td->date->qd_year = 1918;
00387 td->date->qd_mon = 11;
00388 td->date->qd_mday = 11;
00389 td->date->qd_hour = 11;
00390 td->date->qd_min = 0;
00391 td->date->qd_sec = G_GINT64_CONSTANT(0);
00392 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00393 qof_date_valid (td->date);
00394 td->string_list = NULL;
00395 td->string_list = g_list_prepend (td->string_list, "11/11/1918");
00396 td->string_list = g_list_prepend (td->string_list, "11/11/1918");
00397 td->string_list = g_list_prepend (td->string_list, "11.11.1918");
00398 td->string_list = g_list_prepend (td->string_list, "1918-11-11");
00399 td->string_list = g_list_prepend (td->string_list, "1918-11-11T11:00:00Z");
00400 td->string_list = g_list_prepend (td->string_list,
00401 "1918-11-11 11:00:00.000000000 +0000");
00402 td->string_list = g_list_reverse (td->string_list);
00403 td->id = "Armistice 1918";
00404 test_data = g_list_prepend (test_data, td);
00405 }
00406 {
00407 QTestDate *td = g_new0 (QTestDate, 1);
00408 td->time = qof_time_new ();
00409 qof_time_set_secs (td->time,
00410 G_GINT64_CONSTANT(-2208988801));
00411 qof_time_set_nanosecs (td->time, 0);
00412 td->date = qof_date_new ();
00413 td->date->qd_year = 1899;
00414 td->date->qd_mon = 12;
00415 td->date->qd_mday = 31;
00416 td->date->qd_hour = 23;
00417 td->date->qd_min = 59;
00418 td->date->qd_sec = G_GINT64_CONSTANT(59);
00419 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00420 qof_date_valid (td->date);
00421 td->string_list = NULL;
00422 td->string_list = g_list_prepend (td->string_list, "12/31/1899");
00423 td->string_list = g_list_prepend (td->string_list, "31/12/1899");
00424 td->string_list = g_list_prepend (td->string_list, "31.12.1899");
00425 td->string_list = g_list_prepend (td->string_list, "1899-12-31");
00426 td->string_list = g_list_prepend (td->string_list, "1899-12-31T23:59:59Z");
00427 td->string_list = g_list_prepend (td->string_list,
00428 "1899-12-31 23:59:59.000000000 +0000");
00429 td->string_list = g_list_reverse (td->string_list);
00430 td->id = "19th century Millenium Eve";
00431 test_data = g_list_prepend (test_data, td);
00432 }
00433 {
00434 QTestDate *td = g_new0 (QTestDate, 1);
00435 td->time = qof_time_new ();
00436 qof_time_set_secs (td->time,
00437 G_GINT64_CONSTANT(-13311993599));
00438 qof_time_set_nanosecs (td->time, 0);
00439 td->date = qof_date_new ();
00440 td->date->qd_year = 1548;
00441 td->date->qd_mon = 2;
00442 td->date->qd_mday = 29;
00443 td->date->qd_hour = 0;
00444 td->date->qd_min = 0;
00445 td->date->qd_sec = G_GINT64_CONSTANT(1);
00446 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00447 qof_date_valid (td->date);
00448 td->string_list = NULL;
00449 td->string_list = g_list_prepend (td->string_list, "02/29/1548");
00450 td->string_list = g_list_prepend (td->string_list, "29/02/1548");
00451 td->string_list = g_list_prepend (td->string_list, "29.02.1548");
00452 td->string_list = g_list_prepend (td->string_list, "1548-02-29");
00453 td->string_list = g_list_prepend (td->string_list, "1548-02-29T00:00:01Z");
00454 td->string_list = g_list_prepend (td->string_list,
00455 "1548-02-29 00:00:01.000000000 +0000");
00456 td->string_list = g_list_reverse (td->string_list);
00457 td->id = "16th century leap day";
00458 test_data = g_list_prepend (test_data, td);
00459 }
00460 {
00461 QTestDate *td = g_new0 (QTestDate, 1);
00462 td->time = qof_time_new ();
00463 qof_time_set_secs (td->time, G_GINT64_CONSTANT(-28502726400));
00464 qof_time_set_nanosecs (td->time, 0);
00465 td->date = qof_date_new ();
00466 td->date->qd_year = 1066;
00467 td->date->qd_mon = 10;
00468 td->date->qd_mday = 14;
00469 td->date->qd_hour = 8;
00470 td->date->qd_min = 0;
00471 td->date->qd_sec = G_GINT64_CONSTANT(0);
00472 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00473 qof_date_valid (td->date);
00474 td->string_list = NULL;
00475 td->string_list = g_list_prepend (td->string_list, "10/14/1066");
00476 td->string_list = g_list_prepend (td->string_list, "14/10/1066");
00477 td->string_list = g_list_prepend (td->string_list, "14.10.1066");
00478 td->string_list = g_list_prepend (td->string_list, "1066-10-14");
00479 td->string_list = g_list_prepend (td->string_list, "1066-10-14T08:00:00Z");
00480 td->string_list = g_list_prepend (td->string_list,
00481 "1066-10-14 08:00:00.000000000 +0000");
00482 td->string_list = g_list_reverse (td->string_list);
00483 td->id = "Battle of Hastings, 1066";
00484 test_data = g_list_prepend (test_data, td);
00485 }
00486 {
00487 QTestDate *td = g_new0 (QTestDate, 1);
00488 td->time = qof_time_new ();
00489 qof_time_set_secs (td->time,
00490 G_GINT64_CONSTANT(-36417340799));
00491 qof_time_set_nanosecs (td->time, 0);
00492 td->date = qof_date_new ();
00493 td->date->qd_year = 815;
00494 td->date->qd_mon = 12;
00495 td->date->qd_mday = 25;
00496 td->date->qd_hour = 0;
00497 td->date->qd_min = 0;
00498 td->date->qd_sec = G_GINT64_CONSTANT(1);
00499 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00500 qof_date_valid (td->date);
00501 td->string_list = NULL;
00502 td->string_list = g_list_prepend (td->string_list, "12/25/0815");
00503 td->string_list = g_list_prepend (td->string_list, "25/12/0815");
00504 td->string_list = g_list_prepend (td->string_list, "25.12.0815");
00505 td->string_list = g_list_prepend (td->string_list, "0815-12-25");
00506 td->string_list = g_list_prepend (td->string_list, "0815-12-25T00:00:01Z");
00507 td->string_list = g_list_prepend (td->string_list,
00508 "0815-12-25 00:00:01.000000000 +0000");
00509 td->string_list = g_list_reverse (td->string_list);
00510 td->id = "9th century Christmas Day";
00511 test_data = g_list_prepend (test_data, td);
00512 }
00513 {
00514 QTestDate *td = g_new0 (QTestDate, 1);
00515 td->time = qof_time_new ();
00516 qof_time_set_secs (td->time, G_GINT64_CONSTANT(-60798160800));
00517 qof_time_set_nanosecs (td->time, 0);
00518 td->date = qof_date_new ();
00519 td->date->qd_year = 43;
00520 td->date->qd_mon = 5;
00521 td->date->qd_mday = 20;
00522 td->date->qd_hour = 14;
00523 td->date->qd_min = 0;
00524 td->date->qd_sec = G_GINT64_CONSTANT(0);
00525 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00526 qof_date_valid (td->date);
00527 td->string_list = NULL;
00528 td->string_list = g_list_prepend (td->string_list, "05/20/0043");
00529 td->string_list = g_list_prepend (td->string_list, "20/05/0043");
00530 td->string_list = g_list_prepend (td->string_list, "20.05.0043");
00531 td->string_list = g_list_prepend (td->string_list, "0043-05-20");
00532 td->string_list = g_list_prepend (td->string_list, "0043-05-20T14:00:00Z");
00533 td->string_list = g_list_prepend (td->string_list,
00534 "0043-05-20 14:00:00.000000000 +0000");
00535 td->string_list = g_list_reverse (td->string_list);
00536 td->id = "approx Roman invasion, 43AD";
00537 test_data = g_list_prepend (test_data, td);
00538 }
00543 {
00544 QTestDate *td = g_new0 (QTestDate, 1);
00545 td->time = qof_time_new ();
00546 qof_time_set_secs (td->time, G_GINT64_CONSTANT(-62167824001));
00547 qof_time_set_nanosecs (td->time, 0);
00548 td->date = qof_date_new ();
00549 td->date->qd_year = -1;
00550 td->date->qd_mon = 12;
00551 td->date->qd_mday = 24;
00552 td->date->qd_hour = 23;
00553 td->date->qd_min = 59;
00554 td->date->qd_sec = G_GINT64_CONSTANT(59);
00555 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00556 qof_date_valid (td->date);
00557 td->string_list = NULL;
00558 td->string_list = g_list_prepend (td->string_list, "12/24/-001");
00559 td->string_list = g_list_prepend (td->string_list, "24/12/-001");
00560 td->string_list = g_list_prepend (td->string_list, "24.12.-001");
00561 td->string_list = g_list_prepend (td->string_list, "-001-12-24");
00562 td->string_list = g_list_prepend (td->string_list, "-001-12-24T23:59:59Z");
00563 td->string_list = g_list_prepend (td->string_list,
00564 "-001-12-24 23:59:59.000000000 +0000");
00565 td->string_list = g_list_reverse (td->string_list);
00566 td->id = "Xmas eve, 1BC";
00567 test_data = g_list_prepend (test_data, td);
00568 }
00569 {
00570 QTestDate *td = g_new0 (QTestDate, 1);
00571 td->time = qof_time_new ();
00572 qof_time_set_secs (td->time,
00573 G_GINT64_CONSTANT(-204110409601));
00574 qof_time_set_nanosecs (td->time, 0);
00575 td->date = qof_date_new ();
00576 td->date->qd_year = -4499;
00577 td->date->qd_mon = 12;
00578 td->date->qd_mday = 31;
00579 td->date->qd_hour = 23;
00580 td->date->qd_min = 59;
00581 td->date->qd_sec = G_GINT64_CONSTANT(59);
00582 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00583 qof_date_valid (td->date);
00584 td->string_list = NULL;
00585 td->string_list = g_list_prepend (td->string_list, "12/31/-4499");
00586 td->string_list = g_list_prepend (td->string_list, "31/12/-4499");
00587 td->string_list = g_list_prepend (td->string_list, "31.12.-4499");
00588 td->string_list = g_list_prepend (td->string_list, "-4499-12-31");
00589 td->string_list = g_list_prepend (td->string_list, "-4499-12-31T23:59:59Z");
00590 td->string_list = g_list_prepend (td->string_list,
00591 "-4499-12-31 23:59:59.000000000 +0000");
00592 td->string_list = g_list_reverse (td->string_list);
00593 td->id = "far past.";
00594 test_data = g_list_prepend (test_data, td);
00595 }
00596 {
00597 QTestDate *td = g_new0 (QTestDate, 1);
00598 td->time = qof_time_new ();
00599 qof_time_set_secs (td->time,
00600 G_GINT64_CONSTANT(-2097527529601));
00601 qof_time_set_nanosecs (td->time, 0);
00602 td->date = qof_date_new ();
00603 td->date->qd_year = -64499;
00604 td->date->qd_mon = 12;
00605 td->date->qd_mday = 31;
00606 td->date->qd_hour = 23;
00607 td->date->qd_min = 59;
00608 td->date->qd_sec = G_GINT64_CONSTANT(59);
00609 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00610 qof_date_valid (td->date);
00611 td->string_list = NULL;
00612 td->string_list = g_list_prepend (td->string_list, "12/31/-64499");
00613 td->string_list = g_list_prepend (td->string_list, "31/12/-64499");
00614 td->string_list = g_list_prepend (td->string_list, "31.12.-64499");
00615 td->string_list = g_list_prepend (td->string_list, "-64499-12-31");
00616 td->string_list = g_list_prepend (td->string_list, "-64499-12-31T23:59:59Z");
00617 td->string_list = g_list_prepend (td->string_list,
00618 "-64499-12-31 23:59:59.000000000 +0000");
00619 td->string_list = g_list_reverse (td->string_list);
00620 td->id = "far, far past.";
00621 test_data = g_list_prepend (test_data, td);
00622 }
00623
00624 {
00625 QTestDate *td = g_new0 (QTestDate, 1);
00626 td->time = qof_time_new ();
00627 qof_time_set_secs (td->time, G_GINT64_CONSTANT(32679095666));
00628 qof_time_set_nanosecs (td->time, 800);
00629 td->date = qof_date_new ();
00630 td->date->qd_year = 3005;
00631 td->date->qd_mon = 7;
00632 td->date->qd_mday = 24;
00633 td->date->qd_hour = 6;
00634 td->date->qd_min = 34;
00635 td->date->qd_sec = G_GINT64_CONSTANT(26);
00636 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00637 qof_date_valid (td->date);
00638 td->string_list = NULL;
00639 td->string_list = g_list_prepend (td->string_list, "07/24/3005");
00640 td->string_list = g_list_prepend (td->string_list, "24/07/3005");
00641 td->string_list = g_list_prepend (td->string_list, "24.07.3005");
00642 td->string_list = g_list_prepend (td->string_list, "3005-07-24");
00643 td->string_list = g_list_prepend (td->string_list, "3005-07-24T06:34:26Z");
00644 td->string_list = g_list_prepend (td->string_list,
00645 "3005-07-24 06:34:26.000000800 +0000");
00646 td->string_list = g_list_reverse (td->string_list);
00647 td->id = "24th July 3005";
00648 test_data = g_list_prepend (test_data, td);
00649 }
00650 {
00651 QTestDate *td = g_new0 (QTestDate, 1);
00652 td->time = qof_time_new ();
00653 qof_time_set_secs (td->time,
00654 G_GINT64_CONSTANT(79839129599));
00655 qof_time_set_nanosecs (td->time, 50000);
00656 td->date = qof_date_new ();
00657 td->date->qd_year = 4499;
00658 td->date->qd_mon = 12;
00659 td->date->qd_mday = 31;
00660 td->date->qd_hour = 23;
00661 td->date->qd_min = 59;
00662 td->date->qd_sec = G_GINT64_CONSTANT(59);
00663 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00664 qof_date_valid (td->date);
00665 td->string_list = NULL;
00666 td->string_list = g_list_prepend (td->string_list, "12/31/4499");
00667 td->string_list = g_list_prepend (td->string_list, "31/12/4499");
00668 td->string_list = g_list_prepend (td->string_list, "31.12.4499");
00669 td->string_list = g_list_prepend (td->string_list, "4499-12-31");
00670 td->string_list = g_list_prepend (td->string_list, "4499-12-31T23:59:59Z");
00671 td->string_list = g_list_prepend (td->string_list,
00672 "4499-12-31 23:59:59.000050000 +0000");
00673 td->string_list = g_list_reverse (td->string_list);
00674 td->id = "44th century Millenium Eve";
00675 test_data = g_list_prepend (test_data, td);
00676 }
00677 {
00678 QTestDate *td = g_new0 (QTestDate, 1);
00679 td->time = qof_time_new ();
00680 qof_time_set_secs (td->time,
00681 G_GINT64_CONSTANT(395408649599));
00682 qof_time_set_nanosecs (td->time, 7000000);
00683 td->date = qof_date_new ();
00684 td->date->qd_year = 14499;
00685 td->date->qd_mon = 12;
00686 td->date->qd_mday = 31;
00687 td->date->qd_hour = 23;
00688 td->date->qd_min = 59;
00689 td->date->qd_sec = G_GINT64_CONSTANT(59);
00690 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00691 qof_date_valid (td->date);
00692 td->string_list = NULL;
00693 td->string_list = g_list_prepend (td->string_list, "12/31/14499");
00694 td->string_list = g_list_prepend (td->string_list, "31/12/14499");
00695 td->string_list = g_list_prepend (td->string_list, "31.12.14499");
00696 td->string_list = g_list_prepend (td->string_list, "14499-12-31");
00697 td->string_list = g_list_prepend (td->string_list, "14499-12-31T23:59:59Z");
00698 td->string_list = g_list_prepend (td->string_list,
00699 "14499-12-31 23:59:59.007000000 +0000");
00700 td->string_list = g_list_reverse (td->string_list);
00701 td->id = "144th century Millenium Eve";
00702 test_data = g_list_prepend (test_data, td);
00703 }
00704 {
00705 QTestDate *td = g_new0 (QTestDate, 1);
00706 td->time = qof_time_new ();
00707 qof_time_set_secs (td->time,
00708 G_GINT64_CONSTANT(74869815369599));
00709 qof_time_set_nanosecs (td->time, 45321545);
00710 td->date = qof_date_new ();
00711 td->date->qd_year = 2374499;
00712 td->date->qd_mon = 12;
00713 td->date->qd_mday = 31;
00714 td->date->qd_hour = 23;
00715 td->date->qd_min = 59;
00716 td->date->qd_sec = G_GINT64_CONSTANT(59);
00717 td->date->qd_nanosecs = qof_time_get_nanosecs (td->time);
00718 qof_date_valid (td->date);
00719 td->string_list = NULL;
00720 td->string_list = g_list_prepend (td->string_list, "12/31/2374499");
00721 td->string_list = g_list_prepend (td->string_list, "31/12/2374499");
00722 td->string_list = g_list_prepend (td->string_list, "31.12.2374499");
00723 td->string_list = g_list_prepend (td->string_list, "2374499-12-31");
00724 td->string_list = g_list_prepend (td->string_list, "2374499-12-31T23:59:59Z");
00725 td->string_list = g_list_prepend (td->string_list,
00726 "2374499-12-31 23:59:59.045321545 +0000");
00727 td->string_list = g_list_reverse (td->string_list);
00728 td->id = "far, far future";
00729 test_data = g_list_prepend (test_data, td);
00730 }
00731 }
00732
00733 static void
00734 free_test_data (gpointer data, gpointer user_data __attribute__ ((unused)))
00735 {
00736 QTestDate *td;
00737
00738 td = (QTestDate*)data;
00739 qof_date_free (td->date);
00740 qof_time_free (td->time);
00741 td->string_list = NULL;
00742 g_free (td);
00743 td = NULL;
00744 }
00745
00746 static void
00747 test_date_close (void)
00748 {
00749 g_list_foreach (test_data, free_test_data, NULL);
00750 }
00751
00752 static void
00753 scan_and_stamp (const gchar * str, QofDateFormat df, QofTimeSecs check)
00754 {
00755 QofTime *scan;
00756 QofDate *qd;
00757 gchar *stamp;
00758
00759 qd = qof_date_parse (str, df);
00760 scan = qof_date_to_qtime (qd);
00761 do_test ((scan != NULL), "scan failed");
00762 if (scan == NULL)
00763 return;
00764 do_test ((0 == safe_strcasecmp(qd->qd_zone, "GMT")),
00765 " timezone reset incorrect");
00766 do_test ((qof_time_get_secs (scan) == check),
00767 "wrong time value");
00768 if (qof_time_get_secs (scan) != check)
00769 PERR (" wrong time value %"
00770 G_GINT64_FORMAT " %" G_GINT64_FORMAT " diff=%"
00771 G_GINT64_FORMAT " df=%d str=%s cmp=%s",
00772 qof_time_get_secs (scan), check,
00773 qof_time_get_secs (scan) - check, df, str,
00774 qof_date_print(qd, QOF_DATE_FORMAT_UTC));
00775 stamp = qof_date_print (qof_date_from_qtime(scan), df);
00776 do_test ((stamp != NULL), "stamp failed");
00777
00778 qof_time_free (scan);
00779 }
00780
00781 static void
00782 stamp_and_scan (QofTimeSecs start, glong nanosecs,
00783 QofDateFormat df)
00784 {
00785 gchar *str1, *str2;
00786 QofDate *check;
00787 QofTime *time, *scan;
00788
00789 time = qof_time_new ();
00790 qof_time_set_secs (time, start);
00791 qof_time_set_nanosecs (time, nanosecs);
00792 str1 = qof_date_print (qof_date_from_qtime(time), df);
00793 do_test ((str1 != NULL), "stamp failed");
00794 if (!str1)
00795 return;
00796 check = qof_date_parse (str1, df);
00797 do_test ((check != NULL), "parse failed");
00798 if (!check)
00799 PERR (" tried to parse %s\n", str1);
00800 scan = qof_date_to_qtime (check);
00801 qof_date_free (check);
00802 do_test ((scan != NULL), "scan failed");
00803 if (!scan)
00804 return;
00805
00806
00807
00808 check = qof_date_from_qtime (scan);
00809 str2 = qof_date_print (check, df);
00810 qof_date_free (check);
00811
00812 do_test ((str2 != NULL), "printed string is null");
00813 do_test ((qof_date_from_qtime (scan) != NULL),
00814 "from_qtime failed");
00815 do_test ((0 == safe_strcasecmp (str1, str2)),
00816 "stamp different to scan");
00817 if (0 != safe_strcasecmp (str1, str2))
00818 PERR (" df=%d str=%s scan=%s", df, str1, str2);
00819 qof_time_free (scan);
00820 qof_time_free (time);
00821 g_free (str1);
00822 g_free (str2);
00823 }
00824
00825 static void
00826 run_print_scan_tests (void)
00827 {
00828 gint i;
00829 gint64 secs;
00830 QofDateFormat df;
00831
00832
00833
00834 secs = G_MAXINT32;
00835
00836 secs += SECS_PER_DAY * 10;
00837 for (i = 1; i <= QOF_DATE_FORMAT_ISO8601; i++)
00838 {
00839 stamp_and_scan (796179600, 0, i);
00840 stamp_and_scan (796179500, 72000, i);
00841 stamp_and_scan (152098136, 0, i);
00842 stamp_and_scan (1964049931, 0, i);
00843 stamp_and_scan (1162088421, 12548000, i);
00844 stamp_and_scan (325659000 - 6500, 0, i);
00845 stamp_and_scan (1143943200, 0, i);
00846 stamp_and_scan (1603591171, 595311000, i);
00847 stamp_and_scan (1738909365, 204102000, i);
00848 stamp_and_scan (1603591171, 595311000, i);
00849 stamp_and_scan (1143943200 - 1, 0, i);
00850 stamp_and_scan (1143943200, 0, i);
00851 stamp_and_scan (1143943200 + (7 * 60 * 60), 0, i);
00852 stamp_and_scan (1143943200 + (8 * 60 * 60), 0, i);
00853 stamp_and_scan (1841443200, 0, i);
00854
00855
00856 stamp_and_scan (G_GINT64_CONSTANT (-796179600), 253, i);
00857 stamp_and_scan (G_GINT64_CONSTANT (-152098136), 865, i);
00858 stamp_and_scan (G_GINT64_CONSTANT (-1143943200), 67, i);
00859 stamp_and_scan (G_GINT64_CONSTANT (-1964049931), 53, i);
00860 stamp_and_scan (G_GINT64_CONSTANT (-2463880447), 48, i);
00861 stamp_and_scan (G_GINT64_CONSTANT (-22905158401), 9, i);
00862 stamp_and_scan (G_GINT64_CONSTANT (-28502726400), 1, i);
00863 stamp_and_scan (G_GINT64_CONSTANT (-60798211200), 0, i);
00864 stamp_and_scan (G_GINT64_CONSTANT (-32727638740), 0, i);
00865
00866 stamp_and_scan (secs, 0, i);
00867
00868 stamp_and_scan (G_GINT64_CONSTANT (2463880447), 0, i);
00869
00870 stamp_and_scan (G_GINT64_CONSTANT (2527038847), 0, i);
00871
00872
00873 stamp_and_scan (G_GINT64_CONSTANT (32727638740), 0, i);
00874
00875 stamp_and_scan (G_GINT64_CONSTANT (88313632867), 0, i);
00876
00877 stamp_and_scan (G_GINT64_CONSTANT (189216632865), 0, i);
00878
00879 stamp_and_scan (G_GINT64_CONSTANT (378432632864), 0, i);
00880
00881 stamp_and_scan (G_GINT64_CONSTANT (3165071328567), 0, i);
00882 }
00883
00884 #ifdef TEST_DEBUG
00885
00886 stamp_and_scan (G_GINT64_CONSTANT (3165071328567),
00887 0, QOF_DATE_FORMAT_UTC);
00888
00889 stamp_and_scan (G_GINT64_CONSTANT(843165071328567),
00890 0, QOF_DATE_FORMAT_UTC);
00892
00893
00894
00895
00896
00897 #endif
00898 scan_and_stamp ("05/09/2006", QOF_DATE_FORMAT_US,
00899 1147132800);
00900 scan_and_stamp ("09/05/2006", QOF_DATE_FORMAT_UK,
00901 1147132800);
00902 scan_and_stamp ("09.05.2006", QOF_DATE_FORMAT_CE,
00903 1147132800);
00904 scan_and_stamp ("2006-05-09", QOF_DATE_FORMAT_ISO,
00905 1147132800);
00906 scan_and_stamp ("2006-05-09T00:00:00Z", QOF_DATE_FORMAT_UTC,
00907 1147132800);
00908 scan_and_stamp ("2006-05-09T14:49:04Z", QOF_DATE_FORMAT_UTC,
00909 1147186144);
00910
00911
00912 df = 0;
00913 do_test ((qof_date_format_add ("%Y-%m-%d %H:%M:%S %z",
00914 &df) == TRUE), "failed to add scan suitable format");
00915
00916
00917 scan_and_stamp ("1971-12-31 15:00:00 -0900", df, 63072000);
00918
00919 scan_and_stamp ("1979-12-31 15:00:00 -0900", df, 315532800);
00920 scan_and_stamp ("1980-01-01 00:00:00 -0000", df, 315532800);
00921 scan_and_stamp ("1980-01-01 00:00:00 +0000", df, 315532800);
00922 scan_and_stamp ("1980-01-01 09:00:00 +0900", df, 315532800);
00923 scan_and_stamp ("1980-01-01 08:30:00 +0830", df, 315532800);
00924
00925 scan_and_stamp ("1963-11-22 14:00:00 -0500", df, -192776400);
00926 scan_and_stamp ("1945-09-08 11:02:00 +0900", df, -767311080);
00927 scan_and_stamp ("1918-11-11 11:00:00 +0000", df, -1613826000);
00928
00929
00930 scan_and_stamp ("1066-10-14 08:00:00 +0000", df,
00931 G_GINT64_CONSTANT (-28502726400));
00932
00933 scan_and_stamp ("0043-05-20 14:00:00 +0000", df,
00934 G_GINT64_CONSTANT (-60798160800));
00935 {
00936 QofDate *qd;
00937 QofTime *qt;
00938 gint64 secs;
00939
00940 qt = qof_time_new ();
00941
00942 qof_time_set_secs (qt, 1147186210);
00943 qd = qof_date_from_qtime (qt);
00944 do_test ((qof_date_adddays (qd, 45) == TRUE),
00945 "add_days failed");
00946 secs = 1147186210 + (45 * SECS_PER_DAY);
00947 qof_time_free (qt);
00948 qt = qof_date_to_qtime (qd);
00949 qof_date_free (qd);
00950 do_test ((secs == qof_time_get_secs (qt)),
00951 "add_days gave incorrect result.");
00952 if (secs != qof_time_get_secs (qt))
00953 PERR (" secs=%" G_GINT64_FORMAT "cmp=%"
00954 G_GINT64_FORMAT, secs, qof_time_get_secs (qt));
00955 qof_time_set_secs (qt, 1147186210);
00956 qd = qof_date_from_qtime (qt);
00957 do_test ((qof_date_addmonths (qd, 50, TRUE) == TRUE),
00958 "add_months failed");
00959 qof_time_free (qt);
00960 qt = qof_date_to_qtime (qd);
00961 do_test ((1278687010 == qof_time_get_secs (qt)),
00962 "add_months gave incorrect result.");
00963 qof_time_free (qt);
00964 qof_date_free (qd);
00965 }
00966 }
00967
00968 static void
00969 run_qofdate_test (void)
00970 {
00971 QofDateFormat df, test;
00972
00973 df = qof_date_format_get_current ();
00974
00975 {
00976 qof_date_format_set_current (QOF_DATE_FORMAT_UK);
00977 test = qof_date_format_get_current ();
00978 do_test ((test == QOF_DATE_FORMAT_UK),
00979 "setting current format as UK");
00980 do_test ((safe_strcasecmp (qof_date_format_to_name (test),
00981 "uk") == 0), "getting the shorthand name");
00982 do_test ((FALSE ==
00983 qof_date_format_set_name ("foo", QOF_DATE_FORMAT_UK)),
00984 "default name should not be overridden");
00985 do_test ((QOF_DATE_FORMAT_UK ==
00986 qof_date_format_from_name ("uk")),
00987 "getting date format from shorthand name");
00988 do_test (('/' == qof_date_format_get_date_separator (test)),
00989 "getting date format separator from date format");
00990 do_test ((FALSE == qof_date_format_set_date_separator (':',
00991 test)), "default separator should not be overridden");
00992 }
00993
00994 {
00995 QofDateFormat df, df2;
00996 do_test ((qof_date_format_add ("%T", &df) == TRUE),
00997 "adding a valid format");
00998 do_test ((qof_date_format_add ("%a%A%b%B%c%C%d%D%e%F%f%r",
00999 &df2) == FALSE), "adding an invalid format");
01000 qof_date_format_set_current (df);
01001 test = qof_date_format_get_current ();
01002 do_test ((test == df), "setting current format");
01003 do_test ((safe_strcasecmp (qof_date_format_to_name (test),
01004 "%T") == 0), "getting the shorthand name");
01005 do_test ((TRUE == qof_date_format_set_name ("foo", test)),
01006 "custom name should be overridden");
01007 do_test ((test == qof_date_format_from_name ("foo")),
01008 "getting date format from shorthand name");
01009 do_test (('\0' == qof_date_format_get_date_separator (test)),
01010 "getting date format separator from date format");
01011 do_test ((TRUE == qof_date_format_set_date_separator (':', test)),
01012 "custom separator should be overridden");
01013 do_test ((':' == qof_date_format_get_date_separator (test)),
01014 "getting modified date format separator from date format");
01015 }
01016 qof_date_format_set_current (df);
01017
01018 {
01019 QofDate *date;
01020
01021 date = qof_date_new ();
01022
01023 date->qd_sec = SECS_PER_DAY * 2 + 30;
01024 date->qd_year = 2000;
01025 do_test ((qof_date_valid (date) == TRUE), "date 1 was invalid");
01026 do_test ((date->qd_sec == 30), "normalised seconds incorrect - 1");
01027 do_test ((date->qd_mday == 2), "normalised day incorrect - 1");
01028 date->qd_mday = 54;
01029 do_test ((qof_date_valid (date) == TRUE), "date 2 was invalid");
01030 do_test ((date->qd_sec == 30), "normalised seconds incorrect - 2");
01031 do_test ((date->qd_mday == 23), "normalised day incorrect - 2");
01032 date->qd_hour = 34;
01033 qof_date_valid (date);
01034 do_test ((date->qd_hour == 10), "normalised hour incorrect");
01035 do_test ((date->qd_mday == 24), "normalised day incorrect - 3");
01036 date->qd_mon = 17;
01037 qof_date_valid (date);
01038 do_test ((date->qd_mon == 5), "normalised month incorrect");
01039 do_test ((date->qd_year == 2001), "normalised year incorrect");
01040 date->qd_hour = 27;
01041 qof_date_valid (date);
01042 do_test ((date->qd_hour == 3), "normalised hour incorrect - 1");
01043 do_test ((date->qd_mday == 25), "normalised day incorrect - 4");
01044 date->qd_hour = -53;
01045 qof_date_valid (date);
01046 do_test ((date->qd_hour == 19), "normalised hour incorrect - 2");
01047 do_test ((date->qd_mday == 22), "normalised day incorrect - 5");
01048 date->qd_min = -225;
01049 qof_date_valid (date);
01050 do_test ((date->qd_min == 15), "normalised min incorrect");
01051 do_test ((date->qd_hour == 15), "normalised hour incorrect - 3");
01052 date->qd_min = 5;
01053 date->qd_sec = 68;
01054 qof_date_valid (date);
01055 do_test ((date->qd_sec == 8), "doxygen sec example incorrect - 1");
01056 do_test ((date->qd_min == 6), "doxygen min example incorrect - 1");
01057 date->qd_min = 5;
01058 date->qd_sec = -64;
01059 qof_date_valid (date);
01060 do_test ((date->qd_sec == 56), "doxygen sec example incorrect - 2");
01061 do_test ((date->qd_min == 3), "doxygen min example incorrect - 2");
01062 }
01063
01064
01065 {
01066 QofDate *qd;
01067 QofTime *qt;
01068 gchar *str1;
01069
01070 qd = qof_date_new ();
01071 qd->qd_year = 2006;
01072 qd->qd_mon = 5;
01073 qd->qd_mday = 30;
01074 qd->qd_hour = 18;
01075 qd->qd_min = 24;
01076 qd->qd_sec = 17;
01077 qd->qd_nanosecs = 123456789;
01078 do_test ((qof_date_valid (qd)), "date not valid");
01079 qt = qof_date_to_qtime (qd);
01080 str1 = qof_date_print (qd, QOF_DATE_FORMAT_UTC);
01081 do_test ((0 == safe_strcasecmp (str1,
01082 "2006-05-30T18:24:17Z")), "with nanosecs");
01083 do_test ((qof_time_get_nanosecs (qt) ==
01084 123456789L), "nanosecs mismatched.");
01085 do_test ((0 == safe_strcasecmp ("05/30/2006",
01086 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01087 "strftime:US:first");
01088 do_test ((0 == safe_strcasecmp ("30/05/2006",
01089 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01090 "strftime:UK:first");
01091 do_test ((0 == safe_strcasecmp ("30.05.2006",
01092 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01093 "strftime:CE:first");
01094 do_test ((0 == safe_strcasecmp ("2006-05-30",
01095 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01096 "strftime:ISO:first");
01097 do_test ((0 == safe_strcasecmp ("2006-05-30T18:24:17Z",
01098 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01099 "strftime:UTC:first");
01100 do_test ((0 != safe_strcasecmp (NULL,
01101 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01102 "strftime:LOCALE:first:inrange");
01103 do_test ((0 != safe_strcasecmp (NULL,
01104 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01105 "strftime:CUSTOM:first:inrange");
01106 qof_date_free (qd);
01107 qof_time_free (qt);
01108 qd = qof_date_new ();
01109 qd->qd_year = 3005;
01110 qd->qd_mon = 07;
01111 qd->qd_mday = 24;
01112 qd->qd_hour = 06;
01113 qd->qd_min = 34;
01114 qd->qd_sec = 26;
01115 do_test ((qof_date_valid (qd)), "date not valid");
01116 qt = qof_date_to_qtime (qd);
01117 do_test ((0 == safe_strcasecmp ("07/24/3005",
01118 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01119 "strftime:US:forward");
01120 do_test ((0 == safe_strcasecmp ("24/07/3005",
01121 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01122 "strftime:UK:forward");
01123 do_test ((0 == safe_strcasecmp ("24.07.3005",
01124 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01125 "strftime:CE:forward");
01126 do_test ((0 == safe_strcasecmp ("3005-07-24",
01127 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01128 "strftime:ISO:forward");
01129 do_test ((0 == safe_strcasecmp ("3005-07-24T06:34:26Z",
01130 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01131 "strftime:UTC:forward");
01132 do_test ((0 != safe_strcasecmp (NULL,
01133 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01134 "strftime:LOCALE:forward:inrange");
01135 do_test ((0 != safe_strcasecmp (NULL,
01136 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01137 "strftime:CUSTOM:forward:inrange");
01138 qof_date_free (qd);
01139 qd = qof_date_new ();
01140 qd->qd_year = 4500;
01141 qd->qd_mon = 07;
01142 qd->qd_mday = 24;
01143 qd->qd_hour = 06;
01144 qd->qd_min = 34;
01145 qd->qd_sec = 26;
01146 do_test ((qof_date_valid (qd)), "date not valid");
01147 qt = qof_date_to_qtime (qd);
01148 do_test ((0 == safe_strcasecmp ("07/24/4500",
01149 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01150 "strftime:US:fifth");
01151 do_test ((0 == safe_strcasecmp ("24/07/4500",
01152 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01153 "strftime:UK:fifth");
01154 do_test ((0 == safe_strcasecmp ("24.07.4500",
01155 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01156 "strftime:CE:fifth");
01157 do_test ((0 == safe_strcasecmp ("4500-07-24",
01158 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01159 "strftime:ISO:fifth");
01160 do_test ((0 == safe_strcasecmp ("4500-07-24T06:34:26Z",
01161 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01162 "strftime:UTC:fifth");
01163 do_test ((0 != safe_strcasecmp (NULL,
01164 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01165 "strftime:LOCALE:fifth:inrange");
01166 do_test ((0 != safe_strcasecmp (NULL,
01167 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01168 "strftime:CUSTOM:fifth:inrange");
01169 qof_date_free (qd);
01170 qd = qof_date_new ();
01171 qd->qd_year = 45000;
01172 qd->qd_mon = 07;
01173 qd->qd_mday = 24;
01174 qd->qd_hour = 06;
01175 qd->qd_min = 34;
01176 qd->qd_sec = 26;
01177 do_test ((qof_date_valid (qd)), "date not valid");
01178 qt = qof_date_to_qtime (qd);
01179 do_test ((0 == safe_strcasecmp ("07/24/45000",
01180 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01181 "strftime:US:forward2");
01182 do_test ((0 == safe_strcasecmp ("24/07/45000",
01183 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01184 "strftime:UK:forward2");
01185 do_test ((0 == safe_strcasecmp ("24.07.45000",
01186 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01187 "strftime:CE:forward2");
01188 do_test ((0 == safe_strcasecmp ("45000-07-24",
01189 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01190 "strftime:ISO:forward2");
01191 do_test ((0 == safe_strcasecmp ("45000-07-24T06:34:26Z",
01192 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01193 "strftime:UTC:forward2");
01194 do_test ((0 != safe_strcasecmp (NULL,
01195 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01196 "strftime:LOCALE:forward2:outofrange");
01197 do_test ((0 != safe_strcasecmp (NULL,
01198 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01199 "strftime:CUSTOM:forward2:outofrange");
01200 qof_date_free (qd);
01201 g_free (str1);
01202 qd = qof_date_new ();
01203 qd->qd_year = 1914;
01204 qd->qd_mon = 11;
01205 qd->qd_mday = 11;
01206 qd->qd_hour = 11;
01207 do_test ((qof_date_valid (qd)), "date not valid");
01208 qt = qof_date_to_qtime (qd);
01209 str1 = qof_date_print (qd, QOF_DATE_FORMAT_UTC);
01210 do_test ((0 == safe_strcasecmp (str1,
01211 "1914-11-11T11:00:00Z")), "armistice day");
01212 do_test ((0 == safe_strcasecmp ("11/11/1914",
01213 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01214 "strftime:US:second");
01215 do_test ((0 == safe_strcasecmp ("11/11/1914",
01216 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01217 "strftime:UK:second");
01218 do_test ((0 == safe_strcasecmp ("11.11.1914",
01219 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01220 "strftime:CE:second");
01221 do_test ((0 == safe_strcasecmp ("1914-11-11",
01222 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01223 "strftime:ISO:second");
01224 do_test ((0 == safe_strcasecmp ("1914-11-11T11:00:00Z",
01225 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01226 "strftime:UTC:second");
01227 do_test ((0 != safe_strcasecmp (NULL,
01228 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01229 "strftime:LOCALE:second:inrange");
01230 do_test ((0 != safe_strcasecmp (NULL,
01231 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01232 "strftime:CUSTOM:second:inrange");
01233 qof_date_free (qd);
01234 qof_time_free (qt);
01235 qd = qof_date_new ();
01236 qd->qd_year = 1812;
01237 qd->qd_mon = 07;
01238 qd->qd_mday = 24;
01239 qd->qd_hour = 06;
01240 qd->qd_min = 34;
01241 qd->qd_sec = 26;
01242 do_test ((qof_date_valid (qd)), "date not valid");
01243 qt = qof_date_to_qtime (qd);
01244 do_test ((0 == safe_strcasecmp ("07/24/1812",
01245 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01246 "strftime:US:third");
01247 do_test ((0 == safe_strcasecmp ("24/07/1812",
01248 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01249 "strftime:UK:third");
01250 do_test ((0 == safe_strcasecmp ("24.07.1812",
01251 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01252 "strftime:CE:third");
01253 do_test ((0 == safe_strcasecmp ("1812-07-24",
01254 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01255 "strftime:ISO:third");
01256 do_test ((0 == safe_strcasecmp ("1812-07-24T06:34:26Z",
01257 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01258 "strftime:UTC:third");
01259 do_test ((0 == safe_strcasecmp (NULL,
01260 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01261 "strftime:LOCALE:third:outofrange");
01262 do_test ((0 == safe_strcasecmp (NULL,
01263 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01264 "strftime:CUSTOM:third:outofrange");
01265 qof_date_free (qd);
01266 qd = qof_date_new ();
01267 qd->qd_year = 1066;
01268 qd->qd_mon = 07;
01269 qd->qd_mday = 24;
01270 qd->qd_hour = 06;
01271 qd->qd_min = 34;
01272 qd->qd_sec = 26;
01273 do_test ((qof_date_valid (qd)), "date not valid");
01274 qt = qof_date_to_qtime (qd);
01275 do_test ((0 == safe_strcasecmp ("07/24/1066",
01276 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01277 "strftime:US:fourth");
01278 do_test ((0 == safe_strcasecmp ("24/07/1066",
01279 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01280 "strftime:UK:fourth");
01281 do_test ((0 == safe_strcasecmp ("24.07.1066",
01282 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01283 "strftime:CE:fourth");
01284 do_test ((0 == safe_strcasecmp ("1066-07-24",
01285 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01286 "strftime:ISO:fourth");
01287 do_test ((0 == safe_strcasecmp ("1066-07-24T06:34:26Z",
01288 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01289 "strftime:UTC:fourth");
01290 do_test ((0 == safe_strcasecmp (NULL,
01291 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01292 "strftime:LOCALE:fourth:outofrange");
01293 do_test ((0 == safe_strcasecmp (NULL,
01294 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01295 "strftime:CUSTOM:fourth:outofrange");
01296 qof_date_free (qd);
01297 qd = qof_date_new ();
01298 qd->qd_year = -45;
01299 qd->qd_mon = 07;
01300 qd->qd_mday = 24;
01301 qd->qd_hour = 06;
01302 qd->qd_min = 34;
01303 qd->qd_sec = 26;
01304 do_test ((qof_date_valid (qd)), "date not valid");
01305 qt = qof_date_to_qtime (qd);
01306 do_test ((0 == safe_strcasecmp ("07/24/-045",
01307 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01308 "strftime:US:fifth");
01309 do_test ((0 == safe_strcasecmp ("24/07/-045",
01310 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01311 "strftime:UK:fifth");
01312 do_test ((0 == safe_strcasecmp ("24.07.-045",
01313 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01314 "strftime:CE:fifth");
01315 do_test ((0 == safe_strcasecmp ("-045-07-24",
01316 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01317 "strftime:ISO:fifth");
01318 do_test ((0 == safe_strcasecmp ("-045-07-24T06:34:26Z",
01319 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01320 "strftime:UTC:fifth");
01321 do_test ((0 == safe_strcasecmp (NULL,
01322 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01323 "strftime:LOCALE:fifth:outofrange");
01324 do_test ((0 == safe_strcasecmp (NULL,
01325 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01326 "strftime:CUSTOM:fifth:outofrange");
01327 qof_date_free (qd);
01328 qd = qof_date_new ();
01329 qd->qd_year = -4500;
01330 qd->qd_mon = 07;
01331 qd->qd_mday = 24;
01332 qd->qd_hour = 06;
01333 qd->qd_min = 34;
01334 qd->qd_sec = 26;
01335 do_test ((qof_date_valid (qd)), "date not valid");
01336 qt = qof_date_to_qtime (qd);
01337 do_test ((0 == safe_strcasecmp ("07/24/-4500",
01338 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01339 "strftime:US:sixth");
01340 do_test ((0 == safe_strcasecmp ("24/07/-4500",
01341 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01342 "strftime:UK:sixth");
01343 do_test ((0 == safe_strcasecmp ("24.07.-4500",
01344 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01345 "strftime:CE:sixth");
01346 do_test ((0 == safe_strcasecmp ("-4500-07-24",
01347 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01348 "strftime:ISO:sixth");
01349 do_test ((0 == safe_strcasecmp ("-4500-07-24T06:34:26Z",
01350 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01351 "strftime:UTC:sixth");
01352 do_test ((0 == safe_strcasecmp (NULL,
01353 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01354 "strftime:LOCALE:sixth:outofrange");
01355 do_test ((0 == safe_strcasecmp (NULL,
01356 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01357 "strftime:CUSTOM:sixth:outofrange");
01358 qof_date_free (qd);
01359 qd = qof_date_new ();
01360 qd->qd_year = -4500000;
01361 qd->qd_mon = 07;
01362 qd->qd_mday = 24;
01363 qd->qd_hour = 06;
01364 qd->qd_min = 34;
01365 qd->qd_sec = 26;
01366 do_test ((qof_date_valid (qd)), "date not valid");
01367 qt = qof_date_to_qtime (qd);
01368 do_test ((0 == safe_strcasecmp ("07/24/-4500000",
01369 qof_date_print (qd, QOF_DATE_FORMAT_US))),
01370 "strftime:US:seventh");
01371 do_test ((0 == safe_strcasecmp ("24/07/-4500000",
01372 qof_date_print (qd, QOF_DATE_FORMAT_UK))),
01373 "strftime:UK:seventh");
01374 do_test ((0 == safe_strcasecmp ("24.07.-4500000",
01375 qof_date_print (qd, QOF_DATE_FORMAT_CE))),
01376 "strftime:CE:seventh");
01377 do_test ((0 == safe_strcasecmp ("-4500000-07-24",
01378 qof_date_print (qd, QOF_DATE_FORMAT_ISO))),
01379 "strftime:ISO:seventh");
01380 do_test ((0 == safe_strcasecmp ("-4500000-07-24T06:34:26Z",
01381 qof_date_print (qd, QOF_DATE_FORMAT_UTC))),
01382 "strftime:UTC:seventh");
01383 do_test ((0 == safe_strcasecmp (NULL,
01384 qof_date_print (qd, QOF_DATE_FORMAT_LOCALE))),
01385 "strftime:LOCALE:seventh:outofrange");
01386 do_test ((0 == safe_strcasecmp (NULL,
01387 qof_date_print (qd, QOF_DATE_FORMAT_CUSTOM))),
01388 "strftime:CUSTOM:seventh:outofrange");
01389 qof_date_free (qd);
01390 }
01391 }
01392
01393 static void
01394 run_qoftime_test (void)
01395 {
01396 QofTime *time, *cmp, *diff;
01397
01398 time = qof_time_new ();
01399 do_test ((time != NULL), "failed to initialise QofTime.");
01400
01401 {
01402 qof_time_set_secs (time, 796179600);
01403 do_test ((qof_time_get_secs (time) > 0), "failed to set secs");
01404 do_test ((time != NULL), "error found");
01405 qof_time_set_secs (time, 0);
01406 qof_time_set_nanosecs (time, 2041020040);
01407 do_test ((qof_time_get_secs (time) > 0), "failed to normalise.");
01408 do_test ((time != NULL), "error found");
01409 do_test ((qof_time_get_nanosecs (time) > 0),
01410 "failed to set nanosecs");
01411 }
01412
01413 {
01414 qof_time_add_secs (time, -1143943200);
01415 do_test ((qof_time_get_secs (time) < 0), "failed to subtract.");
01416 do_test ((time != NULL), "error found");
01417 cmp = qof_time_new ();
01418 qof_time_set_secs (cmp, 0);
01419 do_test ((qof_time_equal (time, cmp) == FALSE), "test equal failed.");
01420 do_test ((qof_time_cmp (time, cmp) == -1), "compare cmp test");
01421 diff = qof_time_new ();
01422 qof_time_set_secs (diff, qof_time_get_secs (cmp));
01423 do_test ((qof_time_cmp (diff, cmp) == 0), "compare diff test");
01424 qof_time_free (diff);
01425 diff = qof_time_diff (time, cmp);
01426 do_test ((qof_time_get_secs (diff) < 0), "diff of negative value");
01427 qof_time_free (diff);
01428 diff = qof_time_diff (cmp, time);
01429 do_test ((qof_time_get_secs (diff) > 0), "diff of negative value");
01430 time = qof_time_abs (time);
01431 do_test ((qof_time_get_nanosecs (time) != 0), "abs failed");
01432 qof_time_set_secs (cmp, qof_time_get_secs (time));
01433 qof_time_set_nanosecs (cmp, qof_time_get_nanosecs (time));
01434 do_test ((qof_time_equal (cmp, time) == TRUE),
01435 "equality test failed");
01436 qof_time_free (cmp);
01437 qof_time_free (time);
01438 }
01439
01440 {
01441 GDate *date, *test;
01442 GTimeVal gtv;
01443
01444 date = g_date_new_dmy (15, 5, 2006);
01445 time = qof_time_from_gdate (date);
01446 test = qof_time_to_gdate (time);
01447 do_test ((g_date_get_day (test) == 15), "gdate day fail");
01448 do_test ((g_date_get_month (test) == 5), "gdate month fail");
01449 do_test ((g_date_get_year (test) == 2006), "gdate year fail");
01450 do_test ((qof_time_to_gtimeval (time, >v)), "gtimeval fail");
01451 {
01452 QofTime *copy, *diff;
01453
01454 qof_time_add_secs (time, 26451);
01455 copy = qof_time_add_secs_copy (time, -26451);
01456 diff = qof_time_diff (time, copy);
01457 do_test ((qof_time_get_secs (diff) == 26451),
01458 "add seconds failed");
01459 qof_time_free (copy);
01460 qof_time_free (diff);
01461 }
01462 g_date_free (date);
01463 g_date_free (test);
01464 qof_time_free (time);
01465 }
01466
01467 {
01468 GDate *date, *cmp;
01469 gint64 time_secs, diff;
01470 glong time_nano;
01471
01472 date = g_date_new_dmy (15, 5, 2006);
01473 time = qof_time_from_gdate (date);
01474 cmp = qof_time_to_gdate (time);
01475 do_test ((g_date_compare (date, cmp) == 0),
01476 "convert to and from gdate failed");
01477 g_date_free (cmp);
01478 time_secs = qof_time_get_secs (time);
01479 time_nano = qof_time_get_nanosecs (time);
01480 qof_time_add_secs (time, 7252);
01481 qof_time_set_nanosecs (time, 123456);
01482 do_test ((qof_time_set_day_start (time)), "set_day_start failed");
01483 do_test ((qof_time_get_secs (time) == time_secs),
01484 "start of day incorrect");
01485 do_test ((qof_time_get_nanosecs (time) == 0),
01486 "set nano at day start incorrect");
01487 do_test ((qof_time_set_day_middle (time) == TRUE),
01488 "set_day_middle failed");
01489 diff = qof_time_get_secs (time) - time_secs;
01490 do_test (diff == (SECS_PER_DAY / 2), "middle of day incorrect");
01491
01492 cmp = qof_time_to_gdate (time);
01493 do_test ((g_date_compare (date, cmp) == 0),
01494 "middle of day not the same as original day");
01495 g_date_free (cmp);
01496
01497 do_test ((qof_time_set_day_end (time)), "set_day_end failed");
01498 do_test ((qof_time_get_secs (time) - time_secs)
01499 == (SECS_PER_DAY - 1), "end of day incorrect");
01500
01501
01502 cmp = qof_time_to_gdate (time);
01503
01504 do_test ((g_date_compare (date, cmp) == 0),
01505 "end of day not the same as original day");
01506 qof_time_free (time);
01507 g_date_free (cmp);
01508 g_date_free (date);
01509 }
01510
01511 {
01512 GTimeVal *current;
01513 gint64 time_secs;
01514 QofTime *diff;
01515
01516 current = qof_time_get_current_start ();
01517 diff = qof_time_new ();
01518 qof_time_from_gtimeval (diff, current);
01519 time_secs = qof_time_get_secs (diff);
01520 time = qof_time_get_today_start ();
01521 do_test ((qof_time_get_secs (time) == time_secs),
01522 "start of day incorrect");
01523 do_test ((qof_time_get_nanosecs (time) == 0),
01524 "today start nanosecs non zero");
01525 qof_time_free (time);
01526 time = qof_time_get_today_end ();
01527 time_secs += SECS_PER_DAY - 1;
01528 do_test ((qof_time_get_secs (time) == time_secs),
01529 "start of today incorrect");
01530 do_test ((qof_time_get_nanosecs (time) == 0),
01531 "today start nanosecs non zero");
01532 qof_time_free (time);
01533 }
01534
01535 {
01536 GDate *date;
01537 guint8 mday;
01538
01539 date = g_date_new_dmy (15, 5, 2006);
01540 time = qof_time_from_gdate (date);
01541 mday = qof_time_last_mday (time);
01542 do_test ((mday == 31), " wrong last day of May");
01543 }
01544
01545 {
01546 QofDate *d;
01547
01548 do_test ((246 == qof_date_get_yday (3, 9, 2003)),
01549 "get year day test, September");
01550 d = qof_date_new ();
01551 d->qd_mday = 3;
01552 d->qd_mon = 9;
01553 d->qd_year = 2003;
01554 do_test ((TRUE == qof_date_valid (d)),
01555 "3/9/2003 not valid");
01556 do_test ((3 == d->qd_wday), "not Wednesday");
01557 qof_date_free (d);
01558
01559 do_test ((247 == qof_date_get_yday (3, 9, 2004)),
01560 "get year day test, leap year");
01561 d = qof_date_new ();
01562 d->qd_mday = 3;
01563 d->qd_mon = 9;
01564 d->qd_year = 2004;
01565 do_test ((TRUE == qof_date_valid (d)),
01566 "3/9/2003 not valid");
01567 do_test ((5 == d->qd_wday), "not Friday");
01568 qof_date_free (d);
01569
01570 do_test ((139 == qof_date_get_yday (19, 5, 2002)),
01571 "get year day test, May");
01572 d = qof_date_new ();
01573 d->qd_mday = 19;
01574 d->qd_mon = 5;
01575 d->qd_year = 2002;
01576 do_test ((TRUE == qof_date_valid (d)),
01577 "3/9/2003 not valid");
01578 do_test ((0 == d->qd_wday), "not Sunday");
01579 qof_date_free (d);
01580
01581 do_test ((140 == qof_date_get_yday (19, 5, 2004)),
01582 "get year day test, May");
01583 d = qof_date_new ();
01584 d->qd_mday = 19;
01585 d->qd_mon = 5;
01586 d->qd_year = 2004;
01587 do_test ((TRUE == qof_date_valid (d)),
01588 "3/9/2003 not valid");
01589 do_test ((3 == d->qd_wday), "not Wednesday, May");
01590 qof_date_free (d);
01591
01592 do_test ((30 == qof_date_get_mday (11, 2003)),
01593 "get days in month, non-leap");
01594
01595 do_test ((29 == qof_date_get_mday (2, 2004)),
01596 "get days in month, leap year");
01597 }
01598
01599 {
01600 gboolean success;
01601 guint8 day, month;
01602 guint16 year;
01603 GDate *date;
01604 QofTime *reverse;
01605
01606 date = g_date_new_dmy (15, 5, 2006);
01607 time = qof_time_from_gdate (date);
01608 success = qof_time_to_dmy (time, &day, &month, &year);
01609 do_test ((success == TRUE), "time to dmy failed");
01610 do_test ((day == 15), "wrong day calculated");
01611 do_test ((month == 5), "wrong month calculated");
01612 do_test ((year == 2006), "wrong year calculated");
01613 reverse = qof_time_dmy_to_time (day, month, year);
01614 do_test ((qof_time_cmp (time, reverse) == 0), "dmy to time failed");
01615 g_date_free (date);
01616 qof_time_free (time);
01617 qof_time_free (reverse);
01618 }
01619 {
01620
01621 QofTime *wrap_t;
01622 QofDate *wrap_d;
01623 glong day, month, year;
01624
01625
01626 wrap_t = qof_time_set (1152814827, 345634);
01627 wrap_d = qof_date_from_qtime (wrap_t);
01628 PINFO (" base date for section = %s",
01629 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
01630 day = wrap_d->qd_mday;
01631 month = wrap_d->qd_mon;
01632 year = wrap_d->qd_year;
01633 qof_date_free (wrap_d);
01634
01635 wrap_d = qof_date_from_qtime (wrap_t);
01636 wrap_d->qd_mday -= 1;
01637 qof_date_valid (wrap_d);
01638 do_test ((wrap_d->qd_mon == month &&
01639 wrap_d->qd_year == year &&
01640 wrap_d->qd_mday == (day - 1)),
01641 " this time on previous day");
01642 PINFO (" same time previous day = %s",
01643 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
01644 qof_date_free (wrap_d);
01645
01646 wrap_d = qof_date_from_qtime (wrap_t);
01647 wrap_d->qd_mon -= 1;
01648 qof_date_valid (wrap_d);
01649 do_test ((wrap_d->qd_mon == (month - 1) &&
01650 wrap_d->qd_year == year &&
01651 wrap_d->qd_mday == day),
01652 " this time last month");
01653 PINFO (" same time previous month = %s",
01654 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
01655 qof_date_free (wrap_d);
01656
01657 wrap_d = qof_date_from_qtime (wrap_t);
01658 wrap_d->qd_year -= 1;
01659 qof_date_valid (wrap_d);
01660 do_test ((wrap_d->qd_mon == month &&
01661 wrap_d->qd_year == (year - 1) &&
01662 wrap_d->qd_mday == day),
01663 " this time last year");
01664 PINFO (" same time previous year = %s",
01665 qof_date_print (wrap_d, QOF_DATE_FORMAT_ISO8601));
01666 qof_time_free (wrap_t);
01667 qof_date_free (wrap_d);
01668 }
01669 }
01670
01671 int
01672 main (void)
01673 {
01674 qof_init ();
01675 #ifdef TEST_DEBUG
01676 qof_log_init_filename ("test-date.trace");
01677 qof_log_set_default (QOF_LOG_DETAIL);
01678 ENTER (" ");
01679 #endif
01680 test_date_init ();
01681 run_qoftime_test ();
01682 run_qofdate_test ();
01683 run_print_scan_tests ();
01684 g_list_foreach (test_data, check_date_cycles, NULL);
01685 print_test_results ();
01686 test_date_close ();
01687 #ifdef TEST_DEBUG
01688 LEAVE (" ");
01689 qof_log_shutdown ();
01690 #endif
01691 qof_close ();
01692 exit (get_rv ());
01693 }