LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 <ctime>
45 #include <sys/time.h>
46 #include <string>
47 
48 // Forward declaration of the boost::serialization::access class.
49 namespace boost {
50 namespace serialization {
51  class access;
52 }} // namespace boost::serialization
53 
54 namespace lsst {
55 namespace daf {
56 namespace base {
57 
58 class DateTime {
59 public:
60  enum Timescale { TAI, UTC, TT };
61  enum DateSystem { JD, MJD, EPOCH };
62  explicit DateTime(long long nsecs = 0LL, Timescale scale = TAI);
63  explicit DateTime(double date, DateSystem system = MJD, Timescale scale = TAI);
64  DateTime(int year, int month, int day, int hr, int min, int sec,
65  Timescale scale = TAI);
66  explicit DateTime(std::string const& iso8601);
67 
68  long long nsecs(Timescale scale = TAI) const;
69  double get(DateSystem system = MJD, Timescale scale = TAI) const;
70  std::string toString(void) const;
71 
72  struct tm gmtime(void) const; // Always UTC
73  struct timespec timespec(void) const; // Always UTC
74  struct timeval timeval(void) const; // Always UTC
75 
76  bool operator==(DateTime const& rhs) const;
77 
78  static DateTime now(void);
79 
80  static void initializeLeapSeconds(std::string const& leapString);
81 
82 private:
83  long long _nsecs;
84 
85  double _getMjd(Timescale scale) const;
86  double _getJd(Timescale scale) const;
87  double _getEpoch(Timescale scale) const;
88 
89  void setNsecsFromMjd(double mjd, Timescale scale);
90  void setNsecsFromJd(double jd, Timescale scale);
91  void setNsecsFromEpoch(double epoch, Timescale scale);
92 
98  template <class Archive> void serialize(Archive ar, int const version) {
99  ar & _nsecs;
100  }
101 
102 };
103 
104 }}} // namespace lsst::daf::base
105 
106 #endif
static DateTime now(void)
Definition: DateTime.cc:554
double _getEpoch(Timescale scale) const
Definition: DateTime.cc:482
void setNsecsFromEpoch(double epoch, Timescale scale)
a function to convert epoch to internal nsecs
Definition: DateTime.cc:281
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58
double _getJd(Timescale scale) const
Definition: DateTime.cc:474
bool operator==(DateTime const &rhs) const
Definition: DateTime.cc:547
void setNsecsFromJd(double jd, Timescale scale)
a function to convert JD to internal nsecs
Definition: DateTime.cc:272
void serialize(Archive ar, int const version)
Definition: DateTime.h:98
void setNsecsFromMjd(double mjd, Timescale scale)
a function to convert MJD to interal nsecs
Definition: DateTime.cc:246
static void initializeLeapSeconds(std::string const &leapString)
Definition: DateTime.cc:568
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:441
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:83
double _getMjd(Timescale scale) const
Definition: DateTime.cc:456
struct tm gmtime(void) const
Definition: DateTime.cc:492
struct timespec timespec(void) const
Definition: DateTime.cc:511
friend class boost::serialization::access
Definition: DateTime.h:93
std::string toString(void) const
Definition: DateTime.cc:533
DateTime(long long nsecs=0LL, Timescale scale=TAI)
Definition: DateTime.cc:291
struct timeval timeval(void) const
Definition: DateTime.cc:522