LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Time.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
33 #include <sys/time.h> // for ::gettimeofday()
34 #if LSST_AP_HAVE_CLOCK_GETTIME
35 # include <time.h> // for ::clock_gettime()
36 #endif
37 
38 #include <ostream>
39 #include <sstream>
40 
41 #include <boost/numeric/conversion/converter.hpp>
42 
43 #include <lsst/ap/Time.h>
44 
45 
46 namespace lsst {
47 namespace ap {
48 
49 
50 // -- TimeSpec ----------------
51 
53 typedef boost::numeric::converter<
54  time_t,
55  double,
56  boost::numeric::conversion_traits<time_t, double>,
57  boost::numeric::def_overflow_handler,
58  boost::numeric::Floor<double>
59 > DoubleToTime;
61 
62 TimeSpec::TimeSpec(double const seconds) {
63  time_t const sec = DoubleToTime::convert(seconds);
64  tv_sec = sec;
65  tv_nsec = static_cast<long>((seconds - static_cast<double>(sec))*1e9);
66 }
67 
68 
69 TimeSpec & TimeSpec::operator=(double const seconds) {
70  time_t const sec = DoubleToTime::convert(seconds);
71  tv_sec = sec;
72  tv_nsec = static_cast<long>((seconds - static_cast<double>(sec))*1e9);
73  return *this;
74 }
75 
76 
77 TimeSpec & TimeSpec::operator+=(double const seconds) {
78  time_t const sec = DoubleToTime::convert(seconds);
79  tv_sec += sec;
80  tv_nsec += static_cast<long>((seconds - static_cast<double>(sec))*1e9);
81  if (tv_nsec >= 1000000000l) {
82  tv_nsec -= 1000000000l;
83  ++tv_sec;
84  }
85  return *this;
86 }
87 
88 
94 #if LSST_AP_HAVE_CLOCK_GETTIME
95 # if defined(CLOCK_MONOTONIC)
96  clock_gettime(CLOCK_MONOTONIC, this);
97 # elif defined(CLOCK_HIGHRES)
98  clock_gettime(CLOCK_HIGHRES, this);
99 # else
100  clock_gettime(CLOCK_REALTIME, this); // Use system clock if nothing else is available
101 # endif
102  return *this;
103 #else
104  return systemTime();
105 #endif
106 }
107 
108 
114  ::timeval tv;
115  ::gettimeofday(&tv, 0);
116  tv_sec = tv.tv_sec;
117  tv_nsec = tv.tv_usec*1000l;
118  return *this;
119 }
120 
121 
122 // -- Stopwatch ----------------
123 
124 Stopwatch::Stopwatch(bool const go) : _ts(), _stopped(true) {
125  if (go) {
126  start();
127  }
128 }
129 
130 
132  if (_stopped) {
133  _ts.now();
134  _stopped = false;
135  }
136 }
137 
138 
140  if (!_stopped) {
141  TimeSpec t;
142  t.now() -= _ts;
143  _ts = t;
144  _stopped = true;
145  }
146 }
147 
148 
149 std::string const Stopwatch::toString() const {
150  std::ostringstream os;
151  os << *this;
152  return os.str();
153 }
154 
155 
156 double Stopwatch::seconds() const {
157  TimeSpec t;
158  if (_stopped) {
159  t = _ts;
160  } else {
161  t.now();
162  t -= _ts;
163  }
164  return t.seconds();
165 }
166 
167 
168 std::ostream & operator<<(std::ostream & os, Stopwatch const & watch) {
169  TimeSpec t;
170  if (watch._stopped) {
171  t = watch._ts;
172  } else {
173  t.now();
174  t -= watch._ts;
175  }
176  long nsec = t.tv_nsec % 1000000000l;
177  long sec = t.tv_sec + t.tv_nsec/1000000000l;
178  long min = (sec/60) % 60;
179  long hour = sec/3600;
180 
181  double s = static_cast<double>(sec % 60) + 1e-9 * static_cast<double>(nsec);
182 
183  if (hour > 0) {
184  os << hour << "hr ";
185  }
186  if (min > 0) {
187  os << min << "min ";
188  }
189  os << s << "sec";
190  return os;
191 }
192 
193 
194 }} // end of namespace lsst::ap
Wraps the C library timespec struct.
Definition: Time.h:48
std::string const toString() const
Definition: Time.cc:149
TimeSpec & now()
Definition: Time.cc:93
double min
Definition: attributes.cc:216
Stopwatch(bool const go=true)
Definition: Time.cc:124
TimeSpec & operator=(double const seconds)
Definition: Time.cc:69
TimeSpec & systemTime()
Definition: Time.cc:113
Convenience wrapper for the C library timespec struct and a simple profiling class.
double seconds() const
Definition: Time.h:87
Utility class for profiling.
Definition: Time.h:98
TimeSpec _ts
Definition: Time.h:114
TimeSpec & operator+=(TimeSpec const &ts)
Definition: Time.h:59
double seconds() const
Definition: Time.cc:156
std::ostream & operator<<(std::ostream &os, Stopwatch const &watch)
Definition: Time.cc:168