ProteoWizard
Functions | Variables
DateTimeTest.cpp File Reference
#include "Std.hpp"
#include "pwiz/utility/misc/unit.hpp"
#include "pwiz/utility/misc/DateTime.hpp"

Go to the source code of this file.

Functions

template<typename time_type >
void test_time_from_OADATE ()
 
void test_format_date_time ()
 
void test_parse_date_time ()
 
void test_xml_datetime ()
 
int main (int argc, char *argv[])
 

Variables

ostream * os_ = 0
 

Function Documentation

◆ test_time_from_OADATE()

template<typename time_type >
void test_time_from_OADATE ( )

Definition at line 36 of file DateTimeTest.cpp.

37{
38 typedef typename time_type::date_type date_type;
39 typedef typename time_type::date_duration_type date_duration_type;
40 typedef typename time_type::time_duration_type time_duration_type;
41
42 if (os_) *os_ << "OADATE: 0.0 -> " << time_from_OADATE<time_type>(0.0) << endl;
43 unit_assert(time_from_OADATE<time_type>(0.0) == time_type(date_type(1899, bdt::Dec, 30), time_duration_type(0,0,0)));
44
45 if (os_) *os_ << "OADATE: 1.0 -> " << time_from_OADATE<time_type>(1.0) << endl;
46 unit_assert(time_from_OADATE<time_type>(1.0) == time_type(date_type(1899, bdt::Dec, 31), time_duration_type(0,0,0)));
47
48 if (os_) *os_ << "OADATE: -1.0 -> " << time_from_OADATE<time_type>(-1.0) << endl;
49 unit_assert(time_from_OADATE<time_type>(-1.0) == time_type(date_type(1899, bdt::Dec, 29), time_duration_type(0,0,0)));
50
51 if (os_) *os_ << "OADATE: 2.0 -> " << time_from_OADATE<time_type>(2.0) << endl;
52 unit_assert(time_from_OADATE<time_type>(2.0) == time_type(date_type(1900, bdt::Jan, 1), time_duration_type(0,0,0)));
53
54 if (os_) *os_ << "OADATE: 2.25 -> " << time_from_OADATE<time_type>(2.25) << endl;
55 unit_assert(time_from_OADATE<time_type>(2.25) == time_type(date_type(1900, bdt::Jan, 1), time_duration_type(6,0,0)));
56
57 if (os_) *os_ << "OADATE: -1.25 -> " << time_from_OADATE<time_type>(-1.25) << endl;
58 unit_assert(time_from_OADATE<time_type>(-1.25) == time_type(date_type(1899, bdt::Dec, 29), time_duration_type(6,0,0)));
59}
ostream * os_
#define unit_assert(x)
Definition unit.hpp:85

References os_, and unit_assert.

◆ test_format_date_time()

void test_format_date_time ( )

Definition at line 62 of file DateTimeTest.cpp.

63{
64 using bpt::ptime;
65 typedef blt::local_date_time datetime;
66 typedef datetime::time_duration_type time;
67
68 string encoded;
69
70 ptime pt = ptime(date(1942, bdt::Apr, 2));
71 encoded = format_date_time("%Y-%m-%d", pt);
72 if (os_) *os_ << pt << " -> " << encoded << endl;
73 unit_assert(encoded == "1942-04-02");
74
75 pt = ptime(date(2011, bdt::Nov, 11));
76 encoded = format_date_time("--=%d/%m/%y=--", pt);
77 if (os_) *os_ << pt << " -> " << encoded << endl;
78 unit_assert(encoded == "--=11/11/11=--");
79
80 pt = ptime(date(2000, bdt::Nov, 11), time(1, 2, 3));
81 encoded = format_date_time("%H:%M:%S", pt);
82 if (os_) *os_ << pt << " -> " << encoded << endl;
83 unit_assert(encoded == "01:02:03");
84
85 time elapsed = time(1, 2, 3) - time(0, 0, 3);
86 encoded = format_date_time("%H:%M:%S", elapsed);
87 if (os_) *os_ << bpt::to_simple_string(elapsed) << " -> " << encoded << endl;
88 unit_assert(encoded == "01:02:00");
89
90 elapsed = time(1, 2, 3) - time(0, 1, 2);
91 encoded = format_date_time("%H:%M:%S", elapsed);
92 if (os_) *os_ << bpt::to_simple_string(elapsed) << " -> " << encoded << endl;
93 unit_assert(encoded == "01:01:01");
94
95 elapsed = time(1, 2, 3) - time(1, 2, 3);
96 encoded = format_date_time("%H:%M:%S", elapsed);
97 if (os_) *os_ << bpt::to_simple_string(elapsed) << " -> " << encoded << endl;
98 unit_assert(encoded == "00:00:00");
99}
std::string format_date_time(const std::string &format, const bpt::ptime &t)
formats a boost ptime according to a custom format string
Definition DateTime.hpp:81

References pwiz::util::format_date_time(), os_, and unit_assert.

Referenced by main().

◆ test_parse_date_time()

void test_parse_date_time ( )

Definition at line 102 of file DateTimeTest.cpp.

103{
104 typedef blt::local_date_time datetime;
105
106 string encoded;
107
108 encoded = "1942-04-02";
109 datetime decoded = parse_date_time("%Y-%m-%d", encoded);
110 if (os_) *os_ << encoded << " -> " << format_date_time("%Y-%m-%d", decoded.local_time()) << endl;
111 unit_assert(decoded.local_time().date().year() == 1942);
112 unit_assert(decoded.local_time().date().month() == 4);
113 unit_assert(decoded.local_time().date().day() == 2);
114
115 encoded = "1400-12-11";
116 decoded = parse_date_time("%Y-%d-%m", encoded);
117 if (os_) *os_ << encoded << " -> " << format_date_time("%Y-%d-%m", decoded.local_time()) << endl;
118 unit_assert(decoded.local_time().date().year() == 1400);
119 unit_assert(decoded.local_time().date().month() == 11);
120 unit_assert(decoded.local_time().date().day() == 12);
121
122 encoded = "124221";
123 decoded = parse_date_time("%H%M%S", encoded);
124 if (os_) *os_ << encoded << " -> " << format_date_time("%H%M%S", decoded.local_time()) << endl;
125 unit_assert(decoded.local_time().time_of_day().hours() == 12);
126 unit_assert(decoded.local_time().time_of_day().minutes() == 42);
127 unit_assert(decoded.local_time().time_of_day().seconds() == 21);
128
129 encoded = "16:42:21 on 01-02-2011";
130 decoded = parse_date_time("%H:%M:%S on %m-%d-%Y", encoded);
131 if (os_) *os_ << encoded << " -> " << format_date_time("%H:%M:%S on %m-%d-%Y", decoded.local_time()) << endl;
132 unit_assert(decoded.local_time().date().year() == 2011);
133 unit_assert(decoded.local_time().date().month() == 1);
134 unit_assert(decoded.local_time().date().day() == 2);
135 unit_assert(decoded.local_time().time_of_day().hours() == 16);
136 unit_assert(decoded.local_time().time_of_day().minutes() == 42);
137 unit_assert(decoded.local_time().time_of_day().seconds() == 21);
138}
blt::local_date_time parse_date_time(const std::string &format, const std::string &t)
converts a custom formatted datetime string to a boost local_date_time
Definition DateTime.hpp:114

References pwiz::util::format_date_time(), os_, pwiz::util::parse_date_time(), and unit_assert.

Referenced by main().

◆ test_xml_datetime()

void test_xml_datetime ( )

Definition at line 141 of file DateTimeTest.cpp.

142{
143 using bpt::ptime;
144 typedef blt::local_date_time datetime;
145 typedef datetime::time_duration_type time;
146
147 // New York City time zone for testing local->UTC conversion
148 blt::time_zone_ptr nyc(new blt::posix_time_zone("EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00"));
149 blt::time_zone_ptr utc;
150
151 std::string encoded;
152 datetime decoded(bdt::not_a_date_time);
153
154 // test output from UTC times
155 datetime dt = datetime(ptime(date(1899, bdt::Dec, 30), time(0,0,0)), utc);
156 encoded = encode_xml_datetime(dt);
157 if (os_) *os_ << dt << " -> " << encoded << endl;
158 unit_assert(encoded == "1899-12-30T00:00:00Z");
159 decoded = decode_xml_datetime(encoded);
160 if (os_) *os_ << encoded << " -> " << decoded << endl;
161 unit_assert(decoded == dt);
162
163 dt = datetime(ptime(date(1999, bdt::Dec, 31), time(23,59,59)), utc);
164 encoded = encode_xml_datetime(dt);
165 if (os_) *os_ << dt << " -> " << encoded << endl;
166 unit_assert(encoded == "1999-12-31T23:59:59Z");
167 decoded = decode_xml_datetime(encoded);
168 if (os_) *os_ << encoded << " -> " << decoded << endl;
169 unit_assert(decoded == dt);
170
171 dt = datetime(ptime(date(2525, bdt::Jan, 1), time(1,2,3)), utc);
172 encoded = encode_xml_datetime(dt);
173 if (os_) *os_ << dt << " -> " << encoded << endl;
174 unit_assert(encoded == "2525-01-01T01:02:03Z");
175 decoded = decode_xml_datetime(encoded);
176 if (os_) *os_ << encoded << " -> " << decoded << endl;
177 unit_assert(decoded == dt);
178
179 dt = datetime(ptime(date(1492, bdt::Feb, 3), time(4,5,6)), utc);
180 encoded = encode_xml_datetime(dt);
181 if (os_) *os_ << dt << " -> " << encoded << endl;
182 unit_assert(encoded == "1492-02-03T04:05:06Z");
183 decoded = decode_xml_datetime(encoded);
184 if (os_) *os_ << encoded << " -> " << decoded << endl;
185 unit_assert(decoded == dt);
186
187 // test output from NYC times
188 dt = datetime(date(1899, bdt::Dec, 30), time(0,0,0), nyc, datetime::NOT_DATE_TIME_ON_ERROR);
189 encoded = encode_xml_datetime(dt);
190 if (os_) *os_ << dt << " -> " << encoded << endl;
191 unit_assert(encoded == "1899-12-30T05:00:00Z"); // UTC=EST+5
192 decoded = decode_xml_datetime(encoded);
193 if (os_) *os_ << encoded << " -> " << decoded << endl;
194 unit_assert(decoded == dt);
195
196 dt = datetime(date(1999, bdt::Dec, 31), time(23,59,59), nyc, datetime::NOT_DATE_TIME_ON_ERROR);
197 encoded = encode_xml_datetime(dt);
198 if (os_) *os_ << dt << " -> " << encoded << endl;
199 unit_assert(encoded == "2000-01-01T04:59:59Z"); // UTC=EST+5
200 decoded = decode_xml_datetime(encoded);
201 if (os_) *os_ << encoded << " -> " << decoded << endl;
202 unit_assert(decoded == dt);
203
204 dt = datetime(date(2525, bdt::Jan, 1), time(1,2,3), nyc, datetime::NOT_DATE_TIME_ON_ERROR);
205 encoded = encode_xml_datetime(dt);
206 if (os_) *os_ << dt << " -> " << encoded << endl;
207 unit_assert(encoded == "2525-01-01T06:02:03Z"); // UTC=EST+5
208 decoded = decode_xml_datetime(encoded);
209 if (os_) *os_ << encoded << " -> " << decoded << endl;
210 unit_assert(decoded == dt);
211
212 dt = datetime(date(1492, bdt::Jun, 3), time(4,5,6), nyc, datetime::NOT_DATE_TIME_ON_ERROR);
213 encoded = encode_xml_datetime(dt);
214 if (os_) *os_ << dt << " -> " << encoded << endl;
215 unit_assert(encoded == "1492-06-03T08:05:06Z"); // UTC=EDT+4
216 // TODO: figure out why this test case is failing
217 /*decoded = decode_xml_datetime(encoded);
218 if (os_) *os_ << encoded << " -> " << decoded << endl;
219 unit_assert(decoded == dt);*/
220}
std::string encode_xml_datetime(const bpt::ptime &t)
returns a string representation suitable for an xsd:datetime attribute; input is assumed to be UTC ti...
Definition DateTime.hpp:130
blt::local_date_time decode_xml_datetime(const std::string &t)
converts an xsd:datetime attribute to a local_date_time
Definition DateTime.hpp:148

References pwiz::util::decode_xml_datetime(), pwiz::util::encode_xml_datetime(), os_, and unit_assert.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 223 of file DateTimeTest.cpp.

224{
225 TEST_PROLOG(argc, argv)
226
227 try
228 {
229 if (argc>1 && !strcmp(argv[1],"-v")) os_ = &cout;
230 if (os_)
231 {
232 using namespace boost::local_time;
233 *os_ << "DateTimeTest\n";
234 local_time_facet* output_facet = new local_time_facet;
235 output_facet->format("%Y-%m-%d %H:%M:%S %z"); // 2007-06-27 15:23:45 EST
236 os_->imbue(std::locale(std::locale::classic(), output_facet));
237 }
238 test_time_from_OADATE<boost::posix_time::ptime>();
239 //test_time_from_OADATE<boost::local_time::local_date_time>();
243 }
244 catch (exception& e)
245 {
246 TEST_FAILED(e.what())
247 }
248 catch (...)
249 {
250 TEST_FAILED("Caught unknown exception.")
251 }
252
254}
void test_format_date_time()
void test_xml_datetime()
void test_parse_date_time()
#define TEST_EPILOG
Definition unit.hpp:183
#define TEST_FAILED(x)
Definition unit.hpp:177
#define TEST_PROLOG(argc, argv)
Definition unit.hpp:175

References os_, TEST_EPILOG, TEST_FAILED, test_format_date_time(), test_parse_date_time(), TEST_PROLOG, and test_xml_datetime().

Variable Documentation

◆ os_

ostream* os_ = 0