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
21using namespace std;
22namespace Isis {
23
24 // Static initializations
25 bool iTime::p_lpInitialized = false;
26
27 //---------------------------------------------------------------------------
28 // Constructors
29 //---------------------------------------------------------------------------
30
33 p_et = 0.0;
34 }
35
42 iTime::iTime(const QString &time) {
44
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;
53 }
54
55
56 //---------------------------------------------------------------------------
57 // Public members
58 //---------------------------------------------------------------------------
59
66 void iTime::operator=(const QString &time) {
68
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;
76 }
77
78 // Overload of "=" with a c string
79 void iTime::operator=(const char *time) {
81
83 // Convert the time string to a double ephemeris time
84 SpiceDouble et;
85 str2et_c(time, &et);
86
87 p_et = et;
89 }
90
91
92 // Overload of "=" with a double
93 void iTime::operator=(const double time) {
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 {
231 SpiceChar out[5];
232
233 // Populate the private year member
234 timout_c(p_et, "YYYY", 5, out);
236 return IString(out).ToInteger();
237 }
238
244 QString iTime::MonthString() const {
245 return toString(Month());
246 }
247
253 int iTime::Month() const {
255 SpiceChar out[3];
256
257 // Populate the private year member
258 timout_c(p_et, "MM", 3, out);
260 return IString(out).ToInteger();
261 }
262
268 QString iTime::DayString() const {
269 return toString(Day());
270 }
271
277 int iTime::Day() const {
279 SpiceChar out[3];
280
281 // Populate the private year member
282 timout_c(p_et, "DD", 3, out);
284 return IString(out).ToInteger();
285 }
286
292 QString iTime::HourString() const {
293 return toString(Hour());
294 }
295
301 int iTime::Hour() const {
303 SpiceChar out[3];
304
305 // Populate the private year member
306 timout_c(p_et, "HR", 3, out);
308 return IString(out).ToInteger();
309 }
310
316 QString iTime::MinuteString() const {
317 return toString(Minute());
318 }
319
325 int iTime::Minute() const {
327 SpiceChar out[3];
328
329 // Populate the private year member
330 timout_c(p_et, "MN", 3, out);
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 {
358 SpiceChar out[256];
359
360 // Populate the private year member
361 timout_c(p_et, "SC.#######::RND", 256, out);
363 return IString(out).ToDouble();
364 }
365
371 QString iTime::DayOfYearString() const {
372 return toString(DayOfYear());
373 }
374
380 int iTime::DayOfYear() const {
382 SpiceChar out[4];
383
384 // Populate the private year member
385 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(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
458
459 double et;
460 utc2et_c(utcString.toLatin1().data(), &et);
461 setEt(et);
463 }
464
465 //---------------------------------------------------
466 // Private members
467 //---------------------------------------------------
468
469
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 QString leapSecondName;
482 try {
483 leapSecondName = QString(leapSecond.highestVersion().expanded());
484 }
485 catch (IException &e) {
486 QString msg = "Unable to load leadsecond file. Either the data area is not set or there are no naif####.tls files present";
487 throw IException(e, IException::User, msg, _FILEINFO_);
488 }
489
491 furnsh_c(leapSecondName.toLatin1().data());
493
494 p_lpInitialized = true;
495 }
496
505 time_t startTime = time(NULL);
506 struct tm *tmbuf = gmtime(&startTime);
507 char timestr[80];
508 strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
509 return (QString) timestr;
510 }
511
512
521 time_t startTime = time(NULL);
522 struct tm *tmbuf = localtime(&startTime);
523 char timestr[80];
524 strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
525 return (QString) timestr;
526 }
527} // end namespace isis
File name manipulation and expansion.
Definition FileName.h:100
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition FileName.cpp:196
FileName highestVersion() const
Searches the directory specified in the file name for the highest version of the file name.
Definition FileName.cpp:313
Isis exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
Adds specific functionality to C++ strings.
Definition IString.h:165
int ToInteger() const
Returns the object string as an integer.
Definition IString.cpp:718
double ToDouble() const
Returns the floating point value the IString represents.
Definition IString.cpp:799
static void CheckErrors(bool resetNaif=true)
This method looks for any naif errors that might have occurred.
Contains multiple PvlContainers.
Definition PvlGroup.h:41
Parse and return pieces of a time string.
Definition iTime.h:65
QString HourString() const
Returns the hour portion of the time as a string.
Definition iTime.cpp:292
bool operator>=(const iTime &time)
Compare two iTime objects for greater than or equal.
Definition iTime.cpp:105
int Hour() const
Returns the hour portion of the time as an int.
Definition iTime.cpp:301
QString DayOfYearString() const
Returns the day of year portion of the time as a string.
Definition iTime.cpp:371
double Second() const
Returns the second portion of the time as a double.
Definition iTime.cpp:356
bool operator!=(const iTime &time)
Compare two iTime objects for inequality.
Definition iTime.cpp:150
QString EtString() const
Returns the ephemeris time (TDB) representation of the time as a string.
Definition iTime.cpp:396
static QString CurrentLocalTime()
Returns the current local time This time is taken directly from the system clock, so if the system cl...
Definition iTime.cpp:520
void operator=(const QString &time)
Changes the value of the iTime object.
Definition iTime.cpp:66
QString DayString() const
Returns the dat portion of the time as a string.
Definition iTime.cpp:268
int Day() const
Returns the day portion of the time as an int.
Definition iTime.cpp:277
QString SecondString(int precision=8) const
Returns the second portion of the time as a string.
Definition iTime.cpp:340
bool operator>(const iTime &time)
Compare two iTime objects for greater than.
Definition iTime.cpp:127
QString MonthString() const
Returns the month portion of the time as a string.
Definition iTime.cpp:244
QString MinuteString() const
Returns the minute portion of the time as a string.
Definition iTime.cpp:316
bool operator<(const iTime &time)
Compare two iTime objects for less than.
Definition iTime.cpp:139
int Month() const
Returns the month portion of the time as an int.
Definition iTime.cpp:253
static QString CurrentGMT()
Returns the current Greenwich Mean iTime The time is based on the system time, so it is only as accur...
Definition iTime.cpp:504
int Minute() const
Returns the minute portion of the time as an int.
Definition iTime.cpp:325
int DayOfYear() const
Returns the day of year portion of the time as an int.
Definition iTime.cpp:380
QString YearString() const
Returns the year portion of the time as a string.
Definition iTime.cpp:220
QString UTC(int precision=8) const
Returns the internally stored time, formatted as a UTC time.
Definition iTime.cpp:405
bool operator<=(const iTime &time)
Compare two iTime objects for less than or equal.
Definition iTime.cpp:116
double p_et
The ephemeris representaion of the original string passed into the constructor or the operator= membe...
Definition iTime.h:138
void LoadLeapSecondKernel()
Uses the Naif routines to load the most current leap second kernel.
Definition iTime.cpp:471
int Year() const
Returns the year portion of the time as an int.
Definition iTime.cpp:229
iTime()
Constructs an empty iTime object.
Definition iTime.cpp:32
bool operator==(const iTime &time)
Compare two iTime objects for equality.
Definition iTime.cpp:161
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition IString.cpp:211
Coordinate operator+(const Coordinate &A, const Coordinate &B)
Summation operator for Coordinate.
Definition GruenTypes.h:130
bool IsSpecial(const double d)
Returns if the input pixel is special.
Coordinate operator-(const Coordinate &A, const Coordinate &B)
Subtraction operator for Coordinate.
Definition GruenTypes.h:148
Namespace for the standard library.