LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
lsst.daf.base::DateTime Class Reference

Class for handling dates/times, including MJD, UTC, and TAI. More...

#include <DateTime.h>

Public Types

enum  Timescale { TAI, UTC, TT }
 
enum  DateSystem { JD, MJD, EPOCH }
 

Public Member Functions

 DateTime (long long nsecs=0LL, Timescale scale=TAI)
 
 DateTime (double date, DateSystem system=MJD, Timescale scale=TAI)
 
 DateTime (int year, int month, int day, int hr, int min, int sec, Timescale scale=TAI)
 
 DateTime (std::string const &iso8601)
 
long long nsecs (Timescale scale=TAI) const
 
double mjd (Timescale scale=TAI) const
 
double get (DateSystem system=MJD, Timescale scale=TAI) const
 
std::string toString (void) const
 
struct tm gmtime (void) const
 
struct timespec timespec (void) const
 
struct timeval timeval (void) const
 
bool operator== (DateTime const &rhs) const
 

Static Public Member Functions

static DateTime now (void)
 
static void initializeLeapSeconds (std::string const &leapString)
 

Private Member Functions

double _getMjd (Timescale scale) const
 
double _getJd (Timescale scale) const
 
double _getEpoch (Timescale scale) const
 
void setNsecsFromMjd (double mjd, Timescale scale)
 a function to convert MJD to interal nsecs More...
 
void setNsecsFromJd (double jd, Timescale scale)
 a function to convert JD to internal nsecs More...
 
void setNsecsFromEpoch (double epoch, Timescale scale)
 a function to convert epoch to internal nsecs More...
 
template<class Archive >
void serialize (Archive ar, int const version)
 

Private Attributes

long long _nsecs
 Nanoseconds since Unix epoch. More...
 

Friends

class boost::serialization::access
 

Detailed Description

Class for handling dates/times, including MJD, UTC, and TAI.

Definition at line 58 of file DateTime.h.

Member Enumeration Documentation

Enumerator
JD 
MJD 
EPOCH 

Definition at line 61 of file DateTime.h.

Enumerator
TAI 
UTC 
TT 

Definition at line 60 of file DateTime.h.

Constructor & Destructor Documentation

lsst.daf.base::DateTime::DateTime ( long long  nsecs = 0LL,
Timescale  scale = TAI 
)
explicit

Constructor.

Parameters
[in]nsecsNumber of nanoseconds since the epoch.
[in]scaleTimescale of input (TAI or UTC, default TAI).

Definition at line 290 of file DateTime.cc.

290  : _nsecs(nsecs) {
291  if (scale == UTC) {
292  _nsecs = utcToTai(_nsecs);
293  } else if (scale == TT) {
294  _nsecs -= TT_MINUS_TAI_NSECS;
295  }
296 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
lsst.daf.base::DateTime::DateTime ( double  date,
DateSystem  system = MJD,
Timescale  scale = TAI 
)
explicit

Constructor.

Parameters
[in]dateDate.
[in]systemThe requested date system (JD, MJD, or Julian epoch)
[in]scaleTimescale of input (TAI or UTC, default TAI).

Definition at line 303 of file DateTime.cc.

303  {
304  switch (system) {
305  case MJD:
306  setNsecsFromMjd(date, scale);
307  break;
308  case JD:
309  setNsecsFromJd(date, scale);
310  break;
311  case EPOCH:
312  setNsecsFromEpoch(date, scale);
313  break;
314  default:
315  throw LSST_EXCEPT(pexEx::InvalidParameterError, "DateSystem must be MJD, JD, or EPOCH.");
316  break;
317  }
318 }
void setNsecsFromJd(double jd, Timescale scale)
a function to convert JD to internal nsecs
Definition: DateTime.cc:271
void setNsecsFromEpoch(double epoch, Timescale scale)
a function to convert epoch to internal nsecs
Definition: DateTime.cc:280
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
void setNsecsFromMjd(double mjd, Timescale scale)
a function to convert MJD to interal nsecs
Definition: DateTime.cc:245
lsst.daf.base::DateTime::DateTime ( int  year,
int  month,
int  day,
int  hr,
int  min,
int  sec,
Timescale  scale = TAI 
)

Constructor.

Parameters
[in]yearYear number.
[in]monthMonth number (Jan = 1).
[in]dayDay number (1 to 31).
[in]hrHour number (0 to 23).
[in]minMinute number (0 to 59).
[in]secSecond number (0 to 60).
[in]scaleTimescale of input (TAI or UTC, default TAI).

Definition at line 331 of file DateTime.cc.

332  {
333 
334 
335  struct tm tm;
336  tm.tm_year = year - 1900;
337  tm.tm_mon = month - 1;
338  tm.tm_mday = day;
339  tm.tm_hour = hr;
340  tm.tm_min = min;
341  tm.tm_sec = sec;
342  tm.tm_wday = 0;
343  tm.tm_yday = 0;
344  tm.tm_isdst = 0;
345  tm.tm_gmtoff = 0;
346 
347  // Convert to seconds since the epoch, correcting to UTC.
348  // Although timegm() is non-standard, it is a commonly-supported
349  // extension and is much safer/more reliable than mktime(3) in that
350  // it doesn't try to deal with the anomalies of local time zones.
351  time_t secs = timegm(&tm);
352 
353  // long long nsecs will blow out beyond sep 21, 1677 0:00:00, and apr 12 2262 00:00:00
354  // (refering to the values of EPOCH_IN_MJD +/- MAX_DAYS ... exceeds 64 bits.)
355  // However, a tm struct is only 32 bits, and saturates at:
356  // low end - Dec 13 1901, 20:45:52
357  // hi end - Jan 19 2038, 03:14:07
358 
359  if (secs == -1) {
360  throw LSST_EXCEPT(
361  lsst::pex::exceptions::DomainError,
362  (boost::format("Unconvertible date: %04d-%02d-%02dT%02d:%02d:%02d")
363  % year % month % day % hr % min % sec).str());
364  }
365 
366  _nsecs = secs * LL_NSEC_PER_SEC;
367  if (scale == UTC) {
368  _nsecs = utcToTai(_nsecs);
369  } else if (scale == TT) {
370  _nsecs -= TT_MINUS_TAI_NSECS;
371  }
372 
373 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
double min
Definition: attributes.cc:216
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
lsst.daf.base::DateTime::DateTime ( std::string const &  iso8601)
explicit

Constructor. Accepts a restricted subset of ISO8601: yyyy-mm-ddThh:mm:ss.nnnnnnnnnZ where the - and : separators are optional, the fractional seconds are also optional, and the decimal point may be a comma.

Parameters
[in]iso8601ISO8601 representation of date and time. Must be UTC.

Definition at line 381 of file DateTime.cc.

381  {
382  boost::regex re("(\\d{4})-?(\\d{2})-?(\\d{2})" "T"
383  "(\\d{2}):?(\\d{2}):?(\\d{2})" "([.,](\\d*))?" "Z");
384  boost::smatch matches;
385  if (!regex_match(iso8601, matches, re)) {
386  throw LSST_EXCEPT(lsst::pex::exceptions::DomainError,
387  "Not in acceptable ISO8601 format: " + iso8601);
388  }
389  DateTime dt(atoi(matches.str(1).c_str()), atoi(matches.str(2).c_str()),
390  atoi(matches.str(3).c_str()), atoi(matches.str(4).c_str()),
391  atoi(matches.str(5).c_str()), atoi(matches.str(6).c_str()),
392  UTC);
393  _nsecs = dt._nsecs;
394  if (matches[7].matched) {
395  std::string frac = matches.str(8);
396  int places = frac.size();
397  if (places > 9) { // truncate fractional nanosec
398  frac.erase(9);
399  }
400  int value = atoi(frac.c_str());
401  while (places < 9) {
402  value *= 10;
403  ++places;
404  }
405  _nsecs += value;
406  }
407 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
DateTime(long long nsecs=0LL, Timescale scale=TAI)
Definition: DateTime.cc:290
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46

Member Function Documentation

double lsst.daf.base::DateTime::_getEpoch ( Timescale  scale) const
private

Convert to Julian Epoch.

Parameters
[in]scaleDesired timescale (TAI or UTC, default TAI).
Returns
The Julian Epoch corresponding to the time.

Definition at line 481 of file DateTime.cc.

481  {
482  return 2000.0 + (_getJd(scale) - JD2000)/365.25;
483 }
double _getJd(Timescale scale) const
Definition: DateTime.cc:473
double lsst.daf.base::DateTime::_getJd ( Timescale  scale) const
private

Convert to Julian Day.

Parameters
[in]scaleDesired timescale (TAI or UTC, default TAI).
Returns
The Julian Day corresponding to the time.

Definition at line 473 of file DateTime.cc.

473  {
474  return _getMjd(scale) + MJD_TO_JD;
475 }
double _getMjd(Timescale scale) const
Definition: DateTime.cc:455
double lsst.daf.base::DateTime::_getMjd ( Timescale  scale) const
private

Convert to Modified Julian Day.

Parameters
[in]scaleDesired timescale (TAI or UTC, default TAI).
Returns
The Modified Julian Day corresponding to the time.

Definition at line 455 of file DateTime.cc.

455  {
456 
457  double nsecs;
458  if (scale == TAI) {
459  nsecs = static_cast<double>(_nsecs);
460  } else if (scale == TT) {
461  nsecs = static_cast<double>(_nsecs) + TT_MINUS_TAI_NSECS;
462  } else {
463  nsecs = static_cast<double>(taiToUtc(_nsecs));
464  }
465  return nsecs / NSEC_PER_DAY + EPOCH_IN_MJD;
466 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
double lsst.daf.base::DateTime::get ( DateSystem  system = MJD,
Timescale  scale = TAI 
) const

Generic Accessor

Returns
the date in the required system, for the requested scale
Parameters
[in]systemThe type of date requested (JD, MJD, or EPOCH)
[in]scaleThe time scale (UTC, or TAI)
Note
The NSECS can't be requested here as they're in long long form. A factory could be constructed, but it's more trouble than it's worth at this point.

Definition at line 418 of file DateTime.cc.

418  {
419  switch (system) {
420  case MJD:
421  return _getMjd(scale);
422  break;
423  case JD:
424  return _getJd(scale);
425  break;
426  case EPOCH:
427  return _getEpoch(scale);
428  break;
429  default:
430  throw LSST_EXCEPT(pexEx::InvalidParameterError,
431  "DateSystem must be MJD, JD, or EPOCH.");
432  break;
433  }
434 }
double _getJd(Timescale scale) const
Definition: DateTime.cc:473
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
double _getMjd(Timescale scale) const
Definition: DateTime.cc:455
double _getEpoch(Timescale scale) const
Definition: DateTime.cc:481
struct tm lsst.daf.base::DateTime::gmtime ( void  ) const

Convert to struct tm. Truncate fractional seconds.

Returns
Structure with decoded time in UTC.

Definition at line 491 of file DateTime.cc.

491  {
492  struct tm gmt;
493  long long nsecs = taiToUtc(_nsecs);
494  // Round to negative infinity
495  long long frac = nsecs % LL_NSEC_PER_SEC;
496  if (nsecs < 0 && frac < 0) {
497  nsecs -= LL_NSEC_PER_SEC + frac;
498  }
499  else {
500  nsecs -= frac;
501  }
502  time_t secs = static_cast<time_t>(nsecs / LL_NSEC_PER_SEC);
503  gmtime_r(&secs, &gmt);
504  return gmt;
505 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
void lsst.daf.base::DateTime::initializeLeapSeconds ( std::string const &  leapString)
static

Initialize leap second table.

Parameters
leapStringLeap second table from USNO as a single multiline string.

Definition at line 567 of file DateTime.cc.

567  {
568  Leap l;
569  leapSecTable.clear();
570  boost::regex re("^\\d{4}.*?=JD\\s*([\\d.]+)\\s+TAI-UTC=\\s+([\\d.]+)\\s+S"
571  " \\+ \\(MJD - ([\\d.]+)\\) X ([\\d.]+)\\s*S$");
572  for (boost::cregex_iterator i = make_regex_iterator(leapString.c_str(), re);
573  i != boost::cregex_iterator(); ++i) {
574  double mjdUtc = strtod((*i)[1].first, 0) - MJD_TO_JD;
575  l.offset = strtod((*i)[2].first, 0);
576  l.mjdRef = strtod((*i)[3].first, 0);
577  l.drift = strtod((*i)[4].first, 0);
578  l.whenUtc = static_cast<long long>(
579  (mjdUtc - EPOCH_IN_MJD) * NSEC_PER_DAY);
580  l.whenTai = l.whenUtc + static_cast<long long>(
581  1.0e9 * (l.offset + (mjdUtc - l.mjdRef) * l.drift));
582  leapSecTable.push_back(l);
583  }
584 }
double lsst.daf.base::DateTime::mjd ( Timescale  scale = TAI) const
inline

Definition at line 69 of file DateTime.h.

69  { // deprecated, please use get(MJD)
70  return _getMjd(scale);
71  }
double _getMjd(Timescale scale) const
Definition: DateTime.cc:455
dafBase::DateTime lsst.daf.base::DateTime::now ( void  )
static

Return current time as a DateTime.

Returns
DateTime representing the current time.

Definition at line 553 of file DateTime.cc.

553  {
554  struct timeval tv;
555  int ret = gettimeofday(&tv, 0);
556  if (ret != 0) {
557  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
558  "Unable to get current time");
559  }
560  long long nsecs = tv.tv_sec * LL_NSEC_PER_SEC + tv.tv_usec * 1000LL;
561  return DateTime(nsecs, DateTime::UTC);
562 }
struct timeval timeval(void) const
Definition: DateTime.cc:521
DateTime(long long nsecs=0LL, Timescale scale=TAI)
Definition: DateTime.cc:290
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
long long lsst.daf.base::DateTime::nsecs ( Timescale  scale = TAI) const

Accessor.

Returns
Number of nanoseconds since the epoch in UTC or TAI.

Definition at line 440 of file DateTime.cc.

440  {
441  if (scale == TAI) {
442  return _nsecs;
443  } else if (scale == TT) {
444  return _nsecs + TT_MINUS_TAI_NSECS;
445  } else {
446  return taiToUtc(_nsecs);
447  }
448 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
bool lsst.daf.base::DateTime::operator== ( DateTime const &  rhs) const

Equality operator.

Returns
True if both DateTimes have the same nanosecond representation.

Definition at line 546 of file DateTime.cc.

546  {
547  return _nsecs == rhs._nsecs;
548 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
template<class Archive >
void lsst.daf.base::DateTime::serialize ( Archive  ar,
int const  version 
)
inlineprivate

Serialize DateTime to/from a Boost archive.

Parameters
[in,out]arArchive to access.
[in]versionVersion of class serializer.

Definition at line 101 of file DateTime.h.

101  {
102  ar & _nsecs;
103  }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
void lsst.daf.base::DateTime::setNsecsFromEpoch ( double  epoch,
Timescale  scale 
)
private

a function to convert epoch to internal nsecs

Parameters
[in]epochThe Julian epoch
[in]scaleThe time scale (TAI, or UTC)

Definition at line 280 of file DateTime.cc.

280  {
281  setNsecsFromMjd(365.25*(epoch - 2000.0) + JD2000 - MJD_TO_JD, scale);
282 }
void setNsecsFromMjd(double mjd, Timescale scale)
a function to convert MJD to interal nsecs
Definition: DateTime.cc:245
void lsst.daf.base::DateTime::setNsecsFromJd ( double  jd,
Timescale  scale 
)
private

a function to convert JD to internal nsecs

Parameters
[in]jdThe Julian Day
[in]scaleThe time scale (TAI, or UTC)

Definition at line 271 of file DateTime.cc.

271  {
272  setNsecsFromMjd(jd - MJD_TO_JD, scale);
273 }
void setNsecsFromMjd(double mjd, Timescale scale)
a function to convert MJD to interal nsecs
Definition: DateTime.cc:245
void lsst.daf.base::DateTime::setNsecsFromMjd ( double  mjd,
Timescale  scale 
)
private

a function to convert MJD to interal nsecs

Parameters
[in]mjdThe Modified Julian Day
[in]scaleThe time scale (TAI, or UTC)

Definition at line 245 of file DateTime.cc.

245  {
246 
247  if (mjd > EPOCH_IN_MJD + MAX_DAYS) {
248  throw LSST_EXCEPT(
249  lsst::pex::exceptions::DomainError,
250  (boost::format("MJD too far in the future: %1%") % mjd).str());
251  }
252  if (mjd < EPOCH_IN_MJD - MAX_DAYS) {
253  throw LSST_EXCEPT(
254  lsst::pex::exceptions::DomainError,
255  (boost::format("MJD too far in the past: %1%") % mjd).str());
256  }
257  _nsecs = static_cast<long long>((mjd - EPOCH_IN_MJD) * NSEC_PER_DAY);
258  if (scale == UTC) {
259  _nsecs = utcToTai(_nsecs);
260  } else if (scale == TT) {
261  _nsecs -= TT_MINUS_TAI_NSECS;
262  }
263 
264 }
double mjd(Timescale scale=TAI) const
Definition: DateTime.h:69
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
struct timespec lsst.daf.base::DateTime::timespec ( void  ) const

Convert to struct timespec.

Returns
Structure with UTC time in seconds and nanoseconds.

Definition at line 510 of file DateTime.cc.

510  {
511  struct timespec ts;
512  long long nsecs = taiToUtc(_nsecs);
513  ts.tv_sec = static_cast<time_t>(nsecs / LL_NSEC_PER_SEC);
514  ts.tv_nsec = static_cast<int>(nsecs % LL_NSEC_PER_SEC);
515  return ts;
516 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
struct timespec timespec(void) const
Definition: DateTime.cc:510
struct timeval lsst.daf.base::DateTime::timeval ( void  ) const

Convert time to struct timeval.

Returns
Structure with UTC time in seconds and microseconds.

Definition at line 521 of file DateTime.cc.

521  {
522  struct timeval tv;
523  long long nsecs = taiToUtc(_nsecs);
524  tv.tv_sec = static_cast<time_t>(nsecs / LL_NSEC_PER_SEC);
525  tv.tv_usec = static_cast<int>((nsecs % LL_NSEC_PER_SEC) / 1000);
526  return tv;
527 }
struct timeval timeval(void) const
Definition: DateTime.cc:521
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
std::string lsst.daf.base::DateTime::toString ( void  ) const

Accessor.

Returns
ISO8601-formatted string representation. Always UTC.

Definition at line 532 of file DateTime.cc.

532  {
533  struct tm gmt(this->gmtime());
534  long long nsecs = taiToUtc(_nsecs) % LL_NSEC_PER_SEC;
535  if (nsecs < 0) {
536  nsecs += LL_NSEC_PER_SEC;
537  }
538  return (boost::format("%04d-%02d-%02dT%02d:%02d:%02d.%09dZ") %
539  (gmt.tm_year + 1900) % (gmt.tm_mon + 1) % gmt.tm_mday %
540  gmt.tm_hour % gmt.tm_min % gmt.tm_sec % nsecs).str();
541 }
long long _nsecs
Nanoseconds since Unix epoch.
Definition: DateTime.h:86
long long nsecs(Timescale scale=TAI) const
Definition: DateTime.cc:440
struct tm gmtime(void) const
Definition: DateTime.cc:491

Friends And Related Function Documentation

friend class boost::serialization::access
friend

Definition at line 96 of file DateTime.h.

Member Data Documentation

long long lsst.daf.base::DateTime::_nsecs
private

Nanoseconds since Unix epoch.

Definition at line 86 of file DateTime.h.


The documentation for this class was generated from the following files: