1 /* 2 * Copyright 2017, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "timer.h" 18 19 #include <time.h> 20 now()21uint64_t now() { 22 struct timespec time = {0, 0}; 23 clock_gettime(CLOCK_MONOTONIC, &time); 24 return static_cast<uint64_t>(time.tv_sec) * 1000u + 25 static_cast<uint64_t>(time.tv_nsec / 1000000u); 26 } 27 Timer()28Timer::Timer() : mExpires(0) {} 29 expireSeconds(uint64_t seconds)30void Timer::expireSeconds(uint64_t seconds) { 31 mExpires = now() + seconds * 1000u; 32 } 33 expired() const34bool Timer::expired() const { 35 return now() >= mExpires; 36 } 37 remainingMillis() const38uint64_t Timer::remainingMillis() const { 39 uint64_t current = now(); 40 if (current > mExpires) { 41 return 0; 42 } 43 return mExpires - current; 44 } 45