LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
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