diff options
| author | Carl Hetherington <cth@carlh.net> | 2025-01-06 20:00:59 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2025-01-07 20:22:28 +0100 |
| commit | 9eb50432aabf0e09f3db66294564ec91fac2c94d (patch) | |
| tree | 1ba55735215ae3f39d061513b6a58d91f0e40f9e | |
| parent | cf50dd3ac0153935f6b4f1ae9da16bc2b473d1a0 (diff) | |
Fix email date format in non-English locales (#2928).2928-email
| -rw-r--r-- | src/lib/email.cc | 7 | ||||
| -rw-r--r-- | src/lib/email.h | 4 | ||||
| -rw-r--r-- | test/email_test.cc | 56 | ||||
| -rw-r--r-- | test/wscript | 1 |
4 files changed, 67 insertions, 1 deletions
diff --git a/src/lib/email.cc b/src/lib/email.cc index 2da57e257..a731501bc 100644 --- a/src/lib/email.cc +++ b/src/lib/email.cc @@ -24,6 +24,7 @@ #include "email.h" #include "exceptions.h" #include "variant.h" +#include <dcp/scope_guard.h> #include <curl/curl.h> #include <boost/algorithm/string.hpp> #include <boost/date_time/c_local_time_adjustor.hpp> @@ -113,7 +114,11 @@ void Email::create(time_t time) { char date_buffer[128]; - strftime(date_buffer, sizeof(date_buffer), "%a, %d %b %Y %H:%M:%S ", localtime(&time)); + + auto english_locale = newlocale(LC_TIME_MASK, "C", 0); + DCPOMATIC_ASSERT(english_locale); + dcp::ScopeGuard sg = [english_locale]() { freelocale(english_locale); }; + strftime_l(date_buffer, sizeof(date_buffer), "%a, %d %b %Y %H:%M:%S ", localtime(&time), english_locale); auto const utc_time = boost::posix_time::second_clock::universal_time(); auto const local_time = boost::date_time::c_local_adjustor<boost::posix_time::ptime>::utc_to_local(utc_time); diff --git a/src/lib/email.h b/src/lib/email.h index 24f20956a..3f9cf1dd8 100644 --- a/src/lib/email.h +++ b/src/lib/email.h @@ -24,6 +24,9 @@ #include <boost/scoped_array.hpp> +struct email_date_format_test; + + class Email { public: @@ -51,6 +54,7 @@ public: static std::string address_list(std::vector<std::string> addresses); private: + friend struct ::email_date_format_test; std::string fix (std::string s) const; static std::string encode_rfc1342 (std::string subject); diff --git a/test/email_test.cc b/test/email_test.cc new file mode 100644 index 000000000..2c444de39 --- /dev/null +++ b/test/email_test.cc @@ -0,0 +1,56 @@ +/* + Copyright (C) 2025 Carl Hetherington <cth@carlh.net> + + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + DCP-o-matic is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>. + +*/ + + +#include "lib/email.h" +#include <dcp/scope_guard.h> +#include <boost/algorithm/string.hpp> +#include <boost/test/unit_test.hpp> +#include <iostream> +#include <string> +#include <vector> + + +using std::string; +using std::vector; + + +/* Make sure email dates are always in English (per RFC 2822) */ +BOOST_AUTO_TEST_CASE(email_date_format_test) +{ + auto previous = setlocale(LC_TIME, nullptr); + dcp::ScopeGuard sg = [previous]() { setlocale(LC_TIME, previous); }; + + auto result = setlocale(LC_TIME, "de_DE.UTF8"); + BOOST_REQUIRE(result); + + Email email("bob@popcornwiththat.com", { "orders@popcorn.com" }, "Popcorn order", "Please send popcorn."); + email.create(0); + + vector<string> lines; + boost::algorithm::split(lines, email.email(), boost::is_any_of("\n\r")); + + for (auto line: lines) { + if (boost::starts_with(line, "Date: ")) { + BOOST_CHECK_EQUAL(line, "Date: Thu, 01 Jan 1970 01:00:00 +0100"); + } + } +} + diff --git a/test/wscript b/test/wscript index 229330265..115121bde 100644 --- a/test/wscript +++ b/test/wscript @@ -80,6 +80,7 @@ def build(bld): dcp_subtitle_test.cc digest_test.cc dkdm_recipient_list_test.cc + email_test.cc empty_caption_test.cc empty_test.cc encryption_test.cc |
