aboutsummaryrefslogtreecommitdiff
path: root/tests/core/test_strftime.in
blob: 4a3a7fdad43b2dce1a3edb6fb55f1f0a849ae914 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void test(int result, const char* comment, const char* parsed = "") {
  printf("%d", result);
  if (!result) {
    printf("\nERROR: %s (\"%s\")\n", comment, parsed);
  }
}

int cmp(const char* s1, const char* s2) {
  for (; *s1 == *s2; s1++, s2++) {
    if (*s1 == '\0') break;
  }

  return (*s1 - *s2);
}

int main() {
  struct tm tm;
  char s[1000];
  size_t size;

  tm.tm_sec = 4;
  tm.tm_min = 23;
  tm.tm_hour = 20;
  tm.tm_mday = 21;
  tm.tm_mon = 1;
  tm.tm_year = 74;
  tm.tm_wday = 4;
  tm.tm_yday = 51;
  tm.tm_isdst = 0;

  size = strftime(s, 1000, "", &tm);
  test((size == 0) && (*s == '\0'), "strftime test #1", s);

  size = strftime(s, 1000, "%a", &tm);
  test((size == 3) && !cmp(s, "Thu"), "strftime test #2", s);

  size = strftime(s, 1000, "%A", &tm);
  test((size == 8) && !cmp(s, "Thursday"), "strftime test #3", s);

  size = strftime(s, 1000, "%b", &tm);
  test((size == 3) && !cmp(s, "Feb"), "strftime test #4", s);

  size = strftime(s, 1000, "%B", &tm);
  test((size == 8) && !cmp(s, "February"), "strftime test #5", s);

  size = strftime(s, 1000, "%d", &tm);
  test((size == 2) && !cmp(s, "21"), "strftime test #6", s);

  size = strftime(s, 1000, "%H", &tm);
  test((size == 2) && !cmp(s, "20"), "strftime test #7", s);

  size = strftime(s, 1000, "%I", &tm);
  test((size == 2) && !cmp(s, "08"), "strftime test #8", s);

  size = strftime(s, 1000, "%j", &tm);
  test((size == 3) && !cmp(s, "052"), "strftime test #9", s);

  size = strftime(s, 1000, "%m", &tm);
  test((size == 2) && !cmp(s, "02"), "strftime test #10", s);

  size = strftime(s, 1000, "%M", &tm);
  test((size == 2) && !cmp(s, "23"), "strftime test #11", s);

  size = strftime(s, 1000, "%p", &tm);
  test((size == 2) && !cmp(s, "PM"), "strftime test #12", s);

  size = strftime(s, 1000, "%S", &tm);
  test((size == 2) && !cmp(s, "04"), "strftime test #13", s);

  size = strftime(s, 1000, "%U", &tm);
  test((size == 2) && !cmp(s, "07"), "strftime test #14", s);

  size = strftime(s, 1000, "%w", &tm);
  test((size == 1) && !cmp(s, "4"), "strftime test #15", s);

  size = strftime(s, 1000, "%W", &tm);
  test((size == 2) && !cmp(s, "07"), "strftime test #16", s);

  size = strftime(s, 1000, "%y", &tm);
  test((size == 2) && !cmp(s, "74"), "strftime test #17", s);

  size = strftime(s, 1000, "%Y", &tm);
  test((size == 4) && !cmp(s, "1974"), "strftime test #18", s);

  size = strftime(s, 1000, "%%", &tm);
  test((size == 1) && !cmp(s, "%"), "strftime test #19", s);

  size = strftime(s, 5, "%Y", &tm);
  test((size == 4) && !cmp(s, "1974"), "strftime test #20", s);

  size = strftime(s, 4, "%Y", &tm);
  test((size == 0), "strftime test #21", s);

  tm.tm_mon = 0;
  tm.tm_mday = 1;
  size = strftime(s, 10, "%U", &tm);
  test((size == 2) && !cmp(s, "00"), "strftime test #22", s);

  size = strftime(s, 10, "%W", &tm);
  test((size == 2) && !cmp(s, "00"), "strftime test #23", s);

  // 1/1/1973 was a Sunday and is in CW 1
  tm.tm_year = 73;
  size = strftime(s, 10, "%W", &tm);
  test((size == 2) && !cmp(s, "01"), "strftime test #24", s);

  // 1/1/1978 was a Monday and is in CW 1
  tm.tm_year = 78;
  size = strftime(s, 10, "%U", &tm);
  test((size == 2) && !cmp(s, "01"), "strftime test #25", s);

  // 2/1/1999
  tm.tm_year = 99;
  tm.tm_yday = 1;
  size = strftime(s, 10, "%G (%V)", &tm);
  test((size == 9) && !cmp(s, "1998 (53)"), "strftime test #26", s);

  size = strftime(s, 10, "%g", &tm);
  test((size == 2) && !cmp(s, "98"), "strftime test #27", s);

  // 30/12/1997
  tm.tm_year = 97;
  tm.tm_yday = 363;
  size = strftime(s, 10, "%G (%V)", &tm);
  test((size == 9) && !cmp(s, "1998 (01)"), "strftime test #28", s);

  size = strftime(s, 10, "%g", &tm);
  test((size == 2) && !cmp(s, "98"), "strftime test #29", s);
}