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> |
| 9 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 12 | #include <base/lazy_instance.h> |
| 13 | |
| 14 | namespace shill { |
| 15 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 16 | // Timestamp encapsulates a |monotonic| time that can be used to compare the |
| 17 | // relative order and distance of events as well as a |wall_clock| time that can |
| 18 | // be used for presenting the time in human-readable format. Note that the |
| 19 | // monotonic clock does not necessarily advance during suspend. |
| 20 | struct Timestamp { |
| 21 | Timestamp() : monotonic((const struct timeval){ 0 }) {} |
| 22 | Timestamp(const struct timeval &in_monotonic, |
| 23 | const std::string &in_wall_clock) |
| 24 | : monotonic(in_monotonic), |
| 25 | wall_clock(in_wall_clock) {} |
| 26 | |
| 27 | struct timeval monotonic; |
| 28 | std::string wall_clock; |
| 29 | }; |
| 30 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 31 | // A "sys/time.h" abstraction allowing mocking in tests. |
| 32 | class Time { |
| 33 | public: |
| 34 | virtual ~Time(); |
| 35 | |
| 36 | static Time *GetInstance(); |
| 37 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 38 | // clock_gettime(CLOCK_MONOTONIC ... |
| 39 | virtual int GetTimeMonotonic(struct timeval *tv); |
| 40 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 41 | // gettimeofday |
| 42 | virtual int GetTimeOfDay(struct timeval *tv, struct timezone *tz); |
| 43 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 44 | // Returns a snapshot of the current time. |
| 45 | virtual Timestamp GetNow(); |
| 46 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 47 | protected: |
| 48 | Time(); |
| 49 | |
| 50 | private: |
| 51 | friend struct base::DefaultLazyInstanceTraits<Time>; |
| 52 | |
| 53 | DISALLOW_COPY_AND_ASSIGN(Time); |
| 54 | }; |
| 55 | |
| 56 | } // namespace shill |
| 57 | |
| 58 | #endif // SHILL_TIME_H_ |