summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/date_time/test/gregorian/testformat_date_parser.cpp
blob: f29ab4ed2df410cb438cd626e4347e790da33c95 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Copyright (c) 2004 CrystalClear Software, Inc.
 * Use, modification and distribution is subject to the 
 * Boost Software License, Version 1.0. (See accompanying
 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 * Author: Jeff Garland
 */


#include "boost/date_time/gregorian/gregorian.hpp"
#include "../testfrmwk.hpp"
#include "boost/date_time/format_date_parser.hpp"
#include <sstream>
#include <algorithm>
#include <iostream>

const wchar_t* const wmonth_short_names[]={L"Jan",L"Feb",L"Mar",
                                          L"Apr",L"May",L"Jun",
                                          L"Jul",L"Aug",L"Sep",
                                          L"Oct",L"Nov",L"Dec"};

const char* const month_short_names[]={"Jan","Feb","Mar",
                                       "Apr","May","Jun",
                                       "Jul","Aug","Sep",
                                       "Oct","Nov","Dec"};

const char* const month_long_names[]={"January","February","March",
                                      "April","May","June",
                                      "July","August","September",
                                      "October","November","December"};

const wchar_t* const wmonth_long_names[]={L"January",L"February",L"March",
                                          L"April",L"May",L"June",
                                          L"July",L"August",L"September",
                                          L"October",L"Novomber",L"December"};

const wchar_t* const wweek_short_names[]= {L"Sun", L"Mon", L"Tue", L"Wed",
                                           L"Thu", L"Fri", L"Sat"};

const char* const week_short_names[]={"Sun", "Mon","Tue","Wed",
                                      "Thu","Fri","Sat"};

const wchar_t* const wweek_long_names[]= {L"Sunday", L"Monday", L"Tuesday", 
                                          L"Wednesday", L"Thursday", 
                                          L"Friday", L"Saturday"};

const char* const week_long_names[]= {"Sunday", "Monday", "Tuesday", 
                                      "Wednesday", "Thursday", 
                                      "Friday", "Saturday"};




std::vector<std::basic_string<char> > short_month_names;
std::vector<std::basic_string<wchar_t> > wshort_month_names;
std::vector<std::basic_string<char> > long_month_names;
std::vector<std::basic_string<wchar_t> > wlong_month_names;
std::vector<std::basic_string<char> > short_week_names;
std::vector<std::basic_string<wchar_t> > wshort_week_names;
std::vector<std::basic_string<char> > long_week_names;
std::vector<std::basic_string<wchar_t> > wlong_week_names;


using namespace boost::gregorian;

void
wtest_format(const std::basic_string<wchar_t>& format,
             const std::basic_string<wchar_t>& value,
             const std::string& testname,
             boost::gregorian::date expected_res)
{
  typedef boost::date_time::format_date_parser<date, wchar_t> parser_type;
  typedef std::basic_string<wchar_t> string_type;
  typedef std::istreambuf_iterator<wchar_t> iter_type;
  try {
    //    string_type format(format);
    std::basic_stringstream<wchar_t> ws;
    ws << value;
    iter_type sitr(ws);
    iter_type stream_end;

    parser_type p(format, wshort_month_names, wlong_month_names,
                  wshort_week_names, wlong_week_names);
    date d = p.parse_date(sitr, stream_end, format);
    check_equal(testname, d, expected_res);
  }
  catch(std::exception& e) {
    std::cout << "Got an exception: " << e.what() << std::endl;
    check(testname, false);
  }
}


void
test_format(const std::basic_string<char>& format,
            const std::basic_string<char>& value,
            const std::string& testname,
            boost::gregorian::date expected_res)
{
  typedef boost::date_time::format_date_parser<date, char> parser_type;
  typedef std::basic_string<char> string_type;
  typedef std::istreambuf_iterator<char> iter_type;
  try {
    string_type format(format);
    std::basic_stringstream<char> ws;
    ws << value; 
    iter_type sitr(ws);
    iter_type stream_end;

    parser_type pt(format, short_month_names, long_month_names,
                   short_week_names, long_week_names);
    date d = pt.parse_date(sitr, stream_end, format);
    check_equal(testname, d, expected_res);
  }
  catch(std::exception& e) {
    std::cout << "Got an exception: " << e.what() << std::endl;
    check(testname, false);
  }
}


template<typename charT>
void
test_format2(boost::date_time::format_date_parser<date, charT>& parser,
             const charT* const format,
             const charT* const value,
             const std::string& testname,
             boost::gregorian::date expected_res)
{
  try {
    date d = parser.parse_date(value, format);
    check_equal(testname, d == expected_res);
  }
  catch(std::exception& e) {
    std::cout << "Got an exception: " << e.what() << std::endl;
    check(testname, false);
  }
}

int
main()
{
  std::copy(&wmonth_short_names[0], 
            &wmonth_short_names[12],
            std::back_inserter(wshort_month_names));

  std::copy(&month_short_names[0], 
            &month_short_names[12],
            std::back_inserter(short_month_names));

  std::copy(&month_long_names[0], 
            &month_long_names[12],
            std::back_inserter(long_month_names));

  std::copy(&wmonth_long_names[0], 
            &wmonth_long_names[12],
            std::back_inserter(wlong_month_names));


  std::copy(&wweek_short_names[0], 
            &wweek_short_names[7],
            std::back_inserter(wshort_week_names));

  std::copy(&week_short_names[0], 
            &week_short_names[7],
            std::back_inserter(short_week_names));

  std::copy(&wweek_long_names[0], 
            &wweek_long_names[7],
            std::back_inserter(wlong_week_names));

  std::copy(&week_long_names[0], 
            &week_long_names[7],
            std::back_inserter(long_week_names));

  
  wtest_format(L"1%%23%Y %m %d", L"1232004 12 31 other stuff...",
               "wide and weird", date(2004,12,31));

  wtest_format(L"%Y-%m-%d", L"2004-12-31",
               "%Y-%m-%d wide", date(2004,12,31));

  wtest_format(L"%Y day %j", L"2004 day 001",
               "%Y day %j wide", date(2004,1,1));

  test_format("%m/%d/%y", "10/31/04",
              "%m/%d/%y", date(2004,10,31));


  wtest_format(L"%Y/%m/%d", L"2004-12-31",
               "%Y/%m/%d wide 2004-12-31 input", date(2004,12,31));

  test_format("%Y.%d.%m", "2004.31.1",
              "%Y.%d.%m var length", date(2004,Jan,31));

  test_format("%d.%m.%Y", "1.1.2004",
              "%d.%m.%Y var length month and day", date(2004,1,1));

  test_format("%Y.%m.%d", "2004.1.31",
              "%Y.%m.%d var length month", date(2004,Jan,31));

  test_format("%Y.%b.%d", "2004.Jan.1",
              "%Y.%b.%d var length month", date(2004,Jan,1));

  test_format("%Y%m%d", "20041231",
              "%Y%m%d undelimited", date(2004,12,31));

  test_format("%Y/%d/%b", "2004/01/Jan",
              "%Y/%d/%b month at end", date(2004,1,1));

  test_format("%Y/%b/%d", "2004/Jan/01",
              "%Y/%b/%d named month jan", date(2004,1,1));

  test_format("%Y/%b/%d", "2004/Dec/20",
              "%Y/%b/%d named month dec", date(2004,12,20));

  wtest_format(L"%Y/%b/%d", L"2004-Jul-31",
               "%Y/%b/%d wide 2004-Jul-31 input", date(2004,7,31));

  wtest_format(L"%B %d, %Y", L"March 15, 2004",
               "%B %d, %Y", date(2004,3,15));

  wtest_format(L"%a %B %d, %Y", L"Sun March 15, 2004",
               "%a %B %d, %Y", date(2004,3,15));

  wtest_format(L"%A %B %d, %Y", L"Sunday March 15, 2004",
               "%A %B %d, %Y", date(2004,3,15));

  // bad format case...

  {
    try {
      std::wstring format(L"%Y-%d");
      std::wstringstream ws;
      ws << L"2004-12-31";
      std::istreambuf_iterator<wchar_t> sitr(ws);
      std::istreambuf_iterator<wchar_t> stream_end;

      boost::date_time::format_date_parser<date,wchar_t> pt(format, 
                                                            wshort_month_names,
                                                            wlong_month_names,
                                                            wshort_week_names,
                                                            wlong_week_names);
      date d = pt.parse_date(sitr, stream_end);
      check("Bad format spec test", false);
    }
    catch(std::exception& e) {
      std::cout << "Got an expected exception: " << e.what() << std::endl;
      check("Bad format spec test -- pass", true);

    }
  }

  {
    //some interesting month names 
    const char* const roman_months[]={"I","II","III",
                                      "IV","V","VI",
                                      "VII","VIII","IX",
                                      "X","XI","XII"};
    std::vector<std::basic_string<char> > roman_month_names;
    std::copy(&roman_months[0], 
              &roman_months[12],
              std::back_inserter(roman_month_names));

    std::string format("%Y.%b.%d");

    boost::date_time::format_date_parser<date,char> parser(format, 
                                                           roman_month_names,
                                                           long_month_names,
                                                           short_week_names,
                                                           long_week_names);


    test_format2(parser, "%Y.%b.%d", "2004-I-1",
                 "roman I", date(2004,Jan,1));
    test_format2(parser, "%Y.%b.%d", "2004-II-01",
                 "roman II", date(2004,Feb,1));
    test_format2(parser, "%Y.%b.%d", "2004-III-01",
                 "roman III", date(2004,Mar,1));
    test_format2(parser, "%Y.%b.%d", "2004-IV-01",
                 "roman IV", date(2004,Apr,1));
    test_format2(parser, "%Y.%b.%d", "2004-V-01",
                 "roman V", date(2004,May,1));
    test_format2(parser, "%Y.%b.%d", "2004-VI-01",
                 "roman VI", date(2004,Jun,1));
    test_format2(parser, "%Y.%b.%d", "2004-VII-01",
                 "roman VII", date(2004,Jul,1));
    test_format2(parser, "%Y.%b.%d", "2004-VIII-01",
                 "roman VIII", date(2004,Aug,1));
    test_format2(parser, "%Y.%b.%d", "2004-IX-01",
                 "roman IX", date(2004,Sep,1));
    test_format2(parser, "%Y.%b.%d", "2004-X-01",
                 "roman X", date(2004,Oct,1));
    test_format2(parser, "%Y.%b.%d", "2004-XI-01",
                 "roman XI", date(2004,Nov,1));
    test_format2(parser, "%Y.%b.%d", "2004 XII 1",
                 "roman XII", date(2004,Dec,1));

  }

  { 
    //alternate constructor that takes month/weekday strings
    //from a locale
    std::string format("%Y %m %d");
    boost::date_time::format_date_parser<date,char> parser(format, 
                                                           std::locale::classic());
    test_format2(parser, "%a %Y.%b.%d", "Sun 2004 Jan 1",
                 "strings from locale", date(2004,Jan,1));
    test_format2(parser, "%a %Y.%b.%d", "Mon 2004 Feb 1",
                 "strings from locale", date(2004,Feb,1));
    test_format2(parser, "%a %Y.%b.%d", "Tue 2004 Mar 1",
                 "strings from locale", date(2004,Mar,1));
    test_format2(parser, "%a %Y.%b.%d", "Wed 2004 Apr 1",
                 "strings from locale", date(2004,Apr,1));
    test_format2(parser, "%a %Y.%b.%d", "thu 2004 May 1",
                 "strings from locale", date(2004,May,1));
    test_format2(parser, "%a %Y.%b.%d", "fri 2004 Jun 1",
                 "strings from locale", date(2004,Jun,1));
    test_format2(parser, "%a %Y.%b.%d", "sat 2004 Jul 1",
                 "strings from locale", date(2004,Jul,1));
    test_format2(parser, "%Y.%b.%d", "2004 Aug 1",
                 "strings from locale", date(2004,Aug,1));
    test_format2(parser, "%Y.%b.%d", "2004 Sep 1",
                 "strings from locale", date(2004,Sep,1));
    test_format2(parser, "%Y.%b.%d", "2004 Sep 1",
                 "strings from locale", date(2004,Sep,1));
    test_format2(parser, "%Y.%b.%d", "2004 Oct 1",
                 "strings from locale", date(2004,Oct,1));
    test_format2(parser, "%Y.%b.%d", "2004 Nov 1",
                 "strings from locale", date(2004,Nov,1));
    test_format2(parser, "%Y.%b.%d", "2004 Dec 1",
                 "strings from locale", date(2004,Dec,1));

    test_format2(parser, "%A %B %d, %Y", "Sunday January 1, 2004",
                 "long strings from locale", date(2004,Jan,1));
    test_format2(parser, "%A %B %d, %Y", "Monday February 29, 2004",
                 "long strings from locale", date(2004,Feb,29));
    test_format2(parser, "%A %B %d, %Y", "Tuesday March 1, 2004",
                 "long strings from locale", date(2004,Mar,1));
    test_format2(parser, "%A %B %d, %Y", "Wednesday APRIL 1, 2004",
                 "long strings from locale", date(2004,Apr,1));
    test_format2(parser, "%A %B %d, %Y", "thursday may 15, 2004",
                 "long strings from locale", date(2004,May,15));
    test_format2(parser, "%A %B %d, %Y", "friday june 15, 2004",
                 "long strings from locale", date(2004,Jun,15));
    test_format2(parser, "%A %B %d, %Y", "saturday july 30, 2004",
                 "long strings from locale", date(2004,Jul,30));
    test_format2(parser, "%A %B %d, %Y", "thursday auguST 15, 2004",
                 "long strings from locale", date(2004,Aug,15));
    test_format2(parser, "%A %B %d, %Y", "thursday september 1, 2004",
                 "long strings from locale", date(2004,Sep,1));
    test_format2(parser, "%A %B %d, %Y", "thursday october 1, 2004",
                 "long strings from locale", date(2004,Oct,1));
    test_format2(parser, "%A %B %d, %Y", "thursday november 1, 2004",
                 "long strings from locale", date(2004,Nov,1));
    test_format2(parser, "%A %B %d, %Y", "thursday december 31, 2004",
                 "long strings from locale", date(2004,Dec,31));

  }

  return printTestStats();

}