Home | History | Annotate | Download | only in android-base
      1 /*
      2  * Copyright (C) 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 #ifndef ANDROID_BASE_CHRONO_UTILS_H
     18 #define ANDROID_BASE_CHRONO_UTILS_H
     19 
     20 #include <chrono>
     21 #include <sstream>
     22 
     23 #if __cplusplus > 201103L  // C++14
     24 using namespace std::chrono_literals;
     25 #endif
     26 
     27 namespace android {
     28 namespace base {
     29 
     30 // A std::chrono clock based on CLOCK_BOOTTIME.
     31 class boot_clock {
     32  public:
     33   typedef std::chrono::nanoseconds duration;
     34   typedef std::chrono::time_point<boot_clock, duration> time_point;
     35 
     36   static time_point now();
     37 };
     38 
     39 class Timer {
     40  public:
     41   Timer() : start_(boot_clock::now()) {}
     42 
     43   std::chrono::milliseconds duration() const {
     44     return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_);
     45   }
     46 
     47  private:
     48   boot_clock::time_point start_;
     49 };
     50 
     51 std::ostream& operator<<(std::ostream& os, const Timer& t);
     52 
     53 }  // namespace base
     54 }  // namespace android
     55 
     56 #endif  // ANDROID_BASE_CHRONO_UTILS_H
     57