OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
time_test.c
Go to the documentation of this file.
1 #include <timeutils.h>
2 
3 #include <stdio.h>
4 #include <string.h>
5 
6 int main(int argc, char *argv[]) {
7  // test this leap second record
8  // 2015 JUL 1 =JD 2457204.5 TAI-UTC= 36.0 S + (MJD - 41317.) X 0.0 S
9 
10  double unixTime; // unix time = UTC epoch 1970
11  double tai58Time; // TAI58 time
12  double tai93Time; // TAI93 time
13  double leapSeconds;
14 
15  double leapSecondsAt93 = 27;
16  double tai58at93 = 1104537600.0 + leapSecondsAt93;
17  double unixAt93 = ymds2unix(1993, 1, 1, 0.0);
18 
19  unixTime = ymds2unix(1958, 1, 1, 0.0);
20  tai93Time = unix_to_tai93(unixTime);
21  printf("%s unix=%.1f, TAI93=%.1f\n", unix2isodate(unixTime, 'G'), unixTime, tai93Time);
22 
23  unixTime = ymds2unix(1993, 1, 1, 0.0);
24  tai93Time = unix_to_tai93(unixTime);
25  printf("%s unix=%.1f, TAI93=%.1f\n", unix2isodate(unixTime, 'G'), unixTime, tai93Time);
26 
27  unixTime = ymds2unix(2015, 7, 1, 0.0);
28  tai93Time = unix_to_tai93(unixTime);
29  printf("%s unix=%.1f, TAI93=%.1f\n", unix2isodate(unixTime, 'G'), unixTime, tai93Time);
30 
31  unixTime = tai93_to_unix(tai93Time);
32  printf("%s unix=%.1f, TAI93=%.1f\n", unix2isodate(unixTime, 'G'), unixTime, tai93Time);
33 
34  double u93 = ymds2unix(1993, 1, 1, 0.0);
35  double u58 = ymds2unix(1958, 1, 1, 0.0);
36  printf("unix93 - unix58 = %.1f\n", u93-u58);
37 
38 
39  printf("\n\n");
40  unixTime = ymds2unix(2015, 7, 1, 0.0);
41  unixTime -= 11;
42 
43  // loop over the leap second
44  for(int i=0; i<22; i++) {
45  tai58Time = unix_to_tai58(unixTime);
46  tai93Time = unix_to_tai93(unixTime);
47  leapSeconds = tai93Time - (unixTime - unixAt93) + leapSecondsAt93;
48  printf("%s unix=%.1f, TAI58=%.1f, TAI93=%.1f, leap=%.1f\n", unix2isodate(unixTime, 'G'),
49  unixTime, tai58Time, tai93Time, leapSeconds);
50  unixTime += 1;
51  }
52 
53 
54  printf("\n\n");
55  unixTime = ymds2unix(2015, 7, 1, 0.0);
56  tai93Time = unix_to_tai93(unixTime);
57  tai93Time -= 11;
58 
59  for(int i=0; i<23; i++) {
60  unixTime = tai93_to_unix(tai93Time);
61  tai58Time = tai93Time + tai58at93;
62  leapSeconds = tai93Time - (unixTime - unixAt93) + leapSecondsAt93;
63  printf("%s unix=%.1f, TAI58=%.1f, TAI93=%.1f, leap=%.1f\n", unix2isodate(unixTime, 'G'),
64  unixTime, tai58Time, tai93Time, leapSeconds);
65  tai93Time += 1;
66  }
67 
68 
69  // test the values around the leap second 2015-7-1
70  int returnVal = EXIT_SUCCESS;
71  int numPoints = 4;
72  double unix_times[] = {1435708799, 1435708799, 1435708800, 1435708801};
73  double tai58[] = {1814400034, 1814400035, 1814400036, 1814400037};
74  double tai93[] = { 709862407, 709862408, 709862409, 709862410};
75 
76  // test unix -> tai58
77  // and unix -> tai93
78  for(int i=0; i<numPoints; i++) {
79  if(i==1) // we need to skip the second point since UTC can not represent it
80  i++;
81  tai58Time = unix_to_tai58(unix_times[i]);
82  if(tai58Time != tai58[i]) {
83  printf("unix_to_tai58(%.1f) Failed, %.1f != %.1f\n", unix_times[i], tai58Time, tai58[i]);
84  returnVal = EXIT_FAILURE;
85  }
86  tai93Time = unix_to_tai93(unix_times[i]);
87  if(tai93Time != tai93[i]) {
88  printf("unix_to_tai93(%.1f) Failed, %.1f != %.1f\n", unix_times[i], tai58Time, tai58[i]);
89  returnVal = EXIT_FAILURE;
90  }
91  }
92 
93  // test tai58 -> unix
94  // and tai93 -> unix
95  for(int i=0; i<numPoints; i++) {
96  unixTime = tai58_to_unix(tai58[i]);
97  if(unixTime != unix_times[i]) {
98  printf("tai58_to_unix(%.1f) Failed, %.1f != %.1f\n", tai58[i], unixTime, unix_times[i]);
99  returnVal = EXIT_FAILURE;
100  }
101  unixTime = tai93_to_unix(tai93[i]);
102  if(unixTime != unix_times[i]) {
103  printf("tai93_to_unix(%.1f) Failed, %.1f != %.1f\n", tai93[i], unixTime, unix_times[i]);
104  returnVal = EXIT_FAILURE;
105  }
106  }
107 
108  return returnVal;
109 }
110 
#define EXIT_SUCCESS
Definition: GEO_basic.h:72
double unix_to_tai93(double unixtime)
Definition: yds2tai.c:11
double tai93_to_unix(double tai93)
Definition: yds2tai.c:16
double unix_to_tai58(double unixtime)
Definition: yds2tai.c:23
int main(int argc, char *argv[])
Definition: time_test.c:6
double tai58_to_unix(double tai58)
Definition: yds2tai.c:29
char * unix2isodate(double dtime, char zone)
Definition: unix2isodate.c:10
double ymds2unix(short year, short month, short day, double secs)
int i
Definition: decode_rs.h:71