Official websites use .gov
A .gov website belongs to an official government organization in the United States.

Secure .gov websites use HTTPS
A lock ( ) or https:// means you’ve safely connected to the .gov website. Share sensitive information only on official, secure websites.

Isis 3 Programmer Reference
iTime.cpp
1
5
6/* SPDX-License-Identifier: CC0-1.0 */
7#include <iostream>
8#include <iomanip>
9#include <sstream>
10
11#include <QString>
12#include <QRegularExpression>
13
14#include "Preference.h"
15
16#include "FileName.h"
17#include "IString.h"
18#include "iTime.h"
19#include "SpecialPixel.h"
20#include "NaifStatus.h"
21#include "spiceql.h"
22
23using namespace std;
24namespace Isis {
25
26 // Static initializations
27 bool iTime::p_lpInitialized = false;
28
29 //---------------------------------------------------------------------------
30 // Constructors
31 //---------------------------------------------------------------------------
32
35 p_et = 0.0;
36 }
37
44 iTime::iTime(const QString &time) {
46
48
49 // Convert the time string to a double ephemeris time
50 SpiceDouble et;
51 str2et_c(time.toLatin1().data(), &et);
52
53 p_et = et;
55 }
56
57
58 //---------------------------------------------------------------------------
59 // Public members
60 //---------------------------------------------------------------------------
61
68 void iTime::operator=(const QString &time) {
70
72 // Convert the time string to a double ephemeris time
73 SpiceDouble et;
74 str2et_c(time.toLatin1().data(), &et);
75
76 p_et = et;
78 }
79
80 // Overload of "=" with a c string
81 void iTime::operator=(const char *time) {
83
85 // Convert the time string to a double ephemeris time
86 SpiceDouble et;
87 str2et_c(time, &et);
88
89 p_et = et;
91 }
92
93
94 // Overload of "=" with a double
95 void iTime::operator=(const double time) {
97 p_et = time;
98 }
99
107 bool iTime::operator>=(const iTime &time) {
108 return (p_et >= time.p_et);
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
133
141 bool iTime::operator<(const iTime &time) {
142 return (p_et < time.p_et);
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
167
168 iTime iTime::operator +(const double &secondsToAdd) const {
169 iTime tmp(*this);
170 tmp += secondsToAdd;
171 return tmp;
172 }
173
174
175 void iTime::operator +=(const double &secondsToAdd) {
176 if(!IsSpecial(secondsToAdd) && !IsSpecial(p_et))
177 p_et += secondsToAdd;
178 }
179
180
181 iTime operator +(const double &secondsToAdd, iTime time) {
182 time += secondsToAdd;
183 return time;
184 }
185
186
187
188
189 iTime iTime::operator -(const double &secondsToSubtract) const {
190 iTime tmp(*this);
191 tmp -= secondsToSubtract;
192 return tmp;
193 }
194
195
196 double iTime::operator -(const iTime &iTimeToSubtract) const {
197 return p_et - iTimeToSubtract.p_et;
198 }
199
200
201 void iTime::operator -=(const double &secondsToSubtract) {
202 if (!IsSpecial(secondsToSubtract) && !IsSpecial(p_et))
203 p_et -= secondsToSubtract;
204 }
205
206
207 iTime operator -(const double &secondsToSubtract, iTime time) {
208 time -= secondsToSubtract;
209 return time;
210 }
211
212
213
214
215
216
222 QString iTime::YearString() const {
223 return toString(Year());
224 }
225
231 int iTime::Year() const {
233 SpiceChar out[5];
234
235 // Populate the private year member
236 timout_c(p_et, "YYYY", 5, out);
238 return IString(out).ToInteger();
239 }
240
246 QString iTime::MonthString() const {
247 return toString(Month());
248 }
249
255 int iTime::Month() const {
257 SpiceChar out[3];
258
259 // Populate the private year member
260 timout_c(p_et, "MM", 3, out);
262 return IString(out).ToInteger();
263 }
264
270 QString iTime::DayString() const {
271 return toString(Day());
272 }
273
279 int iTime::Day() const {
281 SpiceChar out[3];
282
283 // Populate the private year member
284 timout_c(p_et, "DD", 3, out);
286 return IString(out).ToInteger();
287 }
288
294 QString iTime::HourString() const {
295 return toString(Hour());
296 }
297
303 int iTime::Hour() const {
305 SpiceChar out[3];
306
307 // Populate the private year member
308 timout_c(p_et, "HR", 3, out);
310 return IString(out).ToInteger();
311 }
312
318 QString iTime::MinuteString() const {
319 return toString(Minute());
320 }
321
327 int iTime::Minute() const {
329 SpiceChar out[3];
330
331 // Populate the private year member
332 timout_c(p_et, "MN", 3, out);
334 return IString(out).ToInteger();
335 }
336
342 QString iTime::SecondString(int precision) const {
343 ostringstream osec;
344 osec.setf(ios::fixed);
345 osec << setprecision(precision) << Second();
346 QString sSeconds(osec.str().c_str());
347 sSeconds = sSeconds.remove(QRegularExpression("(\\.0*|0*)$"));
348
349 if(sSeconds.isEmpty()) sSeconds = "0";
350 return sSeconds;
351 }
352
358 double iTime::Second() const {
360 SpiceChar out[256];
361
362 // Populate the private year member
363 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 {
384 SpiceChar out[4];
385
386 // Populate the private year member
387 timout_c(p_et, "DOY", 4, out);
389 return IString(out).ToInteger();
390 }
391
398 QString iTime::EtString() const {
399 return toString(p_et);
400 }
401
407 QString iTime::UTC(int precision) const {
408 QString utc = YearString() + "-" ;
409 if(Month() < 10) utc += "0" + MonthString() + "-";
410 else utc += MonthString() + "-";
411
412 if(Day() < 10) utc += "0" + DayString() + "T";
413 else utc += DayString() + "T";
414
415 if(Hour() < 10) utc += "0" + HourString() + ":";
416 else utc += HourString() + ":";
417
418 if(Minute() < 10) utc += "0" + MinuteString() + ":";
419 else utc += MinuteString() + ":";
420
421 if(Second() < 10) utc += "0" + SecondString(precision);
422 else utc += SecondString(precision);
423
424 return utc;
425 }
426
427 void iTime::setEt(double et) {
428 if(!IsSpecial(et))
429 p_et = et;
430 else
431 p_et = 0.0;
432 }
433
434 void iTime::setUtc(QString utcString) {
435 // If the time string is in ISO basic format add separators for utc2et
436 if ( utcString.contains("T") && // Check for ISO T format
437 !utcString.contains("-") && // Check for missing data separator
438 !utcString.contains(":")) { // Check for missing time separator
439 QString dateString = utcString.split("T").front();
440 dateString.insert(4, "-");
441 // If format is YYYYDOY we are done with the date string
442 // Otherwise we are in YYYYMMDD format
443 if (dateString.size() > 8) {
444 dateString.insert(7, "-");
445 }
446
447 QString timeString = utcString.split("T").back();
448 // If the format is hh or hhmm, resize and pad with 0s out to hh0000 or hhmm00
449 if (timeString.size() < 6) {
450 timeString.resize(6, '0');
451 }
452 timeString.insert(2, ":");
453 timeString.insert(5, ":");
454
455 utcString = dateString + "T" + timeString;
456 }
457
460
461 double et;
462 utc2et_c(utcString.toLatin1().data(), &et);
463 setEt(et);
465 }
466
467 //---------------------------------------------------
468 // Private members
469 //---------------------------------------------------
470
471
474 // Inorder to improve the speed of iTime comparisons, the leapsecond
475 // kernel is loaded only once and left open.
476 if(p_lpInitialized) return;
477
478 // Get the leap second kernel file open
479 Isis::PvlGroup &dataDir = Isis::Preference::Preferences().findGroup("DataDirectory");
480 QString baseDir = dataDir["Base"];
481 baseDir += "/kernels/lsk/";
482 FileName leapSecond(baseDir + "naif????.tls");
483 QString leapSecondName;
484 try {
485 leapSecondName = QString(leapSecond.highestVersion().expanded());
486 }
487 catch (IException &e) {
488 QString msg = "Unable to load leadsecond file. Either the data area is not set or there are no naif####.tls files present";
489 throw IException(e, IException::User, msg, _FILEINFO_);
490 }
491
493 SpiceQL::load(leapSecondName.toLatin1().data());
495
496 p_lpInitialized = true;
497 }
498
507 time_t startTime = time(NULL);
508 struct tm *tmbuf = gmtime(&startTime);
509 char timestr[80];
510 strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
511 return (QString) timestr;
512 }
513
514
523 time_t startTime = time(NULL);
524 struct tm *tmbuf = localtime(&startTime);
525 char timestr[80];
526 strftime(timestr, 80, "%Y-%m-%dT%H:%M:%S", tmbuf);
527 return (QString) timestr;
528 }
529} // end namespace isis
static void CheckErrors(bool resetNaif=true)
This method looks for any naif errors that might have occurred.
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:294
bool operator>=(const iTime &time)
Compare two iTime objects for greater than or equal.
Definition iTime.cpp:107
int Hour() const
Returns the hour portion of the time as an int.
Definition iTime.cpp:303
QString DayOfYearString() const
Returns the day of year portion of the time as a string.
Definition iTime.cpp:373
double Second() const
Returns the second portion of the time as a double.
Definition iTime.cpp:358
bool operator!=(const iTime &time)
Compare two iTime objects for inequality.
Definition iTime.cpp:152
QString EtString() const
Returns the ephemeris time (TDB) representation of the time as a string.
Definition iTime.cpp:398
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:522
void operator=(const QString &time)
Changes the value of the iTime object.
Definition iTime.cpp:68
QString DayString() const
Returns the dat portion of the time as a string.
Definition iTime.cpp:270
int Day() const
Returns the day portion of the time as an int.
Definition iTime.cpp:279
QString SecondString(int precision=8) const
Returns the second portion of the time as a string.
Definition iTime.cpp:342
bool operator>(const iTime &time)
Compare two iTime objects for greater than.
Definition iTime.cpp:129
QString MonthString() const
Returns the month portion of the time as a string.
Definition iTime.cpp:246
QString MinuteString() const
Returns the minute portion of the time as a string.
Definition iTime.cpp:318
bool operator<(const iTime &time)
Compare two iTime objects for less than.
Definition iTime.cpp:141
int Month() const
Returns the month portion of the time as an int.
Definition iTime.cpp:255
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:506
int Minute() const
Returns the minute portion of the time as an int.
Definition iTime.cpp:327
int DayOfYear() const
Returns the day of year portion of the time as an int.
Definition iTime.cpp:382
QString YearString() const
Returns the year portion of the time as a string.
Definition iTime.cpp:222
QString UTC(int precision=8) const
Returns the internally stored time, formatted as a UTC time.
Definition iTime.cpp:407
bool operator<=(const iTime &time)
Compare two iTime objects for less than or equal.
Definition iTime.cpp:118
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:473
int Year() const
Returns the year portion of the time as an int.
Definition iTime.cpp:231
iTime()
Constructs an empty iTime object.
Definition iTime.cpp:34
bool operator==(const iTime &time)
Compare two iTime objects for equality.
Definition iTime.cpp:163
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
QString toString(const LinearAlgebra::Vector &vector, int precision)
A global function to format LinearAlgebra::Vector as a QString with the given precision.
Coordinate operator+(const Coordinate &A, const Coordinate &B)
Summation operator for Coordinate.
Definition GruenTypes.h:130
Coordinate operator-(const Coordinate &A, const Coordinate &B)
Subtraction operator for Coordinate.
Definition GruenTypes.h:148
Namespace for the standard library.