lsst.daf.base  14.0-5-g744ff5f
DateTime.h
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 #ifndef LSST_DAF_BASE_DATETIME_H
26 #define LSST_DAF_BASE_DATETIME_H
27 
44 #include <cstdint>
45 #include <ctime>
46 #include <limits>
47 #include <sys/time.h>
48 #include <string>
49 
50 #include "lsst/pex/exceptions.h"
51 
52 // Forward declaration of the boost::serialization::access class.
53 namespace boost {
54 namespace serialization {
55  class access;
56 }} // namespace boost::serialization
57 
58 namespace lsst {
59 namespace daf {
60 namespace base {
61 
62 class DateTime {
63 public:
64  enum DateSystem { JD=0, MJD, EPOCH };
65  enum Timescale { TAI=5, UTC, TT }; // use values that do not overlap DateSystem
66  // to avoid confusing one for the other in Python
67  // an invalid DateTime has _nsec == invalid_nsecs
68  constexpr static long long invalid_nsecs = std::numeric_limits<std::int64_t>::min();
69 
73  explicit DateTime();
74 
84  explicit DateTime(long long nsecs, Timescale scale=TAI);
85 
94  explicit DateTime(double date, DateSystem system=MJD, Timescale scale=TAI);
95 
109  DateTime(int year, int month, int day, int hr, int min, int sec, Timescale scale=TAI);
110 
127  explicit DateTime(std::string const& iso8601, Timescale scale);
128 
139  long long nsecs(Timescale scale=TAI) const;
140 
150  double get(DateSystem system=MJD, Timescale scale=TAI) const;
151 
162  std::string toString(Timescale scale) const;
163 
171  struct tm gmtime(Timescale scale) const;
172 
181  struct timespec timespec(Timescale scale) const;
182 
191  struct timeval timeval(Timescale scale) const;
192 
196  bool isValid() const { return _nsecs != DateTime::invalid_nsecs; };
197 
198  bool operator==(DateTime const& rhs) const;
199 
204  static DateTime now(void);
205 
214  static void initializeLeapSeconds(std::string const& leapString);
215 
216 private:
217  long long _nsecs;
218 
220  void _assertValid() const {
221  if (!isValid()) {
222  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "DateTime not valid");
223  }
224  }
225 
233  double _getMjd(Timescale scale) const;
234 
242  double _getJd(Timescale scale) const;
243 
251  double _getEpoch(Timescale scale) const;
252 
260  void setNsecsFromMjd(double mjd, Timescale scale);
261 
269  void setNsecsFromJd(double jd, Timescale scale);
270 
278  void setNsecsFromEpoch(double epoch, Timescale scale);
279 
280  friend class boost::serialization::access;
281 
288  template <class Archive> void serialize(Archive ar, int const version) {
289  ar & _nsecs;
290  }
291 
292 };
293 
294 }}} // namespace lsst::daf::base
295 
296 #endif
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:62
Definition: DateTime.h:53
STL class.
T min(T... args)
#define LSST_EXCEPT(type,...)
bool isValid() const
Is this date valid?
Definition: DateTime.h:196