]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/ceph_time.cc
add subtree-ish sources for 12.0.3
[ceph.git] / 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 "include/types.h"
17 #include "ceph_time.h"
18 #include "config.h"
19
20 #if defined(DARWIN)
21 int clock_gettime(int clk_id, struct timespec *tp)
22 {
23 if (clk_id == CALENDAR_CLOCK) {
24 // gettimeofday is much faster than clock_get_time
25 struct timeval now;
26 int ret = gettimeofday(&now, NULL);
27 if (ret)
28 return ret;
29 tp->tv_sec = now.tv_sec;
30 tp->tv_nsec = now.tv_usec * 1000L;
31 } else {
32 clock_serv_t cclock;
33 mach_timespec_t mts;
34
35 host_get_clock_service(mach_host_self(), clk_id, &cclock);
36 clock_get_time(cclock, &mts);
37 mach_port_deallocate(mach_task_self(), cclock);
38
39 tp->tv_sec = mts.tv_sec;
40 tp->tv_nsec = mts.tv_nsec;
41 }
42 return 0;
43 }
44 #endif
45
46 namespace ceph {
47 namespace time_detail {
48 void real_clock::to_ceph_timespec(const time_point& t,
49 struct ceph_timespec& ts) {
50 ts.tv_sec = to_time_t(t);
51 ts.tv_nsec = (t.time_since_epoch() % seconds(1)).count();
52 }
53 struct ceph_timespec real_clock::to_ceph_timespec(const time_point& t) {
54 struct ceph_timespec ts;
55 to_ceph_timespec(t, ts);
56 return ts;
57 }
58 real_clock::time_point real_clock::from_ceph_timespec(
59 const struct ceph_timespec& ts) {
60 return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
61 }
62
63 void coarse_real_clock::to_ceph_timespec(const time_point& t,
64 struct ceph_timespec& ts) {
65 ts.tv_sec = to_time_t(t);
66 ts.tv_nsec = (t.time_since_epoch() % seconds(1)).count();
67 }
68 struct ceph_timespec coarse_real_clock::to_ceph_timespec(
69 const time_point& t) {
70 struct ceph_timespec ts;
71 to_ceph_timespec(t, ts);
72 return ts;
73 }
74 coarse_real_clock::time_point coarse_real_clock::from_ceph_timespec(
75 const struct ceph_timespec& ts) {
76 return time_point(seconds(ts.tv_sec) + nanoseconds(ts.tv_nsec));
77 }
78 }
79
80 using std::chrono::duration_cast;
81 using std::chrono::seconds;
82 using std::chrono::microseconds;
83
84 template<typename Clock,
85 typename std::enable_if<Clock::is_steady>::type*>
86 std::ostream& operator<<(std::ostream& m,
87 const std::chrono::time_point<Clock>& t) {
88 return m << std::chrono::duration<double>(t.time_since_epoch()).count()
89 << "s";
90 }
91
92 std::ostream& operator<<(std::ostream& m, const timespan& t) {
93 return m << std::chrono::duration<double>(t).count() << "s";
94 }
95
96 template<typename Clock,
97 typename std::enable_if<!Clock::is_steady>::type*>
98 std::ostream& operator<<(std::ostream& m,
99 const std::chrono::time_point<Clock>& t) {
100 m.setf(std::ios::right);
101 char oldfill = m.fill();
102 m.fill('0');
103 // localtime. this looks like an absolute time.
104 // aim for http://en.wikipedia.org/wiki/ISO_8601
105 struct tm bdt;
106 time_t tt = Clock::to_time_t(t);
107 localtime_r(&tt, &bdt);
108 m << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07'
109 << '-' << std::setw(2) << (bdt.tm_mon+1)
110 << '-' << std::setw(2) << bdt.tm_mday
111 << ' '
112 << std::setw(2) << bdt.tm_hour
113 << ':' << std::setw(2) << bdt.tm_min
114 << ':' << std::setw(2) << bdt.tm_sec
115 << "." << std::setw(6) << duration_cast<microseconds>(
116 t.time_since_epoch() % seconds(1));
117 m.fill(oldfill);
118 m.unsetf(std::ios::right);
119 return m;
120 }
121
122 template std::ostream&
123 operator<< <mono_clock>(std::ostream& m, const mono_time& t);
124 template std::ostream&
125 operator<< <real_clock>(std::ostream& m, const real_time& t);
126 template std::ostream&
127 operator<< <coarse_mono_clock>(std::ostream& m, const coarse_mono_time& t);
128 template std::ostream&
129 operator<< <coarse_real_clock>(std::ostream& m, const coarse_real_time& t);
130 }