USGS

Isis 3.0 Object Programmers' Reference

Home

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  // Convert the time string to a double ephemeris time
60  SpiceDouble et;
61  str2et_c(time.toAscii().data(), &et);
62 
63  p_et = et;
64  NaifStatus::CheckErrors();
65 
66  UnloadLeapSecondKernel();
67  }
68 
69 
70  //---------------------------------------------------------------------------
71  // Public members
72  //---------------------------------------------------------------------------
73 
80  void iTime::operator=(const QString &time) {
81  LoadLeapSecondKernel();
82 
83  NaifStatus::CheckErrors();
84  // Convert the time string to a double ephemeris time
85  SpiceDouble et;
86  str2et_c(time.toAscii().data(), &et);
87 
88  p_et = et;
89  NaifStatus::CheckErrors();
90 
91  UnloadLeapSecondKernel();
92  }
93 
94  // Overload of "=" with a c string
95  void iTime::operator=(const char *time) {
96  LoadLeapSecondKernel();
97 
98  NaifStatus::CheckErrors();
99  // Convert the time string to a double ephemeris time
100  SpiceDouble et;
101  str2et_c(time, &et);
102 
103  p_et = et;
104  NaifStatus::CheckErrors();
105 
106  UnloadLeapSecondKernel();
107  }
108 
109 
110  // Overload of "=" with a double
111  void iTime::operator=(const double time) {
112  LoadLeapSecondKernel();
113  p_et = time;
114  UnloadLeapSecondKernel();
115  }
116 
124  bool iTime::operator>=(const iTime &time) {
125  return (p_et >= time.p_et);
126  }
127 
135  bool iTime::operator<=(const iTime &time) {
136  return (p_et <= time.p_et);
137  }
138 
146  bool iTime::operator>(const iTime &time) {
147  return (p_et > time.p_et);
148  }
149 
150 
158  bool iTime::operator<(const iTime &time) {
159  return (p_et < time.p_et);
160  }
161 
169  bool iTime::operator!=(const iTime &time) {
170  return (p_et != time.p_et);
171  }
172 
180  bool iTime::operator==(const iTime &time) {
181  return (p_et == time.p_et);
182  }
183 
184 
185  iTime iTime::operator +(const double &secondsToAdd) const {
186  iTime tmp(*this);
187  tmp += secondsToAdd;
188  return tmp;
189  }
190 
191 
192  void iTime::operator +=(const double &secondsToAdd) {
193  if(!IsSpecial(secondsToAdd) && !IsSpecial(p_et))
194  p_et += secondsToAdd;
195  }
196 
197 
198  iTime operator +(const double &secondsToAdd, iTime time) {
199  time += secondsToAdd;
200  return time;
201  }
202 
203 
204 
205 
206  iTime iTime::operator -(const double &secondsToSubtract) const {
207  iTime tmp(*this);
208  tmp -= secondsToSubtract;
209  return tmp;
210  }
211 
212 
213  double iTime::operator -(const iTime &iTimeToSubtract) const {
214  return p_et - iTimeToSubtract.p_et;
215  }
216 
217 
218  void iTime::operator -=(const double &secondsToSubtract) {
219  if (!IsSpecial(secondsToSubtract) && !IsSpecial(p_et))
220  p_et -= secondsToSubtract;
221  }
222 
223 
224  iTime operator -(const double &secondsToSubtract, iTime time) {
225  time -= secondsToSubtract;
226  return time;
227  }
228 
229 
230 
231 
232 
233 
239  QString iTime::YearString() const {
240  return toString(Year());
241  }
242 
248  int iTime::Year() const {
249  NaifStatus::CheckErrors();
250  SpiceChar out[5];
251 
252  // Populate the private year member
253  timout_c(p_et, "YYYY", 5, out);
254  NaifStatus::CheckErrors();
255  return IString(out).ToInteger();
256  }
257 
263  QString iTime::MonthString() const {
264  return toString(Month());
265  }
266 
272  int iTime::Month() const {
273  NaifStatus::CheckErrors();
274  SpiceChar out[3];
275 
276  // Populate the private year member
277  timout_c(p_et, "MM", 3, out);
278  NaifStatus::CheckErrors();
279  return IString(out).ToInteger();
280  }
281 
287  QString iTime::DayString() const {
288  return toString(Day());
289  }
290 
296  int iTime::Day() const {
297  NaifStatus::CheckErrors();
298  SpiceChar out[3];
299 
300  // Populate the private year member
301  timout_c(p_et, "DD", 3, out);
302  NaifStatus::CheckErrors();
303  return IString(out).ToInteger();
304  }
305 
311  QString iTime::HourString() const {
312  return toString(Hour());
313  }
314 
320  int iTime::Hour() const {
321  NaifStatus::CheckErrors();
322  SpiceChar out[3];
323 
324  // Populate the private year member
325  timout_c(p_et, "HR", 3, out);
326  NaifStatus::CheckErrors();
327  return IString(out).ToInteger();
328  }
329 
335  QString iTime::MinuteString() const {
336  return toString(Minute());
337  }
338 
344  int iTime::Minute() const {
345  NaifStatus::CheckErrors();
346  SpiceChar out[3];
347 
348  // Populate the private year member
349  timout_c(p_et, "MN", 3, out);
350  NaifStatus::CheckErrors();
351  return IString(out).ToInteger();
352  }
353 
359  QString iTime::SecondString() const {
360  ostringstream osec;
361  osec.setf(ios::fixed);
362  osec << setprecision(8) << Second();
363  QString sSeconds(osec.str().c_str());
364  sSeconds = sSeconds.remove(QRegExp("(\\.0*|0*)$"));
365 
366  if(sSeconds.isEmpty()) sSeconds = "0";
367 
368  return sSeconds;
369  }
370 
376  double iTime::Second() const {
377  NaifStatus::CheckErrors();
378  SpiceChar out[256];
379 
380  // Populate the private year member
381  timout_c(p_et, "SC.#######::RND", 256, out);
382  NaifStatus::CheckErrors();
383  return IString(out).ToDouble();
384  }
385 
391  QString iTime::DayOfYearString() const {
392  return toString(DayOfYear());
393  }
394 
400  int iTime::DayOfYear() const {
401  NaifStatus::CheckErrors();
402  SpiceChar out[4];
403 
404  // Populate the private year member
405  timout_c(p_et, "DOY", 4, out);
406  NaifStatus::CheckErrors();
407  return IString(out).ToInteger();
408  }
409 
416  QString iTime::EtString() const {
417  return toString(p_et);
418  }
419 
425  QString iTime::UTC() const {
426  QString utc = YearString() + "-" ;
427  if(Month() < 10) utc += "0" + MonthString() + "-";
428  else utc += MonthString() + "-";
429 
430  if(Day() < 10) utc += "0" + DayString() + "T";
431  else utc += DayString() + "T";
432 
433  if(Hour() < 10) utc += "0" + HourString() + ":";
434  else utc += HourString() + ":";
435 
436  if(Minute() < 10) utc += "0" + MinuteString() + ":";
437  else utc += MinuteString() + ":";
438 
439  if(Second() < 10) utc += "0" + SecondString();
440  else utc += SecondString();
441 
442  return utc;
443  }
444 
445  void iTime::setEt(double et) {
446  if(!IsSpecial(et))
447  p_et = et;
448  else
449  p_et = 0.0;
450  }
451 
452  void iTime::setUtc(QString utcString) {
453  NaifStatus::CheckErrors();
454  LoadLeapSecondKernel();
455 
456  double et;
457  utc2et_c(utcString.toAscii().data(), &et);
458  setEt(et);
459  NaifStatus::CheckErrors();
460  }
461 
462  //---------------------------------------------------
463  // Private members
464  //---------------------------------------------------
465 
466 
468  void iTime::LoadLeapSecondKernel() {
469  // Inorder to improve the speed of iTime comparisons, the leapsecond
470  // kernel is loaded only once and left open.
471  if(p_lpInitialized) return;
472 
473  // Get the leap second kernel file open
474  Isis::PvlGroup &dataDir = Isis::Preference::Preferences().findGroup("DataDirectory");
475  QString baseDir = dataDir["Base"];
476  baseDir += "/kernels/lsk/";
477  FileName leapSecond(baseDir + "naif????.tls");
478 
479  NaifStatus::CheckErrors();
480  QString leapSecondName(leapSecond.highestVersion().expanded());
481  furnsh_c(leapSecondName.toAscii().data());
482  NaifStatus::CheckErrors();
483 
484  p_lpInitialized = true;
485  }
486 
488  void iTime::UnloadLeapSecondKernel() {
489  // Inorder to improve the speed of iTime comparisons, the leapsecond
490  // kernel is loaded only once and left open.
491 
492  //string leapSecondName(p_leapSecond.expanded());
493  //unload_c (leapSecondName.c_str());
494  }
495 
503  QString iTime::CurrentGMT() {
504  time_t startTime = time(NULL);
505  struct tm *tmbuf = gmtime(&startTime);
506  char timestr[80];
507  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
508  return (QString) timestr;
509  }
510 
511 
519  QString iTime::CurrentLocalTime() {
520  time_t startTime = time(NULL);
521  struct tm *tmbuf = localtime(&startTime);
522  char timestr[80];
523  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
524  return (QString) timestr;
525  }
526 } // end namespace isis