blob: c6b4a38075248205071c02226599845d52856c5a [file] [log] [blame]
Paul Stewarte6927402012-01-23 16:11:30 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewartc2350ee2011-10-19 12:28:40 -07002// 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 Petkov0c65bdd2012-12-05 13:42:41 +010010#include <string>
11
Paul Stewartc2350ee2011-10-19 12:28:40 -070012#include <base/lazy_instance.h>
13
14namespace shill {
15
Darin Petkov0c65bdd2012-12-05 13:42:41 +010016// 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.
20struct 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 Stewartc2350ee2011-10-19 12:28:40 -070031// A "sys/time.h" abstraction allowing mocking in tests.
32class Time {
33 public:
34 virtual ~Time();
35
36 static Time *GetInstance();
37
Paul Stewarte6927402012-01-23 16:11:30 -080038 // clock_gettime(CLOCK_MONOTONIC ...
39 virtual int GetTimeMonotonic(struct timeval *tv);
40
Paul Stewartc2350ee2011-10-19 12:28:40 -070041 // gettimeofday
42 virtual int GetTimeOfDay(struct timeval *tv, struct timezone *tz);
43
Darin Petkov0c65bdd2012-12-05 13:42:41 +010044 // Returns a snapshot of the current time.
45 virtual Timestamp GetNow();
46
Paul Stewartc2350ee2011-10-19 12:28:40 -070047 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_