1//
2// DateTimeFormatterTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "DateTimeFormatterTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/DateTimeFormatter.h"
15#include "Poco/DateTimeFormat.h"
16#include "Poco/DateTime.h"
17#include "Poco/Timespan.h"
18
19
20using Poco::DateTime;
21using Poco::Timespan;
22using Poco::DateTimeFormat;
23using Poco::DateTimeFormatter;
24
25
26DateTimeFormatterTest::DateTimeFormatterTest(const std::string& rName)
27 : CppUnit::TestCase(rName)
28{
29 // Linker regresion SF #3288584
30 std::string message;
31 Poco::LocalDateTime now;
32 Poco::DateTimeFormatter::append(message, now, "%H:%M:%S.%i");
33}
34
35
36DateTimeFormatterTest::~DateTimeFormatterTest()
37{
38}
39
40
41void DateTimeFormatterTest::testISO8601()
42{
43 DateTime dt(2005, 1, 8, 12, 30, 00);
44
45 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FORMAT);
46 assertTrue (str == "2005-01-08T12:30:00Z");
47
48 str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FORMAT, 3600);
49 assertTrue (str == "2005-01-08T12:30:00+01:00");
50
51 str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FORMAT, -3600);
52 assertTrue (str == "2005-01-08T12:30:00-01:00");
53}
54
55
56void DateTimeFormatterTest::testISO8601Frac()
57{
58 DateTime dt(2005, 1, 8, 12, 30, 00, 12, 34);
59
60 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FRAC_FORMAT);
61 assertTrue (str == "2005-01-08T12:30:00.012034Z");
62
63 str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FRAC_FORMAT, 3600);
64 assertTrue (str == "2005-01-08T12:30:00.012034+01:00");
65
66 str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FRAC_FORMAT, -3600);
67 assertTrue (str == "2005-01-08T12:30:00.012034-01:00");
68}
69
70
71void DateTimeFormatterTest::testRFC822()
72{
73 DateTime dt(2005, 1, 8, 12, 30, 00);
74
75 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC822_FORMAT);
76 assertTrue (str == "Sat, 8 Jan 05 12:30:00 GMT");
77
78 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC822_FORMAT, 3600);
79 assertTrue (str == "Sat, 8 Jan 05 12:30:00 +0100");
80
81 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC822_FORMAT, -3600);
82 assertTrue (str == "Sat, 8 Jan 05 12:30:00 -0100");
83}
84
85
86void DateTimeFormatterTest::testRFC1123()
87{
88 DateTime dt(2005, 1, 8, 12, 30, 00);
89
90 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1123_FORMAT);
91 assertTrue (str == "Sat, 8 Jan 2005 12:30:00 GMT");
92
93 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1123_FORMAT, 3600);
94 assertTrue (str == "Sat, 8 Jan 2005 12:30:00 +0100");
95
96 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1123_FORMAT, -3600);
97 assertTrue (str == "Sat, 8 Jan 2005 12:30:00 -0100");
98}
99
100
101void DateTimeFormatterTest::testHTTP()
102{
103 DateTime dt(2005, 1, 8, 12, 30, 00);
104
105 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::HTTP_FORMAT);
106 assertTrue (str == "Sat, 08 Jan 2005 12:30:00 GMT");
107
108 str = DateTimeFormatter::format(dt, DateTimeFormat::HTTP_FORMAT, 3600);
109 assertTrue (str == "Sat, 08 Jan 2005 12:30:00 +0100");
110
111 str = DateTimeFormatter::format(dt, DateTimeFormat::HTTP_FORMAT, -3600);
112 assertTrue (str == "Sat, 08 Jan 2005 12:30:00 -0100");
113}
114
115
116void DateTimeFormatterTest::testRFC850()
117{
118 DateTime dt(2005, 1, 8, 12, 30, 00);
119
120 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC850_FORMAT);
121 assertTrue (str == "Saturday, 8-Jan-05 12:30:00 GMT");
122
123 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC850_FORMAT, 3600);
124 assertTrue (str == "Saturday, 8-Jan-05 12:30:00 +0100");
125
126 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC850_FORMAT, -3600);
127 assertTrue (str == "Saturday, 8-Jan-05 12:30:00 -0100");
128}
129
130
131void DateTimeFormatterTest::testRFC1036()
132{
133 DateTime dt(2005, 1, 8, 12, 30, 00);
134
135 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1036_FORMAT);
136 assertTrue (str == "Saturday, 8 Jan 05 12:30:00 GMT");
137
138 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1036_FORMAT, 3600);
139 assertTrue (str == "Saturday, 8 Jan 05 12:30:00 +0100");
140
141 str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1036_FORMAT, -3600);
142 assertTrue (str == "Saturday, 8 Jan 05 12:30:00 -0100");
143}
144
145
146void DateTimeFormatterTest::testASCTIME()
147{
148 DateTime dt(2005, 1, 8, 12, 30, 00);
149
150 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::ASCTIME_FORMAT);
151 assertTrue (str == "Sat Jan 8 12:30:00 2005");
152}
153
154
155void DateTimeFormatterTest::testSORTABLE()
156{
157 DateTime dt(2005, 1, 8, 12, 30, 00);
158
159 std::string str = DateTimeFormatter::format(dt, DateTimeFormat::SORTABLE_FORMAT);
160 assertTrue (str == "2005-01-08 12:30:00");
161}
162
163
164void DateTimeFormatterTest::testCustom()
165{
166 DateTime dt(2005, 1, 8, 12, 30, 00, 250);
167
168 std::string str = DateTimeFormatter::format(dt, "%w/%W/%b/%B/%d/%e/%f/%m/%n/%o/%y/%Y/%H/%h/%a/%A/%M/%S/%i/%c/%z/%Z/%%");
169 assertTrue (str == "Sat/Saturday/Jan/January/08/8/ 8/01/1/ 1/05/2005/12/12/pm/PM/30/00/250/2/Z/GMT/%");
170}
171
172
173void DateTimeFormatterTest::testTimespan()
174{
175 Timespan ts(1, 1, 1, 1, 1000);
176 std::string str = DateTimeFormatter::format(ts);
177 assertTrue (str == "1d 01:01:01.001");
178
179 Timespan ts1(1, 24, 1, 1, 1000);
180 str = DateTimeFormatter::format(ts1);
181 assertTrue (str == "2d 00:01:01.001");
182
183 Timespan ts2(1, 25, 1, 1, 1000);
184 str = DateTimeFormatter::format(ts2);
185 assertTrue (str == "2d 01:01:01.001");
186
187 Timespan ts3(5, 4, 3, 2, 1000);
188 str = DateTimeFormatter::format(ts3, "%i.%S:%M:%H d%d %%");
189 assertTrue (str == "001.02:03:04 d5 %");
190
191 Timespan ts4(0, 24, 60, 60, 1001000);
192 str = DateTimeFormatter::format(ts4);
193 assertTrue (str == "1d 01:01:01.001");
194
195 Timespan ts5(2, 11, 30, 20, 0);
196 str = DateTimeFormatter::format(ts5, "%h %m %s");
197 assertTrue (str == "59 3570 214220");
198}
199
200
201void DateTimeFormatterTest::setUp()
202{
203}
204
205
206void DateTimeFormatterTest::tearDown()
207{
208}
209
210
211CppUnit::Test* DateTimeFormatterTest::suite()
212{
213 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DateTimeFormatterTest");
214
215 CppUnit_addTest(pSuite, DateTimeFormatterTest, testISO8601);
216 CppUnit_addTest(pSuite, DateTimeFormatterTest, testISO8601Frac);
217 CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC822);
218 CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC1123);
219 CppUnit_addTest(pSuite, DateTimeFormatterTest, testHTTP);
220 CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC850);
221 CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC1036);
222 CppUnit_addTest(pSuite, DateTimeFormatterTest, testASCTIME);
223 CppUnit_addTest(pSuite, DateTimeFormatterTest, testSORTABLE);
224 CppUnit_addTest(pSuite, DateTimeFormatterTest, testCustom);
225 CppUnit_addTest(pSuite, DateTimeFormatterTest, testTimespan);
226
227 return pSuite;
228}
229