File: testgdateparser.c

package info (click to toggle)
glib2.0 2.58.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-proposed-updates
  • size: 48,744 kB
  • sloc: ansic: 452,196; xml: 16,781; python: 6,149; makefile: 3,776; sh: 1,499; perl: 1,140; cpp: 9
file content (100 lines) | stat: -rw-r--r-- 1,937 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN

#ifdef GLIB_COMPILATION
#undef GLIB_COMPILATION
#endif

#include "glib.h"

#include <stdio.h>
#include <string.h>
#include <locale.h>

/* These only work in the POSIX locale, maybe C too - 
 * type POSIX into the program to check them
 */
char* posix_tests [] = {
  "19981024",
  "981024",
  "October 1998",
  "October 98",
  "oCT 98",
  "10/24/98",
  "10 -- 24 -- 98",
  "10/24/1998",
  "October 24, 1998",
  NULL
};

int main(int argc, char** argv)
{
  GDate* d;
  gchar* loc;
  gchar input[1024];

  loc = setlocale(LC_ALL,"");
  if (loc) 
    g_print("\nLocale set to %s\n", loc);
  else 
    g_print("\nLocale unchanged\n");

  d = g_date_new();

  while (fgets(input, 1023, stdin))
    {
      if (input[0] == '\n') 
        {
          g_print("Enter a date to parse and press enter, or type 'POSIX':\n");
          continue;
        }

      if (strcmp(input,"POSIX\n") == 0) 
        {
          char** s = posix_tests;
          while (*s) {
            g_date_set_parse(d, *s);
            
            g_print("POSIXy parse test '%s' ...", *s);

            if (!g_date_valid(d))
              {
                g_print(" failed.\n");
              }
            else 
              {
                gchar buf[256];
                
                g_date_strftime(buf,100," parsed '%x' (%B %d %Y)\n",
                                d);
                g_print("%s", buf);
              }

            ++s;
          }
        }
      else 
        {
          g_date_set_parse(d, input);
          
          if (!g_date_valid(d))
            {
              g_print("Parse failed.\n");
            }
          else 
            {
              gchar buf[256];
              
              g_date_strftime(buf,100,"Parsed: '%x' (%B %d %Y)\n",
                              d);
              g_print("%s", buf);
            }
        }
    }

  g_date_free(d);

  return 0;
}