LAL  7.5.0.1-89842e6
UTCtoGPSTest.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2016 Karl Wette
3 * Copyright (C) 2007 David Chin
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with with program; see the file COPYING. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301 USA
19 */
20 
21 #include <stdio.h>
22 #include <math.h>
23 #include <stdlib.h>
24 #include <time.h>
25 
26 #include <lal/LALStdlib.h>
27 #include <lal/Date.h>
28 #include <lal/AVFactories.h>
29 
30 
31 static int test(struct tm *t, int correct_gps)
32 {
33 
34  {
35  struct tm tf = *t;
36  tf.tm_wday = tf.tm_yday = -1;
37  XLAL_CHECK(XLALFillUTC(&tf) != NULL, XLAL_EFUNC, "UTCtoGPSTest: error in XLALFillUTC()");
38  XLAL_CHECK(tf.tm_wday == t->tm_wday, XLAL_EFAILED, "UTCtoGPSTest: incorrect day of week\n output = %d\n expected = %d\n", tf.tm_wday, t->tm_wday);
39  XLAL_CHECK(tf.tm_yday == t->tm_yday, XLAL_EFAILED, "UTCtoGPSTest: incorrect day of year\n output = %d\n expected = %d\n", tf.tm_yday, t->tm_yday);
40  }
41 
42  t->tm_wday = t->tm_yday = -1;
43  int gps = XLALUTCToGPS(t);
44  XLAL_CHECK(xlalErrno == 0, XLAL_EFUNC, "UTCtoGPSTest: error in XLALUTCToGPS()");
45 
46  if (gps != correct_gps)
47  {
48  char buf[64];
49  strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t);
50  XLAL_ERROR(XLAL_EFAILED, "UTCtoGPSTest: incorrect UTC to GPS conversion\n input = %s\n output = %d\n expected = %d\n", buf, gps, correct_gps);
51  }
52 
53  return XLAL_SUCCESS;
54 
55 }
56 
57 int main(void)
58 {
59 
60  struct tm utcDate;
61  time_t sec;
62 
63  utcDate.tm_year = 80;
64  utcDate.tm_yday = 5;
65  utcDate.tm_wday = 0;
66  utcDate.tm_mon = 0;
67  utcDate.tm_mday = 6;
68  utcDate.tm_hour = 0;
69  utcDate.tm_min = 0;
70  utcDate.tm_sec = 0;
71  utcDate.tm_isdst = 0;
73 
74  utcDate.tm_year = 94;
75  utcDate.tm_yday = 186;
76  utcDate.tm_wday = 3;
77  utcDate.tm_mon = 6;
78  utcDate.tm_mday = 6;
79  utcDate.tm_hour = 23;
80  utcDate.tm_min = 59;
81  utcDate.tm_sec = 50;
82  utcDate.tm_isdst = 1;
83  XLAL_CHECK_MAIN(test(&utcDate, 457574400) == XLAL_SUCCESS, XLAL_EFAILED);
84 
85  utcDate.tm_year = 94;
86  utcDate.tm_yday = 181;
87  utcDate.tm_wday = 5;
88  utcDate.tm_mon = 6;
89  utcDate.tm_mday = 1;
90  utcDate.tm_hour = 0;
91  utcDate.tm_min = 0;
92  utcDate.tm_sec = 0;
93  utcDate.tm_isdst = 1;
94  XLAL_CHECK_MAIN(test(&utcDate, 457056010) == XLAL_SUCCESS, XLAL_EFAILED);
95 
96  for (sec = 457056007; sec < 457056012; sec++)
97  {
98  utcDate.tm_year = 94;
99  utcDate.tm_sec = 58 + (sec - 457056007);
100  if (utcDate.tm_sec <= 60)
101  {
102  utcDate.tm_yday = 180;
103  utcDate.tm_wday = 4;
104  utcDate.tm_mon = 5;
105  utcDate.tm_mday = 30;
106  utcDate.tm_hour = 23;
107  utcDate.tm_min = 59;
108  }
109  else
110  {
111  utcDate.tm_sec -= 61;
112  utcDate.tm_yday = 181;
113  utcDate.tm_wday = 5;
114  utcDate.tm_mon = 6;
115  utcDate.tm_mday = 1;
116  utcDate.tm_hour = 00;
117  utcDate.tm_min = 00;
118  }
119  utcDate.tm_isdst = 1;
120  XLAL_CHECK_MAIN(test(&utcDate, sec) == XLAL_SUCCESS, XLAL_EFAILED);
121  }
122 
123  utcDate.tm_year = 94;
124  utcDate.tm_yday = 319;
125  utcDate.tm_wday = 3;
126  utcDate.tm_mon = 10;
127  utcDate.tm_mday = 16;
128  utcDate.tm_hour = 0;
129  utcDate.tm_min = 0;
130  utcDate.tm_sec = 0;
131  utcDate.tm_isdst = 0;
132  XLAL_CHECK_MAIN(test(&utcDate, 468979210) == XLAL_SUCCESS, XLAL_EFAILED);
133 
134  /* Y2038 check */
135  const time_t max_32bit_unix_time = (time_t)LAL_INT4_MAX;
136  const int max_32bit_unix_time_as_gps = 1831518865;
137  utcDate.tm_year = 138;
138  utcDate.tm_yday = 18;
139  utcDate.tm_wday = 2;
140  utcDate.tm_mon = 0;
141  utcDate.tm_mday = 19;
142  utcDate.tm_hour = 3;
143  utcDate.tm_min = 14;
144  utcDate.tm_sec = 7;
145  utcDate.tm_isdst = 0;
146  XLAL_CHECK_MAIN(XLALSecondsSinceUnixEpoch(&utcDate) == max_32bit_unix_time, XLAL_EFAILED);
147  XLAL_CHECK_MAIN(test(&utcDate, max_32bit_unix_time_as_gps) == XLAL_SUCCESS, XLAL_EFAILED);
148  utcDate.tm_yday = 18;
149  utcDate.tm_wday = 2;
150  utcDate.tm_sec = 8;
151  XLAL_CHECK_MAIN(XLALSecondsSinceUnixEpoch(&utcDate) == max_32bit_unix_time + 1, XLAL_EFAILED);
152  XLAL_CHECK_MAIN(test(&utcDate, max_32bit_unix_time_as_gps + 1) == XLAL_SUCCESS, XLAL_EFAILED);
153 
155 
156  return EXIT_SUCCESS;
157 
158 }
void LALCheckMemoryLeaks(void)
Definition: LALMalloc.c:784
static int test(struct tm *t, int correct_gps)
Definition: UTCtoGPSTest.c:31
int main(void)
Definition: UTCtoGPSTest.c:57
#define LAL_INT4_MAX
Definition: LALConstants.h:83
INT4 XLALUTCToGPS(const struct tm *utc)
Returns the GPS seconds since the GPS epoch for a specified UTC time structure.
time_t XLALSecondsSinceUnixEpoch(const struct tm *utc)
Compute Unix epoch time: seconds since 1970 January 1 0h UTC (POSIX:2001 definition of Unix Epoch).
struct tm * XLALFillUTC(struct tm *utc)
Fill in derived fields, e.g.
#define xlalErrno
Modifiable lvalue containing the XLAL error number.
Definition: XLALError.h:571
#define XLAL_ERROR(...)
Macro to invoke a failure from a XLAL routine returning an integer.
Definition: XLALError.h:700
#define XLAL_CHECK(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a function that returns an integ...
Definition: XLALError.h:810
#define XLAL_CHECK_MAIN(assertion,...)
Macro to test an assertion and invoke a failure if it is not true in a C main() routine.
Definition: XLALError.h:885
@ XLAL_SUCCESS
Success return value (not an error number)
Definition: XLALError.h:401
@ XLAL_EFUNC
Internal function call failed bit: "or" this with existing error number.
Definition: XLALError.h:462
@ XLAL_EFAILED
Generic failure.
Definition: XLALError.h:418