Ticket #2904: time_test.h

File time_test.h, 4.7 KB (added by mgl@…, 14 years ago)

date_time test to demo problem with boost::date_time ticket 2904

Line 
1/*
2 * Copyright (C) 2000-2004 Absolute Performance, Inc.
3 * All Rights Reserved
4 *
5 * THIS IS PROPRIETARY SOFTWARE DEVELOPED FOR THE SYSSHEP PROJECT AT
6 * ABSOLUTE PERFORMANCE, INC.; IT MAY NOT BE DISCLOSED TO THIRD PARTIES,
7 * COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART, WITHOUT THE PRIOR
8 * WRITTEN PERMISSION OF ABSOLUTE PERFORMANCE, INC.
9 *
10 * FURTHERMORE, THIS SOFTWARE IS DISTRIBUTED AS IS, AND ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
13 * IN NOT EVENT SHALL ABSOLUTE PERFORMANCE BE LIABLE FOR ANY DIRECT,
14 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16 * SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE AND OTHERWISE) ARISING IN ANY
19 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20 * SUCH DAMAGE. RECEIVING PARTY MAY NOT REVERSE ENGINEER, DECOMPILE OR
21 * DISASSEMBLE ANY SOFTWARE DISCLOSED TO RECEIVING PARTY.
22 *
23 * Author: Michael Linck 7/07
24 */
25
26
27#ifndef TIME_TEST_H
28#define TIME_TEST_H
29
30// our includes
31
32// tp includes
33#include <cxxtest/TestSuite.h>
34#include<boost/date_time.hpp>
35
36// std includes
37
38//#############################################################################
39/**
40 */
41//#############################################################################
42class TimeTest : public CxxTest::TestSuite
43{
44 public:
45
46 //#########################################################################
47 //#########################################################################
48 void testTime()
49 {
50 typedef boost::date_time::time_facet<boost::posix_time::ptime, char>
51 BoostTimeOutputFacetTS;
52 typedef boost::date_time::time_input_facet
53 <boost::posix_time::ptime, char> BoostTimeInputFacetTS;
54
55 // standard HTTP format, can't possibly have a useful library if you
56 // can't read and write these
57 std::string tFacetFormat("%a, %d %b %Y %H:%M:%S %Z");
58
59 BoostTimeInputFacetTS * _InputFacet
60 (new BoostTimeInputFacetTS(tFacetFormat.c_str()));
61 BoostTimeOutputFacetTS * _OutputFacet
62 (new BoostTimeOutputFacetTS(tFacetFormat.c_str()));
63
64 std::stringstream tTimeStream;
65 tTimeStream.imbue(std::locale(std::locale::classic(), _InputFacet));
66 tTimeStream.imbue(std::locale(std::locale::classic(), _OutputFacet));
67
68
69 std::string tFirstConvertedString;
70 std::string tSecondConvertedString;
71
72 std::time_t tConvertedTime;
73
74 // Thu Apr 2 14:33:23 MDT 2009
75 std::time_t tTime(1238704403);
76 std::cout<<"parse::tTime #"<<tTime<<"#"<<std::endl;
77
78
79 // TIME TO STRING ********************************************
80 boost::posix_time::ptime tPTime(boost::posix_time::from_time_t(tTime));
81 std::cout<<"format::tPTime #"<<tPTime<<"#"<<std::endl;
82 tTimeStream.str("");
83 tTimeStream<<tPTime;
84 tFirstConvertedString = tTimeStream.str();
85 std::cout<<"format::tFirstConvertedString #"
86 <<tFirstConvertedString<<"#"<<std::endl;
87
88
89
90 // STRING TO TIME ********************************************
91 // if you don't declare a new ptime here this test will "look" like
92 // it succeeds, because >> fails to modify it.
93 boost::posix_time::ptime tSecondPTime;
94 tTimeStream.str(tFirstConvertedString);
95 // fails, does not modify tSecondPTime so you get "not-a-date-time"
96 tTimeStream>>tSecondPTime;
97 std::cout<<"parse::tSecondPTime #"<<tSecondPTime<<"#"<<std::endl;
98 // throws exception in std lib
99 std::tm tTM(boost::posix_time::to_tm(tSecondPTime));
100 // should all this really be necessary?
101 tConvertedTime = mktime(&tTM);
102 std::cout<<"parse::tConvertedTime #"<<tConvertedTime<<"#"<<std::endl;
103
104
105
106 // SECOND TIME THROUGH
107 boost::posix_time::ptime tThirdPTime =
108 boost::posix_time::from_time_t(tConvertedTime);
109 std::cout<<"format::tThirdPTime #"<<tThirdPTime<<"#"<<std::endl;
110 tTimeStream.str("");
111 tTimeStream<<tThirdPTime;
112 tSecondConvertedString = tTimeStream.str();
113 std::cout<<"format::tSecondConvertedString #"
114 <<tSecondConvertedString<<"#"<<std::endl;
115
116 // never gets here, but should eventually pass these
117 TS_ASSERT_EQUALS(tConvertedTime, tTime);
118 TS_ASSERT_EQUALS(tFirstConvertedString, tSecondConvertedString);
119 }
120
121 protected:
122
123};
124#endif