Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / ceph_time.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 // For ceph_timespec
16 #include "ceph_time.h"
17 #include "config.h"
18
19 #if defined(DARWIN)
20 int clock_gettime(int clk_id, struct timespec *tp)
21 {
22   if (clk_id == CALENDAR_CLOCK) {
23     // gettimeofday is much faster than clock_get_time
24     struct timeval now;
25     int ret = gettimeofday(&now, NULL);
26     if (ret)
27       return ret;
28     tp->tv_sec = now.tv_sec;
29     tp->tv_nsec = now.tv_usec * 1000L;
30   } else {
31     clock_serv_t cclock;
32     mach_timespec_t mts;
33
34     host_get_clock_service(mach_host_self(), clk_id, &cclock);
35     clock_get_time(cclock, &mts);
36     mach_port_deallocate(mach_task_self(), cclock);
37
38     tp->tv_sec = mts.tv_sec;
39     tp->tv_nsec = mts.tv_nsec;
40   }
41   return 0;
42 }
43 #endif
44
45 namespace ceph {
46   namespace time_detail {
47     void real_clock::to_ceph_timespec(const time_point& t,
48                                       struct ceph_timespec& ts) {
49       ts.tv_sec = to_time_t(t);
50       ts.tv_nsec = (t.time_since_epoch() % seconds(1)).count();
51     }
52     struct ceph_timespec real_clock::to_ceph_timespec(const time_point& t) {
53       struct ceph_timespec ts;
54       to_ceph_timespec(t, ts);
55       return ts;
56     }
57     real_clock::time_point real_clock::from_ceph_timespec(
58       const struct ceph_timespec& ts) {
59       return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
60     }
61
62     void coarse_real_clock::to_ceph_timespec(const time_point& t,
63                                              struct ceph_timespec& ts) {
64       ts.tv_sec = to_time_t(t);
65       ts.tv_nsec = (t.time_since_epoch() % seconds(1)).count();
66     }
67     struct ceph_timespec coarse_real_clock::to_ceph_timespec(
68       const time_point& t) {
69       struct ceph_timespec ts;
70       to_ceph_timespec(t, ts);
71       return ts;
72     }
73     coarse_real_clock::time_point coarse_real_clock::from_ceph_timespec(
74       const struct ceph_timespec& ts) {
75       return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
76     }
77   }
78
79   using std::chrono::duration_cast;
80   using std::chrono::seconds;
81   using std::chrono::microseconds;
82
83   template<typename Clock,
84            typename std::enable_if<Clock::is_steady>::type*>
85   std::ostream& operator<<(std::ostream& m,
86                            const std::chrono::time_point<Clock>& t) {
87     return m << std::chrono::duration<double>(t.time_since_epoch()).count()
88              << "s";
89   }
90
91   std::ostream& operator<<(std::ostream& m, const timespan& t) {
92     return m << std::chrono::duration<double>(t).count() << "s";
93   }
94
95   template<typename Clock,
96            typename std::enable_if<!Clock::is_steady>::type*>
97   std::ostream& operator<<(std::ostream& m,
98                            const std::chrono::time_point<Clock>& t) {
99     m.setf(std::ios::right);
100     char oldfill = m.fill();
101     m.fill('0');
102     // localtime.  this looks like an absolute time.
103     //  aim for http://en.wikipedia.org/wiki/ISO_8601
104     struct tm bdt;
105     time_t tt = Clock::to_time_t(t);
106     localtime_r(&tt, &bdt);
107     m << std::setw(4) << (bdt.tm_year+1900)  // 2007 -> '07'
108       << '-' << std::setw(2) << (bdt.tm_mon+1)
109       << '-' << std::setw(2) << bdt.tm_mday
110       << ' '
111       << std::setw(2) << bdt.tm_hour
112       << ':' << std::setw(2) << bdt.tm_min
113       << ':' << std::setw(2) << bdt.tm_sec
114       << "." << std::setw(6) << duration_cast<microseconds>(
115         t.time_since_epoch() % seconds(1));
116     m.fill(oldfill);
117     m.unsetf(std::ios::right);
118     return m;
119   }
120
121   template std::ostream&
122   operator<< <mono_clock>(std::ostream& m, const mono_time& t);
123   template std::ostream&
124   operator<< <real_clock>(std::ostream& m, const real_time& t);
125   template std::ostream&
126   operator<< <coarse_mono_clock>(std::ostream& m, const coarse_mono_time& t);
127   template std::ostream&
128   operator<< <coarse_real_clock>(std::ostream& m, const coarse_real_time& t);
129 }