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