📄 datetimeformattertest.cpp
字号:
//
// DateTimeFormatterTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/DateTimeFormatterTest.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include "DateTimeFormatterTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Foundation/DateTimeFormatter.h"
#include "Foundation/DateTimeFormat.h"
#include "Foundation/DateTime.h"
using Foundation::DateTime;
using Foundation::DateTimeFormat;
using Foundation::DateTimeFormatter;
DateTimeFormatterTest::DateTimeFormatterTest(const std::string& name): CppUnit::TestCase(name)
{
}
DateTimeFormatterTest::~DateTimeFormatterTest()
{
}
void DateTimeFormatterTest::testISO8601()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FORMAT);
assert (str == "2005-01-08T12:30:00Z");
str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FORMAT, 3600);
assert (str == "2005-01-08T12:30:00+01:00");
str = DateTimeFormatter::format(dt, DateTimeFormat::ISO8601_FORMAT, -3600);
assert (str == "2005-01-08T12:30:00-01:00");
}
void DateTimeFormatterTest::testRFC822()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC822_FORMAT);
assert (str == "Sat, 8 Jan 05 12:30:00 GMT");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC822_FORMAT, 3600);
assert (str == "Sat, 8 Jan 05 12:30:00 +0100");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC822_FORMAT, -3600);
assert (str == "Sat, 8 Jan 05 12:30:00 -0100");
}
void DateTimeFormatterTest::testRFC1123()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1123_FORMAT);
assert (str == "Sat, 8 Jan 2005 12:30:00 GMT");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1123_FORMAT, 3600);
assert (str == "Sat, 8 Jan 2005 12:30:00 +0100");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1123_FORMAT, -3600);
assert (str == "Sat, 8 Jan 2005 12:30:00 -0100");
}
void DateTimeFormatterTest::testHTTP()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::HTTP_FORMAT);
assert (str == "Sat, 08 Jan 2005 12:30:00 GMT");
str = DateTimeFormatter::format(dt, DateTimeFormat::HTTP_FORMAT, 3600);
assert (str == "Sat, 08 Jan 2005 12:30:00 +0100");
str = DateTimeFormatter::format(dt, DateTimeFormat::HTTP_FORMAT, -3600);
assert (str == "Sat, 08 Jan 2005 12:30:00 -0100");
}
void DateTimeFormatterTest::testRFC850()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC850_FORMAT);
assert (str == "Saturday, 8-Jan-05 12:30:00 GMT");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC850_FORMAT, 3600);
assert (str == "Saturday, 8-Jan-05 12:30:00 +0100");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC850_FORMAT, -3600);
assert (str == "Saturday, 8-Jan-05 12:30:00 -0100");
}
void DateTimeFormatterTest::testRFC1036()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1036_FORMAT);
assert (str == "Saturday, 8 Jan 05 12:30:00 GMT");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1036_FORMAT, 3600);
assert (str == "Saturday, 8 Jan 05 12:30:00 +0100");
str = DateTimeFormatter::format(dt, DateTimeFormat::RFC1036_FORMAT, -3600);
assert (str == "Saturday, 8 Jan 05 12:30:00 -0100");
}
void DateTimeFormatterTest::testASCTIME()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::ASCTIME_FORMAT);
assert (str == "Sat Jan 8 12:30:00 2005");
}
void DateTimeFormatterTest::testSORTABLE()
{
DateTime dt(2005, 1, 8, 12, 30, 00);
std::string str = DateTimeFormatter::format(dt, DateTimeFormat::SORTABLE_FORMAT);
assert (str == "2005-01-08 12:30:00");
}
void DateTimeFormatterTest::testCustom()
{
DateTime dt(2005, 1, 8, 12, 30, 00, 250);
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/%%");
assert (str == "Sat/Saturday/Jan/January/08/8/ 8/01/1/ 1/05/2005/12/12/pm/PM/30/00/250/2/Z/GMT/%");
}
void DateTimeFormatterTest::setUp()
{
}
void DateTimeFormatterTest::tearDown()
{
}
CppUnit::Test* DateTimeFormatterTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DateTimeFormatterTest");
CppUnit_addTest(pSuite, DateTimeFormatterTest, testISO8601);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC822);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC1123);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testHTTP);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC850);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testRFC1036);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testASCTIME);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testSORTABLE);
CppUnit_addTest(pSuite, DateTimeFormatterTest, testCustom);
return pSuite;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -