1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   form_date_time.cpp
9  * \author Andrey Semashev
10  * \date   07.02.2009
11  *
12  * \brief  This header contains tests for the date and time formatters.
13  */
14 
15 #define BOOST_TEST_MODULE form_date_time
16 
17 #include <memory>
18 #include <locale>
19 #include <string>
20 #include <iomanip>
21 #include <ostream>
22 #include <algorithm>
23 #include <boost/date_time.hpp>
24 #include <boost/test/unit_test.hpp>
25 #include <boost/log/expressions.hpp>
26 #include <boost/log/attributes/constant.hpp>
27 #include <boost/log/attributes/attribute_set.hpp>
28 #include <boost/log/utility/string_literal.hpp>
29 #include <boost/log/utility/formatting_ostream.hpp>
30 #include <boost/log/core/record.hpp>
31 #include <boost/log/support/date_time.hpp>
32 #include "char_definitions.hpp"
33 #include "make_record.hpp"
34 
35 namespace logging = boost::log;
36 namespace attrs = logging::attributes;
37 namespace expr = logging::expressions;
38 namespace keywords = logging::keywords;
39 
40 typedef boost::posix_time::ptime ptime;
41 typedef boost::gregorian::date gdate;
42 typedef ptime::time_duration_type duration;
43 
44 namespace {
45 
46     template< typename CharT >
47     struct date_time_formats;
48 
49 #ifdef BOOST_LOG_USE_CHAR
50     template< >
51     struct date_time_formats< char >
52     {
53         typedef logging::basic_string_literal< char > string_literal_type;
54 
default_date_format__anon18bbfe7f0111::date_time_formats55         static string_literal_type default_date_format() { return logging::str_literal("%Y-%b-%d"); }
default_time_format__anon18bbfe7f0111::date_time_formats56         static string_literal_type default_time_format() { return logging::str_literal("%H:%M:%S.%f"); }
default_date_time_format__anon18bbfe7f0111::date_time_formats57         static string_literal_type default_date_time_format() { return logging::str_literal("%Y-%b-%d %H:%M:%S.%f"); }
default_time_duration_format__anon18bbfe7f0111::date_time_formats58         static string_literal_type default_time_duration_format() { return logging::str_literal("%-%H:%M:%S.%f"); }
59 
date_format__anon18bbfe7f0111::date_time_formats60         static string_literal_type date_format() { return logging::str_literal("%d/%m/%Y"); }
time_format__anon18bbfe7f0111::date_time_formats61         static string_literal_type time_format() { return logging::str_literal("%H.%M.%S"); }
date_time_format__anon18bbfe7f0111::date_time_formats62         static string_literal_type date_time_format() { return logging::str_literal("%d/%m/%Y %H.%M.%S"); }
time_duration_format__anon18bbfe7f0111::date_time_formats63         static string_literal_type time_duration_format() { return logging::str_literal("%+%H.%M.%S.%f"); }
64     };
65 #endif // BOOST_LOG_USE_CHAR
66 
67 #ifdef BOOST_LOG_USE_WCHAR_T
68     template< >
69     struct date_time_formats< wchar_t >
70     {
71         typedef logging::basic_string_literal< wchar_t > string_literal_type;
72 
default_date_format__anon18bbfe7f0111::date_time_formats73         static string_literal_type default_date_format() { return logging::str_literal(L"%Y-%b-%d"); }
default_time_format__anon18bbfe7f0111::date_time_formats74         static string_literal_type default_time_format() { return logging::str_literal(L"%H:%M:%S.%f"); }
default_date_time_format__anon18bbfe7f0111::date_time_formats75         static string_literal_type default_date_time_format() { return logging::str_literal(L"%Y-%b-%d %H:%M:%S.%f"); }
default_time_duration_format__anon18bbfe7f0111::date_time_formats76         static string_literal_type default_time_duration_format() { return logging::str_literal(L"%-%H:%M:%S.%f"); }
77 
date_format__anon18bbfe7f0111::date_time_formats78         static string_literal_type date_format() { return logging::str_literal(L"%d/%m/%Y"); }
time_format__anon18bbfe7f0111::date_time_formats79         static string_literal_type time_format() { return logging::str_literal(L"%H.%M.%S"); }
date_time_format__anon18bbfe7f0111::date_time_formats80         static string_literal_type date_time_format() { return logging::str_literal(L"%d/%m/%Y %H.%M.%S"); }
time_duration_format__anon18bbfe7f0111::date_time_formats81         static string_literal_type time_duration_format() { return logging::str_literal(L"%+%H.%M.%S.%f"); }
82     };
83 #endif // BOOST_LOG_USE_WCHAR_T
84 
85 } // namespace
86 
87 // The test checks that date_time formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(date_time,CharT,char_types)88 BOOST_AUTO_TEST_CASE_TEMPLATE(date_time, CharT, char_types)
89 {
90     typedef logging::attribute_set attr_set;
91     typedef std::basic_string< CharT > string;
92     typedef logging::basic_formatting_ostream< CharT > osstream;
93     typedef logging::record_view record_view;
94     typedef logging::basic_formatter< CharT > formatter;
95     typedef test_data< CharT > data;
96     typedef date_time_formats< CharT > formats;
97     typedef boost::date_time::time_facet< ptime, CharT > facet;
98 
99     ptime t1(gdate(2009, 2, 7), ptime::time_duration_type(14, 40, 15));
100     attrs::constant< ptime > attr1(t1);
101 
102     attr_set set1;
103     set1[data::attr1()] = attr1;
104 
105     record_view rec = make_record_view(set1);
106 
107     // Check for various formats specification
108     {
109         string str1, str2;
110         osstream strm1(str1), strm2(str2);
111         formatter f = expr::stream << expr::format_date_time< ptime >(data::attr1(), formats::default_date_time_format().c_str());
112         f(rec, strm1);
113         strm2.imbue(std::locale(strm2.getloc(), new facet(formats::default_date_time_format().c_str())));
114         strm2 << t1;
115         BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
116     }
117     {
118         string str1, str2;
119         osstream strm1(str1), strm2(str2);
120         formatter f = expr::stream << expr::format_date_time< ptime >(data::attr1(), formats::date_time_format().c_str());
121         f(rec, strm1);
122         strm2.imbue(std::locale(strm2.getloc(), new facet(formats::date_time_format().c_str())));
123         strm2 << t1;
124         BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
125     }
126 }
127 
128 // The test checks that date formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(date,CharT,char_types)129 BOOST_AUTO_TEST_CASE_TEMPLATE(date, CharT, char_types)
130 {
131     typedef logging::attribute_set attr_set;
132     typedef std::basic_string< CharT > string;
133     typedef logging::basic_formatting_ostream< CharT > osstream;
134     typedef logging::record_view record_view;
135     typedef logging::basic_formatter< CharT > formatter;
136     typedef test_data< CharT > data;
137     typedef date_time_formats< CharT > formats;
138     typedef boost::date_time::date_facet< gdate, CharT > facet;
139 
140     gdate d1(2009, 2, 7);
141     attrs::constant< gdate > attr1(d1);
142 
143     attr_set set1;
144     set1[data::attr1()] = attr1;
145 
146     record_view rec = make_record_view(set1);
147 
148     // Check for various formats specification
149     {
150         string str1, str2;
151         osstream strm1(str1), strm2(str2);
152         formatter f = expr::stream << expr::format_date_time< gdate >(data::attr1(), formats::default_date_format().c_str());
153         f(rec, strm1);
154         strm2.imbue(std::locale(strm2.getloc(), new facet(formats::default_date_format().c_str())));
155         strm2 << d1;
156         BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
157     }
158     {
159         string str1, str2;
160         osstream strm1(str1), strm2(str2);
161         formatter f = expr::stream << expr::format_date_time< gdate >(data::attr1(), formats::date_format().c_str());
162         f(rec, strm1);
163         strm2.imbue(std::locale(strm2.getloc(), new facet(formats::date_format().c_str())));
164         strm2 << d1;
165         BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
166     }
167 }
168 
169 // The test checks that time_duration formatting work
BOOST_AUTO_TEST_CASE_TEMPLATE(time_duration,CharT,char_types)170 BOOST_AUTO_TEST_CASE_TEMPLATE(time_duration, CharT, char_types)
171 {
172     typedef logging::attribute_set attr_set;
173     typedef std::basic_string< CharT > string;
174     typedef logging::basic_formatting_ostream< CharT > osstream;
175     typedef logging::record_view record_view;
176     typedef logging::basic_formatter< CharT > formatter;
177     typedef test_data< CharT > data;
178     typedef date_time_formats< CharT > formats;
179     typedef boost::date_time::time_facet< ptime, CharT > facet;
180 
181     duration t1(14, 40, 15);
182     attrs::constant< duration > attr1(t1);
183 
184     attr_set set1;
185     set1[data::attr1()] = attr1;
186 
187     record_view rec = make_record_view(set1);
188 
189     // Check for various formats specification
190     {
191         string str1, str2;
192         osstream strm1(str1), strm2(str2);
193         formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::default_time_duration_format().c_str());
194         f(rec, strm1);
195         facet* fac = new facet();
196         fac->time_duration_format(formats::default_time_duration_format().c_str());
197         strm2.imbue(std::locale(strm2.getloc(), fac));
198         strm2 << t1;
199         BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
200     }
201     {
202         string str1, str2;
203         osstream strm1(str1), strm2(str2);
204         formatter f = expr::stream << expr::format_date_time< duration >(data::attr1(), formats::time_duration_format().c_str());
205         f(rec, strm1);
206         facet* fac = new facet();
207         fac->time_duration_format(formats::time_duration_format().c_str());
208         strm2.imbue(std::locale(strm2.getloc(), fac));
209         strm2 << t1;
210         BOOST_CHECK(equal_strings(strm1.str(), strm2.str()));
211     }
212 }
213