aboutsummaryrefslogtreecommitdiff
path: root/tests/timestamp/driver.cxx
blob: 4c366fca79db005e6390449d0b7d6fac8b4f1bb2 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// file      : tests/timestamp/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <time.h> // tzset()

#include <chrono>
#include <locale>
#include <clocale>
#include <cassert>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <system_error>

#include <butl/timestamp>

using namespace std;
using namespace butl;

// Parse the input using the format string. Print the resulted time with the
// same format string, ensure the output matches the input.
//
static bool
parse (const char* in, const char* fmt, bool local, string out)
{
  if (out.empty ())
    out = in;

  try
  {
    const char* e;
    timestamp t (from_string (in, fmt, local, &e));

    ostringstream o;
    if (!to_stream (o, t, fmt, false, local))
      return false;

    return o.str () + e == out;
  }
  catch (...)
  {
    return false;
  }
}

static bool
parse (const char* in, const char* fmt, const string& out = "")
{
  return parse (in, fmt, true, out) && parse (in, fmt, false, out);
}

static bool
fail (const char* in, const char* fmt)
{
  try
  {
    from_string (in, fmt, true);
    return false;
  }
  catch (const system_error&)
  {
    return true;
  }
}

// Convert nanoseconds to string according to the butl::duration period.
//
static string
ns (unsigned long long t)
{
  duration d (chrono::duration_cast<duration> (chrono::nanoseconds (t)));
  chrono::nanoseconds n (chrono::duration_cast<chrono::nanoseconds> (d));

  ostringstream o;
  o.fill ('0');
  o << setw (9) << n.count ();
  return o.str ();
}

int
main ()
{
  tzset (); // To use butl::to_stream() later on.

  // Invalid %[].
  //
  assert (fail ("Apr 08 19:31:10 2016", "%b %d %H:%M:%S%["));
  assert (fail ("Apr 08 19:31:10 2016", "%b %d %H:%M:%S%[."));
  assert (fail ("Apr 08 19:31:10 2016", "%b %d %H:%M:%S%[.U"));
  assert (fail ("Apr 08 19:31:10 2016", "%b %d %H:%M:%S%[.A]"));
  assert (fail ("Apr 08 19:31:10 2016", "%d %H:%M:%S%[.U] %Y"));
  assert (fail ("2016-10-20 11:12:13.123456789", "%Y-%m-%d %H:%M:%S%[N]"));

  // Invalid fraction of a second.
  //
  assert (fail ("Apr 08 19:31:10. 2016", "%b %d %H:%M:%S%[.U] %Y"));
  assert (fail ("Apr 08 19:31:10.1 2016", "%b %d %H:%M:%S%[.M] %Y"));
  assert (fail ("Apr 08 19:31:10.12 2016", "%b %d %H:%M:%S%[.M] %Y"));
  assert (fail ("Apr 08 19:31:10.", "%b %d %H:%M:%S%[.U] %Y"));
  assert (fail ("Apr 08 19:31:10.1", "%b %d %H:%M:%S%[.M] %Y"));
  assert (fail ("Apr 08 19:31:10.12", "%b %d %H:%M:%S%[.M] %Y"));

  // Input is not fully parsed.
  //
  assert (fail (
    "Feb 21 19:31:10.123456789 2016 GMT", "%b %d %H:%M:%S%[.N] %Y"));

  // Invalid input (%[] unrelated).
  //
  assert (fail ("Apr 08 19:31:10.123456789 ABC", "%b %d %H:%M:%S%[.N] %Y"));
  assert (fail ("Apr 19:31:10 2016", "%b %d %H:%M:%S %Y"));
  assert (fail ("Opr 08 19:31:10 2016", "%b %d %H:%M:%S %Y"));

  // Parse valid input with a valid format.
  //
  assert (parse (
    "Apr  18 19:31:10 2016", "%b %d %H:%M:%S  %Y", "Apr 18 19:31:10  2016"));

  assert (parse ("Apr 08 19:31:10 2016", "%b %d %H:%M:%S %Y"));
  assert (parse ("2016-04-08 19:31:10", "%Y-%m-%d %H:%M:%S"));

  assert (parse ("ABC=Apr 18 19:31:10 2016 ABC", "ABC=%b %d %H:%M:%S %Y"));
  assert (parse ("ABC=2016-04-08 19:31:10 ABC", "ABC=%Y-%m-%d %H:%M:%S"));

  assert (parse ("Feb 11 19:31:10 2016 GMT", "%b %d %H:%M:%S%[.N] %Y"));
  assert (parse ("2016-02-11 19:31:10 GMT", "%Y-%m-%d %H:%M:%S%[.N]"));

  assert (parse ("Feb 21 19:31:10.384902285 2016 GMT",
                 "%b %d %H:%M:%S%[.N] %Y",
                 "Feb 21 19:31:10." + ns (384902285) + " 2016 GMT"));

  assert (parse ("2016-02-21 19:31:10.384902285 GMT",
                 "%Y-%m-%d %H:%M:%S%[.N]",
                 "2016-02-21 19:31:10." + ns (384902285) + " GMT"));

  assert (parse ("Feb 21 19:31:10 .384902285 2016 GMT",
                 "%b %d %H:%M:%S %[.N] %Y",
                 "Feb 21 19:31:10 ." + ns (384902285) + " 2016 GMT"));

  assert (parse ("2016-02-21 19:31:10 .384902285 GMT",
                 "%Y-%m-%d %H:%M:%S %[.N]",
                 "2016-02-21 19:31:10 ." + ns (384902285) + " GMT"));

  assert (parse (
    "2016-02-21 19:31:10  .384902285 GMT",
    "%Y-%m-%d %H:%M:%S %[.N]",
    "2016-02-21 19:31:10 ." + ns (384902285) + " GMT"));

  assert (parse (
    "2016-02-21 19:31:10 .384902285  GMT",
    "%Y-%m-%d %H:%M:%S  %[.N]",
    "2016-02-21 19:31:10  ." + ns (384902285) + "  GMT"));

  assert (parse ("Feb 21 19:31:10 .384902285NS 2016 GMT",
                 "%b %d %H:%M:%S %[.N]NS %Y",
                 "Feb 21 19:31:10 ." + ns (384902285) + "NS 2016 GMT"));

  assert (parse ("2016-02-21 19:31:10 .384902285NS GMT",
                 "%Y-%m-%d %H:%M:%S %[.N]NS",
                 "2016-02-21 19:31:10 ." + ns (384902285) + "NS GMT"));

  assert (parse (".384902285 Feb 21 19:31:10 2016",
                 "%[.N] %b %d %H:%M:%S %Y",
                 "." + ns (384902285) + " Feb 21 19:31:10 2016"));

  assert (parse (".384902285 2016-02-21 19:31:10",
                 "%[.N] %Y-%m-%d %H:%M:%S",
                 "." + ns (384902285) + " 2016-02-21 19:31:10"));

  assert (parse (".3849022852016-02-21 19:31:10",
                 "%[.N]%Y-%m-%d %H:%M:%S",
                 "." + ns (384902285) + "2016-02-21 19:31:10"));

  /*
  setlocale (LC_ALL, "de_DE.utf-8");
  locale::global (locale ("de_DE.utf-8"));
  assert (parse ("Mai 11 19:31:10 2016 GMT", "%b %d %H:%M:%S%[.N] %Y"));
  locale::global (locale ("C"));
  */

  // @@ When debuging strptime() fallback implementation compiled with GCC
  //    5.3.1, the following asserts will fail due to bugs in implementation
  //    of std::get_time() manipulator. So need to be commented out.
  //
  assert (fail ("Apr 08 19:31:10 2016", "%b %d %H:%M:%S %Y %"));
  assert (fail ("Apr 08 19:31:10", "%b %d %H:%M:%S %Y"));

  assert (parse (
    "Apr  8 19:31:10 2016", "%b %d %H:%M:%S %Y", "Apr 08 19:31:10 2016"));
}