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 | #include "shill/shill_time.h" |
| 6 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 7 | #include <time.h> |
| 8 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 9 | namespace shill { |
| 10 | |
Ben Chan | bbdef5f | 2012-04-23 13:58:15 -0700 | [diff] [blame] | 11 | namespace { |
Ben Chan | f9b1856 | 2012-08-13 16:57:24 -0700 | [diff] [blame] | 12 | |
| 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. |
| 18 | base::LazyInstance<Time>::Leaky g_time = LAZY_INSTANCE_INITIALIZER; |
| 19 | |
Ben Chan | bbdef5f | 2012-04-23 13:58:15 -0700 | [diff] [blame] | 20 | } // namespace |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 21 | |
| 22 | Time::Time() { } |
| 23 | |
| 24 | Time::~Time() { } |
| 25 | |
| 26 | Time* Time::GetInstance() { |
| 27 | return g_time.Pointer(); |
| 28 | } |
| 29 | |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 30 | int 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 Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 41 | int Time::GetTimeOfDay(struct timeval *tv, struct timezone *tz) { |
| 42 | return gettimeofday(tv, tz); |
| 43 | } |
| 44 | |
| 45 | } // namespace shill |