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.
23 #include "include/types.h"
24 #include "include/timegm.h"
25 #include "common/strtol.h"
26 #include "common/ceph_time.h"
27 #include "include/denc.h"
33 /* WARNING: If add member in utime_t, please make sure the encode/decode funtion
34 * work well. For little-endian machine, we should make sure there is no padding
35 * in 32-bit machine and 64-bit machine.
36 * You should also modify the padding_check function.
41 __u32 tv_sec, tv_nsec;
45 bool is_zero() const {
46 return (tv.tv_sec == 0) && (tv.tv_nsec == 0);
49 if (tv.tv_nsec > 1000000000ul) {
50 tv.tv_sec += tv.tv_nsec / (1000000000ul);
51 tv.tv_nsec %= 1000000000ul;
56 utime_t() { tv.tv_sec = 0; tv.tv_nsec = 0; }
57 utime_t(time_t s, int n) { tv.tv_sec = s; tv.tv_nsec = n; normalize(); }
58 utime_t(const struct ceph_timespec &v) {
61 utime_t(const struct timespec v)
64 tv.tv_nsec = v.tv_nsec;
66 explicit utime_t(const ceph::real_time& rt) {
67 ceph_timespec ts = real_clock::to_ceph_timespec(rt);
70 utime_t(const struct timeval &v) {
73 utime_t(const struct timeval *v) {
76 void to_timespec(struct timespec *ts) const {
77 ts->tv_sec = tv.tv_sec;
78 ts->tv_nsec = tv.tv_nsec;
80 void set_from_double(double d) {
81 tv.tv_sec = (__u32)trunc(d);
82 tv.tv_nsec = (__u32)((d - (double)tv.tv_sec) * (double)1000000000.0);
85 real_time to_real_time() const {
88 return ceph::real_clock::from_ceph_timespec(ts);
92 time_t sec() const { return tv.tv_sec; }
93 long usec() const { return tv.tv_nsec/1000; }
94 int nsec() const { return tv.tv_nsec; }
96 // ref accessors/modifiers
97 __u32& sec_ref() { return tv.tv_sec; }
98 __u32& nsec_ref() { return tv.tv_nsec; }
100 uint64_t to_nsec() const {
101 return (uint64_t)tv.tv_nsec + (uint64_t)tv.tv_sec * 1000000000ull;
103 uint64_t to_msec() const {
104 return (uint64_t)tv.tv_nsec / 1000000ull + (uint64_t)tv.tv_sec * 1000ull;
107 void copy_to_timeval(struct timeval *v) const {
108 v->tv_sec = tv.tv_sec;
109 v->tv_usec = tv.tv_nsec/1000;
111 void set_from_timeval(const struct timeval *v) {
112 tv.tv_sec = v->tv_sec;
113 tv.tv_nsec = v->tv_usec*1000;
115 void padding_check() {
121 "utime_t have padding");
123 void encode(bufferlist &bl) const {
124 #if defined(CEPH_LITTLE_ENDIAN)
125 bl.append((char *)(this), sizeof(__u32) + sizeof(__u32));
127 ::encode(tv.tv_sec, bl);
128 ::encode(tv.tv_nsec, bl);
131 void decode(bufferlist::iterator &p) {
132 #if defined(CEPH_LITTLE_ENDIAN)
133 p.copy(sizeof(__u32) + sizeof(__u32), (char *)(this));
135 ::decode(tv.tv_sec, p);
136 ::decode(tv.tv_nsec, p);
140 DENC(utime_t, v, p) {
141 denc(v.tv.tv_sec, p);
142 denc(v.tv.tv_nsec, p);
146 void encode_timeval(struct ceph_timespec *t) const {
147 t->tv_sec = tv.tv_sec;
148 t->tv_nsec = tv.tv_nsec;
150 void decode_timeval(const struct ceph_timespec *t) {
151 tv.tv_sec = t->tv_sec;
152 tv.tv_nsec = t->tv_nsec;
155 utime_t round_to_minute() {
158 localtime_r(&tt, &bdt);
161 return utime_t(tt, 0);
164 utime_t round_to_hour() {
167 localtime_r(&tt, &bdt);
171 return utime_t(tt, 0);
175 operator double() const {
176 return (double)sec() + ((double)nsec() / 1000000000.0L);
178 operator ceph_timespec() const {
188 nanosleep(&ts, NULL);
192 ostream& gmtime(ostream& out) const {
193 out.setf(std::ios::right);
194 char oldfill = out.fill();
196 if (sec() < ((time_t)(60*60*24*365*10))) {
197 // raw seconds. this looks like a relative time.
198 out << (long)sec() << "." << std::setw(6) << usec();
200 // localtime. this looks like an absolute time.
201 // aim for http://en.wikipedia.org/wiki/ISO_8601
205 out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07'
206 << '-' << std::setw(2) << (bdt.tm_mon+1)
207 << '-' << std::setw(2) << bdt.tm_mday
209 << std::setw(2) << bdt.tm_hour
210 << ':' << std::setw(2) << bdt.tm_min
211 << ':' << std::setw(2) << bdt.tm_sec;
212 out << "." << std::setw(6) << usec();
216 out.unsetf(std::ios::right);
221 ostream& gmtime_nsec(ostream& out) const {
222 out.setf(std::ios::right);
223 char oldfill = out.fill();
225 if (sec() < ((time_t)(60*60*24*365*10))) {
226 // raw seconds. this looks like a relative time.
227 out << (long)sec() << "." << std::setw(6) << usec();
229 // localtime. this looks like an absolute time.
230 // aim for http://en.wikipedia.org/wiki/ISO_8601
234 out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07'
235 << '-' << std::setw(2) << (bdt.tm_mon+1)
236 << '-' << std::setw(2) << bdt.tm_mday
238 << std::setw(2) << bdt.tm_hour
239 << ':' << std::setw(2) << bdt.tm_min
240 << ':' << std::setw(2) << bdt.tm_sec;
241 out << "." << std::setw(9) << nsec();
245 out.unsetf(std::ios::right);
250 ostream& asctime(ostream& out) const {
251 out.setf(std::ios::right);
252 char oldfill = out.fill();
254 if (sec() < ((time_t)(60*60*24*365*10))) {
255 // raw seconds. this looks like a relative time.
256 out << (long)sec() << "." << std::setw(6) << usec();
258 // localtime. this looks like an absolute time.
259 // aim for http://en.wikipedia.org/wiki/ISO_8601
265 asctime_r(&bdt, buf);
266 int len = strlen(buf);
267 if (buf[len - 1] == '\n')
272 out.unsetf(std::ios::right);
276 ostream& localtime(ostream& out) const {
277 out.setf(std::ios::right);
278 char oldfill = out.fill();
280 if (sec() < ((time_t)(60*60*24*365*10))) {
281 // raw seconds. this looks like a relative time.
282 out << (long)sec() << "." << std::setw(6) << usec();
284 // localtime. this looks like an absolute time.
285 // aim for http://en.wikipedia.org/wiki/ISO_8601
288 localtime_r(&tt, &bdt);
289 out << std::setw(4) << (bdt.tm_year+1900) // 2007 -> '07'
290 << '-' << std::setw(2) << (bdt.tm_mon+1)
291 << '-' << std::setw(2) << bdt.tm_mday
293 << std::setw(2) << bdt.tm_hour
294 << ':' << std::setw(2) << bdt.tm_min
295 << ':' << std::setw(2) << bdt.tm_sec;
296 out << "." << std::setw(6) << usec();
297 //out << '_' << bdt.tm_zone;
300 out.unsetf(std::ios::right);
304 int sprintf(char *out, int outlen) const {
307 localtime_r(&tt, &bdt);
309 return ::snprintf(out, outlen,
310 "%04d-%02d-%02d %02d:%02d:%02d.%06ld",
311 bdt.tm_year + 1900, bdt.tm_mon + 1, bdt.tm_mday,
312 bdt.tm_hour, bdt.tm_min, bdt.tm_sec, usec());
315 static int snprintf(char *out, int outlen, time_t tt) {
317 localtime_r(&tt, &bdt);
319 return ::snprintf(out, outlen,
320 "%04d-%02d-%02d %02d:%02d:%02d",
321 bdt.tm_year + 1900, bdt.tm_mon + 1, bdt.tm_mday,
322 bdt.tm_hour, bdt.tm_min, bdt.tm_sec);
325 static int parse_date(const string& date, uint64_t *epoch, uint64_t *nsec,
326 string *out_date=NULL, string *out_time=NULL) {
328 memset(&tm, 0, sizeof(tm));
333 const char *p = strptime(date.c_str(), "%Y-%m-%d", &tm);
337 p = strptime(p, " %H:%M:%S", &tm);
340 if (nsec && *p == '.') {
343 char buf[10]; /* 9 digit + null termination */
344 for (i = 0; (i < sizeof(buf) - 1) && isdigit(*p); ++i, ++p) {
347 for (; i < sizeof(buf) - 1; ++i) {
352 *nsec = (uint64_t)strict_strtol(buf, 10, &err);
360 int r = sscanf(date.c_str(), "%d.%d", &sec, &usec);
369 *nsec = (uint64_t)usec * 1000;
372 time_t t = internal_timegm(&tm);
374 *epoch = (uint64_t)t;
378 strftime(buf, sizeof(buf), "%F", &tm);
383 strftime(buf, sizeof(buf), "%T", &tm);
390 WRITE_CLASS_ENCODER(utime_t)
391 WRITE_CLASS_DENC(utime_t)
394 // arithmetic operators
395 inline utime_t operator+(const utime_t& l, const utime_t& r) {
396 return utime_t( l.sec() + r.sec() + (l.nsec()+r.nsec())/1000000000L,
397 (l.nsec()+r.nsec())%1000000000L );
399 inline utime_t& operator+=(utime_t& l, const utime_t& r) {
400 l.sec_ref() += r.sec() + (l.nsec()+r.nsec())/1000000000L;
401 l.nsec_ref() += r.nsec();
402 l.nsec_ref() %= 1000000000L;
405 inline utime_t& operator+=(utime_t& l, double f) {
406 double fs = trunc(f);
407 double ns = (f - fs) * (double)1000000000.0;
408 l.sec_ref() += (long)fs;
409 l.nsec_ref() += (long)ns;
414 inline utime_t operator-(const utime_t& l, const utime_t& r) {
415 return utime_t( l.sec() - r.sec() - (l.nsec()<r.nsec() ? 1:0),
416 l.nsec() - r.nsec() + (l.nsec()<r.nsec() ? 1000000000:0) );
418 inline utime_t& operator-=(utime_t& l, const utime_t& r) {
419 l.sec_ref() -= r.sec();
420 if (l.nsec() >= r.nsec())
421 l.nsec_ref() -= r.nsec();
423 l.nsec_ref() += 1000000000L - r.nsec();
428 inline utime_t& operator-=(utime_t& l, double f) {
429 double fs = trunc(f);
430 double ns = (f - fs) * (double)1000000000.0;
431 l.sec_ref() -= (long)fs;
435 l.nsec_ref() = 1000000000L + l.nsec_ref() - nsl;
443 inline bool operator>(const utime_t& a, const utime_t& b)
445 return (a.sec() > b.sec()) || (a.sec() == b.sec() && a.nsec() > b.nsec());
447 inline bool operator<=(const utime_t& a, const utime_t& b)
449 return !(operator>(a, b));
451 inline bool operator<(const utime_t& a, const utime_t& b)
453 return (a.sec() < b.sec()) || (a.sec() == b.sec() && a.nsec() < b.nsec());
455 inline bool operator>=(const utime_t& a, const utime_t& b)
457 return !(operator<(a, b));
460 inline bool operator==(const utime_t& a, const utime_t& b)
462 return a.sec() == b.sec() && a.nsec() == b.nsec();
464 inline bool operator!=(const utime_t& a, const utime_t& b)
466 return a.sec() != b.sec() || a.nsec() != b.nsec();
473 inline std::ostream& operator<<(std::ostream& out, const utime_t& t)
475 return t.localtime(out);