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