aboutsummaryrefslogtreecommitdiff
path: root/tests/timestamp/driver.cxx
blob: 2db726d0375ee9846e47baab74822dd64200cba5 (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
// 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 <locale>
#include <clocale>
#include <cassert>
#include <sstream>
#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, const char* out)
{
  if (out == nullptr)
    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 char* out = nullptr)
{
  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;
  }
}

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"));
  assert (parse (
    "2016-02-21 19:31:10.384902285 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"));
  assert (parse (
    "2016-02-21 19:31:10 .384902285 GMT", "%Y-%m-%d %H:%M:%S %[.N]"));

  assert (parse (
    "2016-02-21 19:31:10  .384902285 GMT",
    "%Y-%m-%d %H:%M:%S %[.N]",
    "2016-02-21 19:31:10 .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  .384902285  GMT"));

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

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

  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"));
}