1 /** 2 * Copyright (c) 2021-2022 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 // This is a local header 16 // NOLINTNEXTLINE(modernize-deprecated-headers, hicpp-deprecated-headers) 17 #include "time.h" 18 #include <chrono> 19 20 namespace panda::time { 21 22 template <class T> GetCurrentTime(bool need_system)23static uint64_t GetCurrentTime(bool need_system) 24 { 25 if (need_system) { 26 return std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch()).count(); 27 } 28 return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); 29 } 30 31 /** 32 * Return current time in milliseconds 33 */ GetCurrentTimeInMillis(bool need_system)34uint64_t GetCurrentTimeInMillis(bool need_system) 35 { 36 return GetCurrentTime<std::chrono::milliseconds>(need_system); 37 } 38 39 /** 40 * Return current time in nanoseconds 41 */ GetCurrentTimeInNanos(bool need_system)42uint64_t GetCurrentTimeInNanos(bool need_system) 43 { 44 return GetCurrentTime<std::chrono::nanoseconds>(need_system); 45 } 46 47 } // namespace panda::time 48