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 } 30 expireSeconds(uint64_t seconds)31void Timer::expireSeconds(uint64_t seconds) { 32 mExpires = now() + seconds * 1000u; 33 } 34 expired() const35bool Timer::expired() const { 36 return now() >= mExpires; 37 } 38 remainingMillis() const39uint64_t Timer::remainingMillis() const { 40 uint64_t current = now(); 41 if (current > mExpires) { 42 return 0; 43 } 44 return mExpires - current; 45 } 46 47