summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2015-09-21 10:06:57 (GMT)
committerTobias Markmann <tm@ayena.de>2015-09-21 10:06:57 (GMT)
commiteed183fbd5d121049d5965d7c60abd65f44d0376 (patch)
tree63caaca65cc39a9fffc3f91dd7cafad79d4be4a5
parent68bb160b993dde045f502f10ecbbccce9d9184c1 (diff)
downloadswift-eed183fbd5d121049d5965d7c60abd65f44d0376.zip
swift-eed183fbd5d121049d5965d7c60abd65f44d0376.tar.bz2
Stop throwing out of range exception from dateTimeToLocalString
The dateTimeToLocalString uses boost functions that may throw an out of range exception for times earlier than 1970. This commit caches this exception and just returns an empty string in this case. Test-Information: Added a unit test to verify this behavior. Change-Id: I05eb17605331e14d9eac04fbfd286362e7d4eb46
-rw-r--r--Swiften/Base/DateTime.cpp10
-rw-r--r--Swiften/Base/UnitTest/DateTimeTest.cpp7
2 files changed, 16 insertions, 1 deletions
diff --git a/Swiften/Base/DateTime.cpp b/Swiften/Base/DateTime.cpp
index 4f55a8e..d19e9c7 100644
--- a/Swiften/Base/DateTime.cpp
+++ b/Swiften/Base/DateTime.cpp
@@ -11,10 +11,11 @@
11#include <boost/date_time/local_time/local_time.hpp> 11#include <boost/date_time/local_time/local_time.hpp>
12#include <boost/date_time/posix_time/posix_time.hpp> 12#include <boost/date_time/posix_time/posix_time.hpp>
13#include <boost/date_time/c_local_time_adjustor.hpp> 13#include <boost/date_time/c_local_time_adjustor.hpp>
14 14
15#include <Swiften/Base/String.h> 15#include <Swiften/Base/String.h>
16#include <Swiften/Base/Log.h>
16 17
17namespace Swift { 18namespace Swift {
18 19
19boost::posix_time::ptime stringToDateTime(const std::string& string) { 20boost::posix_time::ptime stringToDateTime(const std::string& string) {
20 static std::locale locale(std::locale::classic(), new boost::local_time::local_time_input_facet("%Y-%m-%d %H:%M:%S%F%ZP")); 21 static std::locale locale(std::locale::classic(), new boost::local_time::local_time_input_facet("%Y-%m-%d %H:%M:%S%F%ZP"));
@@ -31,9 +32,16 @@ std::string dateTimeToString(const boost::posix_time::ptime& time) {
31 stampString += "Z"; 32 stampString += "Z";
32 return stampString; 33 return stampString;
33} 34}
34 35
35std::string dateTimeToLocalString(const boost::posix_time::ptime& time) { 36std::string dateTimeToLocalString(const boost::posix_time::ptime& time) {
36 return boost::posix_time::to_simple_string(boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(time)); 37 std::string localString;
38 try {
39 localString = boost::posix_time::to_simple_string(boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(time));
40 }
41 catch(std::out_of_range& exception) {
42 SWIFT_LOG(debug) << exception.what() << std::endl;
43 }
44 return localString;
37} 45}
38 46
39} 47}
diff --git a/Swiften/Base/UnitTest/DateTimeTest.cpp b/Swiften/Base/UnitTest/DateTimeTest.cpp
index 936a3ec..a9350fa 100644
--- a/Swiften/Base/UnitTest/DateTimeTest.cpp
+++ b/Swiften/Base/UnitTest/DateTimeTest.cpp
@@ -16,10 +16,11 @@ using namespace Swift;
16class DateTimeTest : public CppUnit::TestFixture { 16class DateTimeTest : public CppUnit::TestFixture {
17 CPPUNIT_TEST_SUITE(DateTimeTest); 17 CPPUNIT_TEST_SUITE(DateTimeTest);
18 CPPUNIT_TEST(testStringToDateTime_UTC); 18 CPPUNIT_TEST(testStringToDateTime_UTC);
19 CPPUNIT_TEST(testStringToDateTime_WithTimezone); 19 CPPUNIT_TEST(testStringToDateTime_WithTimezone);
20 CPPUNIT_TEST(testDateTimeToString); 20 CPPUNIT_TEST(testDateTimeToString);
21 CPPUNIT_TEST(testDateTimeToLocalStringNotThrowingException);
21 CPPUNIT_TEST_SUITE_END(); 22 CPPUNIT_TEST_SUITE_END();
22 23
23 public: 24 public:
24 void testStringToDateTime_UTC() { 25 void testStringToDateTime_UTC() {
25 boost::posix_time::ptime time = stringToDateTime("1969-07-21T02:56:15Z"); 26 boost::posix_time::ptime time = stringToDateTime("1969-07-21T02:56:15Z");
@@ -36,8 +37,14 @@ class DateTimeTest : public CppUnit::TestFixture {
36 void testDateTimeToString() { 37 void testDateTimeToString() {
37 boost::posix_time::ptime time = stringToDateTime("1969-07-20T21:56:15-05:00"); 38 boost::posix_time::ptime time = stringToDateTime("1969-07-20T21:56:15-05:00");
38 39
39 CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15Z"), dateTimeToString(time)); 40 CPPUNIT_ASSERT_EQUAL(std::string("1969-07-21T02:56:15Z"), dateTimeToString(time));
40 } 41 }
42
43 void testDateTimeToLocalStringNotThrowingException() {
44 boost::posix_time::ptime time = stringToDateTime("1954-07-20T21:56:15-05:00");
45
46 CPPUNIT_ASSERT_EQUAL(std::string(""), dateTimeToLocalString(time));
47 }
41}; 48};
42 49
43CPPUNIT_TEST_SUITE_REGISTRATION(DateTimeTest); 50CPPUNIT_TEST_SUITE_REGISTRATION(DateTimeTest);