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 RESSCHED_COMMON_INCLUDE_RES_SCHED_TIME_UTIL 17 #define RESSCHED_COMMON_INCLUDE_RES_SCHED_TIME_UTIL 18 #include <iomanip> 19 #include <sstream> 20 #include <string> 21 #include <sys/time.h> 22 23 const char* const DATE_FORMAT = "%Y-%m-%d"; 24 25 namespace OHOS { 26 namespace ResourceSchedule { 27 namespace ResCommonUtil { 28 29 /** 30 * @brief Obtain current seconds timestamp 31 * 32 * @param steady true if time will not affected by the system clock. 33 * @return Returns current second timestamp; 34 */ 35 int64_t GetNowSecTime(bool steady = false); 36 37 /** 38 * @brief Obtain current milliseconds timestamp 39 * 40 * @param steady true if time will not affected by the system clock. 41 * @return Returns current millsecond timestamp; 42 */ 43 int64_t GetNowMillTime(bool steady = false); 44 45 /** 46 * @brief Obtain current microseconds timestamp. 47 * 48 * @param steady true if time will not affected by the system clock. 49 * @return Returns current microsecond timestamp; 50 */ 51 int64_t GetNowMicroTime(bool steady = false); 52 53 /** 54 * @brief Obtain current milliseconds system clock timestamp. 55 * 56 * @return Returns current millsecond timestamp; 57 */ 58 int64_t GetCurrentTimestamp(); 59 60 /** 61 * @brief Converts given timestamp to string. 62 * 63 * @param timestamp Indicates the string to be converted 64 * @return Returns current millsecond timestamp; 65 */ 66 std::string ConvertTimestampToStr(int64_t timestamp); 67 68 /** 69 * @brief Calculate the timestamp of the current time after the duration. 70 * 71 * @param duration the duration has passed. 72 * @return Returns the next timestamp after duration; 73 */ 74 int64_t GetNextMillTimeStamp(const int64_t duration, bool steady = false); 75 76 /** 77 * @brief format input seconds to date. 78 * 79 * @param second the input second time. 80 * @param format the date format 81 * @return Returns the date of sconds format; 82 */ 83 std::string FormatSeconds(const int64_t seconds, const std::string& format); 84 85 /** 86 * @brief format input millis timestamp to %Y-%m-%H-%M-ms. 87 * 88 * @param millis the input millisecond timestamp. 89 * @return Returns the string of date; 90 */ 91 std::string FormatTimeWithMillis(const int64_t millis); 92 } 93 94 } // namespace ResourceSchedule 95 } // namespace OHOS 96 97 #endif // RESSCHED_COMMON_INCLUDE_RES_SCHED_TIME_UTIL