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