OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
VcstTime.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * NAME: VcstTime.h
4  *
5  * DESCRIPTION: This is the header file for the VcstTime object class.
6  *
7  * Adapted directly from various IDPS files, InfUtil_Tim.h, InfUtil_TimApi.h,
8  * etc., published by Raytheon Company.
9  *
10  *******************************************************************************/
11 
12 #ifndef _VcstTime_h
13 #define _VcstTime_h
14 
20 #include <map>
21 #include <set>
22 #include <string>
23 #include <exception>
24 #include <time.h>
25 
26 #define MAX_LEAP 156
27 #define MAXBUFFER 256
28 
29 /*
30  * The following are all valid error numbers for the time utility
31  */
32 #define TIMERR_ROOT -1600
33 
34 #define TIMERR_BADDAY TIMERR_ROOT - 3
35 #define TIMERR_BADHOUR TIMERR_ROOT - 4
36 #define TIMERR_BADMIN TIMERR_ROOT - 5
37 #define TIMERR_BADMONTH TIMERR_ROOT - 6
38 #define TIMERR_BADMSEC TIMERR_ROOT - 7
39 #define TIMERR_BADSEC TIMERR_ROOT - 8
40 #define TIMERR_BADYEAR TIMERR_ROOT - 9
41 #define TIMERR_LEAPACCESS TIMERR_ROOT - 10
42 #define TIMERR_LEAPBADYEAR TIMERR_ROOT - 11
43 #define TIMERR_LEAPBADMONTH TIMERR_ROOT - 12
44 #define TIMERR_LEAPBADDAY TIMERR_ROOT - 13
45 #define TIMERR_LEAPBADLEAP TIMERR_ROOT - 14
46 #define TIMERR_LEAPOPEN TIMERR_ROOT - 15
47 #define TIMERR_MONTHCALC TIMERR_ROOT - 16
48 #define TIMERR_BADUTC TIMERR_ROOT - 17
49 #define TIMERR_UTCCOLON2 TIMERR_ROOT - 18
50 #define TIMERR_UTCDASH1 TIMERR_ROOT - 19
51 #define TIMERR_UTCDOT1 TIMERR_ROOT - 20
52 #define TIMERR_UTCMONTH TIMERR_ROOT - 21
53 #define TIMERR_UTCT TIMERR_ROOT - 22
54 #define TIMERR_NOINIT TIMERR_ROOT - 23
55 #define TIMERR_BADTAI TIMERR_ROOT - 24
56 
62 struct TimGdtStruct {
63  int year;
64  int month;
65  int day;
66  int hour;
67  int min;
68  int sec;
69  int mill;
70  int msec;
71  int wkday;
72  int numday;
73 
74 };
75 
84 struct LeapStruct {
85  int year;
86  int month;
87  int day;
88  double tai;
89  int leap;
90 };
91 
95 struct TimError {
96  int errNo;
98 };
99 
105 struct ltGDT {
106 
107  bool operator()(const TimGdtStruct* gdt1, const TimGdtStruct* gdt2) const {
108  if (gdt1 == NULL) {
109  if (gdt2 == NULL) {
110  return false;
111  }
112 
113  return true; // gdt1 == null && gdt2 !=null
114  } else if (gdt2 == NULL) //gdt1 != null && gdt2 == null
115  {
116  return false;
117  } else {
118  if (gdt1->year < gdt2->year) {
119  return true;
120  } else if ((gdt1->year == gdt2->year)
121  && (gdt1->month < gdt2->month)) {
122  return true;
123  } else if ((gdt1->year == gdt2->year)
124  && (gdt1->month == gdt2->month)
125  && (gdt1->day < gdt2->day)) {
126  return true;
127  } else if ((gdt1->year == gdt2->year)
128  && (gdt1->month == gdt2->month) && (gdt1->day == gdt2->day)
129  && (gdt1->hour < gdt2->hour)) {
130  return true;
131  } else if ((gdt1->year == gdt2->year)
132  && (gdt1->month == gdt2->month) && (gdt1->day == gdt2->day)
133  && (gdt1->hour == gdt2->hour) && (gdt1->min < gdt2->min)) {
134  return true;
135  } else if ((gdt1->year == gdt2->year)
136  && (gdt1->month == gdt2->month) && (gdt1->day == gdt2->day)
137  && (gdt1->hour == gdt2->hour) && (gdt1->min == gdt2->min)
138  && (gdt1->sec < gdt2->sec)) {
139  return true;
140  } else {
141  // We stop at second because we want to see if this GDT is
142  // during the same second. NOT that it is EXACTLY the same.
143  return false;
144  }
145  }
146  }
147 };
148 
153 struct ltLeap {
154 
155  bool operator()(const LeapStruct* leap1, const LeapStruct* leap2) const {
156  if (leap1 == NULL) {
157  if (leap2 == NULL) {
158  return false;
159  }
160  return true; // leap1 == null && leap2 !=null
161  } else if (leap2 == NULL) //leap1 != null && leap2 == null
162  {
163  return false;
164  } else {
165  if (leap1->tai < leap2->tai) {
166  // If the TAI is the same ALL other values should be the same
167  // in a consistent CDS Struct.
168  return true;
169  }
170  return false;
171  }
172  }
173 };
174 
179 struct lsCDS {
180 
181  bool operator()(const long long l1, const long long l2) const {
182  return l1 < l2;
183  }
184 };
185 
186 typedef std::set<const TimGdtStruct*, ltGDT> GDTSet;
187 typedef std::set<unsigned long long, lsCDS> CDSSet;
188 
193 class VcstTime {
194 public:
195 
202  static VcstTime* getInstance(void);
203 
215  int initialize(void);
216 
230  int initialize(const char* inLeapFile);
231 
239  double taiNow(void);
240 
248  void tai2Unix(double inTai, timespec*);
249 
257  double unix2Tai(timespec *tspec);
258 
267  int taiAddDelta(double *ioTai, double inDelta);
268 
277  int taiSubtractDelta(double *ioTai, double inDelta);
278 
290  int cds2Tai(unsigned long long inCds, double *outTai);
291 
303  int tai2Cds(double inTai, unsigned long long *outCds);
304 
312  int dispCds(unsigned long long inCds);
313 
325  int tai2Gdt(double inTai, TimGdtStruct *outGdt);
326 
338  int gdt2Tai(TimGdtStruct *inGdt, double *outTai);
339 
356  int tai2String(double inTai, char* outString, int inFormat = 1);
357 
370  int string2Tai(std::string inString, double *outTai);
371 
375  void printInfo();
376 
382  timespec getSystemTime();
383 
391  static std::string errorMessage(int errNo = 1);
392 
393 private:
394 
396 
397  enum tokenType {
398  NONE, INT, FLOAT, STRING, EOS
399  };
400 
401  // Constant memebers
402  static const char *TOKEN_SEPARATORS;
403 
404  // Constant memebers
405  static const char *TOKEN_SEPARATORS_UTC;
406 
409 
410  struct TokenStruct {
411  tokenType type;
412 
413  union tokenVal {
414  char *s;
415  int i;
416  float f;
417  long long l;
418  double d;
419  } val;
420  };
421 
426  VcstTime(void);
427 
436  void calcTai(TimGdtStruct *inGdt, double *outTai);
437 
445  int adjustLeapDay(int inYear);
446 
454  int calcLeapDays(int inYear);
455 
464  int getTaiLeapSecond(double inTai, int withLeap);
465 
473  int isExactLeapSecond(double inTai);
474 
484  int getLeapSecond(int inYear, int inMonth, int inDay);
485 
495  int readLeapSecondFile(const char * inFilename);
496 
510  int parseLeapFileLine(char *inBuffer, int *outYear, int *outMonth,
511  int *outDay, int *outLeap);
512 
526  TimGdtStruct utcParse(std::string inString);
527 
531  void displayLeap(void);
532 
540  void displayGdt(char *inPrompt, TimGdtStruct *inGdt);
541 
550  char *nextField(char *inBuffer, char delin, char *outSub);
551 
560  TokenStruct getNextToken(const char* tokens);
561 
572  TokenStruct getNextToken(char * parseString, const char* tokens);
573 
582  bool isNegativeLeapSecond(TimGdtStruct *inGdt);
583 
592  bool isNegativeLeapSecondCDS(long long inCDS);
593 
599  void initNegativeLeaps(void);
600 
601 private:
602  // Members
603 
605  struct LeapStruct myLeapList[MAX_LEAP + 1];
606 
608  int myNleap;
609 
610  // The pointer to the string currently being parsed.
611  char *myBuffer;
612 
614  static char myMonthStr[13][13];
615 
617  static int myDayInMonth[13];
618 
620  bool myInit;
621 
622  // A Set of TimGdtStructs representing negative Leaps
623  GDTSet* myGDTNegativeLeaps;
624 
625  // A Set of long long (CDS representations) Leaps representing negative Leaps
626  CDSSet* myCDSNegativeLeaps;
627 
628  // The hash map of month to integer conversion.
629  std::map<std::string, int> myMonthMap;
630 };
631 
635 extern const std::string EXCEPTION_STRING;
636 
640 extern const std::string ERR_BADDAY_MSG;
641 
645 extern const std::string ERR_BADHOUR_MSG;
646 
650 extern const std::string ERR_BADMIN_MSG;
651 
655 extern const std::string ERR_BADMONTH_MSG;
656 
660 extern const std::string ERR_BADMSEC_MSG;
661 
665 extern const std::string ERR_BADSEC_MSG;
666 
670 extern const std::string ERR_BADYEAR_MSG;
671 
675 extern const std::string ERR_LEAPACCESS_MSG;
676 
680 extern const std::string ERR_LEAPREAD_MSG;
681 
685 extern const std::string ERR_LEAPBADDAY_MSG;
686 
690 extern const std::string ERR_LEAPBADLEAP_MSG;
691 
695 extern const std::string ERR_LEAPBADMONTH_MSG;
696 
700 extern const std::string ERR_LEAPBADYEAR_MSG;
701 
705 extern const std::string ERR_LEAPOPEN_MSG;
706 
710 extern const std::string ERR_MONTHCALC_MSG;
711 
715 extern const std::string ERR_BADUTC_MSG;
716 
720 extern const std::string ERR_UTCCOLON2_MSG;
721 
725 extern const std::string ERR_UTCDASH1_MSG;
726 
730 extern const std::string ERR_UTCDOT1_MSG;
731 
735 extern const std::string ERR_UTCMONTH_MSG;
736 
740 extern const std::string ERR_UTCT_MSG;
741 
745 extern const std::string ERR_NOINIT_MSG;
746 
750 extern const std::string ERR_BADTAI_MSG;
751 
757 
761 extern const std::string IOEXCEPTION_Msg;
762 
768 class TimException : public std::exception {
769 public:
770 
774  static const int EXCEPTION_CODE;
775 
779  static const int NAME_NOT_FOUND;
780 
784  static const int ERR_BADDAY;
785 
789  static const int ERR_BADHOUR;
790 
794  static const int ERR_BADMIN;
795 
799  static const int ERR_BADMONTH;
800 
804  static const int ERR_BADMSEC;
805 
809  static const int ERR_BADSEC;
810 
814  static const int ERR_BADYEAR;
815 
819  static const int ERR_LEAPACCESS;
820 
824  static const int ERR_LEAPREAD;
825 
829  static const int ERR_LEAPBADYEAR;
830 
834  static const int ERR_LEAPBADMONTH;
835 
839  static const int ERR_LEAPBADDAY;
840 
844  static const int ERR_LEAPBADLEAP;
845 
849  static const int ERR_LEAPOPEN;
850 
854  static const int ERR_MONTHCALC;
855 
859  static const int ERR_BADUTC;
860 
864  static const int ERR_UTCCOLON2;
865 
869  static const int ERR_UTCDASH1;
870 
874  static const int ERR_UTCDOT1;
875 
879  static const int ERR_UTCMONTH;
880 
884  static const int ERR_UTCT;
885 
889  static const int ERR_NOINIT;
890 
894  static const int ERR_BADTAI;
895 
900  static const int MAX_TAI_VALUE_EXCEEDED;
901 
905  static const int IOEXCEPTION;
906 
911  TimException(void);
912 
921  TimException(const std::string& errorString, int errorCode);
922 
928  ~TimException(void) throw () {
929  if (myCause) {
930  delete myCause;
931  myCause = 0;
932  }
933  }
934  ;
935 
936  inline int getErrorCode() const {
937  return myErrorCode;
938  }
939 
940  inline std::string getErrorString() const {
941  return myErrorString;
942  }
943 
944 private:
945 
950  int myErrorCode;
951 
955  std::string myErrorString;
956 
960  std::exception* myCause;
961 };
962 
967 union TimCDS {
968 public:
969  unsigned long long ll;
970  unsigned char uc[8];
971 
972  //getters
973  unsigned short getDays();
974  unsigned int getMillis();
975  unsigned short getMicros();
976 
977  // Setters
978  void setDays(unsigned short);
979  void setMillis(unsigned int);
980  void setMicros(unsigned short);
981 
991  static int makeCds(unsigned int inDay, unsigned long long inMsec,
992  unsigned long long *outCds);
993 
1004  static int parseCds(unsigned long long inCds, unsigned int *outDay,
1005  unsigned long long *outMsec);
1006 
1015  static int displayCds(unsigned long long inCds);
1016 
1017 };
1018 
1019 #endif
1020 
int cds2Tai(unsigned long long inCds, double *outTai)
int leap
Definition: VcstTime.h:89
double tai
Definition: VcstTime.h:88
static const int ERR_MONTHCALC
Definition: VcstTime.h:854
bool operator()(const TimGdtStruct *gdt1, const TimGdtStruct *gdt2) const
Definition: VcstTime.h:107
timespec getSystemTime()
static const int ERR_LEAPOPEN
Definition: VcstTime.h:849
static const int ERR_UTCDOT1
Definition: VcstTime.h:874
void setMicros(unsigned short)
unsigned short getMicros()
int errNo
Definition: VcstTime.h:96
int taiSubtractDelta(double *ioTai, double inDelta)
const std::string ERR_BADHOUR_MSG
unsigned int getMillis()
int numday
Definition: VcstTime.h:72
double taiNow(void)
const std::string ERR_UTCMONTH_MSG
#define MAXBUFFER
Definition: VcstTime.h:27
#define NULL
Definition: decode_rs.h:63
const std::string MAX_TAI_VALUE_EXCEEDED_MSG
static const int ERR_LEAPBADMONTH
Definition: VcstTime.h:834
static const int ERR_LEAPBADYEAR
Definition: VcstTime.h:829
unsigned long long ll
Definition: VcstTime.h:969
static const int ERR_UTCDASH1
Definition: VcstTime.h:869
double unix2Tai(timespec *tspec)
std::string getErrorString() const
Definition: VcstTime.h:940
const std::string ERR_UTCDASH1_MSG
const std::string ERR_NOINIT_MSG
const std::string ERR_BADDAY_MSG
static const int ERR_BADMSEC
Definition: VcstTime.h:804
static const int NAME_NOT_FOUND
Definition: VcstTime.h:779
@ string
unsigned char uc[8]
Definition: VcstTime.h:970
int initialize(void)
TimException(void)
static const int MAX_TAI_VALUE_EXCEEDED
Definition: VcstTime.h:900
const std::string ERR_BADMSEC_MSG
int taiAddDelta(double *ioTai, double inDelta)
#define MAX_LEAP
Definition: VcstTime.h:26
const std::string ERR_BADUTC_MSG
~TimException(void)
Definition: VcstTime.h:928
const std::string IOEXCEPTION_Msg
const std::string ERR_MONTHCALC_MSG
static const int ERR_LEAPBADDAY
Definition: VcstTime.h:839
static int parseCds(unsigned long long inCds, unsigned int *outDay, unsigned long long *outMsec)
const std::string ERR_LEAPREAD_MSG
void setDays(unsigned short)
static const int ERR_BADMIN
Definition: VcstTime.h:794
static VcstTime * getInstance(void)
static const int ERR_BADUTC
Definition: VcstTime.h:859
std::set< unsigned long long, lsCDS > CDSSet
Definition: VcstTime.h:187
const std::string EXCEPTION_STRING
const std::string ERR_LEAPBADMONTH_MSG
static const int ERR_BADMONTH
Definition: VcstTime.h:799
static int displayCds(unsigned long long inCds)
char errmsg[MAXBUFFER]
Definition: VcstTime.h:97
static const int ERR_UTCMONTH
Definition: VcstTime.h:879
static const int ERR_LEAPREAD
Definition: VcstTime.h:824
const std::string ERR_LEAPBADYEAR_MSG
bool operator()(const LeapStruct *leap1, const LeapStruct *leap2) const
Definition: VcstTime.h:155
const std::string ERR_BADMIN_MSG
int tai2Cds(double inTai, unsigned long long *outCds)
int tai2Gdt(double inTai, TimGdtStruct *outGdt)
static const int ERR_LEAPACCESS
Definition: VcstTime.h:819
static const int ERR_LEAPBADLEAP
Definition: VcstTime.h:844
int tai2String(double inTai, char *outString, int inFormat=1)
int getErrorCode() const
Definition: VcstTime.h:936
static int makeCds(unsigned int inDay, unsigned long long inMsec, unsigned long long *outCds)
const std::string ERR_BADSEC_MSG
static const int ERR_UTCCOLON2
Definition: VcstTime.h:864
static const int ERR_UTCT
Definition: VcstTime.h:884
static const int ERR_BADTAI
Definition: VcstTime.h:894
static const int IOEXCEPTION
Definition: VcstTime.h:905
void tai2Unix(double inTai, timespec *)
const std::string ERR_LEAPBADLEAP_MSG
static std::string errorMessage(int errNo=1)
const std::string ERR_UTCDOT1_MSG
static const int ERR_BADHOUR
Definition: VcstTime.h:789
const std::string ERR_LEAPOPEN_MSG
const std::string ERR_LEAPBADDAY_MSG
const std::string ERR_UTCT_MSG
bool operator()(const long long l1, const long long l2) const
Definition: VcstTime.h:181
static const int ERR_BADSEC
Definition: VcstTime.h:809
void setMillis(unsigned int)
int string2Tai(std::string inString, double *outTai)
std::set< const TimGdtStruct *, ltGDT > GDTSet
Definition: VcstTime.h:186
static const int ERR_NOINIT
Definition: VcstTime.h:889
int gdt2Tai(TimGdtStruct *inGdt, double *outTai)
const std::string ERR_BADTAI_MSG
const std::string ERR_LEAPACCESS_MSG
const std::string ERR_BADYEAR_MSG
const std::string ERR_UTCCOLON2_MSG
static const int EXCEPTION_CODE
Definition: VcstTime.h:774
static const int ERR_BADYEAR
Definition: VcstTime.h:814
msiBandIdx val
Definition: l1c_msi.cpp:34
void printInfo()
static const int ERR_BADDAY
Definition: VcstTime.h:784
int dispCds(unsigned long long inCds)
const std::string ERR_BADMONTH_MSG
unsigned short getDays()