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