File: testdate_facet_new.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (388 lines) | stat: -rw-r--r-- 14,948 bytes parent folder | download | duplicates (12)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388


/* 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, Bart Garst
 * $Date$
 */

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


template<class temporal_type, typename charT>
inline
void
teststreaming(std::string testname,
              temporal_type value,
              std::basic_string<charT> expected_result,
              const std::locale& locale = std::locale::classic())
{
  std::basic_stringstream<charT> ss;
  ss.imbue(locale);
  ss << value;
  check_equal(testname, ss.str(), expected_result);
}


// collections for adding to facet
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 char* const weekday_short_names[]={"day1", "day2","day3","day4",
                                         "day5","day6","day7"};

const char* const weekday_long_names[]= {"Sun-0", "Mon-1", "Tue-2", 
                                         "Wed-3", "Thu-4", 
                                         "Fri-5", "Sat-6"};

std::vector<std::basic_string<char> > short_weekday_names;
std::vector<std::basic_string<char> > long_weekday_names;
std::vector<std::basic_string<char> > short_month_names;
std::vector<std::basic_string<char> > long_month_names;

#if !defined(BOOST_NO_STD_WSTRING)
// collections of test results
const std::wstring full_months[]={L"January",L"February",L"March",
                                  L"April",L"May",L"June",
                                  L"July",L"August",L"September",
                                  L"October",L"November",L"December"};
const std::wstring short_months[]={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 std::wstring full_weekdays[]= {L"Sunday", L"Monday",L"Tuesday", 
                                     L"Wednesday", L"Thursday", 
                                     L"Friday", L"Saturday"};
const std::wstring short_weekdays[]= {L"Sun", L"Mon",L"Tue", 
                                      L"Wed", L"Thu", 
                                      L"Fri", L"Sat"};

//const whcar_t const 
#endif // BOOST_NO_STD_WSTRING

int main() {
  using namespace boost::gregorian;

  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(&weekday_short_names[0],
            &weekday_short_names[7],
            std::back_inserter(short_weekday_names));

  std::copy(&weekday_long_names[0],
            &weekday_long_names[7],
            std::back_inserter(long_weekday_names));

  {
    std::stringstream ss;
    date d(2004,Oct,31);
    date_period dp(d, d + days(7));
    ss << d;
    check("to_string & default formats match",
        to_simple_string(d) == ss.str());
    ss.str("");
    ss << dp;
    check("to_string & default formats match",
        to_simple_string(dp) == ss.str());
  }

  {
    date d(2004,Oct, 13);
    date_period dp(d, d + days(7));
    {
      date_facet* datefacet = new date_facet();
      datefacet->format(date_facet::standard_format_specifier);
      std::cout.imbue(std::locale(std::locale::classic(), datefacet));
      teststreaming("default classic date", d, std::string("10/13/04"),
                    std::locale(std::locale::classic(), datefacet));
      std::cout << "default classic date output: " << d << std::endl;

    }
    {
      date_facet* datefacet = new date_facet();
      datefacet->format(date_facet::standard_format_specifier);
      teststreaming("default classic date period", dp, 
                    std::string("[10/13/04/10/19/04]"),
                    std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet();
      datefacet->format("%Y-%d-%b %a");
      teststreaming("custom date facet date period", dp, 
                    std::string("[2004-13-Oct Wed/2004-19-Oct Tue]"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet();
      datefacet->set_iso_format();
      teststreaming("custom date facet date", d, 
                    std::string("20041013"), 
                    std::locale(std::locale::classic(), datefacet));

    }
    {
      date_facet* datefacet = new date_facet();
      datefacet->set_iso_format();
      teststreaming("custom date facet date period", dp, 
                    std::string("[20041013/20041019]"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet();
      datefacet->set_iso_extended_format();
      teststreaming("custom date facet date", d, 
                    std::string("2004-10-13"), 
                    std::locale(std::locale::classic(), datefacet));

    }
    {
      date_facet* datefacet = new date_facet();
      datefacet->set_iso_extended_format();
      teststreaming("custom date facet date period", dp, 
                    std::string("[2004-10-13/2004-10-19]"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet();
      datefacet->set_iso_extended_format();
      period_formatter pf(period_formatter::AS_OPEN_RANGE, " / ", "[ ", " )", " ]");
      datefacet->period_formatter(pf);
      teststreaming("custom date facet date period - open range custom delimeters", dp, 
                    std::string("[ 2004-10-13 / 2004-10-20 )"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    // trac-11142: actually test delimiter_strings(...)
    {
        date_facet* datefacet = new date_facet();
        datefacet->set_iso_extended_format();
        period_formatter pf(period_formatter::AS_OPEN_RANGE, " / ", "[ ", " )", " ]");
        pf.delimiter_strings(" to ", "from ", " inclusive", " exclusive");
        datefacet->period_formatter(pf);
        teststreaming("custom date facet date period - delimiter_strings", dp,
            std::string("from 2004-10-13 to 2004-10-20 inclusive"),
            std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet("%A %b %d, %Y");
      datefacet->short_month_names(short_month_names);
      teststreaming("custom date facet -- custom short month names", d, 
                    std::string("Wednesday *oct* 13, 2004"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet("%B %A %d, %Y");
      datefacet->long_month_names(long_month_names);
      teststreaming("custom date facet -- custom long month names", d, 
                    std::string("**October** Wednesday 13, 2004"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet("%a - %b %d, %Y");
      datefacet->short_weekday_names(short_weekday_names);
      std::cout.imbue(std::locale(std::locale::classic(), datefacet));
      std::cout << d << std::endl;
      teststreaming("custom date facet -- custom short weekday names", d, 
                    std::string("day4 - Oct 13, 2004"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    {
      date_facet* datefacet = new date_facet("%b %d, %Y ++ %A");
      datefacet->long_weekday_names(long_weekday_names);
      teststreaming("custom date facet -- custom short weekday names", d, 
                    std::string("Oct 13, 2004 ++ Wed-3"), 
                    std::locale(std::locale::classic(), datefacet));
    }

    {//date
      date_facet* datefacet = new date_facet("%Y-%b-%d %%d");
      teststreaming("Literal '%' in date format", d, 
                    std::string("2004-Oct-13 %d"), 
                    std::locale(std::locale::classic(), datefacet));
    }
    {
      date_facet* datefacet = new date_facet("%Y-%b-%d %%%d");
      teststreaming("Multiple literal '%'s in date format", d, 
                    std::string("2004-Oct-13 %13"), 
                    std::locale(std::locale::classic(), datefacet));
    }
    {
      date d1(2004,Oct, 13);
      date_facet* datefacet = new date_facet("%d%m%y");
      teststreaming("Single digit year and %y", d1,
                    std::string("131004"),
                    std::locale(std::locale::classic(), datefacet));
    }
    {//month
      date_facet* datefacet = new date_facet();
      datefacet->month_format("%b %%b");
      teststreaming("Literal '%' in month format", d.month(), 
                    std::string("Oct %b"), 
                    std::locale(std::locale::classic(), datefacet));
    }
    {
      date_facet* datefacet = new date_facet();
      datefacet->month_format("%b %%%b");
      teststreaming("Multiple literal '%'s in month format", d.month(), 
                    std::string("Oct %Oct"), 
                    std::locale(std::locale::classic(), datefacet));
    }
    {//weekday
      date_facet* datefacet = new date_facet();
      datefacet->weekday_format("%a %%a");
      teststreaming("Literal '%' in weekday format", d.day_of_week(), 
                    std::string("Wed %a"), 
                    std::locale(std::locale::classic(), datefacet));
    }
    {
      date_facet* datefacet = new date_facet();
      datefacet->weekday_format("%a %%%a");
      teststreaming("Multiple literal '%'s in weekday format", d.day_of_week(), 
                    std::string("Wed %Wed"), 
                    std::locale(std::locale::classic(), datefacet));
    }



    date d_not_date(not_a_date_time);
    teststreaming("special value, no special facet", d_not_date, std::string("not-a-date-time"));


//       std::cout.imbue(std::locale(std::locale::classic(), datefacet));
//       std::cout << d << std::endl;


  }

  // date_generator tests
  {
    partial_date pd(31,Oct);
    teststreaming("partial date", pd, std::string("31 Oct"));
    first_kday_of_month fkd(Tuesday, Sep);
    teststreaming("first kday", fkd, std::string("first Tue of Sep"));
    nth_kday_of_month nkd2(nth_kday_of_month::second, Tuesday, Sep);
    teststreaming("nth kday", nkd2, std::string("second Tue of Sep"));
    nth_kday_of_month nkd3(nth_kday_of_month::third, Tuesday, Sep);
    teststreaming("nth kday", nkd3, std::string("third Tue of Sep"));
    nth_kday_of_month nkd4(nth_kday_of_month::fourth, Tuesday, Sep);
    teststreaming("nth kday", nkd4, std::string("fourth Tue of Sep"));
    nth_kday_of_month nkd5(nth_kday_of_month::fifth, Tuesday, Sep);
    teststreaming("nth kday", nkd5, std::string("fifth Tue of Sep"));
    last_kday_of_month lkd(Tuesday, Sep);
    teststreaming("last kday", lkd, std::string("last Tue of Sep"));
    first_kday_before fkb(Wednesday);
    teststreaming("First before", fkb, std::string("Wed before"));
    first_kday_after fka(Thursday);
    teststreaming("First after", fka, std::string("Thu after"));
  }

#if !defined(BOOST_NO_STD_WSTRING) 
    date d(2004,Oct, 13);
    date_period dp(d, d + days(7));
    date d_not_date(not_a_date_time);
    
    teststreaming("special value, no special facet wide", d_not_date, 
                  std::wstring(L"not-a-date-time"));
  {
    wdate_facet* wdatefacet = new wdate_facet();
    wdatefacet->format(wdate_facet::standard_format_specifier);
    teststreaming("widestream default classic date", d, 
                  std::wstring(L"10/13/04"),
                  std::locale(std::locale::classic(), wdatefacet));
  }
  {
    wdate_facet* wdatefacet = new wdate_facet();
    wdatefacet->format(wdate_facet::standard_format_specifier);
    teststreaming("widestream default classic date period", dp, 
                  std::wstring(L"[10/13/04/10/19/04]"),
                  std::locale(std::locale::classic(), wdatefacet));
  }
  {
    wdate_facet* wdatefacet = new wdate_facet();
    wdatefacet->format(L"%Y-%d-%b %a");
    teststreaming("widestream custom date facet", d, 
                  std::wstring(L"2004-13-Oct Wed"), 
                  std::locale(std::locale::classic(), wdatefacet));
  }
  {
    wdate_facet* wdatefacet = new wdate_facet();
    wdatefacet->format(L"%Y-%d-%b %a");
    teststreaming("widestream custom date facet date period", dp, 
                  std::wstring(L"[2004-13-Oct Wed/2004-19-Oct Tue]"), 
                  std::locale(std::locale::classic(), wdatefacet));
  }
  {
    wdate_facet* wdatefacet = new wdate_facet();
    wdatefacet->set_iso_extended_format();
    wperiod_formatter pf(wperiod_formatter::AS_OPEN_RANGE, L" / ", L"[ ", L" )", L" ]");
    wdatefacet->period_formatter(pf);
    teststreaming("custom date facet date period - open range custom delimeters", dp, 
                  std::wstring(L"[ 2004-10-13 / 2004-10-20 )"), 
                  std::locale(std::locale::classic(), wdatefacet));
  }
  /************* small gregorian types tests *************/ 
  wdate_facet* small_types_facet = new wdate_facet();
  std::locale loc = std::locale(std::locale::classic(), small_types_facet);

  // greg_year test
  greg_year gy(2004);
  teststreaming("greg_year", gy, std::string("2004"));

  // greg_month tests
  {
    for(greg_month::value_type i = 0; i < 12; ++i) {
      greg_month m(i+1); // month numbers 1-12
      teststreaming("greg_month short", m, short_months[i], loc);
    }
    small_types_facet->month_format(L"%B"); // full name
    for(greg_month::value_type i = 0; i < 12; ++i) {
      greg_month m(i+1); // month numbers 1-12
      teststreaming("greg_month full", m, full_months[i], loc);
    }
  }

  // greg_weekday tests
  {
    for(greg_weekday::value_type i = 0; i < 7; ++i) {
      greg_weekday gw(i); // weekday numbers 0-6
      teststreaming("greg_weekday short", gw, short_weekdays[i], loc);
    }
    small_types_facet->weekday_format(L"%A"); // full name
    for(greg_weekday::value_type i = 0; i < 7; ++i) {
      greg_weekday gw(i); // weekday numbers 0-6
      teststreaming("greg_weekday full", gw, full_weekdays[i], loc);
    }
  }
#endif // BOOST_NO_STD_WSTRING

  return printTestStats();
}