From 7536a1b3f38fbf093c1629b0db209754ada0c906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Thu, 25 Aug 2016 19:43:51 +0200 Subject: Respond to MAM requests on a channel JID At the moment, result-set-management is not implemented, the whole history (well, at most 1024 messages) is returned. --- louloulibs/utils/time.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 louloulibs/utils/time.cpp (limited to 'louloulibs/utils/time.cpp') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp new file mode 100644 index 0000000..e23def4 --- /dev/null +++ b/louloulibs/utils/time.cpp @@ -0,0 +1,12 @@ +#include + +namespace utils +{ +std::string to_string(const std::time_t& timestamp) +{ + constexpr std::size_t stamp_size = 20; + char date_buf[stamp_size]; + std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(×tamp)); + return {std::begin(date_buf), std::end(date_buf)}; +} +} \ No newline at end of file -- cgit v1.2.3 From 2a4905df0153c47656555a21f3d57bbad6f3ffe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 31 Aug 2016 01:17:45 +0200 Subject: Fix to_string(time_t) and write a unit test for it --- louloulibs/utils/time.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'louloulibs/utils/time.cpp') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index e23def4..b85d764 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -4,9 +4,10 @@ namespace utils { std::string to_string(const std::time_t& timestamp) { - constexpr std::size_t stamp_size = 20; - char date_buf[stamp_size]; - std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(×tamp)); - return {std::begin(date_buf), std::end(date_buf)}; + constexpr std::size_t stamp_size = 21; + char date_buf[stamp_size]; + if (std::strftime(date_buf, stamp_size, "%FT%TZ", std::gmtime(×tamp)) != stamp_size - 1) + return ""; + return {std::begin(date_buf), std::end(date_buf) - 1}; } } \ No newline at end of file -- cgit v1.2.3 From 1140db3b88bb70cbcab044efa729707bd5a442f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Wed, 31 Aug 2016 01:58:13 +0200 Subject: Add parse_datetime --- louloulibs/utils/time.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'louloulibs/utils/time.cpp') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index b85d764..305b2ad 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -1,4 +1,5 @@ #include +#include namespace utils { @@ -10,4 +11,14 @@ std::string to_string(const std::time_t& timestamp) return ""; return {std::begin(date_buf), std::end(date_buf) - 1}; } + +std::time_t parse_datetime(const std::string& stamp) +{ + struct tm tm; + if (!::strptime(stamp.data(), "%FT%T%z", &tm)) + return -1; + auto res = ::timegm(&tm); + return res; +} + } \ No newline at end of file -- cgit v1.2.3 From aaa2ca670ab5f19390e77a1f5ea88017b312ceaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sat, 17 Sep 2016 00:34:28 +0200 Subject: Fix the parse_datetime by using %Z instead of %z If anybody knows why fedora accepts both, but it only works with %z on debian, please tell me. --- louloulibs/utils/time.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'louloulibs/utils/time.cpp') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index 305b2ad..bc8b3f8 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -15,10 +15,10 @@ std::string to_string(const std::time_t& timestamp) std::time_t parse_datetime(const std::string& stamp) { struct tm tm; - if (!::strptime(stamp.data(), "%FT%T%z", &tm)) + if (!::strptime(stamp.data(), "%FT%T%Z", &tm)) return -1; auto res = ::timegm(&tm); return res; } -} \ No newline at end of file +} -- cgit v1.2.3 From e5b392ece8c90605b86d0d93f0ca6989048bc1c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Mon, 3 Oct 2016 23:30:20 +0200 Subject: Fix parse_datetime by always using a 'z' as the timezone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Because some plateform accept Z and z, but some only accept z… --- louloulibs/utils/time.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'louloulibs/utils/time.cpp') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index bc8b3f8..abf0a84 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -14,8 +14,9 @@ std::string to_string(const std::time_t& timestamp) std::time_t parse_datetime(const std::string& stamp) { + auto stamp2 = stamp.substr(0, stamp.size() - 1) + "z"; struct tm tm; - if (!::strptime(stamp.data(), "%FT%T%Z", &tm)) + if (!::strptime(stamp2.data(), "%FT%T%Z", &tm)) return -1; auto res = ::timegm(&tm); return res; -- cgit v1.2.3 From 548e4ad473e7be22f971184312cc5ce9b8fe56b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 11 Oct 2016 00:18:48 +0200 Subject: Parse the timezone myself, instead of using the broken strptime See https://lab.louiz.org/louiz/biboumi/issues/3215 https://github.com/andikleen/glibc/blob/master/time/strptime_l.c#L746-L747 for why strptime() sucks We use std::get_time now, to parse the date and time. And we parse the timezone by hand. fix #3215 --- louloulibs/utils/time.cpp | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'louloulibs/utils/time.cpp') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index abf0a84..7ad6663 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -1,6 +1,10 @@ #include #include +#include +#include +#include + namespace utils { std::string to_string(const std::time_t& timestamp) @@ -14,12 +18,42 @@ std::string to_string(const std::time_t& timestamp) std::time_t parse_datetime(const std::string& stamp) { - auto stamp2 = stamp.substr(0, stamp.size() - 1) + "z"; - struct tm tm; - if (!::strptime(stamp2.data(), "%FT%T%Z", &tm)) + static const char* format = "%Y-%m-%dT%H:%M:%S"; + std::tm t = {}; + std::istringstream ss(stamp); + ss.imbue(std::locale("en_US.utf-8")); + + std::string timezone; + ss >> std::get_time(&t, format) >> timezone; + if (ss.fail()) + return -1; + + if (timezone.empty()) return -1; - auto res = ::timegm(&tm); - return res; + + if (timezone.compare(0, 1, "Z") != 0) + { + std::stringstream tz_ss; + tz_ss << timezone; + int multiplier = -1; + char prefix; + int hours; + char sep; + int minutes; + tz_ss >> prefix >> hours >> sep >> minutes; + if (tz_ss.fail()) + return -1; + if (prefix == '-') + multiplier = +1; + else if (prefix != '+') + return -1; + + t.tm_hour += multiplier * hours; + t.tm_min += multiplier * minutes; + } + return ::timegm(&t); } } + + -- cgit v1.2.3 From c54f28d29d5f1d7a2bb973609beffbe5ad56d422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 11 Oct 2016 21:00:56 +0200 Subject: =?UTF-8?q?Conditionally=20use=20strptime=20if=20we=20don=E2=80=99?= =?UTF-8?q?t=20have=20std::get=5Ftime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- louloulibs/utils/time.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'louloulibs/utils/time.cpp') diff --git a/louloulibs/utils/time.cpp b/louloulibs/utils/time.cpp index 7ad6663..afd6117 100644 --- a/louloulibs/utils/time.cpp +++ b/louloulibs/utils/time.cpp @@ -5,6 +5,8 @@ #include #include +#include "louloulibs.h" + namespace utils { std::string to_string(const std::time_t& timestamp) @@ -20,6 +22,7 @@ std::time_t parse_datetime(const std::string& stamp) { static const char* format = "%Y-%m-%dT%H:%M:%S"; std::tm t = {}; +#ifdef HAS_GET_TIME std::istringstream ss(stamp); ss.imbue(std::locale("en_US.utf-8")); @@ -27,6 +30,14 @@ std::time_t parse_datetime(const std::string& stamp) ss >> std::get_time(&t, format) >> timezone; if (ss.fail()) return -1; +#else + /* Y - m - d T H : M : S */ + constexpr std::size_t stamp_size_without_tz = 4 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2 + 1 + 2; + if (!strptime(stamp.data(), format, &t)) { + return -1; + } + const std::string timezone(stamp.data() + stamp_size_without_tz); +#endif if (timezone.empty()) return -1; -- cgit v1.2.3