Isis 3 Programmer Reference
iTime.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include <iostream>
8 #include <iomanip>
9 #include <sstream>
10 
11 #include <QString>
12 
13 #include "Preference.h"
14 
15 #include "FileName.h"
16 #include "IString.h"
17 #include "iTime.h"
18 #include "SpecialPixel.h"
19 #include "NaifStatus.h"
20 
21 using namespace std;
22 namespace Isis {
23 
24  // Static initializations
25  bool iTime::p_lpInitialized = false;
26 
27  //---------------------------------------------------------------------------
28  // Constructors
29  //---------------------------------------------------------------------------
30 
32  iTime::iTime() {
33  p_et = 0.0;
34  }
35 
42  iTime::iTime(const QString &time) {
43  LoadLeapSecondKernel();
44 
45  NaifStatus::CheckErrors();
46 
47  // Convert the time string to a double ephemeris time
48  SpiceDouble et;
49  str2et_c(time.toLatin1().data(), &et);
50 
51  p_et = et;
52  NaifStatus::CheckErrors();
53  }
54 
55 
56  //---------------------------------------------------------------------------
57  // Public members
58  //---------------------------------------------------------------------------
59 
66  void iTime::operator=(const QString &time) {
67  LoadLeapSecondKernel();
68 
69  NaifStatus::CheckErrors();
70  // Convert the time string to a double ephemeris time
71  SpiceDouble et;
72  str2et_c(time.toLatin1().data(), &et);
73 
74  p_et = et;
75  NaifStatus::CheckErrors();
76  }
77 
78  // Overload of "=" with a c string
79  void iTime::operator=(const char *time) {
80  LoadLeapSecondKernel();
81 
82  NaifStatus::CheckErrors();
83  // Convert the time string to a double ephemeris time
84  SpiceDouble et;
85  str2et_c(time, &et);
86 
87  p_et = et;
88  NaifStatus::CheckErrors();
89  }
90 
91 
92  // Overload of "=" with a double
93  void iTime::operator=(const double time) {
94  LoadLeapSecondKernel();
95  p_et = time;
96  }
97 
105  bool iTime::operator>=(const iTime &time) {
106  return (p_et >= time.p_et);
107  }
108 
116  bool iTime::operator<=(const iTime &time) {
117  return (p_et <= time.p_et);
118  }
119 
127  bool iTime::operator>(const iTime &time) {
128  return (p_et > time.p_et);
129  }
130 
131 
139  bool iTime::operator<(const iTime &time) {
140  return (p_et < time.p_et);
141  }
142 
150  bool iTime::operator!=(const iTime &time) {
151  return (p_et != time.p_et);
152  }
153 
161  bool iTime::operator==(const iTime &time) {
162  return (p_et == time.p_et);
163  }
164 
165 
166  iTime iTime::operator +(const double &secondsToAdd) const {
167  iTime tmp(*this);
168  tmp += secondsToAdd;
169  return tmp;
170  }
171 
172 
173  void iTime::operator +=(const double &secondsToAdd) {
174  if(!IsSpecial(secondsToAdd) && !IsSpecial(p_et))
175  p_et += secondsToAdd;
176  }
177 
178 
179  iTime operator +(const double &secondsToAdd, iTime time) {
180  time += secondsToAdd;
181  return time;
182  }
183 
184 
185 
186 
187  iTime iTime::operator -(const double &secondsToSubtract) const {
188  iTime tmp(*this);
189  tmp -= secondsToSubtract;
190  return tmp;
191  }
192 
193 
194  double iTime::operator -(const iTime &iTimeToSubtract) const {
195  return p_et - iTimeToSubtract.p_et;
196  }
197 
198 
199  void iTime::operator -=(const double &secondsToSubtract) {
200  if (!IsSpecial(secondsToSubtract) && !IsSpecial(p_et))
201  p_et -= secondsToSubtract;
202  }
203 
204 
205  iTime operator -(const double &secondsToSubtract, iTime time) {
206  time -= secondsToSubtract;
207  return time;
208  }
209 
210 
211 
212 
213 
214 
220  QString iTime::YearString() const {
221  return toString(Year());
222  }
223 
229  int iTime::Year() const {
230  NaifStatus::CheckErrors();
231  SpiceChar out[5];
232 
233  // Populate the private year member
234  timout_c(p_et, "YYYY", 5, out);
235  NaifStatus::CheckErrors();
236  return IString(out).ToInteger();
237  }
238 
244  QString iTime::MonthString() const {
245  return toString(Month());
246  }
247 
253  int iTime::Month() const {
254  NaifStatus::CheckErrors();
255  SpiceChar out[3];
256 
257  // Populate the private year member
258  timout_c(p_et, "MM", 3, out);
259  NaifStatus::CheckErrors();
260  return IString(out).ToInteger();
261  }
262 
268  QString iTime::DayString() const {
269  return toString(Day());
270  }
271 
277  int iTime::Day() const {
278  NaifStatus::CheckErrors();
279  SpiceChar out[3];
280 
281  // Populate the private year member
282  timout_c(p_et, "DD", 3, out);
283  NaifStatus::CheckErrors();
284  return IString(out).ToInteger();
285  }
286 
292  QString iTime::HourString() const {
293  return toString(Hour());
294  }
295 
301  int iTime::Hour() const {
302  NaifStatus::CheckErrors();
303  SpiceChar out[3];
304 
305  // Populate the private year member
306  timout_c(p_et, "HR", 3, out);
307  NaifStatus::CheckErrors();
308  return IString(out).ToInteger();
309  }
310 
316  QString iTime::MinuteString() const {
317  return toString(Minute());
318  }
319 
325  int iTime::Minute() const {
326  NaifStatus::CheckErrors();
327  SpiceChar out[3];
328 
329  // Populate the private year member
330  timout_c(p_et, "MN", 3, out);
331  NaifStatus::CheckErrors();
332  return IString(out).ToInteger();
333  }
334 
340  QString iTime::SecondString(int precision) const {
341  ostringstream osec;
342  osec.setf(ios::fixed);
343  osec << setprecision(precision) << Second();
344  QString sSeconds(osec.str().c_str());
345  sSeconds = sSeconds.remove(QRegExp("(\\.0*|0*)$"));
346 
347  if(sSeconds.isEmpty()) sSeconds = "0";
348  return sSeconds;
349  }
350 
356  double iTime::Second() const {
357  NaifStatus::CheckErrors();
358  SpiceChar out[256];
359 
360  // Populate the private year member
361  timout_c(p_et, "SC.#######::RND", 256, out);
362  NaifStatus::CheckErrors();
363  return IString(out).ToDouble();
364  }
365 
371  QString iTime::DayOfYearString() const {
372  return toString(DayOfYear());
373  }
374 
380  int iTime::DayOfYear() const {
381  NaifStatus::CheckErrors();
382  SpiceChar out[4];
383 
384  // Populate the private year member
385  timout_c(p_et, "DOY", 4, out);
386  NaifStatus::CheckErrors();
387  return IString(out).ToInteger();
388  }
389 
396  QString iTime::EtString() const {
397  return toString(p_et);
398  }
399 
405  QString iTime::UTC(int precision) const {
406  QString utc = YearString() + "-" ;
407  if(Month() < 10) utc += "0" + MonthString() + "-";
408  else utc += MonthString() + "-";
409 
410  if(Day() < 10) utc += "0" + DayString() + "T";
411  else utc += DayString() + "T";
412 
413  if(Hour() < 10) utc += "0" + HourString() + ":";
414  else utc += HourString() + ":";
415 
416  if(Minute() < 10) utc += "0" + MinuteString() + ":";
417  else utc += MinuteString() + ":";
418 
419  if(Second() < 10) utc += "0" + SecondString(precision);
420  else utc += SecondString(precision);
421 
422  return utc;
423  }
424 
425  void iTime::setEt(double et) {
426  if(!IsSpecial(et))
427  p_et = et;
428  else
429  p_et = 0.0;
430  }
431 
432  void iTime::setUtc(QString utcString) {
433  // If the time string is in ISO basic format add separators for utc2et
434  if ( utcString.contains("T") && // Check for ISO T format
435  !utcString.contains("-") && // Check for missing data separator
436  !utcString.contains(":")) { // Check for missing time separator
437  QString dateString = utcString.split("T").front();
438  dateString.insert(4, "-");
439  // If format is YYYYDOY we are done with the date string
440  // Otherwise we are in YYYYMMDD format
441  if (dateString.size() > 8) {
442  dateString.insert(7, "-");
443  }
444 
445  QString timeString = utcString.split("T").back();
446  // If the format is hh or hhmm, resize and pad with 0s out to hh0000 or hhmm00
447  if (timeString.size() < 6) {
448  timeString.resize(6, '0');
449  }
450  timeString.insert(2, ":");
451  timeString.insert(5, ":");
452 
453  utcString = dateString + "T" + timeString;
454  }
455 
456  NaifStatus::CheckErrors();
457  LoadLeapSecondKernel();
458 
459  double et;
460  utc2et_c(utcString.toLatin1().data(), &et);
461  setEt(et);
462  NaifStatus::CheckErrors();
463  }
464 
465  //---------------------------------------------------
466  // Private members
467  //---------------------------------------------------
468 
469 
471  void iTime::LoadLeapSecondKernel() {
472  // Inorder to improve the speed of iTime comparisons, the leapsecond
473  // kernel is loaded only once and left open.
474  if(p_lpInitialized) return;
475 
476  // Get the leap second kernel file open
477  Isis::PvlGroup &dataDir = Isis::Preference::Preferences().findGroup("DataDirectory");
478  QString baseDir = dataDir["Base"];
479  baseDir += "/kernels/lsk/";
480  FileName leapSecond(baseDir + "naif????.tls");
481 
482  NaifStatus::CheckErrors();
483  QString leapSecondName(leapSecond.highestVersion().expanded());
484  furnsh_c(leapSecondName.toLatin1().data());
485  NaifStatus::CheckErrors();
486 
487  p_lpInitialized = true;
488  }
489 
497  QString iTime::CurrentGMT() {
498  time_t startTime = time(NULL);
499  struct tm *tmbuf = gmtime(&startTime);
500  char timestr[80];
501  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
502  return (QString) timestr;
503  }
504 
505 
513  QString iTime::CurrentLocalTime() {
514  time_t startTime = time(NULL);
515  struct tm *tmbuf = localtime(&startTime);
516  char timestr[80];
517  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
518  return (QString) timestr;
519  }
520 } // end namespace isis
Isis::IString::ToDouble
double ToDouble() const
Returns the floating point value the IString represents.
Definition: IString.cpp:799
Isis::PvlObject::findGroup
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:129
Isis::iTime
Parse and return pieces of a time string.
Definition: iTime.h:65
Isis::FileName
File name manipulation and expansion.
Definition: FileName.h:100
Isis::IString::ToInteger
int ToInteger() const
Returns the object string as an integer.
Definition: IString.cpp:718
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
Isis::IsSpecial
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:197
Isis::FileName::expanded
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:196
Isis::PvlGroup
Contains multiple PvlContainers.
Definition: PvlGroup.h:41
Isis::iTime::p_et
double p_et
The ephemeris representaion of the original string passed into the constructor or the operator= membe...
Definition: iTime.h:138
std
Namespace for the standard library.
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::FileName::highestVersion
FileName highestVersion() const
Searches the directory specified in the file name for the highest version of the file name.
Definition: FileName.cpp:313
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16