blob: f3b212db2b0aa18c916184a1f71cbe2af7a76d28 [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#include "shill/shill_time.h"
6
Paul Stewarte6927402012-01-23 16:11:30 -08007#include <time.h>
8
Paul Stewartc2350ee2011-10-19 12:28:40 -07009namespace shill {
10
Ben Chanbbdef5f2012-04-23 13:58:15 -070011namespace {
Ben Chanf9b18562012-08-13 16:57:24 -070012
13// As Time may be instantiated by MemoryLogMessage during a callback of
14// AtExitManager, it needs to be a leaky singleton to avoid
15// AtExitManager::RegisterCallback() from potentially being called within a
16// callback of AtExitManager, which will lead to a crash. Making Time leaky is
17// fine as it does not need to clean up or release any resource at destruction.
18base::LazyInstance<Time>::Leaky g_time = LAZY_INSTANCE_INITIALIZER;
19
Ben Chanbbdef5f2012-04-23 13:58:15 -070020} // namespace
Paul Stewartc2350ee2011-10-19 12:28:40 -070021
22Time::Time() { }
23
24Time::~Time() { }
25
26Time* Time::GetInstance() {
27 return g_time.Pointer();
28}
29
Paul Stewarte6927402012-01-23 16:11:30 -080030int Time::GetTimeMonotonic(struct timeval *tv) {
31 struct timespec ts;
32 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
33 return -1;
34 }
35
36 tv->tv_sec = ts.tv_sec;
37 tv->tv_usec = ts.tv_nsec / 1000;
38 return 0;
39}
40
Paul Stewartc2350ee2011-10-19 12:28:40 -070041int Time::GetTimeOfDay(struct timeval *tv, struct timezone *tz) {
42 return gettimeofday(tv, tz);
43}
44
45} // namespace shill