Skip to content

Commit

Permalink
Do not call absl::LocalTimeZone() in clock.cc on Windows
Browse files Browse the repository at this point in the history
With google/cctz#242, absl::LocalTimeZone() now calls
RoInitialize(RO_INIT_MULTITHREADED), which may conflict with CUAS' COM
apartment handling when called from Mozc's TIP DLLs.

Note that in the current code ClockImpl::timezone_ is always overridden
with absl::FixedTimeZone on ChromeOS and Windows, which means that
calling absl::LocalTimeZone() on such platforms.

Closes google#856.

PiperOrigin-RevId: 593601170
  • Loading branch information
yukawa authored and coooooooozy committed Dec 29, 2023
1 parent d39a1fa commit e3c2b00
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/base/clock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,42 @@
#include "base/singleton.h"

#if defined(OS_CHROMEOS) || defined(_WIN32)
#include <time.h>
#include <ctime>
#endif // defined(OS_CHROMEOS) || defined(_WIN32)

namespace mozc {
namespace {

class ClockImpl : public ClockInterface {
public:
ClockImpl() : timezone_(absl::LocalTimeZone()) {
absl::TimeZone GetLocalTimeZone() {
#if defined(OS_CHROMEOS) || defined(_WIN32)
// Because absl::LocalTimeZone() always returns UTC timezone on Chrome OS
// and Windows, a work-around for Chrome OS and Windows is required.
int offset_sec = 9 * 60 * 60; // JST as fallback
const time_t epoch(24 * 60 * 60); // 1970-01-02 00:00:00 UTC
const std::tm *offset = std::localtime(&epoch);
if (offset) {
offset_sec =
(offset->tm_mday - 2) * 24 * 60 * 60 // date offset from Jan 2.
+ offset->tm_hour * 60 * 60 // hour offset from 00 am.
+ offset->tm_min * 60; // minute offset.
}
timezone_ = absl::FixedTimeZone(offset_sec);
#endif // defined(OS_CHROMEOS) || defined(_WIN32)
// Do not use absl::LocalTimeZone() here because
// - on Chrome OS, it returns UTC: b/196271425
// - on Windows, it crashes: https://github.com/google/mozc/issues/856
const time_t epoch(24 * 60 * 60); // 1970-01-02 00:00:00 UTC
const std::tm *offset = std::localtime(&epoch);
if (offset == nullptr) {
return absl::FixedTimeZone(9 * 60 * 60); // JST as fallback
}
return absl::FixedTimeZone(
(offset->tm_mday - 2) * 24 * 60 * 60 // date offset from Jan 2.
+ offset->tm_hour * 60 * 60 // hour offset from 00 am.
+ offset->tm_min * 60); // minute offset.
#else // !defined(OS_CHROMEOS) && !defined(_WIN32)
return absl::LocalTimeZone();
#endif // defined(OS_CHROMEOS) || defined(_WIN32)
}

class ClockImpl : public ClockInterface {
public:
ClockImpl() = default;
~ClockImpl() override = default;

absl::Time GetAbslTime() override { return absl::Now(); }

absl::TimeZone GetTimeZone() override { return timezone_; }

private:
absl::TimeZone timezone_;
const absl::TimeZone timezone_ = GetLocalTimeZone();
};
} // namespace

Expand Down

0 comments on commit e3c2b00

Please sign in to comment.