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 "ceph_time.h"
20 int clock_gettime(int clk_id, struct timespec *tp)
22 if (clk_id == CALENDAR_CLOCK) {
23 // gettimeofday is much faster than clock_get_time
25 int ret = gettimeofday(&now, NULL);
28 tp->tv_sec = now.tv_sec;
29 tp->tv_nsec = now.tv_usec * 1000L;
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);
38 tp->tv_sec = mts.tv_sec;
39 tp->tv_nsec = mts.tv_nsec;
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();
52 struct ceph_timespec real_clock::to_ceph_timespec(const time_point& t) {
53 struct ceph_timespec ts;
54 to_ceph_timespec(t, ts);
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));
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();
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);
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));
79 using std::chrono::duration_cast;
80 using std::chrono::seconds;
81 using std::chrono::microseconds;
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()
91 std::ostream& operator<<(std::ostream& m, const timespan& t) {
92 return m << std::chrono::duration<double>(t).count() << "s";
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();
102 // localtime. this looks like an absolute time.
103 // aim for http://en.wikipedia.org/wiki/ISO_8601
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
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));
117 m.unsetf(std::ios::right);
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);