LAL  7.5.0.1-89842e6
JulianDayTest.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 David Chin, Jolien Creighton
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with with program; see the file COPYING. If not, write to the Free
16  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 
20 #include <config.h>
21 #include <math.h>
22 #include <string.h>
23 #include <lal/LALStdlib.h>
24 #include <lal/Date.h>
25 #include <lal/XLALError.h>
26 
27 #ifndef HAVE_LOCALTIME_R
28 #define localtime_r(timep, result) memcpy((result), localtime(timep), sizeof(struct tm))
29 #endif
30 
31 #define SUCCESS 0
32 #define FAIL_JULIAN_DAY 1
33 #define FAIL_MOD_JULIAN_DAY 2
34 #define FAIL_JULIAN_DATE 3
35 #define FAIL_MOD_JULIAN_DATE 4
36 
37 /* good to 1 micro-day */
38 const REAL8 julian_precision = 1.e-6;
39 
40 const REAL8 coarse_precision = 0.001;
41 
42 /* Output of NASA/AMES code:
43  Date/time: 1 1 0 12 0 0
44  PDS format: "2000-01-01T12:00:00.000Z"
45  SQL format: "Jan 1, 2000 12:00:00:000"
46 
47  UTC day = 0
48  UTC secs = 43200.000000
49  TAI secs = 32.000000
50  ET secs = 64.183927
51 
52  JD (UTC) = 2451545.000000
53  JD (TAI) = 2451545.000370
54  JD (ET) = 2451545.001115
55  MJD (UTC) = 51544.500000
56  MJD (TAI) = 51544.500370
57  MJD (ET) = 51544.501115
58 
59  Date/time: 94 11 16 0 0 0
60  PDS format: "1994-11-16T00:00:00.000Z"
61  SQL format: "Nov 16, 1994 00:00:00:000"
62 
63  UTC day = -1872
64  UTC secs = 0.000000
65  TAI secs = -161783971.000000
66  ET secs = -161783938.817245
67 
68  JD (UTC) = 2449672.500000
69  JD (TAI) = 2449672.500336
70  JD (ET) = 2449672.501081
71  MJD (UTC) = 49672.000000
72  MJD (TAI) = 49672.000336
73  MJD (ET) = 49672.001081
74 
75  Date/time: 01 05 15 02 37 54
76  PDS format: "2001-05-15T02:37:54.000Z"
77  SQL format: "May 15, 2001 02:37:54:000"
78 
79  UTC day = 500
80  UTC secs = 9474.000000
81  TAI secs = 43166306.000000
82  ET secs = 43166338.185257
83 
84  JD (UTC) = 2452044.609653
85  JD (TAI) = 2452044.610023
86  JD (ET) = 2452044.610768
87  MJD (UTC) = 52044.109653
88  MJD (TAI) = 52044.110023
89  MJD (ET) = 52044.110768
90 
91 
92  http://tycho.usno.navy.mil/cgi-bin/date
93 15-May-01
94 MJD 52044.109653
95 UTC 02:37:54
96 
97 */
98 
99 
100 /*
101  * pass 0 for expected_julian_day or expected_modified_julian_day to disable testing
102  */
103 
104 static int test(const struct tm *utc, double expected_julian_day, double expected_modified_julian_day, int line)
105 {
106  double julian_day;
107  double modified_julian_day;
108  int result = 0;
109 
110  if(lalDebugLevel) {
111  char buf[64];
112  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", utc);
113  fprintf(stderr, "Testing %s ...\n", buf);
114  }
115 
116  julian_day = XLALConvertCivilTimeToJD(utc);
117  modified_julian_day = XLALConvertCivilTimeToMJD(utc);
118 
119  if(expected_julian_day && (julian_day != expected_julian_day)) {
120  fprintf(stderr, "XLALConvertCivilTimeToJD() failed (line %d): expected %.17g got %.17g\n", line, expected_julian_day, julian_day);
121  result = -1;
122  } else if(lalDebugLevel) {
123  fprintf(stderr, "XLALConvertCivilTimeToJD() returned %.16g\n", julian_day);
124  }
125  if(expected_modified_julian_day && (modified_julian_day != expected_modified_julian_day)) {
126  fprintf(stderr, "XLALModifiedJulianDayUTC() failed (line %d): expected %.17g got %.17g\n", line, expected_modified_julian_day, modified_julian_day);
127  result = -1;
128  } else if(lalDebugLevel) {
129  fprintf(stderr, "XLALModifiedJulianDayUTC() returned %.17g\n", modified_julian_day);
130  }
131 
132  return result;
133 }
134 
135 
136 int main(void)
137 {
138  time_t now;
139  struct tm tnow, utc;
140 #if 0
141  REAL8 ref_julian_day;
142  REAL8 julian_day;
143  REAL8 ref_mod_julian_day;
144  REAL8 mod_julian_day;
145 #endif
146 
147 
148  /*
149  * Distinctly not robust
150  */
151 
152 
153  /*
154  * Get current local time
155  */
156 
157  time(&now);
158  localtime_r(&now, &tnow);
159  if(test(&tnow, 0, 0, __LINE__))
160  return 1;
161 
162  /*
163  * Check Julian Day/Date for special dates and times
164  */
165 
166  utc.tm_sec = 0;
167  utc.tm_min = 0;
168  utc.tm_hour = 12;
169  utc.tm_mday = 1;
170  utc.tm_mon = 0;
171  utc.tm_year = 100;
172  utc.tm_wday = 6;
173  utc.tm_yday = 0;
174  utc.tm_isdst = 0;
175 
176  if(test(&utc, 2451545.0, 51544.5, __LINE__))
177  return 1;
178 
179  utc.tm_sec = 0;
180  utc.tm_min = 0;
181  utc.tm_hour = 11;
182  utc.tm_mday = 1;
183  utc.tm_mon = 0;
184  utc.tm_year = 100;
185  utc.tm_wday = 6;
186  utc.tm_yday = 0;
187  utc.tm_isdst = 0;
188 
189  if(test(&utc, 2451544.9583333333, 51544.458333333489, __LINE__))
190  return 1;
191 
192  /* */
193 
194  if(lalDebugLevel > 1) {
195  utc.tm_sec = 0;
196  utc.tm_min = 0;
197  utc.tm_hour = 11;
198  utc.tm_mday = 1;
199  utc.tm_mon = 0;
200  utc.tm_year = -100;
201  utc.tm_wday = 6;
202  utc.tm_yday = 0;
203  utc.tm_isdst = 0;
204 
205  /* here, we EXPECT an error */
206  if(!test(&utc, XLAL_REAL8_FAIL_NAN, XLAL_FAILURE, __LINE__))
207  return 1;
208  XLALClearErrno();
209 
210  }
211 
212 
213 
214  /* */
215 
216  utc.tm_sec = 0;
217  utc.tm_min = 0;
218  utc.tm_hour = 0;
219  utc.tm_mday = 16;
220  utc.tm_mon = 10;
221  utc.tm_year = 94;
222  utc.tm_wday = 3;
223  utc.tm_yday = 0;
224  utc.tm_isdst = 0;
225 
226  if(test(&utc, 2449672.5, 49672.0, __LINE__))
227  return 1;
228 
229  /* */
230 
231  utc.tm_sec = 54;
232  utc.tm_min = 37;
233  utc.tm_hour = 2;
234  utc.tm_mday = 15;
235  utc.tm_mon = 4;
236  utc.tm_year = 101;
237  utc.tm_wday = 1;
238  utc.tm_yday = 0;
239  utc.tm_isdst = 1;
240 
241  if(test(&utc, 2452044.6096527777, 52044.109652777668, __LINE__))
242  return 1;
243 
244  return SUCCESS;
245 }
int main(void)
const REAL8 julian_precision
Definition: JulianDayTest.c:38
#define SUCCESS
Definition: JulianDayTest.c:31
#define localtime_r(timep, result)
Definition: JulianDayTest.c:28
static int test(const struct tm *utc, double expected_julian_day, double expected_modified_julian_day, int line)
const REAL8 coarse_precision
Definition: JulianDayTest.c:40
#define fprintf
double REAL8
Double precision real floating-point number (8 bytes).
#define lalDebugLevel
Definition: LALDebugLevel.h:58
REAL8 XLALConvertCivilTimeToJD(const struct tm *civil)
Returns the Julian Day (JD) corresponding to the civil date and time given in a broken down time stru...
REAL8 XLALConvertCivilTimeToMJD(const struct tm *civil)
Returns the Modified Julian Day MJD corresponding to the civil date and time given in a broken down t...
#define XLAL_REAL8_FAIL_NAN
Floating-point value of the XLAL REAL8 failure NaN.
Definition: XLALError.h:389
int XLALClearErrno(void)
Clears the XLAL error number, returns the old value.
Definition: XLALError.c:363
@ XLAL_FAILURE
Failure return value (not an error number)
Definition: XLALError.h:402