~ubuntu-branches/ubuntu/quantal/ceph/quantal

« back to all changes in this revision

Viewing changes to src/include/utime.h

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum, Clint Byrum, Micah Gersten
  • Date: 2011-02-12 22:50:26 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110212225026-yyyw4tk0msgql3ul
Tags: 0.24.2-0ubuntu1
[ Clint Byrum <clint@ubuntu.com> ]
* New upstream release. (LP: #658670, LP: #684011)
* debian/patches/fix-mkcephfs.patch: dropped (applied upstream)
* Removed .la files from libceph1-dev, libcrush1-dev and 
  librados1-dev (per Debian policy v3.9.1 10.2).
* debian/control: adding pkg-config as a build dependency
* debian/control: depend on libcrypto++-dev instead of libssl-dev
* debian/watch: added watch file

[ Micah Gersten <micahg@ubuntu.com> ]
* debian/control: add Homepage

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#include <sys/time.h>
20
20
#include <time.h>
21
21
 
 
22
#include "include/types.h"
22
23
// --------
23
24
// utime_t
24
25
 
25
26
class utime_t {
26
27
public:
27
28
  struct {
28
 
    __u32 tv_sec, tv_usec;
 
29
    __u32 tv_sec, tv_nsec;
29
30
  } tv;
30
31
 
31
32
  friend class Clock;
32
33
 
33
34
 public:
34
35
  bool is_zero() {
35
 
    return (tv.tv_sec == 0) && (tv.tv_usec == 0);
 
36
    return (tv.tv_sec == 0) && (tv.tv_nsec == 0);
36
37
  }
37
38
  void normalize() {
38
 
    if (tv.tv_usec > 1000*1000) {
39
 
      tv.tv_sec += tv.tv_usec / (1000*1000);
40
 
      tv.tv_usec %= 1000*1000;
 
39
    if (tv.tv_nsec > 1000000000ul) {
 
40
      tv.tv_sec += tv.tv_nsec / (1000000000ul);
 
41
      tv.tv_nsec %= 1000000000ul;
41
42
    }
42
43
  }
43
44
 
44
45
  // cons
45
 
  utime_t() { tv.tv_sec = 0; tv.tv_usec = 0; normalize(); }
46
 
  //utime_t(time_t s) { tv.tv_sec = s; tv.tv_usec = 0; }
47
 
  utime_t(time_t s, int u) { tv.tv_sec = s; tv.tv_usec = u; normalize(); }
 
46
  utime_t() { tv.tv_sec = 0; tv.tv_nsec = 0; }
 
47
  utime_t(time_t s, int n) { tv.tv_sec = s; tv.tv_nsec = n; normalize(); }
48
48
  utime_t(const struct ceph_timespec &v) {
49
49
    decode_timeval(&v);
50
50
  }
57
57
 
58
58
  void set_from_double(double d) { 
59
59
    tv.tv_sec = (__u32)trunc(d);
60
 
    tv.tv_usec = (__u32)((d - (double)tv.tv_sec) * (double)1000000.0);
 
60
    tv.tv_nsec = (__u32)((d - (double)tv.tv_sec) * (double)1000000000.0);
61
61
  }
62
62
 
63
63
  // accessors
64
64
  time_t        sec()  const { return tv.tv_sec; } 
65
 
  long          usec() const { return tv.tv_usec; }
66
 
  int           nsec() const { return tv.tv_usec*1000; }
 
65
  long          usec() const { return tv.tv_nsec/1000; }
 
66
  int           nsec() const { return tv.tv_nsec; }
67
67
 
68
68
  // ref accessors/modifiers
69
69
  __u32&         sec_ref()  { return tv.tv_sec; }
70
 
  __u32&         usec_ref() { return tv.tv_usec; }
 
70
  __u32&         nsec_ref() { return tv.tv_nsec; }
71
71
 
72
72
  void copy_to_timeval(struct timeval *v) const {
73
73
    v->tv_sec = tv.tv_sec;
74
 
    v->tv_usec = tv.tv_usec;
 
74
    v->tv_usec = tv.tv_nsec/1000;
75
75
  }
76
76
  void set_from_timeval(const struct timeval *v) {
77
77
    tv.tv_sec = v->tv_sec;
78
 
    tv.tv_usec = v->tv_usec;
 
78
    tv.tv_nsec = v->tv_usec*1000;
79
79
  }
80
80
 
81
81
  void encode(bufferlist &bl) const {
82
82
    ::encode(tv.tv_sec, bl);
83
 
    ::encode(tv.tv_usec, bl);
 
83
    ::encode(tv.tv_nsec, bl);
84
84
  }
85
85
  void decode(bufferlist::iterator &p) {
86
86
    ::decode(tv.tv_sec, p);
87
 
    ::decode(tv.tv_usec, p);
 
87
    ::decode(tv.tv_nsec, p);
88
88
  }
89
89
 
90
90
  void encode_timeval(struct ceph_timespec *t) const {
91
91
    t->tv_sec = tv.tv_sec;
92
 
    t->tv_nsec = tv.tv_usec*1000;
 
92
    t->tv_nsec = tv.tv_nsec;
93
93
  }
94
94
  void decode_timeval(const struct ceph_timespec *t) {
95
95
    tv.tv_sec = t->tv_sec;
96
 
    tv.tv_usec = t->tv_nsec / 1000;
 
96
    tv.tv_nsec = t->tv_nsec;
97
97
  }
98
98
 
99
99
  // cast to double
100
100
  operator double() {
101
 
    return (double)sec() + ((double)usec() / 1000000.0L);
 
101
    return (double)sec() + ((double)nsec() / 100000000.0L);
102
102
  }
103
103
  operator ceph_timespec() {
104
104
    ceph_timespec ts;
112
112
 
113
113
// arithmetic operators
114
114
inline utime_t operator+(const utime_t& l, const utime_t& r) {
115
 
  return utime_t( l.sec() + r.sec() + (l.usec()+r.usec())/1000000L,
116
 
                  (l.usec()+r.usec())%1000000L );
 
115
  return utime_t( l.sec() + r.sec() + (l.nsec()+r.nsec())/1000000000L,
 
116
                  (l.nsec()+r.nsec())%1000000000L );
117
117
}
118
118
inline utime_t& operator+=(utime_t& l, const utime_t& r) {
119
 
  l.sec_ref() += r.sec() + (l.usec()+r.usec())/1000000L;
120
 
  l.usec_ref() += r.usec();
121
 
  l.usec_ref() %= 1000000L;
 
119
  l.sec_ref() += r.sec() + (l.nsec()+r.nsec())/1000000000L;
 
120
  l.nsec_ref() += r.nsec();
 
121
  l.nsec_ref() %= 1000000000L;
122
122
  return l;
123
123
}
124
124
inline utime_t& operator+=(utime_t& l, double f) {
125
125
  double fs = trunc(f);
126
 
  double us = (f - fs) * (double)1000000.0;
 
126
  double ns = (f - fs) * (double)1000000000.0;
127
127
  l.sec_ref() += (long)fs;
128
 
  l.usec_ref() += (long)us;
 
128
  l.nsec_ref() += (long)ns;
129
129
  l.normalize();
130
130
  return l;
131
131
}
132
132
 
133
133
inline utime_t operator-(const utime_t& l, const utime_t& r) {
134
 
  return utime_t( l.sec() - r.sec() - (l.usec()<r.usec() ? 1:0),
135
 
                  l.usec() - r.usec() + (l.usec()<r.usec() ? 1000000:0) );
 
134
  return utime_t( l.sec() - r.sec() - (l.nsec()<r.nsec() ? 1:0),
 
135
                  l.nsec() - r.nsec() + (l.nsec()<r.nsec() ? 1000000000:0) );
136
136
}
137
137
inline utime_t& operator-=(utime_t& l, const utime_t& r) {
138
138
  l.sec_ref() -= r.sec();
139
 
  if (l.usec() >= r.usec())
140
 
    l.usec_ref() -= r.usec();
 
139
  if (l.nsec() >= r.nsec())
 
140
    l.nsec_ref() -= r.nsec();
141
141
  else {
142
 
    l.usec_ref() += 1000000L - r.usec();
 
142
    l.nsec_ref() += 1000000000L - r.nsec();
143
143
    l.sec_ref()--;
144
144
  }
145
145
  return l;
146
146
}
147
147
inline utime_t& operator-=(utime_t& l, double f) {
148
148
  double fs = trunc(f);
149
 
  double us = (f - fs) * (double)1000000.0;
 
149
  double ns = (f - fs) * (double)1000000000.0;
150
150
  l.sec_ref() -= (long)fs;
151
 
  if (us) {
 
151
  if (ns) {
152
152
    l.sec_ref()--;
153
 
    l.usec_ref() = 1000000 + l.usec_ref() - (long)us;
 
153
    l.nsec_ref() = 1000000000L + l.nsec_ref() - (long)ns;
154
154
  }
155
155
  l.normalize();
156
156
  return l;
160
160
// comparators
161
161
inline bool operator>(const utime_t& a, const utime_t& b)
162
162
{
163
 
  return (a.sec() > b.sec()) || (a.sec() == b.sec() && a.usec() > b.usec());
 
163
  return (a.sec() > b.sec()) || (a.sec() == b.sec() && a.nsec() > b.nsec());
164
164
}
165
165
inline bool operator<(const utime_t& a, const utime_t& b)
166
166
{
167
 
  return (a.sec() < b.sec()) || (a.sec() == b.sec() && a.usec() < b.usec());
 
167
  return (a.sec() < b.sec()) || (a.sec() == b.sec() && a.nsec() < b.nsec());
168
168
}
169
169
 
170
170
inline bool operator==(const utime_t& a, const utime_t& b)
171
171
{
172
 
  return a.sec() == b.sec() && a.usec() == b.usec();
 
172
  return a.sec() == b.sec() && a.nsec() == b.nsec();
173
173
}
174
174
inline bool operator!=(const utime_t& a, const utime_t& b)
175
175
{
176
 
  return a.sec() != b.sec() || a.usec() != b.usec();
 
176
  return a.sec() != b.sec() || a.nsec() != b.nsec();
177
177
}
178
178
 
179
179
// ostream
186
186
    out << (long)t.sec();
187
187
  } else {
188
188
    // localtime.  this looks like an absolute time.
 
189
    //  aim for http://en.wikipedia.org/wiki/ISO_8601
189
190
    struct tm bdt;
190
191
    time_t tt = t.sec();
191
192
    localtime_r(&tt, &bdt);
192
 
    out << std::setw(2) << (bdt.tm_year-100)  // 2007 -> '07'
193
 
        << '.' << std::setw(2) << (bdt.tm_mon+1)
194
 
        << '.' << std::setw(2) << bdt.tm_mday
195
 
        << '_'
 
193
    out << std::setw(4) << (bdt.tm_year+1900)  // 2007 -> '07'
 
194
        << '-' << std::setw(2) << (bdt.tm_mon+1)
 
195
        << '-' << std::setw(2) << bdt.tm_mday
 
196
        << ' '
196
197
        << std::setw(2) << bdt.tm_hour
197
198
        << ':' << std::setw(2) << bdt.tm_min
198
199
        << ':' << std::setw(2) << bdt.tm_sec;