blob: d145dff92f509b7812ae86545631ceafa3f77695 [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>
Wade Guthrie7c2d34e2013-05-09 14:02:20 -07009#include <time.h>
Paul Stewartc2350ee2011-10-19 12:28:40 -070010
Darin Petkov0c65bdd2012-12-05 13:42:41 +010011#include <string>
12
Paul Stewartc2350ee2011-10-19 12:28:40 -070013#include <base/lazy_instance.h>
14
15namespace shill {
16
Darin Petkov0c65bdd2012-12-05 13:42:41 +010017// 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.
21struct Timestamp {
Wade Guthrie7c2d34e2013-05-09 14:02:20 -070022 Timestamp() : monotonic((const struct timeval) { 0 }) {}
Darin Petkov0c65bdd2012-12-05 13:42:41 +010023 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 Stewartc2350ee2011-10-19 12:28:40 -070032// A "sys/time.h" abstraction allowing mocking in tests.
33class Time {
34 public:
35 virtual ~Time();
36
37 static Time *GetInstance();
38
Paul Stewarte6927402012-01-23 16:11:30 -080039 // clock_gettime(CLOCK_MONOTONIC ...
40 virtual int GetTimeMonotonic(struct timeval *tv);
41
Paul Stewartc2350ee2011-10-19 12:28:40 -070042 // gettimeofday
43 virtual int GetTimeOfDay(struct timeval *tv, struct timezone *tz);
44
Darin Petkov0c65bdd2012-12-05 13:42:41 +010045 // Returns a snapshot of the current time.
46 virtual Timestamp GetNow();
47
Wade Guthrie7c2d34e2013-05-09 14:02:20 -070048 virtual time_t GetSecondsSinceEpoch() const;
49
Paul Stewartc2350ee2011-10-19 12:28:40 -070050 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_