1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_TIME_FORMAT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_TIME_FORMAT_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <future> 22 #include <memory> 23 #include <mutex> 24 #include <new> 25 #include <string> 26 #include <vector> 27 28 #include "base/utils/date_util.h" 29 #include "base/utils/macros.h" 30 #include "base/utils/noncopyable.h" 31 32 namespace OHOS::Ace { 33 34 enum class ZeroPrefixType : int32_t { 35 AUTO = 0, 36 HIDE = 1, 37 SHOW = 2, 38 OFF = 3 39 }; 40 41 struct DateTimeType { 42 ZeroPrefixType hourType; 43 ZeroPrefixType minuteType; 44 ZeroPrefixType secondType; 45 }; 46 47 class ACE_FORCE_EXPORT TimeFormat : public NonCopyable { 48 public: 49 50 virtual ~TimeFormat() = default; 51 GetHourFormat(const int32_t hourType,bool is24Hour)52 static std::string GetHourFormat(const int32_t hourType, bool is24Hour) { 53 if (hourType == static_cast<int32_t>(ZeroPrefixType::AUTO)) { 54 return is24Hour ? "2-digit" : "numeric"; 55 } else if (hourType == static_cast<int32_t>(ZeroPrefixType::HIDE)) { 56 return "numeric"; 57 } else if (hourType == static_cast<int32_t>(ZeroPrefixType::SHOW)) { 58 return "2-digit"; 59 } 60 return ""; // Handle invalid enum values gracefully 61 } 62 GetMinuteFormat(const int32_t minuteType)63 static std::string GetMinuteFormat(const int32_t minuteType) { 64 if (minuteType == static_cast<int32_t>(ZeroPrefixType::AUTO)) { 65 return "2-digit"; 66 } else if (minuteType == static_cast<int32_t>(ZeroPrefixType::HIDE)) { 67 return "numeric"; 68 } else if (minuteType == static_cast<int32_t>(ZeroPrefixType::SHOW)) { 69 return "2-digit"; 70 } 71 return ""; 72 } 73 GetSecondFormat(const int32_t secondType)74 static std::string GetSecondFormat(const int32_t secondType) { 75 if (secondType == static_cast<int32_t>(ZeroPrefixType::AUTO)) { 76 return "2-digit"; 77 } else if (secondType == static_cast<int32_t>(ZeroPrefixType::HIDE)) { 78 return "numeric"; 79 } else if (secondType == static_cast<int32_t>(ZeroPrefixType::SHOW)) { 80 return "2-digit"; 81 } 82 return ""; 83 } 84 }; 85 86 } // namespace OHOS::Ace 87 88 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_I18N_TIME_FORMAT_H