Skip to content

Commit

Permalink
Fix 856
Browse files Browse the repository at this point in the history
  • Loading branch information
yukawa committed Dec 22, 2023
1 parent 68fea8a commit 1a5608d
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/base/clock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,36 @@
namespace mozc {
namespace {

class ClockImpl : public ClockInterface {
public:
ClockImpl() : timezone_(absl::LocalTimeZone()) {
absl::TimeZone GetMozcTimeZone() {
#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
// 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) {
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.
if (!offset) {
return absl::FixedTimeZone(9 * 60 * 60); // JST as fallback
}
timezone_ = absl::FixedTimeZone(offset_sec);
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_;
absl::TimeZone timezone_ = GetMozcTimeZone();
};
} // namespace

Expand Down

0 comments on commit 1a5608d

Please sign in to comment.