LAL  7.5.0.1-fe68b98
GPStoUTCTest.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007 David Chin
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19 
20 #include <stdio.h>
21 #include <math.h>
22 #include <stdlib.h>
23 #include <time.h>
24 
25 #include <lal/LALStdlib.h>
26 #include <lal/Date.h>
27 #include <lal/AVFactories.h>
28 
29 
30 int main(void)
31 {
32  LIGOTimeGPS gpsTime = {0, 0};
33  LIGOTimeGPS tmpGps = {0, 0};
34  struct tm utcDate;
35  CHARVector *timestamp = NULL;
36  char refstamp[128];
37  /* char infostr[256]; */
38 
39  timestamp = XLALCreateCHARVector(128);
40 
41  /*
42  * GPS 0 == 1980-01-06 00:00:00 UTC Sun
43  */
44  if(!XLALGPSToUTC(&utcDate, gpsTime.gpsSeconds))
45  {
46  fprintf(stderr, "TestGPStoUTC: XLALGPSToUTC() failed, line %i, %s\n",
47  __LINE__, "$Id$");
48  return 1;
49  }
50 
51  strftime(timestamp->data, timestamp->length, "%F %T UTC %a", &utcDate);
52 
53  sprintf(refstamp, "1980-01-06 00:00:00 UTC Sun");
54 
55  if (lalDebugLevel > 0)
56  {
57  fprintf(stderr, "refstamp = %s\n", refstamp);
58  fprintf(stderr, "timestamp = %s\n", timestamp->data);
59  }
60 
61  if (strcmp(refstamp, timestamp->data) != 0)
62  {
63  fprintf(stderr, "XLALGPStoUTC conversion failed: wrong UTC result\n");
64  fprintf(stderr, "TestGPStoUTC: date strings do not match, line %i, %s\n",
65  __LINE__, "$Id$");
66  XLALDestroyCHARVector(timestamp);
67  return 1;
68  }
69 
70  /*
71  * GPS 457574400 == 1994-07-06 23:59:50 UTC Wed
72  */
73  gpsTime.gpsSeconds = 457574400;
74  gpsTime.gpsNanoSeconds = 0;
75 
76  if(!XLALGPSToUTC(&utcDate, gpsTime.gpsSeconds))
77  {
78  fprintf(stderr, "TestGPStoUTC: XLALGPSToUTC() failed, line %i, %s\n",
79  __LINE__, "$Id$");
80  return 1;
81  }
82 
83  strftime(timestamp->data, timestamp->length, "%F %T UTC %a", &utcDate);
84 
85  sprintf(refstamp, "1994-07-06 23:59:50 UTC Wed");
86 
87  if (lalDebugLevel > 0)
88  {
89  fprintf(stderr, "refstamp = %s\n", refstamp);
90  fprintf(stderr, "timestamp = %s\n", timestamp->data);
91  }
92 
93  if (strcmp(refstamp, timestamp->data) != 0)
94  {
95  fprintf(stderr, "GPStoUTC conversion failed: wrong UTC result\n");
96  XLALDestroyCHARVector(timestamp);
97  return 1;
98  }
99 
100  /*
101  * GPS 599184012 == 1998-12-31 23:59:60 UTC Thu (leap second introduced)
102  */
103  gpsTime.gpsSeconds = 599184012;
104  gpsTime.gpsNanoSeconds = 0;
105 
106  if(!XLALGPSToUTC(&utcDate, gpsTime.gpsSeconds))
107  {
108  fprintf(stderr, "TestGPStoUTC: XLALGPSToUTC() failed, line %i, %s\n",
109  __LINE__, "$Id$");
110  return 1;
111  }
112 
113  strftime(timestamp->data, timestamp->length, "%F %T UTC %a", &utcDate);
114 
115  sprintf(refstamp, "1998-12-31 23:59:60 UTC Thu");
116 
117  if (lalDebugLevel > 0)
118  {
119  fprintf(stderr, "refstamp = %s\n", refstamp);
120  fprintf(stderr, "timestamp = %s\n", timestamp->data);
121  }
122 
123  if (strcmp(refstamp, timestamp->data) != 0)
124  {
125  fprintf(stderr, "GPStoUTC conversion failed: wrong UTC result\n");
126  XLALDestroyCHARVector(timestamp);
127  return 1;
128  }
129 
130  /*
131  * UPDATEME
132  * GPS 835747214 == 2006-07-01 00:00:00 (one second past expiry)
133  * Expect to fail with status code 5
134  * (see the static constant maxtestedGPS in src/GPStoUTC.c)
135  */
136  gpsTime.gpsSeconds = 835747214; /* use maxtestedGPS + 1 */
137  gpsTime.gpsNanoSeconds = 0;
138 
139  if(!XLALGPSToUTC(&utcDate, gpsTime.gpsSeconds))
140  {
141  fprintf(stderr, "TestGPStoUTC: XLALGPSToUTC() failed, line %i, %s\n",
142  __LINE__, "$Id$");
143  return 1;
144  }
145 
146  strftime(timestamp->data, timestamp->length, "%F %T UTC %a", &utcDate);
147 
148  /* UPDATEME */
149  /* the date here should be one second after maxtestedGPS */
150  sprintf(refstamp, "2006-07-01 00:00:00 UTC Sat");
151 
152  if (lalDebugLevel > 0)
153  {
154  fprintf(stderr, "refstamp = %s\n", refstamp);
155  fprintf(stderr, "timestamp = %s\n", timestamp->data);
156  }
157 
158  if (strcmp(refstamp, timestamp->data) != 0)
159  {
160  fprintf(stderr, "GPStoUTC conversion failed: wrong UTC result\n");
161  XLALDestroyCHARVector(timestamp);
162  return 1;
163  }
164 
165 
166  /*
167  * GPS -44000
168  * should fail. No leap seconds, yet.
169  */
170  gpsTime.gpsSeconds = -44000;
171  gpsTime.gpsNanoSeconds = 0;
172 
173  if(!XLALGPSToUTC(&utcDate, gpsTime.gpsSeconds))
174  {
175  if (XLALGetBaseErrno() != XLAL_EDOM) /* not expected error */
176  {
177  fprintf(stderr, "TestGPStoUTC: XLALGPSToUTC() failed, line %i, %s\n",
178  __LINE__, "$Id$");
179  return 1;
180  }
181  }
182  else /* no error */
183  {
184  fprintf(stderr, "TestGPStoUTC: XLALGPSToUTC() failed, line %i, %s\n",
185  __LINE__, "$Id$");
186  return 1;
187  }
188  XLALClearErrno();
189 
190  /*
191  * Now, let's try converting GPS to UTC and back
192  */
193  gpsTime.gpsSeconds = 701654354;
194  gpsTime.gpsNanoSeconds = 0;
195 
196  if(!XLALGPSToUTC(&utcDate, gpsTime.gpsSeconds))
197  {
198  fprintf(stderr, "TestGPStoUTC: XLALGPSToUTC() failed, line %i, %s\n",
199  __LINE__, "$Id$");
200  return 1;
201  }
202 
203  XLALGPSSet(&tmpGps, XLALUTCToGPS(&utcDate), 0);
204  if (XLALGetBaseErrno() && lalDebugLevel > 0)
205  {
206  fprintf(stderr,
207  "TestUTCtoGPS: error in XLALUTCToGPS, line %i, %s\n",
208  __LINE__, "$Id$");
209  return 1;
210  }
211 
212  if (lalDebugLevel > 0)
213  {
214  fprintf(stderr, "\tgpsTime = {%d, %d}\n", gpsTime.gpsSeconds,
215  gpsTime.gpsNanoSeconds);
216  fprintf(stderr, "\ttmpGps = {%d, %d}\n", tmpGps.gpsSeconds,
217  tmpGps.gpsNanoSeconds);
218  }
219 
220 
221  if (tmpGps.gpsSeconds != gpsTime.gpsSeconds ||
222  tmpGps.gpsNanoSeconds != gpsTime.gpsNanoSeconds)
223  {
224  fprintf(stderr,
225  "TestGPStoUTC: conversion from GPS to UTC and back to GPS failed, line %i, %s\n", __LINE__, "$Id$");
226  return 1;
227  }
228 
229 
230 
231  /*
232  * Cleanup and exit
233  */
234  XLALDestroyCHARVector(timestamp);
236  return 0;
237 }
int main(void)
Definition: GPStoUTCTest.c:30
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
#define fprintf
#define lalDebugLevel
Definition: LALDebugLevel.h:58
CHARVector * XLALCreateCHARVector(UINT4 length)
void XLALDestroyCHARVector(CHARVector *vector)
INT4 XLALUTCToGPS(const struct tm *utc)
Returns the GPS seconds since the GPS epoch for a specified UTC time structure.
struct tm * XLALGPSToUTC(struct tm *utc, INT4 gpssec)
Returns a pointer to a tm structure representing the time specified in seconds since the GPS epoch.
int XLALGetBaseErrno(void)
Gets the XLAL base error number ignoring the internal-function-failed flag.
Definition: XLALError.c:356
int XLALClearErrno(void)
Clears the XLAL error number, returns the old value.
Definition: XLALError.c:363
@ XLAL_EDOM
Input domain error.
Definition: XLALError.h:410
LIGOTimeGPS * XLALGPSSet(LIGOTimeGPS *epoch, INT4 gpssec, INT8 gpsnan)
Sets GPS time given GPS integer seconds and residual nanoseconds.
Definition: XLALTime.c:63
Vector of type CHAR, see DATATYPE-Vector types for more details.
Definition: LALDatatypes.h:73
Epoch relative to GPS epoch, see LIGOTimeGPS type for more details.
Definition: LALDatatypes.h:458
INT4 gpsSeconds
Seconds since 0h UTC 6 Jan 1980.
Definition: LALDatatypes.h:459
INT4 gpsNanoSeconds
Residual nanoseconds.
Definition: LALDatatypes.h:460