|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "app/src/locale.h" |
| 18 | + |
| 19 | +#include "app/src/include/firebase/internal/platform.h" |
| 20 | +#include "app/src/log.h" |
| 21 | + |
| 22 | +#if FIREBASE_PLATFORM_OSX || FIREBASE_PLATFORM_IOS |
| 23 | +#error "This file does not support iOS and OS X, use locale_apple.mm instead." |
| 24 | +#elif FIREBASE_PLATFORM_ANDROID |
| 25 | +#error "This file is not support on Android." |
| 26 | +#elif FIREBASE_PLATFORM_WINDOWS |
| 27 | +#include <time.h> |
| 28 | +#include <windows.h> |
| 29 | +#elif FIREBASE_PLATFORM_LINUX |
| 30 | +#include <clocale> |
| 31 | +#include <ctime> |
| 32 | +#include <locale> |
| 33 | +#else |
| 34 | +#error "Unknown platform." |
| 35 | +#endif // platform selector |
| 36 | + |
| 37 | +#include <algorithm> |
| 38 | +#include <vector> |
| 39 | + |
| 40 | +namespace firebase { |
| 41 | +namespace internal { |
| 42 | + |
| 43 | +// Get the current locale, e.g. "en_US" |
| 44 | +std::string GetLocale() { |
| 45 | +#if FIREBASE_PLATFORM_WINDOWS |
| 46 | + LCID lang_id = GetThreadLocale(); |
| 47 | + std::vector<wchar_t> locale_name(LOCALE_NAME_MAX_LENGTH); |
| 48 | + if (LCIDToLocaleName(lang_id, &locale_name[0], LOCALE_NAME_MAX_LENGTH, 0) == |
| 49 | + 0) { |
| 50 | + return ""; |
| 51 | + } |
| 52 | + std::wstring woutput(&locale_name[0]); |
| 53 | + std::string output(woutput.begin(), woutput.end()); |
| 54 | + // Change all hyphens to underscores to normalize the locale. |
| 55 | + std::replace(output.begin(), output.end(), '-', '_'); |
| 56 | + return output; |
| 57 | +#elif FIREBASE_PLATFORM_LINUX |
| 58 | + // If std::locale() has been customized, return it, else return the contents |
| 59 | + // of the LANG or LC_CTYPE environment variables if set, or otherwise return a |
| 60 | + // default locale (empty in real life, or placeholder when running in a unit |
| 61 | + // test, as the test environment has no locale variables set). |
| 62 | + std::string output = std::locale().name() != "C" |
| 63 | + ? std::locale().name() |
| 64 | + : getenv("LANG") |
| 65 | + ? getenv("LANG") |
| 66 | + : getenv("LC_CTYPE") |
| 67 | + ? getenv("LC_CTYPE") |
| 68 | + : getenv("TEST_TMPDIR") ? "en_US" : ""; |
| 69 | + // Some of the environment variables have a "." after the name to specify the |
| 70 | + // terminal encoding, e.g. "en_US.UTF-8", so we want to cut the string on the |
| 71 | + // ".". |
| 72 | + size_t cut = output.find("."); |
| 73 | + if (cut != std::string::npos) { |
| 74 | + output = output.substr(0, cut); |
| 75 | + } |
| 76 | + // Do the same with a "@" which some locales also have. |
| 77 | + cut = output.find("@"); |
| 78 | + if (cut != std::string::npos) { |
| 79 | + output = output.substr(0, cut); |
| 80 | + } |
| 81 | + return output; |
| 82 | +#else |
| 83 | + // An error was already triggered above. |
| 84 | + return ""; |
| 85 | +#endif // platform selector |
| 86 | +} |
| 87 | + |
| 88 | +// Get the current time zone, e.g. "US/Pacific" |
| 89 | +std::string GetTimezone() { |
| 90 | +#if FIREBASE_PLATFORM_WINDOWS |
| 91 | + // If "TZ" environment variable is defined, use it, else use _get_tzname. |
| 92 | + int tz_bytes = GetEnvironmentVariable("TZ", nullptr, 0); |
| 93 | + if (tz_bytes > 0) { |
| 94 | + std::vector<char> buffer(tz_bytes); |
| 95 | + GetEnvironmentVariable("TZ", &buffer[0], tz_bytes); |
| 96 | + return std::string(&buffer[0]); |
| 97 | + } |
| 98 | + int daylight; // daylight savings time? |
| 99 | + if (_get_daylight(&daylight) != 0) return ""; |
| 100 | + size_t length = 0; // get the needed string length |
| 101 | + if (_get_tzname(&length, nullptr, 0, daylight ? 1 : 0) != 0) return ""; |
| 102 | + std::vector<char> namebuf(length); |
| 103 | + if (_get_tzname(&length, &namebuf[0], length, daylight ? 1 : 0) != 0) |
| 104 | + return ""; |
| 105 | + std::string name_str(&namebuf[0]); |
| 106 | + return name_str; |
| 107 | +#elif FIREBASE_PLATFORM_LINUX |
| 108 | + // If TZ environment variable is defined and not empty, use it, else use |
| 109 | + // tzname. |
| 110 | + return (getenv("TZ") && *getenv("TZ")) ? getenv("TZ") |
| 111 | + : tzname[daylight ? 1 : 0]; |
| 112 | +#else |
| 113 | + // An error was already triggered above. |
| 114 | + return ""; |
| 115 | +#endif // platform selector |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace internal |
| 119 | +} // namespace firebase |
0 commit comments