diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/fs/fs.cpp | 27 | ||||
| -rw-r--r-- | src/common/fs/fs_android.h | 5 | ||||
| -rw-r--r-- | src/common/settings.cpp | 31 | ||||
| -rw-r--r-- | src/common/time_zone.cpp | 63 | ||||
| -rw-r--r-- | src/common/time_zone.h | 6 |
5 files changed, 116 insertions, 16 deletions
diff --git a/src/common/fs/fs.cpp b/src/common/fs/fs.cpp index e1716c62d..6d66c926d 100644 --- a/src/common/fs/fs.cpp +++ b/src/common/fs/fs.cpp | |||
| @@ -3,6 +3,9 @@ | |||
| 3 | 3 | ||
| 4 | #include "common/fs/file.h" | 4 | #include "common/fs/file.h" |
| 5 | #include "common/fs/fs.h" | 5 | #include "common/fs/fs.h" |
| 6 | #ifdef ANDROID | ||
| 7 | #include "common/fs/fs_android.h" | ||
| 8 | #endif | ||
| 6 | #include "common/fs/path_util.h" | 9 | #include "common/fs/path_util.h" |
| 7 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 8 | 11 | ||
| @@ -525,15 +528,39 @@ void IterateDirEntriesRecursively(const std::filesystem::path& path, | |||
| 525 | // Generic Filesystem Operations | 528 | // Generic Filesystem Operations |
| 526 | 529 | ||
| 527 | bool Exists(const fs::path& path) { | 530 | bool Exists(const fs::path& path) { |
| 531 | #ifdef ANDROID | ||
| 532 | if (Android::IsContentUri(path)) { | ||
| 533 | return Android::Exists(path); | ||
| 534 | } else { | ||
| 535 | return fs::exists(path); | ||
| 536 | } | ||
| 537 | #else | ||
| 528 | return fs::exists(path); | 538 | return fs::exists(path); |
| 539 | #endif | ||
| 529 | } | 540 | } |
| 530 | 541 | ||
| 531 | bool IsFile(const fs::path& path) { | 542 | bool IsFile(const fs::path& path) { |
| 543 | #ifdef ANDROID | ||
| 544 | if (Android::IsContentUri(path)) { | ||
| 545 | return !Android::IsDirectory(path); | ||
| 546 | } else { | ||
| 547 | return fs::is_regular_file(path); | ||
| 548 | } | ||
| 549 | #else | ||
| 532 | return fs::is_regular_file(path); | 550 | return fs::is_regular_file(path); |
| 551 | #endif | ||
| 533 | } | 552 | } |
| 534 | 553 | ||
| 535 | bool IsDir(const fs::path& path) { | 554 | bool IsDir(const fs::path& path) { |
| 555 | #ifdef ANDROID | ||
| 556 | if (Android::IsContentUri(path)) { | ||
| 557 | return Android::IsDirectory(path); | ||
| 558 | } else { | ||
| 559 | return fs::is_directory(path); | ||
| 560 | } | ||
| 561 | #else | ||
| 536 | return fs::is_directory(path); | 562 | return fs::is_directory(path); |
| 563 | #endif | ||
| 537 | } | 564 | } |
| 538 | 565 | ||
| 539 | fs::path GetCurrentDir() { | 566 | fs::path GetCurrentDir() { |
diff --git a/src/common/fs/fs_android.h b/src/common/fs/fs_android.h index bb8a52648..b441c2a12 100644 --- a/src/common/fs/fs_android.h +++ b/src/common/fs/fs_android.h | |||
| @@ -12,7 +12,10 @@ | |||
| 12 | "openContentUri", "(Ljava/lang/String;Ljava/lang/String;)I") | 12 | "openContentUri", "(Ljava/lang/String;Ljava/lang/String;)I") |
| 13 | 13 | ||
| 14 | #define ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(V) \ | 14 | #define ANDROID_SINGLE_PATH_DETERMINE_FUNCTIONS(V) \ |
| 15 | V(GetSize, std::uint64_t, get_size, CallStaticLongMethod, "getSize", "(Ljava/lang/String;)J") | 15 | V(GetSize, std::uint64_t, get_size, CallStaticLongMethod, "getSize", "(Ljava/lang/String;)J") \ |
| 16 | V(IsDirectory, bool, is_directory, CallStaticBooleanMethod, "isDirectory", \ | ||
| 17 | "(Ljava/lang/String;)Z") \ | ||
| 18 | V(Exists, bool, file_exists, CallStaticBooleanMethod, "exists", "(Ljava/lang/String;)Z") | ||
| 16 | 19 | ||
| 17 | namespace Common::FS::Android { | 20 | namespace Common::FS::Android { |
| 18 | 21 | ||
diff --git a/src/common/settings.cpp b/src/common/settings.cpp index 9ff3edabb..66dffc9bf 100644 --- a/src/common/settings.cpp +++ b/src/common/settings.cpp | |||
| @@ -1,12 +1,16 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project | 1 | // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #if __cpp_lib_chrono >= 201907L | ||
| 5 | #include <chrono> | ||
| 6 | #endif | ||
| 4 | #include <string_view> | 7 | #include <string_view> |
| 5 | 8 | ||
| 6 | #include "common/assert.h" | 9 | #include "common/assert.h" |
| 7 | #include "common/fs/path_util.h" | 10 | #include "common/fs/path_util.h" |
| 8 | #include "common/logging/log.h" | 11 | #include "common/logging/log.h" |
| 9 | #include "common/settings.h" | 12 | #include "common/settings.h" |
| 13 | #include "common/time_zone.h" | ||
| 10 | 14 | ||
| 11 | namespace Settings { | 15 | namespace Settings { |
| 12 | 16 | ||
| @@ -14,18 +18,23 @@ Values values; | |||
| 14 | static bool configuring_global = true; | 18 | static bool configuring_global = true; |
| 15 | 19 | ||
| 16 | std::string GetTimeZoneString() { | 20 | std::string GetTimeZoneString() { |
| 17 | static constexpr std::array timezones{ | ||
| 18 | "auto", "default", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", | ||
| 19 | "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", | ||
| 20 | "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", | ||
| 21 | "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", | ||
| 22 | "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey", | ||
| 23 | "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu", | ||
| 24 | }; | ||
| 25 | |||
| 26 | const auto time_zone_index = static_cast<std::size_t>(values.time_zone_index.GetValue()); | 21 | const auto time_zone_index = static_cast<std::size_t>(values.time_zone_index.GetValue()); |
| 27 | ASSERT(time_zone_index < timezones.size()); | 22 | ASSERT(time_zone_index < Common::TimeZone::GetTimeZoneStrings().size()); |
| 28 | return timezones[time_zone_index]; | 23 | |
| 24 | std::string location_name; | ||
| 25 | if (time_zone_index == 0) { // Auto | ||
| 26 | #if __cpp_lib_chrono >= 201907L | ||
| 27 | const struct std::chrono::tzdb& time_zone_data = std::chrono::get_tzdb(); | ||
| 28 | const std::chrono::time_zone* current_zone = time_zone_data.current_zone(); | ||
| 29 | std::string_view current_zone_name = current_zone->name(); | ||
| 30 | location_name = current_zone_name; | ||
| 31 | #else | ||
| 32 | location_name = Common::TimeZone::FindSystemTimeZone(); | ||
| 33 | #endif | ||
| 34 | } else { | ||
| 35 | location_name = Common::TimeZone::GetTimeZoneStrings()[time_zone_index]; | ||
| 36 | } | ||
| 37 | return location_name; | ||
| 29 | } | 38 | } |
| 30 | 39 | ||
| 31 | void LogSettings() { | 40 | void LogSettings() { |
diff --git a/src/common/time_zone.cpp b/src/common/time_zone.cpp index 126836b01..d8d7896c6 100644 --- a/src/common/time_zone.cpp +++ b/src/common/time_zone.cpp | |||
| @@ -2,14 +2,33 @@ | |||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include <chrono> | 4 | #include <chrono> |
| 5 | #include <exception> | ||
| 5 | #include <iomanip> | 6 | #include <iomanip> |
| 6 | #include <sstream> | 7 | #include <sstream> |
| 8 | #include <stdexcept> | ||
| 9 | #include <fmt/chrono.h> | ||
| 10 | #include <fmt/core.h> | ||
| 7 | 11 | ||
| 8 | #include "common/logging/log.h" | 12 | #include "common/logging/log.h" |
| 13 | #include "common/settings.h" | ||
| 9 | #include "common/time_zone.h" | 14 | #include "common/time_zone.h" |
| 10 | 15 | ||
| 11 | namespace Common::TimeZone { | 16 | namespace Common::TimeZone { |
| 12 | 17 | ||
| 18 | // Time zone strings | ||
| 19 | constexpr std::array timezones{ | ||
| 20 | "GMT", "GMT", "CET", "CST6CDT", "Cuba", "EET", "Egypt", "Eire", | ||
| 21 | "EST", "EST5EDT", "GB", "GB-Eire", "GMT", "GMT+0", "GMT-0", "GMT0", | ||
| 22 | "Greenwich", "Hongkong", "HST", "Iceland", "Iran", "Israel", "Jamaica", "Japan", | ||
| 23 | "Kwajalein", "Libya", "MET", "MST", "MST7MDT", "Navajo", "NZ", "NZ-CHAT", | ||
| 24 | "Poland", "Portugal", "PRC", "PST8PDT", "ROC", "ROK", "Singapore", "Turkey", | ||
| 25 | "UCT", "Universal", "UTC", "W-SU", "WET", "Zulu", | ||
| 26 | }; | ||
| 27 | |||
| 28 | const std::array<const char*, 46>& GetTimeZoneStrings() { | ||
| 29 | return timezones; | ||
| 30 | } | ||
| 31 | |||
| 13 | std::string GetDefaultTimeZone() { | 32 | std::string GetDefaultTimeZone() { |
| 14 | return "GMT"; | 33 | return "GMT"; |
| 15 | } | 34 | } |
| @@ -18,10 +37,7 @@ static std::string GetOsTimeZoneOffset() { | |||
| 18 | const std::time_t t{std::time(nullptr)}; | 37 | const std::time_t t{std::time(nullptr)}; |
| 19 | const std::tm tm{*std::localtime(&t)}; | 38 | const std::tm tm{*std::localtime(&t)}; |
| 20 | 39 | ||
| 21 | std::stringstream ss; | 40 | return fmt::format("{:%z}", tm); |
| 22 | ss << std::put_time(&tm, "%z"); // Get the current timezone offset, e.g. "-400", as a string | ||
| 23 | |||
| 24 | return ss.str(); | ||
| 25 | } | 41 | } |
| 26 | 42 | ||
| 27 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { | 43 | static int ConvertOsTimeZoneOffsetToInt(const std::string& timezone) { |
| @@ -45,4 +61,43 @@ std::chrono::seconds GetCurrentOffsetSeconds() { | |||
| 45 | return std::chrono::seconds{seconds}; | 61 | return std::chrono::seconds{seconds}; |
| 46 | } | 62 | } |
| 47 | 63 | ||
| 64 | // Key is [Hours * 100 + Minutes], multiplied by 100 if DST | ||
| 65 | const static std::map<s64, const char*> off_timezones = { | ||
| 66 | {530, "Asia/Calcutta"}, {930, "Australia/Darwin"}, {845, "Australia/Eucla"}, | ||
| 67 | {103000, "Australia/Adelaide"}, {1030, "Australia/Lord_Howe"}, {630, "Indian/Cocos"}, | ||
| 68 | {1245, "Pacific/Chatham"}, {134500, "Pacific/Chatham"}, {-330, "Canada/Newfoundland"}, | ||
| 69 | {-23000, "Canada/Newfoundland"}, {430, "Asia/Kabul"}, {330, "Asia/Tehran"}, | ||
| 70 | {43000, "Asia/Tehran"}, {545, "Asia/Kathmandu"}, {-930, "Asia/Marquesas"}, | ||
| 71 | }; | ||
| 72 | |||
| 73 | std::string FindSystemTimeZone() { | ||
| 74 | #if defined(MINGW) | ||
| 75 | // MinGW has broken strftime -- https://sourceforge.net/p/mingw-w64/bugs/793/ | ||
| 76 | // e.g. fmt::format("{:%z}") -- returns "Eastern Daylight Time" when it should be "-0400" | ||
| 77 | return timezones[0]; | ||
| 78 | #else | ||
| 79 | const s64 seconds = static_cast<s64>(GetCurrentOffsetSeconds().count()); | ||
| 80 | |||
| 81 | const s64 minutes = seconds / 60; | ||
| 82 | const s64 hours = minutes / 60; | ||
| 83 | |||
| 84 | const s64 minutes_off = minutes - hours * 60; | ||
| 85 | |||
| 86 | if (minutes_off != 0) { | ||
| 87 | const auto the_time = std::time(nullptr); | ||
| 88 | const struct std::tm& local = *std::localtime(&the_time); | ||
| 89 | const bool is_dst = local.tm_isdst != 0; | ||
| 90 | |||
| 91 | const s64 tz_index = (hours * 100 + minutes_off) * (is_dst ? 100 : 1); | ||
| 92 | |||
| 93 | try { | ||
| 94 | return off_timezones.at(tz_index); | ||
| 95 | } catch (std::out_of_range&) { | ||
| 96 | LOG_ERROR(Common, "Time zone {} not handled, defaulting to hour offset.", tz_index); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return fmt::format("Etc/GMT{:s}{:d}", hours > 0 ? "-" : "+", std::abs(hours)); | ||
| 100 | #endif | ||
| 101 | } | ||
| 102 | |||
| 48 | } // namespace Common::TimeZone | 103 | } // namespace Common::TimeZone |
diff --git a/src/common/time_zone.h b/src/common/time_zone.h index 99cae6ef2..f574d5c04 100644 --- a/src/common/time_zone.h +++ b/src/common/time_zone.h | |||
| @@ -3,15 +3,21 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <array> | ||
| 6 | #include <chrono> | 7 | #include <chrono> |
| 7 | #include <string> | 8 | #include <string> |
| 8 | 9 | ||
| 9 | namespace Common::TimeZone { | 10 | namespace Common::TimeZone { |
| 10 | 11 | ||
| 12 | [[nodiscard]] const std::array<const char*, 46>& GetTimeZoneStrings(); | ||
| 13 | |||
| 11 | /// Gets the default timezone, i.e. "GMT" | 14 | /// Gets the default timezone, i.e. "GMT" |
| 12 | [[nodiscard]] std::string GetDefaultTimeZone(); | 15 | [[nodiscard]] std::string GetDefaultTimeZone(); |
| 13 | 16 | ||
| 14 | /// Gets the offset of the current timezone (from the default), in seconds | 17 | /// Gets the offset of the current timezone (from the default), in seconds |
| 15 | [[nodiscard]] std::chrono::seconds GetCurrentOffsetSeconds(); | 18 | [[nodiscard]] std::chrono::seconds GetCurrentOffsetSeconds(); |
| 16 | 19 | ||
| 20 | /// Searches time zone offsets for the closest offset to the system time zone | ||
| 21 | [[nodiscard]] std::string FindSystemTimeZone(); | ||
| 22 | |||
| 17 | } // namespace Common::TimeZone | 23 | } // namespace Common::TimeZone |