mkgmtime.c
Go to the documentation of this file.
1 /* mkgmtime.c - make time corresponding to a GMT timeval struct
2  $Id: mkgmtime.c,v 1.10 2003/10/22 18:50:12 rjs3 Exp $
3 
4  * Copyright (c) 1998-2003 Carnegie Mellon University. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The name "Carnegie Mellon University" must not be used to
19  * endorse or promote products derived from this software without
20  * prior written permission. For permission or any other legal
21  * details, please contact
22  * Office of Technology Transfer
23  * Carnegie Mellon University
24  * 5000 Forbes Avenue
25  * Pittsburgh, PA 15213-3890
26  * (412) 268-4387, fax: (412) 268-7395
27  * tech-transfer@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  * acknowledgment:
31  * "This product includes software developed by Computing Services
32  * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  *
42  *
43  */
44 /*
45  * Copyright (c) 1987, 1989, 1993
46  * The Regents of the University of California. All rights reserved.
47  *
48  * This code is derived from software contributed to Berkeley by
49  * Arthur David Olson of the National Cancer Institute.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  * notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  * notice, this list of conditions and the following disclaimer in the
58  * documentation and/or other materials provided with the distribution.
59  * 3. All advertising materials mentioning features or use of this software
60  * must display the following acknowledgement:
61  * This product includes software developed by the University of
62  * California, Berkeley and its contributors.
63  * 4. Neither the name of the University nor the names of its contributors
64  * may be used to endorse or promote products derived from this software
65  * without specific prior written permission.
66  *
67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77  * SUCH DAMAGE.
78  */
79 
80 /*
81 ** Adapted from code provided by Robert Elz, who writes:
82 ** The "best" way to do mktime I think is based on an idea of Bob
83 ** Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
84 ** It does a binary search of the time_t space. Since time_t's are
85 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
86 ** would still be very reasonable).
87 */
88 
89 #include "ublox_gps/mkgmtime.h"
90 #ifndef WRONG
91 #define WRONG (-1)
92 #endif /* !defined WRONG */
93 
94 static int tmcomp(register const struct tm * const atmp,
95  register const struct tm * const btmp)
96 {
97  register int result;
98 
99  if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
100  (result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
101  (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
102  (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
103  (result = (atmp->tm_min - btmp->tm_min)) == 0)
104  result = atmp->tm_sec - btmp->tm_sec;
105  return result;
106 }
107 
108 time_t mkgmtime(struct tm * const tmp) {
109  register int dir;
110  register int bits;
111  register int saved_seconds;
112  time_t t;
113  struct tm yourtm, *mytm;
114 
115  yourtm = *tmp;
116  saved_seconds = yourtm.tm_sec;
117  yourtm.tm_sec = 0;
118  /*
119  ** Calculate the number of magnitude bits in a time_t
120  ** (this works regardless of whether time_t is
121  ** signed or unsigned, though lint complains if unsigned).
122  */
123  for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
124  ;
125  /*
126  ** If time_t is signed, then 0 is the median value,
127  ** if time_t is unsigned, then 1 << bits is median.
128  */
129  t = (t < 0) ? 0 : ((time_t) 1 << bits);
130 
131  /* Some gmtime() implementations are broken and will return
132  * NULL for time_ts larger than 40 bits even on 64-bit platforms
133  * so we'll just cap it at 40 bits */
134  if(bits > 40) bits = 40;
135 
136  for ( ; ; ) {
137  mytm = gmtime(&t);
138 
139  if(!mytm) return WRONG;
140 
141  dir = tmcomp(mytm, &yourtm);
142  if (dir != 0) {
143  if (bits-- < 0)
144  return WRONG;
145  if (bits < 0)
146  --t;
147  else if (dir > 0)
148  t -= (time_t) 1 << bits;
149  else t += (time_t) 1 << bits;
150  continue;
151  }
152  break;
153  }
154  t += saved_seconds;
155  return t;
156 }
#define WRONG
Definition: mkgmtime.c:91
static int tmcomp(register const struct tm *const atmp, register const struct tm *const btmp)
Definition: mkgmtime.c:94
time_t mkgmtime(struct tm *const tmp)
Get the UTC time in seconds and nano-seconds from a time struct in GM time.
Definition: mkgmtime.c:108


ublox_gps
Author(s): Johannes Meyer
autogenerated on Thu Jan 28 2021 03:13:52