Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_TIME_H_ |
| 6 | #define SHILL_TIME_H_ |
| 7 | |
| 8 | #include <sys/time.h> |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 9 | #include <time.h> |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 10 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 11 | #include <string> |
| 12 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 13 | #include <base/lazy_instance.h> |
| 14 | |
| 15 | namespace shill { |
| 16 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 17 | // Timestamp encapsulates a |monotonic| time that can be used to compare the |
| 18 | // relative order and distance of events as well as a |wall_clock| time that can |
| 19 | // be used for presenting the time in human-readable format. Note that the |
| 20 | // monotonic clock does not necessarily advance during suspend. |
| 21 | struct Timestamp { |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 22 | Timestamp() : monotonic((const struct timeval) { 0 }) {} |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 23 | Timestamp(const struct timeval &in_monotonic, |
| 24 | const std::string &in_wall_clock) |
| 25 | : monotonic(in_monotonic), |
| 26 | wall_clock(in_wall_clock) {} |
| 27 | |
| 28 | struct timeval monotonic; |
| 29 | std::string wall_clock; |
| 30 | }; |
| 31 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 32 | // A "sys/time.h" abstraction allowing mocking in tests. |
| 33 | class Time { |
| 34 | public: |
| 35 | virtual ~Time(); |
| 36 | |
| 37 | static Time *GetInstance(); |
| 38 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 39 | // clock_gettime(CLOCK_MONOTONIC ... |
| 40 | virtual int GetTimeMonotonic(struct timeval *tv); |
| 41 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 42 | // gettimeofday |
| 43 | virtual int GetTimeOfDay(struct timeval *tv, struct timezone *tz); |
| 44 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 45 | // Returns a snapshot of the current time. |
| 46 | virtual Timestamp GetNow(); |
| 47 | |
Wade Guthrie | 7c2d34e | 2013-05-09 14:02:20 -0700 | [diff] [blame] | 48 | virtual time_t GetSecondsSinceEpoch() const; |
| 49 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 50 | protected: |
| 51 | Time(); |
| 52 | |
| 53 | private: |
| 54 | friend struct base::DefaultLazyInstanceTraits<Time>; |
| 55 | |
| 56 | DISALLOW_COPY_AND_ASSIGN(Time); |
| 57 | }; |
| 58 | |
| 59 | } // namespace shill |
| 60 | |
| 61 | #endif // SHILL_TIME_H_ |