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 
33 using namespace std;
34 namespace Isis {
35 
36  // Static initializations
37  bool iTime::p_lpInitialized = false;
38 
39  //---------------------------------------------------------------------------
40  // Constructors
41  //---------------------------------------------------------------------------
42 
44  iTime::iTime() {
45  p_et = 0.0;
46  }
47 
54  iTime::iTime(const QString &time) {
55  LoadLeapSecondKernel();
56 
57  // Convert the time string to a double ephemeris time
58  SpiceDouble et;
59  str2et_c(time.toAscii().data(), &et);
60 
61  p_et = et;
62 
63  UnloadLeapSecondKernel();
64  }
65 
66 
67  //---------------------------------------------------------------------------
68  // Public members
69  //---------------------------------------------------------------------------
70 
77  void iTime::operator=(const QString &time) {
78  LoadLeapSecondKernel();
79 
80  // Convert the time string to a double ephemeris time
81  SpiceDouble et;
82  str2et_c(time.toAscii().data(), &et);
83 
84  p_et = et;
85 
86  UnloadLeapSecondKernel();
87  }
88 
89  // Overload of "=" with a c string
90  void iTime::operator=(const char *time) {
91 
92  LoadLeapSecondKernel();
93 
94  // Convert the time string to a double ephemeris time
95  SpiceDouble et;
96  str2et_c(time, &et);
97 
98  p_et = et;
99 
100  UnloadLeapSecondKernel();
101  }
102 
103 
104  // Overload of "=" with a double
105  void iTime::operator=(const double time) {
106  LoadLeapSecondKernel();
107  p_et = time;
108  UnloadLeapSecondKernel();
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  SpiceChar out[5];
244 
245  // Populate the private year member
246  timout_c(p_et, "YYYY", 5, out);
247  return IString(out).ToInteger();
248  }
249 
255  QString iTime::MonthString() const {
256  return toString(Month());
257  }
258 
264  int iTime::Month() const {
265  SpiceChar out[3];
266 
267  // Populate the private year member
268  timout_c(p_et, "MM", 3, out);
269  return IString(out).ToInteger();
270  }
271 
277  QString iTime::DayString() const {
278  return toString(Day());
279  }
280 
286  int iTime::Day() const {
287  SpiceChar out[3];
288 
289  // Populate the private year member
290  timout_c(p_et, "DD", 3, out);
291  return IString(out).ToInteger();
292  }
293 
299  QString iTime::HourString() const {
300  return toString(Hour());
301  }
302 
308  int iTime::Hour() const {
309  SpiceChar out[3];
310 
311  // Populate the private year member
312  timout_c(p_et, "HR", 3, out);
313  return IString(out).ToInteger();
314  }
315 
321  QString iTime::MinuteString() const {
322  return toString(Minute());
323  }
324 
330  int iTime::Minute() const {
331  SpiceChar out[3];
332 
333  // Populate the private year member
334  timout_c(p_et, "MN", 3, out);
335  return IString(out).ToInteger();
336  }
337 
343  QString iTime::SecondString() const {
344  ostringstream osec;
345  osec.setf(ios::fixed);
346  osec << setprecision(8) << Second();
347  QString sSeconds(osec.str().c_str());
348  sSeconds = sSeconds.remove(QRegExp("(\\.0*|0*)$"));
349 
350  if(sSeconds.isEmpty()) sSeconds = "0";
351 
352  return sSeconds;
353  }
354 
360  double iTime::Second() const {
361  SpiceChar out[256];
362 
363  // Populate the private year member
364  timout_c(p_et, "SC.#######::RND", 256, out);
365  return IString(out).ToDouble();
366  }
367 
373  QString iTime::DayOfYearString() const {
374  return toString(DayOfYear());
375  }
376 
382  int iTime::DayOfYear() const {
383  SpiceChar out[4];
384 
385  // Populate the private year member
386  timout_c(p_et, "DOY", 4, out);
387  return IString(out).ToInteger();
388  }
389 
396  QString iTime::EtString() const {
397  return toString(p_et);
398  }
399 
405  QString iTime::UTC() 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();
420  else utc += SecondString();
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  LoadLeapSecondKernel();
434 
435  double et;
436  utc2et_c(utcString.toAscii().data(), &et);
437  setEt(et);
438  }
439 
440  //---------------------------------------------------
441  // Private members
442  //---------------------------------------------------
443 
444 
446  void iTime::LoadLeapSecondKernel() {
447  // Inorder to improve the speed of iTime comparisons, the leapsecond
448  // kernel is loaded only once and left open.
449  if(p_lpInitialized) return;
450 
451  // Get the leap second kernel file open
452  Isis::PvlGroup &dataDir = Isis::Preference::Preferences().findGroup("DataDirectory");
453  QString baseDir = dataDir["Base"];
454  baseDir += "/kernels/lsk/";
455  FileName leapSecond(baseDir + "naif????.tls");
456 
457  QString leapSecondName(leapSecond.highestVersion().expanded());
458  furnsh_c(leapSecondName.toAscii().data());
459 
460  p_lpInitialized = true;
461  }
462 
464  void iTime::UnloadLeapSecondKernel() {
465  // Inorder to improve the speed of iTime comparisons, the leapsecond
466  // kernel is loaded only once and left open.
467 
468  //string leapSecondName(p_leapSecond.expanded());
469  //unload_c (leapSecondName.c_str());
470  }
471 
479  QString iTime::CurrentGMT() {
480  time_t startTime = time(NULL);
481  struct tm *tmbuf = gmtime(&startTime);
482  char timestr[80];
483  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
484  return (QString) timestr;
485  }
486 
487 
495  QString iTime::CurrentLocalTime() {
496  time_t startTime = time(NULL);
497  struct tm *tmbuf = localtime(&startTime);
498  char timestr[80];
499  strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
500  return (QString) timestr;
501  }
502 } // end namespace isis