LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
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 
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
void setNsecsFromMjd(double mjd, Timescale scale)
Set internal nanoseconds from Modified Julian Date in the specified time scale.
Definition: DateTime.cc:289
void _assertValid() const
Raise RuntimeError if DateTime is not valid.
Definition: DateTime.h:220
void setNsecsFromJd(double jd, Timescale scale)
Set internal nanoseconds from Julian Days in the specified time scale.
Definition: DateTime.cc:304
struct timeval timeval(Timescale scale) const
Get date as a timeval struct, with time in seconds and microseconds.
Definition: DateTime.cc:505
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:62
double _getEpoch(Timescale scale) const
Get date as an epoch (year) in the specified time scale.
Definition: DateTime.cc:475
Include files required for standard LSST Exception handling.
double _getJd(Timescale scale) const
Get date as Julian Days in the specified time scale.
Definition: DateTime.cc:471
long long _nsecs
TAI nanoseconds since Unix epoch.
Definition: DateTime.h:217
struct tm gmtime(Timescale scale) const
Get date as a tm struct, with truncated fractional seconds.
Definition: DateTime.cc:479
static constexpr long long invalid_nsecs
Definition: DateTime.h:68
bool isValid() const
Is this date valid?
Definition: DateTime.h:196
static void initializeLeapSeconds(std::string const &leapString)
Initialize the leap second table from USNO.
Definition: DateTime.cc:544
long long nsecs(Timescale scale=TAI) const
Get date as nanoseconds since the unix epoch.
Definition: DateTime.cc:457
std::string toString(Timescale scale) const
Get date as an ISO8601-formatted string.
Definition: DateTime.cc:514
bool operator==(DateTime const &rhs) const
Definition: DateTime.cc:529
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
struct timespec timespec(Timescale scale) const
Get date as a timespec struct, with time in seconds and nanoseconds.
Definition: DateTime.cc:496
void setNsecsFromEpoch(double epoch, Timescale scale)
Set internal nanoseconds from an epoch (year) in the specified time scale.
Definition: DateTime.cc:313
double _getMjd(Timescale scale) const
Get date as Modified Julian Days in the specified time scale.
Definition: DateTime.cc:465
DateTime()
Default constructor: construct an invalid DateTime.
Definition: DateTime.cc:317
friend class boost::serialization::access
Definition: DateTime.h:280
static DateTime now(void)
Return current time as a DateTime.
Definition: DateTime.cc:533
void serialize(Archive ar, int const version)
Serialize DateTime to/from a Boost archive.
Definition: DateTime.h:288