Class for handling dates/times, including MJD, UTC, and TAI.
More...
#include <DateTime.h>
|
| DateTime () |
| Default constructor: construct an invalid DateTime. More...
|
|
| DateTime (long long nsecs, Timescale scale=TAI) |
| Construct a DateTime from nanoseconds since the unix epoch. More...
|
|
| DateTime (double date, DateSystem system=MJD, Timescale scale=TAI) |
| Construct a DateTime from a double in the specified system and scale. More...
|
|
| DateTime (int year, int month, int day, int hr, int min, int sec, Timescale scale=TAI) |
| Construct a DateTime from year, month, day, etc. More...
|
|
| DateTime (std::string const &iso8601, Timescale scale) |
| Construct a DateTime from an ISO8601 string. More...
|
|
long long | nsecs (Timescale scale=TAI) const |
| Get date as nanoseconds since the unix epoch. More...
|
|
double | get (DateSystem system=MJD, Timescale scale=TAI) const |
| Get date as a double in a specified representation, such as MJD. More...
|
|
std::string | toString (Timescale scale) const |
| Get date as an ISO8601-formatted string. More...
|
|
struct tm | gmtime (Timescale scale) const |
| Get date as a tm struct, with truncated fractional seconds. More...
|
|
struct timespec | timespec (Timescale scale) const |
| Get date as a timespec struct, with time in seconds and nanoseconds. More...
|
|
struct timeval | timeval (Timescale scale) const |
| Get date as a timeval struct, with time in seconds and microseconds. More...
|
|
bool | isValid () const |
| Is this date valid? More...
|
|
bool | operator== (DateTime const &rhs) const |
|
std::size_t | hash_value () const noexcept |
| Return a hash of this object. More...
|
|
Class for handling dates/times, including MJD, UTC, and TAI.
Definition at line 64 of file DateTime.h.
◆ DateSystem
◆ Timescale
◆ DateTime() [1/5]
lsst::daf::base::DateTime::DateTime |
( |
| ) |
|
|
explicit |
◆ DateTime() [2/5]
lsst::daf::base::DateTime::DateTime |
( |
long long |
nsecs, |
|
|
Timescale |
scale = TAI |
|
) |
| |
|
explicit |
Construct a DateTime from nanoseconds since the unix epoch.
- Parameters
-
[in] | nsecs | integer nanoseconds since the unix epoch; if nsecs == DateTime.invalid_nsecs then the DateTime is invalid, regardless of scale |
[in] | scale | time scale of input (TAI, TT or UTC, default TAI). |
- Exceptions
-
Definition at line 308 of file DateTime.cc.
◆ DateTime() [3/5]
Construct a DateTime from a double in the specified system and scale.
- Parameters
-
[in] | date | specified date |
[in] | system | time system of input (JD, MJD, or EPOCH) |
[in] | scale | time scale of input (TAI, TT or UTC, default TAI). |
- Exceptions
-
Definition at line 310 of file DateTime.cc.
313 setNsecsFromMjd(date,
scale);
316 setNsecsFromJd(date,
scale);
319 setNsecsFromEpoch(date,
scale);
◆ DateTime() [4/5]
lsst::daf::base::DateTime::DateTime |
( |
int |
year, |
|
|
int |
month, |
|
|
int |
day, |
|
|
int |
hr, |
|
|
int |
min, |
|
|
int |
sec, |
|
|
Timescale |
scale = TAI |
|
) |
| |
Construct a DateTime from year, month, day, etc.
(tm struct fields)
- Parameters
-
[in] | year | year; must be in the range [1902, 2261], inclusive. |
[in] | month | month number, where January = 1 |
[in] | day | day of the month (1 to 31). |
[in] | hr | hour (0 to 23) |
[in] | min | minute (0 to 59) |
[in] | sec | integer seconds (0 to 60) |
[in] | scale | time scale of input (TAI, TT or UTC, default TAI). |
- Exceptions
-
Definition at line 327 of file DateTime.cc.
328 int const minYear = 1902;
329 int const maxYear = 2261;
330 if ((year < minYear) || (year > maxYear)) {
333 (
boost::format(
"Year = %d out of range [%04d, %04d]") % year % minYear % maxYear).str());
337 tm.tm_year = year - 1900;
338 tm.tm_mon = month - 1;
352 time_t secs = timegm(&tm);
370 if (timegm(&tm) != -1) {
376 (
boost::format(
"Unconvertible date: %04d-%02d-%02dT%02d:%02d:%02d") % year %
377 month % day % hr %
min % sec)
382 _nsecs = nsecAnyToTai(secs * LL_NSEC_PER_SEC,
scale);
◆ DateTime() [5/5]
Construct a DateTime from an ISO8601 string.
Accepts a restricted subset of ISO8601: yyyy-mm-ddThh:mm:ss.nnnnnnnnnZ where:
- the final Z is required for UTC and prohibited for TAI or TT
- the - and : separators are optional
- the decimal point and fractional seconds are optional
- the decimal point may be a comma
- Parameters
-
[in] | iso8601 | ISO8601 string representation of date and time |
[in] | scale | time scale of input (TAI, TT or UTC, default TAI). |
- Exceptions
-
Definition at line 385 of file DateTime.cc.
390 "(\\d{4})-?(\\d{2})-?(\\d{2})"
392 "(\\d{2}):?(\\d{2}):?(\\d{2})"
398 "(\\d{4})-?(\\d{2})-?(\\d{2})"
400 "(\\d{2}):?(\\d{2}):?(\\d{2})"
403 boost::smatch matches;
409 DateTime dt(
atoi(matches.str(1).c_str()),
atoi(matches.str(2).c_str()),
atoi(matches.str(3).c_str()),
410 atoi(matches.str(4).c_str()),
atoi(matches.str(5).c_str()),
atoi(matches.str(6).c_str()),
414 if (matches[7].matched) {
416 int places = frac.
size();
◆ get()
Get date as a double in a specified representation, such as MJD.
- Returns
- the date in the required system, for the requested scale
- Parameters
-
[in] | system | desired time system (JD, MJD, or EPOCH) |
[in] | scale | desired time scale (TAI, TT or UTC) |
- Exceptions
-
Definition at line 429 of file DateTime.cc.
433 return _getMjd(
scale);
436 return _getJd(
scale);
439 return _getEpoch(
scale);
◆ gmtime()
struct tm lsst::daf::base::DateTime::gmtime |
( |
Timescale |
scale | ) |
const |
Get date as a tm struct, with truncated fractional seconds.
- Parameters
-
[in] | scale | desired time scale (TAI, TT or UTC) |
- Returns
- date as a tm struct
- Exceptions
-
Definition at line 463 of file DateTime.cc.
468 long long nsecs = nsecTaiToAny(_nsecs,
scale);
470 long long frac =
nsecs % LL_NSEC_PER_SEC;
471 if (
nsecs < 0 && frac < 0) {
472 nsecs -= LL_NSEC_PER_SEC + frac;
476 time_t secs =
static_cast<time_t
>(
nsecs / LL_NSEC_PER_SEC);
477 gmtime_r(&secs, &gmt);
◆ hash_value()
std::size_t lsst::daf::base::DateTime::hash_value |
( |
| ) |
const |
|
noexcept |
Return a hash of this object.
Definition at line 515 of file DateTime.cc.
◆ initializeLeapSeconds()
void lsst::daf::base::DateTime::initializeLeapSeconds |
( |
std::string const & |
leapString | ) |
|
|
static |
Initialize the leap second table from USNO.
The data can be found here: http://maia.usno.navy.mil/ser7/tai-utc.dat and is saved in DateTime.cc as static constant leapString
.
- Parameters
-
leapString | leap second table from USNO as a single multiline string. |
Definition at line 527 of file DateTime.cc.
529 leapSecTable.clear();
531 "^\\d{4}.*?=JD\\s*([\\d.]+)\\s+TAI-UTC=\\s+([\\d.]+)\\s+S"
532 " \\+ \\(MJD - ([\\d.]+)\\) X ([\\d.]+)\\s*S$");
533 for (boost::cregex_iterator i = make_regex_iterator(leapString.
c_str(), re);
534 i != boost::cregex_iterator(); ++i) {
535 double mjdUtc =
strtod((*i)[1].
first, 0) - MJD_TO_JD;
539 l.whenUtc =
static_cast<long long>((mjdUtc - EPOCH_IN_MJD) * NSEC_PER_DAY);
540 l.whenTai = l.whenUtc +
static_cast<long long>(1.0e9 * (l.offset + (mjdUtc - l.mjdRef) * l.drift));
541 leapSecTable.push_back(l);
◆ isValid()
bool lsst::daf::base::DateTime::isValid |
( |
| ) |
const |
|
inline |
◆ now()
DateTime lsst::daf::base::DateTime::now |
( |
void |
| ) |
|
|
static |
Return current time as a DateTime.
Assumes the system clock keeps UTC, as almost all computers do.
Definition at line 517 of file DateTime.cc.
519 int ret = gettimeofday(&tv, 0);
523 long long nsecs = tv.tv_sec * LL_NSEC_PER_SEC + tv.tv_usec * 1000
LL;
◆ nsecs()
long long lsst::daf::base::DateTime::nsecs |
( |
Timescale |
scale = TAI | ) |
const |
Get date as nanoseconds since the unix epoch.
- Note
- If the DateTime is invalid then the returned value is DateTime.invalid_nsecs, regardless of scale.
- Parameters
-
[in] | scale | desired time scale (TAI, TT or UTC) |
- Returns
- the date as nanoseconds since the unix epoch in the specified time scale
- Exceptions
-
Definition at line 447 of file DateTime.cc.
452 return nsecTaiToAny(_nsecs,
scale);
◆ operator==()
bool lsst::daf::base::DateTime::operator== |
( |
DateTime const & |
rhs | ) |
const |
Definition at line 513 of file DateTime.cc.
513 {
return _nsecs == rhs._nsecs; }
◆ timespec()
struct timespec lsst::daf::base::DateTime::timespec |
( |
Timescale |
scale | ) |
const |
Get date as a timespec struct, with time in seconds and nanoseconds.
- Parameters
-
[in] | scale | Desired time scale (TAI, TT or UTC) |
- Returns
- date as a timespec struct
- Exceptions
-
Definition at line 463 of file DateTime.cc.
484 long long nsecs = nsecTaiToAny(_nsecs,
scale);
485 ts.tv_sec =
static_cast<time_t
>(
nsecs / LL_NSEC_PER_SEC);
486 ts.tv_nsec =
static_cast<int>(
nsecs % LL_NSEC_PER_SEC);
◆ timeval()
struct timeval lsst::daf::base::DateTime::timeval |
( |
Timescale |
scale | ) |
const |
Get date as a timeval struct, with time in seconds and microseconds.
- Parameters
-
[in] | scale | desired time scale (TAI, TT or UTC) |
- Returns
- date as a timeval struct
- Exceptions
-
Definition at line 463 of file DateTime.cc.
493 long long nsecs = nsecTaiToAny(_nsecs,
scale);
494 tv.tv_sec =
static_cast<time_t
>(
nsecs / LL_NSEC_PER_SEC);
495 tv.tv_usec =
static_cast<int>((
nsecs % LL_NSEC_PER_SEC) / 1000);
◆ toString()
Get date as an ISO8601-formatted string.
The returned format is: yyyy-mm-ddThh:mm:ss.sssssssssZ where the final Z is only present if scale is UTC
- Parameters
-
[in] | scale | Desired time scale (TAI, TT or UTC). |
- Exceptions
-
Definition at line 499 of file DateTime.cc.
503 long long fracnsecs = nsecTaiToAny(_nsecs,
scale) % LL_NSEC_PER_SEC;
505 fracnsecs += LL_NSEC_PER_SEC;
507 auto fmtStr =
scale ==
UTC ?
"%04d-%02d-%02dT%02d:%02d:%02d.%09dZ" :
"%04d-%02d-%02dT%02d:%02d:%02d.%09d";
508 return (
boost::format(fmtStr) % (gmt.tm_year + 1900) % (gmt.tm_mon + 1) % gmt.tm_mday % gmt.tm_hour %
509 gmt.tm_min % gmt.tm_sec % fracnsecs)
◆ boost::serialization::access
friend class boost::serialization::access |
|
friend |
◆ invalid_nsecs
The documentation for this class was generated from the following files:
- /j/snowflake/release/lsstsw/stack/1a1d771/Linux64/daf_base/20.0.0/include/lsst/daf/base/DateTime.h
- /j/snowflake/release/lsstsw/stack/1a1d771/Linux64/daf_base/20.0.0/src/DateTime.cc
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Reports arguments outside the domain of an operation.