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
|
/* 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 or http://www.boost.org/LICENSE-1.0)
* Author: Jeff Garland
* $Date: 2004/10/14 00:04:34 $
*/
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/date_facet.hpp"
#include "boost/date_time/testfrmwk.hpp"
#include <iostream>
#include <sstream>
//temporary output code in the test file. This will eventually move into the core
//of the library...
namespace boost { namespace gregorian {
typedef boost::date_time::period_formatter<wchar_t> wperiod_formatter;
typedef boost::date_time::period_formatter<char> period_formatter;
typedef boost::date_time::date_facet<date,wchar_t> wdate_facet;
typedef boost::date_time::date_facet<date,char> date_facet;
template <class CharT, class TraitsT>
inline std::basic_ostream<CharT, TraitsT>&
operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date& d) {
typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
typedef std::time_put<CharT> std_date_facet;
std::ostreambuf_iterator<CharT> oitr(os);
if (std::has_facet<custom_date_facet>(os.getloc()))
std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), d);
else {
//instantiate a custom facet for dealing with dates since the user
//has not put one in the stream so far. This is for efficiency
//since we would always need to reconstruct for every date
//if the locale did not already exist. Of course this will be overridden
//if the user imbues at some later point. With the default settings
//for the facet the resulting format will be the same as the
//std::time_facet settings.
std::ostreambuf_iterator<CharT> oitr(os);
custom_date_facet* f = new custom_date_facet();
std::locale l = std::locale(os.getloc(), f);
os.imbue(l);
f->put(oitr, os, os.fill(), d);
}
return os;
}
template <class charT, class traits>
inline
std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
{
os << dd.get_rep();
return os;
}
template <class CharT, class TraitsT>
inline std::basic_ostream<CharT, TraitsT>&
operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date_period& dp) {
typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
typedef std::time_put<CharT> std_date_facet;
std::ostreambuf_iterator<CharT> oitr(os);
if (std::has_facet<custom_date_facet>(os.getloc()))
std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), dp);
else {
//instantiate a custom facet for dealing with date periods since the user
//has not put one in the stream so far. This is for efficiency
//since we would always need to reconstruct for every time period
//if the local did not already exist. Of course this will be overridden
//if the user imbues at some later point. With the default settings
//for the facet the resulting format will be the same as the
//std::time_facet settings.
std::ostreambuf_iterator<CharT> oitr(os);
custom_date_facet* f = new custom_date_facet();
std::locale l = std::locale(os.getloc(), f);
os.imbue(l);
f->put(oitr, os, os.fill(), dp);
}
return os;
}
} } // gregorian
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(testname, ss.str() == expected_result);
}
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;
int main() {
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));
using namespace boost::gregorian;
{
date d(2004,Oct, 13);
teststreaming("widestream default classic date", d, std::wstring(L"10/13/04"));
teststreaming("default classic date", d, std::string("10/13/04"));
date_period dp(d, d + days(7));
teststreaming("widestream default classic date period", dp,
std::wstring(L"[10/13/04/10/19/04]"));
teststreaming("default classic date period", dp,
std::string("[10/13/04/10/19/04]"));
{
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));
}
{
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));
}
{
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));
}
{
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 d_not_date(not_a_date_time);
teststreaming("special value, no special facet", d_not_date, std::string("not-a-date-time"));
teststreaming("special value, no special facet wide", d_not_date,
std::wstring(L"not-a-date-time"));
// std::cout.imbue(std::locale(std::locale::classic(), datefacet));
// std::cout << d << std::endl;
}
return printTestStats();
}
|