Deleted Added
full compact
cdf_time.c (192348) cdf_time.c (226048)
1/*-
2 * Copyright (c) 2008 Christos Zoulas
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 13 unchanged lines hidden (view full) ---

22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "file.h"
28
29#ifndef lint
1/*-
2 * Copyright (c) 2008 Christos Zoulas
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 13 unchanged lines hidden (view full) ---

22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "file.h"
28
29#ifndef lint
30FILE_RCSID("@(#)$File: cdf_time.c,v 1.6 2009/03/10 11:44:29 christos Exp $")
30FILE_RCSID("@(#)$File: cdf_time.c,v 1.10 2011/02/10 17:03:16 christos Exp $")
31#endif
32
33#include <time.h>
34#ifdef TEST
35#include <err.h>
36#endif
37#include <string.h>
38

--- 64 unchanged lines hidden (view full) ---

103 static char UTC[] = "UTC";
104#endif
105 int rdays;
106
107 /* Unit is 100's of nanoseconds */
108 ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
109
110 t /= CDF_TIME_PREC;
31#endif
32
33#include <time.h>
34#ifdef TEST
35#include <err.h>
36#endif
37#include <string.h>
38

--- 64 unchanged lines hidden (view full) ---

103 static char UTC[] = "UTC";
104#endif
105 int rdays;
106
107 /* Unit is 100's of nanoseconds */
108 ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
109
110 t /= CDF_TIME_PREC;
111 tm.tm_sec = t % 60;
111 tm.tm_sec = (int)(t % 60);
112 t /= 60;
113
112 t /= 60;
113
114 tm.tm_min = t % 60;
114 tm.tm_min = (int)(t % 60);
115 t /= 60;
116
115 t /= 60;
116
117 tm.tm_hour = t % 24;
117 tm.tm_hour = (int)(t % 24);
118 t /= 24;
119
120 // XXX: Approx
118 t /= 24;
119
120 // XXX: Approx
121 tm.tm_year = CDF_BASE_YEAR + (t / 365);
121 tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));
122
123 rdays = cdf_getdays(tm.tm_year);
124 t -= rdays;
122
123 rdays = cdf_getdays(tm.tm_year);
124 t -= rdays;
125 tm.tm_mday = cdf_getday(tm.tm_year, t);
126 tm.tm_mon = cdf_getmonth(tm.tm_year, t);
125 tm.tm_mday = cdf_getday(tm.tm_year, (int)t);
126 tm.tm_mon = cdf_getmonth(tm.tm_year, (int)t);
127 tm.tm_wday = 0;
128 tm.tm_yday = 0;
129 tm.tm_isdst = 0;
130#ifdef HAVE_STRUCT_TM_TM_GMTOFF
131 tm.tm_gmtoff = 0;
132#endif
133#ifdef HAVE_STRUCT_TM_TM_ZONE
134 tm.tm_zone = UTC;
135#endif
136 tm.tm_year -= 1900;
137 ts->tv_sec = mktime(&tm);
138 if (ts->tv_sec == -1) {
139 errno = EINVAL;
140 return -1;
141 }
142 return 0;
143}
144
145int
127 tm.tm_wday = 0;
128 tm.tm_yday = 0;
129 tm.tm_isdst = 0;
130#ifdef HAVE_STRUCT_TM_TM_GMTOFF
131 tm.tm_gmtoff = 0;
132#endif
133#ifdef HAVE_STRUCT_TM_TM_ZONE
134 tm.tm_zone = UTC;
135#endif
136 tm.tm_year -= 1900;
137 ts->tv_sec = mktime(&tm);
138 if (ts->tv_sec == -1) {
139 errno = EINVAL;
140 return -1;
141 }
142 return 0;
143}
144
145int
146/*ARGSUSED*/
146cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
147{
147cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
148{
149#ifndef __lint__
148 (void)&t;
149 (void)&ts;
150 (void)&t;
151 (void)&ts;
152#endif
150#ifdef notyet
151 struct tm tm;
152 if (gmtime_r(&ts->ts_sec, &tm) == NULL) {
153 errno = EINVAL;
154 return -1;
155 }
156 *t = (ts->ts_nsec / 100) * CDF_TIME_PREC;
157 *t = tm.tm_sec;
158 *t += tm.tm_min * 60;
159 *t += tm.tm_hour * 60 * 60;
160 *t += tm.tm_mday * 60 * 60 * 24;
161#endif
162 return 0;
163}
164
153#ifdef notyet
154 struct tm tm;
155 if (gmtime_r(&ts->ts_sec, &tm) == NULL) {
156 errno = EINVAL;
157 return -1;
158 }
159 *t = (ts->ts_nsec / 100) * CDF_TIME_PREC;
160 *t = tm.tm_sec;
161 *t += tm.tm_min * 60;
162 *t += tm.tm_hour * 60 * 60;
163 *t += tm.tm_mday * 60 * 60 * 24;
164#endif
165 return 0;
166}
167
168char *
169cdf_ctime(const time_t *sec)
170{
171 static char ctbuf[26];
172 char *ptr = ctime(sec);
173 if (ptr != NULL)
174 return ptr;
175 (void)snprintf(ctbuf, sizeof(ctbuf), "*Bad* 0x%16.16llx\n",
176 (long long)*sec);
177 return ctbuf;
178}
165
179
180
166#ifdef TEST
167int
168main(int argc, char *argv[])
169{
170 struct timespec ts;
171 static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
172 static const char *ref = "Sat Apr 23 01:30:00 1977";
173 char *p, *q;
174
175 cdf_timestamp_to_timespec(&ts, tst);
181#ifdef TEST
182int
183main(int argc, char *argv[])
184{
185 struct timespec ts;
186 static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
187 static const char *ref = "Sat Apr 23 01:30:00 1977";
188 char *p, *q;
189
190 cdf_timestamp_to_timespec(&ts, tst);
176 p = ctime(&ts.tv_sec);
191 p = cdf_ctime(&ts.tv_sec);
177 if ((q = strchr(p, '\n')) != NULL)
178 *q = '\0';
179 if (strcmp(ref, p) != 0)
180 errx(1, "Error date %s != %s\n", ref, p);
181 return 0;
182}
183#endif
192 if ((q = strchr(p, '\n')) != NULL)
193 *q = '\0';
194 if (strcmp(ref, p) != 0)
195 errx(1, "Error date %s != %s\n", ref, p);
196 return 0;
197}
198#endif