Isis 3 Programmer Reference
iTime.cpp
Go to the documentation of this file.
1 
22 #include <iostream>
23 #include <iomanip>
24 #include <sstream>
25 
26 #include "Preference.h"
27 
28 #include "FileName.h"
29 #include "IString.h"
30 #include "iTime.h"
31 #include "SpecialPixel.h"
32 #include "NaifStatus.h"
33 
34 using namespace std;
35 namespace Isis {
36 
37  // Static initializations
38  bool iTime::p_lpInitialized = false;
39 
40  //---------------------------------------------------------------------------
41  // Constructors
42  //---------------------------------------------------------------------------
43 
45  iTime::iTime() {
46  p_et = 0.0;
47  }
48 
55  iTime::iTime(const QString &time) {
56  LoadLeapSecondKernel();
57 
58  NaifStatus::CheckErrors();
59 
60  // Convert the time string to a double ephemeris time
61  SpiceDouble et;
62  str2et_c(time.toLatin1().data(), &et);
63 
64  p_et = et;
65  NaifStatus::CheckErrors();
66  }
67 
68 
69  //---------------------------------------------------------------------------
70  // Public members
71  //---------------------------------------------------------------------------
72 
79  void iTime::operator=(const QString &time) {
80  LoadLeapSecondKernel();
81 
82  NaifStatus::CheckErrors();
83  // Convert the time string to a double ephemeris time
84  SpiceDouble et;
85  str2et_c(time.toLatin1().data(), &et);
86 
87  p_et = et;
88  NaifStatus::CheckErrors();
89  }
90 
91  // Overload of "=" with a c string
92  void iTime::operator=(const char *time) {
93  LoadLeapSecondKernel();
94 
95  NaifStatus::CheckErrors();
96  // Convert the time string to a double ephemeris time
97  SpiceDouble et;
98  str2et_c(time, &et);
99 
100  p_et = et;
101  NaifStatus::CheckErrors();
102  }
103 
104 
105  // Overload of "=" with a double
106  void iTime::operator=(const double time) {
107  LoadLeapSecondKernel();
108  p_et = time;
109  }
110 
118  bool iTime::operator>=(const iTime &time) {
119  return (p_et >= time.p_et);
120  }
121 
129  bool iTime::operator<=(const iTime &time) {
130  return (p_et <= time.p_et);
131  }
132 
140  bool iTime::operator>(const iTime &time) {
141  return (p_et > time.p_et);
142  }
143 
144 
152  bool iTime::operator<(const iTime &time) {
153  return (p_et < time.p_et);
154  }
155 
163  bool iTime::operator!=(const iTime &time) {
164  return (p_et != time.p_et);
165  }
166 
174  bool iTime::operator==(const iTime &time) {
175  return (p_et == time.p_et);
176  }
177 
178 
179  iTime iTime::operator +(const double &secondsToAdd) const {
180  iTime tmp(*this);
181  tmp += secondsToAdd;
182  return tmp;
183  }
184 
185 
186  void iTime::operator +=(const double &secondsToAdd) {
187  if(!IsSpecial(secondsToAdd) && !IsSpecial(p_et))
188  p_et += secondsToAdd;
189  }
190 
191 
192  iTime operator +(const double &secondsToAdd, iTime time) {
193  time += secondsToAdd;
194  return time;
195  }
196 
197 
198 
199 
200  iTime iTime::operator -(const double &secondsToSubtract) const {
201  iTime tmp(*this);
202  tmp -= secondsToSubtract;
203  return tmp;
204  }
205 
206 
207  double iTime::operator -(const iTime &iTimeToSubtract) const {
208  return p_et - iTimeToSubtract.p_et;
209  }
210 
211 
212  void iTime::operator -=(const double &secondsToSubtract) {
213  if (!IsSpecial(secondsToSubtract) && !IsSpecial(p_et))
214  p_et -= secondsToSubtract;
215  }
216 
217 
218  iTime operator -(const double &secondsToSubtract, iTime time) {
219  time -= secondsToSubtract;
220  return time;
221  }
222 
223 
224 
225 
226 
227 
233  QString iTime::YearString() const {
234  return toString(Year());
235  }
236 
242  int iTime::Year() const {
243  NaifStatus::CheckErrors();
244  SpiceChar out[5];
245 
246  // Populate the private year member
247  timout_c(p_et, "YYYY", 5, out);
248  NaifStatus::CheckErrors();
249  return IString(out).ToInteger();
250  }
251 
257  QString iTime::MonthString() const {
258  return toString(Month());
259  }
260 
266  int iTime::Month() const {
267  NaifStatus::CheckErrors();
268  SpiceChar out[3];
269 
270  // Populate the private year member
271  timout_c(p_et, "MM", 3, out);
272  NaifStatus::CheckErrors();
273  return IString(out).ToInteger();
274  }
275 
281  QString iTime::DayString() const {
282  return toString(Day());
283  }
284 
290  int iTime::Day() const {
291  NaifStatus::CheckErrors();
292  SpiceChar out[3];
293 
294  // Populate the private year member
295  timout_c(p_et, "DD", 3, out);
296  NaifStatus::CheckErrors();
297  return IString(out).ToInteger();
298  }
299 
305  QString iTime::HourString() const {
306  return toString(Hour());
307  }
308 
314  int iTime::Hour() const {
315  NaifStatus::CheckErrors();
316  SpiceChar out[3];
317 
318  // Populate the private year member
319  timout_c(p_et, "HR", 3, out);
320  NaifStatus::CheckErrors();
321  return IString(out).ToInteger();
322  }
323 
329  QString iTime::MinuteString() const {
330  return toString(Minute());
331  }
332 
338  int iTime::Minute() const {
339  NaifStatus::CheckErrors();
340  SpiceChar out[3];
341 
342  // Populate the private year member
343  timout_c(p_et, "MN", 3, out);
344  NaifStatus::CheckErrors();
345  return IString(out).ToInteger();
346  }
347 
353  QString iTime::SecondString(int precision) const {
354  ostringstream osec;
355  osec.setf(ios::fixed);
356  osec << setprecision(precision) << Second();
357  QString sSeconds(osec.str().c_str());
358  sSeconds = sSeconds.remove(QRegExp("(\\.0*|0*)$"));
359 
360  if(sSeconds.isEmpty()) sSeconds = "0";
361  return sSeconds;
362  }
363 
369  double iTime::Second() const {
370  NaifStatus::CheckErrors();
371  SpiceChar out[256];
372 
373  // Populate the private year member
374  timout_c(p_et, "SC.#######::RND", 256, out);
375  NaifStatus::CheckErrors();
376  return IString(out).ToDouble();
377  }
378 
384  QString iTime::DayOfYearString() const {
385  return toString(DayOfYear());
386  }
387 
393  int iTime::DayOfYear() const {
394  NaifStatus::CheckErrors();
395  SpiceChar out[4];
396 
397  // Populate the private year member
398  timout_c(p_et, "DOY", 4, out);
399  NaifStatus::CheckErrors();
400  return IString(out).ToInteger();
401  }
402 
409  QString iTime::EtString() const {
410  return toString(p_et);
411  }
412 
418  QString iTime::UTC(int precision) const {
419  QString utc = YearString() + "-" ;
420  if(Month() < 10) utc += "0" + MonthString() + "-";
421  else utc += MonthString() + "-";
422 
423  if(Day() < 10) utc += "0" + DayString() + "T";
424  else utc += DayString() + "T";
425 
426  if(Hour() < 10) utc += "0" + HourString() + ":";
427  else utc += HourString() + ":";
428 
429  if(Minute() < 10) utc += "0" + MinuteString() + ":";
430  else utc += MinuteString() + ":";
431 
432  if(Second() < 10) utc += "0" + SecondString(precision);
433  else utc += SecondString(precision);
434 
435  return utc;
436  }
437 
438  void iTime::setEt(double et) {
439  if(!IsSpecial(et))
440  p_et = et;
441  else
442  p_et = 0.0;
443  }
444 
445  void iTime::setUtc(QString utcString) {
446  NaifStatus::CheckErrors();
447  LoadLeapSecondKernel();
448 
449  double et;
450  utc2et_c(utcString.toLatin1().data(), &et);
451  setEt(et);
452  NaifStatus::CheckErrors();
453  }
454 
455  //---------------------------------------------------
456  // Private members
457  //---------------------------------------------------
458 
459 
461  void iTime::LoadLeapSecondKernel() {
462  // Inorder to improve the speed of iTime comparisons, the leapsecond
463  // kernel is loaded only once and left open.
464  if(p_lpInitialized) return;
465 
466  // Get the leap second kernel file open
467  Isis::PvlGroup &dataDir = Isis::Preference::Preferences().findGroup("DataDirectory");
468  QString baseDir = dataDir["Base"];
469  baseDir += "/kernels/lsk/";
470  FileName leapSecond(baseDir + "naif????.tls");
471 
472  NaifStatus::CheckErrors();
473  QString leapSecondName(leapSecond.highestVersion().expanded());
474  furnsh_c(leapSecondName.toLatin1().data());
475  NaifStatus::CheckErrors();
476 
477  p_lpInitialized = true;
478  }
479 
487  QString iTime::CurrentGMT() {
488  time_t startTime = time(NULL);
489  struct tm *tmbuf = gmtime(&startTime);
490  char timestr[80];
491  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
492  return (QString) timestr;
493  }
494 
495 
503  QString iTime::CurrentLocalTime() {
504  time_t startTime = time(NULL);
505  struct tm *tmbuf = localtime(&startTime);
506  char timestr[80];
507  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
508  return (QString) timestr;
509  }
510 } // end namespace isis
PvlGroupIterator findGroup(const QString &name, PvlGroupIterator beg, PvlGroupIterator end)
Find a group with the specified name, within these indexes.
Definition: PvlObject.h:141
File name manipulation and expansion.
Definition: FileName.h:116
Parse and return pieces of a time string.
Definition: iTime.h:78
double ToDouble() const
Returns the floating point value the IString represents.
Definition: IString.cpp:814
Namespace for the standard library.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:226
int ToInteger() const
Returns the object string as an integer.
Definition: IString.cpp:733
Contains multiple PvlContainers.
Definition: PvlGroup.h:57
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition: FileName.cpp:212
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:212
double p_et
The ephemeris representaion of the original string passed into the constructor or the operator= membe...
Definition: iTime.h:151
FileName highestVersion() const
Searches the directory specified in the file name for the highest version of the file name...
Definition: FileName.cpp:329
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31