fixed SF# 2804546

This commit is contained in:
Guenter Obiltschnig 2009-06-17 08:48:49 +00:00
parent 7895040ccf
commit 37713c2ddc
3 changed files with 35 additions and 9 deletions

View File

@ -1,7 +1,7 @@
//
// DateTimeParser.h
//
// $Id: //poco/svn/Foundation/include/Poco/DateTimeParser.h#2 $
// $Id: //poco/Main/Foundation/include/Poco/DateTimeParser.h#4 $
//
// Library: Foundation
// Package: DateTime
@ -61,6 +61,12 @@ class Foundation_API DateTimeParser
/// Note: When parsing a time in 12-hour (AM/PM) format, the hour
/// (%h) must be parsed before the AM/PM designator (%a, %A),
/// otherwise the AM/PM designator will be ignored.
///
/// See the DateTimeFormatter class for a list of supported format specifiers.
/// In addition to the format specifiers supported by DateTimeFormatter, an
/// additional specifier is supported: %r will parse a year given by either
/// two or four digits. Years 69-00 are interpreted in the 20th century
/// (1969-2000), years 01-68 in the 21th century (2001-2068).
{
public:
static void parse(const std::string& fmt, const std::string& str, DateTime& dateTime, int& timeZoneDifferential);

View File

@ -1,7 +1,7 @@
//
// DateTimeParser.cpp
//
// $Id: //poco/Main/Foundation/src/DateTimeParser.cpp#19 $
// $Id: //poco/Main/Foundation/src/DateTimeParser.cpp#20 $
//
// Library: Foundation
// Package: DateTime
@ -105,7 +105,7 @@ void DateTimeParser::parse(const std::string& fmt, const std::string& str, DateT
case 'y':
SKIP_JUNK();
PARSE_NUMBER_N(year, 2);
if (year >= 70)
if (year >= 69)
year += 1900;
else
year += 2000;
@ -114,6 +114,17 @@ void DateTimeParser::parse(const std::string& fmt, const std::string& str, DateT
SKIP_JUNK();
PARSE_NUMBER_N(year, 4);
break;
case 'r':
SKIP_JUNK();
PARSE_NUMBER(year);
if (year < 1000)
{
if (year >= 69)
year += 1900;
else
year += 2000;
}
break;
case 'H':
case 'h':
SKIP_JUNK();
@ -209,11 +220,11 @@ bool DateTimeParser::tryParse(const std::string& str, DateTime& dateTime, int& t
if (str.length() < 4) return false;
if (str[3] == ',')
return tryParse(DateTimeFormat::RFC822_FORMAT, str, dateTime, timeZoneDifferential);
return tryParse("%w, %e %b %r %H:%M:%S %Z", str, dateTime, timeZoneDifferential);
else if (str[3] == ' ')
return tryParse(DateTimeFormat::ASCTIME_FORMAT, str, dateTime, timeZoneDifferential);
else if (str.find(',') != std::string::npos)
return tryParse(DateTimeFormat::RFC850_FORMAT, str, dateTime, timeZoneDifferential);
return tryParse("%W, %e %b %r %H:%M:%S %Z", str, dateTime, timeZoneDifferential);
else if (std::isdigit(str[0]))
{
if (str.find(' ') != std::string::npos || str.length() == 10)
@ -306,7 +317,7 @@ int DateTimeParser::parseTZD(std::string::const_iterator& it, const std::string:
int DateTimeParser::parseMonth(std::string::const_iterator& it, const std::string::const_iterator& end)
{
std::string month;
while (it != end && std::isspace(*it) || std::ispunct(*it)) ++it;
while ((it != end && std::isspace(*it)) || std::ispunct(*it)) ++it;
bool isFirst = true;
while (it != end && std::isalpha(*it))
{
@ -327,7 +338,7 @@ int DateTimeParser::parseMonth(std::string::const_iterator& it, const std::strin
int DateTimeParser::parseDayOfWeek(std::string::const_iterator& it, const std::string::const_iterator& end)
{
std::string dow;
while (it != end && std::isspace(*it) || std::ispunct(*it)) ++it;
while ((it != end && std::isspace(*it)) || std::ispunct(*it)) ++it;
bool isFirst = true;
while (it != end && std::isalpha(*it))
{
@ -348,7 +359,7 @@ int DateTimeParser::parseDayOfWeek(std::string::const_iterator& it, const std::s
int DateTimeParser::parseAMPM(std::string::const_iterator& it, const std::string::const_iterator& end, int hour)
{
std::string ampm;
while (it != end && std::isspace(*it) || std::ispunct(*it)) ++it;
while ((it != end && std::isspace(*it)) || std::ispunct(*it)) ++it;
while (it != end && std::isalpha(*it))
{
char ch = (*it++);

View File

@ -1,7 +1,7 @@
//
// DateTimeParserTest.cpp
//
// $Id: //poco/svn/Foundation/testsuite/src/DateTimeParserTest.cpp#2 $
// $Id: //poco/Main/Foundation/testsuite/src/DateTimeParserTest.cpp#12 $
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@ -402,6 +402,15 @@ void DateTimeParserTest::testGuess()
assert (dt.minute() == 30);
assert (dt.second() == 0);
assert (tzd == 3600);
dt = DateTimeParser::parse("Sat, 8 Jan 2005 12:30:00 +0100", tzd);
assert (dt.year() == 2005);
assert (dt.month() == 1);
assert (dt.day() == 8);
assert (dt.hour() == 12);
assert (dt.minute() == 30);
assert (dt.second() == 0);
assert (tzd == 3600);
dt = DateTimeParser::parse("Sat Jan 8 12:30:00 2005", tzd);
assert (dt.year() == 2005);