updated
[platform/upstream/glib.git] / testgdateparser.c
1 #ifdef GLIB_COMPILATION
2 #undef GLIB_COMPILATION
3 #endif
4
5 #include "glib.h"
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <locale.h>
10
11 void g_date_debug_print(GDate* d)
12 {
13   if (!d) g_print("NULL!\n");
14   else 
15     g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
16             d->julian_days, 
17             d->julian ? "valid" : "invalid",
18             d->day,
19             d->month,
20             d->year,
21             d->dmy ? "valid" : "invalid");
22   
23   fflush(stdout);
24 }
25
26 /* These only work in the POSIX locale, maybe C too - 
27  * type POSIX into the program to check them
28  */
29 char* posix_tests [] = {
30   "19981024",
31   "981024",
32   "October 1998",
33   "October 98",
34   "oCT 98",
35   "10/24/98",
36   "10 -- 24 -- 98",
37   "10/24/1998",
38   "October 24, 1998",
39   NULL
40 };
41
42 int main(int argc, char** argv)
43 {
44   GDate* d;
45   gchar* loc;
46   gchar input[1024];
47
48   loc = setlocale(LC_ALL,"");
49   if (loc) 
50     g_print("\nLocale set to %s\n", loc);
51   else 
52     g_print("\nLocale unchanged\n");
53
54   d = g_date_new();
55
56   while (fgets(input, 1023, stdin))
57     {
58       if (input[0] == '\n') 
59         {
60           g_print("Enter a date to parse and press enter, or type `POSIX':\n");
61           continue;
62         }
63
64       if (strcmp(input,"POSIX\n") == 0) 
65         {
66           char** s = posix_tests;
67           while (*s) {
68             g_date_set_parse(d, *s);
69             
70             g_print("POSIXy parse test `%s' ...", *s);
71
72             if (!g_date_valid(d))
73               {
74                 g_print(" failed.\n");
75               }
76             else 
77               {
78                 gchar buf[256];
79                 
80                 g_date_strftime(buf,100," parsed `%x' (%B %d %Y)\n",
81                                 d);
82                 g_print("%s", buf);
83               }
84
85             ++s;
86           }
87         }
88       else 
89         {
90           g_date_set_parse(d, input);
91           
92           if (!g_date_valid(d))
93             {
94               g_print("Parse failed.\n");
95             }
96           else 
97             {
98               gchar buf[256];
99               
100               g_date_strftime(buf,100,"Parsed: `%x' (%B %d %Y)\n",
101                               d);
102               g_print("%s", buf);
103             }
104         }
105     }
106
107   g_date_free(d);
108
109   return 0;
110 }
111
112