1cb69b360Sopenharmony_ci/* 2cb69b360Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3cb69b360Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cb69b360Sopenharmony_ci * you may not use this file except in compliance with the License. 5cb69b360Sopenharmony_ci * You may obtain a copy of the License at 6cb69b360Sopenharmony_ci * 7cb69b360Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cb69b360Sopenharmony_ci * 9cb69b360Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cb69b360Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cb69b360Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cb69b360Sopenharmony_ci * See the License for the specific language governing permissions and 13cb69b360Sopenharmony_ci * limitations under the License. 14cb69b360Sopenharmony_ci */ 15cb69b360Sopenharmony_ci#ifndef POWERMGR_POWER_MGR_TIME_UTIL_H 16cb69b360Sopenharmony_ci#define POWERMGR_POWER_MGR_TIME_UTIL_H 17cb69b360Sopenharmony_ci 18cb69b360Sopenharmony_ci#include <stdint.h> 19cb69b360Sopenharmony_ci 20cb69b360Sopenharmony_ci#include <time.h> 21cb69b360Sopenharmony_ci 22cb69b360Sopenharmony_ci#ifdef __cplusplus 23cb69b360Sopenharmony_ciextern "C" { 24cb69b360Sopenharmony_ci#endif // __cplusplus 25cb69b360Sopenharmony_ci 26cb69b360Sopenharmony_cienum { 27cb69b360Sopenharmony_ci NSEC_PER_MIN = 60000000000LL, 28cb69b360Sopenharmony_ci NSEC_PER_SEC = 1000000000LL, 29cb69b360Sopenharmony_ci NSEC_PER_MSEC = 1000000LL, 30cb69b360Sopenharmony_ci NSEC_PER_USEC = 1000LL, 31cb69b360Sopenharmony_ci USEC_PER_SEC = 1000000LL, 32cb69b360Sopenharmony_ci USEC_PER_MSEC = 1000LL, 33cb69b360Sopenharmony_ci MSEC_PER_DAY = 86400000LL, 34cb69b360Sopenharmony_ci MSEC_PER_HOUR = 3600000LL, 35cb69b360Sopenharmony_ci MSEC_PER_MIN = 60000LL, 36cb69b360Sopenharmony_ci MSEC_PER_SEC = 1000LL, 37cb69b360Sopenharmony_ci SEC_PER_MIN = 60LL, 38cb69b360Sopenharmony_ci SEC_PER_HOUR = 3600LL, 39cb69b360Sopenharmony_ci SEC_PER_DAY = 86400LL, 40cb69b360Sopenharmony_ci MIN_PER_HOUR = 60LL, 41cb69b360Sopenharmony_ci HOUR_PER_DAY = 24LL, 42cb69b360Sopenharmony_ci}; 43cb69b360Sopenharmony_ci 44cb69b360Sopenharmony_cistatic inline int64_t NsecToUsec(int64_t nsec) 45cb69b360Sopenharmony_ci{ 46cb69b360Sopenharmony_ci return nsec / NSEC_PER_USEC; 47cb69b360Sopenharmony_ci} 48cb69b360Sopenharmony_ci 49cb69b360Sopenharmony_cistatic inline int64_t NsecToMsec(int64_t nsec) 50cb69b360Sopenharmony_ci{ 51cb69b360Sopenharmony_ci return nsec / NSEC_PER_MSEC; 52cb69b360Sopenharmony_ci} 53cb69b360Sopenharmony_ci 54cb69b360Sopenharmony_cistatic inline int64_t UsecToMsec(int64_t usec) 55cb69b360Sopenharmony_ci{ 56cb69b360Sopenharmony_ci return usec / USEC_PER_MSEC; 57cb69b360Sopenharmony_ci} 58cb69b360Sopenharmony_ci 59cb69b360Sopenharmony_cistatic inline int64_t MsecToNsec(int64_t msec) 60cb69b360Sopenharmony_ci{ 61cb69b360Sopenharmony_ci return msec * NSEC_PER_MSEC; 62cb69b360Sopenharmony_ci} 63cb69b360Sopenharmony_ci 64cb69b360Sopenharmony_cistatic inline int64_t MsecToUsec(int64_t msec) 65cb69b360Sopenharmony_ci{ 66cb69b360Sopenharmony_ci return msec * USEC_PER_MSEC; 67cb69b360Sopenharmony_ci} 68cb69b360Sopenharmony_ci 69cb69b360Sopenharmony_cistatic inline int64_t MsecToSec(int64_t msec) 70cb69b360Sopenharmony_ci{ 71cb69b360Sopenharmony_ci return msec / MSEC_PER_SEC; 72cb69b360Sopenharmony_ci} 73cb69b360Sopenharmony_ci 74cb69b360Sopenharmony_cistatic inline int64_t MsecToMin(int64_t msec) 75cb69b360Sopenharmony_ci{ 76cb69b360Sopenharmony_ci return msec / MSEC_PER_MIN; 77cb69b360Sopenharmony_ci} 78cb69b360Sopenharmony_ci 79cb69b360Sopenharmony_cistatic inline int64_t MsecToHour(int64_t msec) 80cb69b360Sopenharmony_ci{ 81cb69b360Sopenharmony_ci return msec / MSEC_PER_HOUR; 82cb69b360Sopenharmony_ci} 83cb69b360Sopenharmony_ci 84cb69b360Sopenharmony_cistatic inline int64_t SecToNsec(int64_t sec) 85cb69b360Sopenharmony_ci{ 86cb69b360Sopenharmony_ci return sec * NSEC_PER_SEC; 87cb69b360Sopenharmony_ci} 88cb69b360Sopenharmony_ci 89cb69b360Sopenharmony_cistatic inline int64_t SecToMsec(int64_t sec) 90cb69b360Sopenharmony_ci{ 91cb69b360Sopenharmony_ci return sec * MSEC_PER_SEC; 92cb69b360Sopenharmony_ci} 93cb69b360Sopenharmony_ci 94cb69b360Sopenharmony_cistatic inline int64_t MinToNsec(int64_t mins) 95cb69b360Sopenharmony_ci{ 96cb69b360Sopenharmony_ci return mins * NSEC_PER_MIN; 97cb69b360Sopenharmony_ci} 98cb69b360Sopenharmony_ci 99cb69b360Sopenharmony_cistatic inline int64_t MinToMsec(int64_t mins) 100cb69b360Sopenharmony_ci{ 101cb69b360Sopenharmony_ci return mins * MSEC_PER_MIN; 102cb69b360Sopenharmony_ci} 103cb69b360Sopenharmony_ci 104cb69b360Sopenharmony_cistatic inline int64_t HourToMsec(int64_t hour) 105cb69b360Sopenharmony_ci{ 106cb69b360Sopenharmony_ci return hour * MSEC_PER_HOUR; 107cb69b360Sopenharmony_ci} 108cb69b360Sopenharmony_ci 109cb69b360Sopenharmony_cistatic inline int64_t DayToMsec(int64_t day) 110cb69b360Sopenharmony_ci{ 111cb69b360Sopenharmony_ci return day * MSEC_PER_DAY; 112cb69b360Sopenharmony_ci} 113cb69b360Sopenharmony_ci 114cb69b360Sopenharmony_ciint64_t GetCurrentTimeMsec(clockid_t clkId); 115cb69b360Sopenharmony_ci 116cb69b360Sopenharmony_ci#ifdef __cplusplus 117cb69b360Sopenharmony_ci} 118cb69b360Sopenharmony_ci#endif // __cplusplus 119cb69b360Sopenharmony_ci#endif // POWERMGR_POWER_MGR_TIME_UTIL_H 120