diff options
| -rw-r--r-- | Swiften/Base/DateTime.cpp | 10 | ||||
| -rw-r--r-- | Swiften/Base/UnitTest/DateTimeTest.cpp | 7 |
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 | ||
| 17 | namespace Swift { | 18 | namespace Swift { |
| 18 | 19 | ||
| 19 | boost::posix_time::ptime stringToDateTime(const std::string& string) { | 20 | boost::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 | ||
| 35 | std::string dateTimeToLocalString(const boost::posix_time::ptime& time) { | 36 | std::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; | |||
| 16 | class DateTimeTest : public CppUnit::TestFixture { | 16 | class 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 | ||
| 43 | CPPUNIT_TEST_SUITE_REGISTRATION(DateTimeTest); | 50 | CPPUNIT_TEST_SUITE_REGISTRATION(DateTimeTest); |
Swift