]>
git.proxmox.com Git - ceph.git/blob - 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
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
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.
16 #include "include/types.h"
17 #include "ceph_time.h"
21 int clock_gettime(int clk_id
, struct timespec
*tp
)
23 if (clk_id
== CALENDAR_CLOCK
) {
24 // gettimeofday is much faster than clock_get_time
26 int ret
= gettimeofday(&now
, NULL
);
29 tp
->tv_sec
= now
.tv_sec
;
30 tp
->tv_nsec
= now
.tv_usec
* 1000L;
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
);
39 tp
->tv_sec
= mts
.tv_sec
;
40 tp
->tv_nsec
= mts
.tv_nsec
;
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();
53 struct ceph_timespec
real_clock::to_ceph_timespec(const time_point
& t
) {
54 struct ceph_timespec ts
;
55 to_ceph_timespec(t
, ts
);
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
));
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();
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
);
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
));
80 using std::chrono::duration_cast
;
81 using std::chrono::seconds
;
82 using std::chrono::microseconds
;
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()
92 std::ostream
& operator<<(std::ostream
& m
, const timespan
& t
) {
93 return m
<< std::chrono::duration
<double>(t
).count() << "s";
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();
103 // localtime. this looks like an absolute time.
104 // aim for http://en.wikipedia.org/wiki/ISO_8601
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
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));
118 m
.unsetf(std::ios::right
);
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
);