1*0a6a1f1dSLionel Sambuc /* $NetBSD: cdf_time.c,v 1.7 2015/01/02 21:15:32 christos Exp $ */
2ef01931fSBen Gras
3ef01931fSBen Gras /*-
4ef01931fSBen Gras * Copyright (c) 2008 Christos Zoulas
5ef01931fSBen Gras * All rights reserved.
6ef01931fSBen Gras *
7ef01931fSBen Gras * Redistribution and use in source and binary forms, with or without
8ef01931fSBen Gras * modification, are permitted provided that the following conditions
9ef01931fSBen Gras * are met:
10ef01931fSBen Gras * 1. Redistributions of source code must retain the above copyright
11ef01931fSBen Gras * notice, this list of conditions and the following disclaimer.
12ef01931fSBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13ef01931fSBen Gras * notice, this list of conditions and the following disclaimer in the
14ef01931fSBen Gras * documentation and/or other materials provided with the distribution.
15ef01931fSBen Gras *
16ef01931fSBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17ef01931fSBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18ef01931fSBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19ef01931fSBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20ef01931fSBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21ef01931fSBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22ef01931fSBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23ef01931fSBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24ef01931fSBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25ef01931fSBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26ef01931fSBen Gras * POSSIBILITY OF SUCH DAMAGE.
27ef01931fSBen Gras */
28ef01931fSBen Gras
29ef01931fSBen Gras #include "file.h"
30ef01931fSBen Gras
31ef01931fSBen Gras #ifndef lint
32ef01931fSBen Gras #if 0
33*0a6a1f1dSLionel Sambuc FILE_RCSID("@(#)$File: cdf_time.c,v 1.15 2014/05/14 23:15:42 christos Exp $")
34ef01931fSBen Gras #else
35*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: cdf_time.c,v 1.7 2015/01/02 21:15:32 christos Exp $");
36ef01931fSBen Gras #endif
37ef01931fSBen Gras #endif
38ef01931fSBen Gras
39ef01931fSBen Gras #include <time.h>
40ef01931fSBen Gras #ifdef TEST
41ef01931fSBen Gras #include <err.h>
42ef01931fSBen Gras #endif
43ef01931fSBen Gras #include <string.h>
44ef01931fSBen Gras
45ef01931fSBen Gras #include "cdf.h"
46ef01931fSBen Gras
47ef01931fSBen Gras #define isleap(y) ((((y) % 4) == 0) && \
48ef01931fSBen Gras ((((y) % 100) != 0) || (((y) % 400) == 0)))
49ef01931fSBen Gras
50ef01931fSBen Gras static const int mdays[] = {
51ef01931fSBen Gras 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
52ef01931fSBen Gras };
53ef01931fSBen Gras
54ef01931fSBen Gras /*
55ef01931fSBen Gras * Return the number of days between jan 01 1601 and jan 01 of year.
56ef01931fSBen Gras */
57ef01931fSBen Gras static int
cdf_getdays(int year)58ef01931fSBen Gras cdf_getdays(int year)
59ef01931fSBen Gras {
60ef01931fSBen Gras int days = 0;
61ef01931fSBen Gras int y;
62ef01931fSBen Gras
63ef01931fSBen Gras for (y = CDF_BASE_YEAR; y < year; y++)
64ef01931fSBen Gras days += isleap(y) + 365;
65ef01931fSBen Gras
66ef01931fSBen Gras return days;
67ef01931fSBen Gras }
68ef01931fSBen Gras
69ef01931fSBen Gras /*
70ef01931fSBen Gras * Return the day within the month
71ef01931fSBen Gras */
72ef01931fSBen Gras static int
cdf_getday(int year,int days)73ef01931fSBen Gras cdf_getday(int year, int days)
74ef01931fSBen Gras {
75ef01931fSBen Gras size_t m;
76ef01931fSBen Gras
77ef01931fSBen Gras for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) {
78ef01931fSBen Gras int sub = mdays[m] + (m == 1 && isleap(year));
79ef01931fSBen Gras if (days < sub)
80ef01931fSBen Gras return days;
81ef01931fSBen Gras days -= sub;
82ef01931fSBen Gras }
83ef01931fSBen Gras return days;
84ef01931fSBen Gras }
85ef01931fSBen Gras
86ef01931fSBen Gras /*
87ef01931fSBen Gras * Return the 0...11 month number.
88ef01931fSBen Gras */
89ef01931fSBen Gras static int
cdf_getmonth(int year,int days)90ef01931fSBen Gras cdf_getmonth(int year, int days)
91ef01931fSBen Gras {
92ef01931fSBen Gras size_t m;
93ef01931fSBen Gras
94ef01931fSBen Gras for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) {
95ef01931fSBen Gras days -= mdays[m];
96ef01931fSBen Gras if (m == 1 && isleap(year))
97ef01931fSBen Gras days--;
98ef01931fSBen Gras if (days <= 0)
99ef01931fSBen Gras return (int)m;
100ef01931fSBen Gras }
101ef01931fSBen Gras return (int)m;
102ef01931fSBen Gras }
103ef01931fSBen Gras
104ef01931fSBen Gras int
cdf_timestamp_to_timespec(struct timespec * ts,cdf_timestamp_t t)105ef01931fSBen Gras cdf_timestamp_to_timespec(struct timespec *ts, cdf_timestamp_t t)
106ef01931fSBen Gras {
107ef01931fSBen Gras struct tm tm;
108ef01931fSBen Gras #ifdef HAVE_STRUCT_TM_TM_ZONE
109ef01931fSBen Gras static char UTC[] = "UTC";
110ef01931fSBen Gras #endif
111ef01931fSBen Gras int rdays;
112ef01931fSBen Gras
113ef01931fSBen Gras /* Unit is 100's of nanoseconds */
114ef01931fSBen Gras ts->tv_nsec = (t % CDF_TIME_PREC) * 100;
115ef01931fSBen Gras
116ef01931fSBen Gras t /= CDF_TIME_PREC;
117ef01931fSBen Gras tm.tm_sec = (int)(t % 60);
118ef01931fSBen Gras t /= 60;
119ef01931fSBen Gras
120ef01931fSBen Gras tm.tm_min = (int)(t % 60);
121ef01931fSBen Gras t /= 60;
122ef01931fSBen Gras
123ef01931fSBen Gras tm.tm_hour = (int)(t % 24);
124ef01931fSBen Gras t /= 24;
125ef01931fSBen Gras
126*0a6a1f1dSLionel Sambuc /* XXX: Approx */
127ef01931fSBen Gras tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));
128ef01931fSBen Gras
129ef01931fSBen Gras rdays = cdf_getdays(tm.tm_year);
13008ff44c4SLionel Sambuc t -= rdays - 1;
131ef01931fSBen Gras tm.tm_mday = cdf_getday(tm.tm_year, (int)t);
132ef01931fSBen Gras tm.tm_mon = cdf_getmonth(tm.tm_year, (int)t);
133ef01931fSBen Gras tm.tm_wday = 0;
134ef01931fSBen Gras tm.tm_yday = 0;
135ef01931fSBen Gras tm.tm_isdst = 0;
136ef01931fSBen Gras #ifdef HAVE_STRUCT_TM_TM_GMTOFF
137ef01931fSBen Gras tm.tm_gmtoff = 0;
138ef01931fSBen Gras #endif
139ef01931fSBen Gras #ifdef HAVE_STRUCT_TM_TM_ZONE
140ef01931fSBen Gras tm.tm_zone = UTC;
141ef01931fSBen Gras #endif
142ef01931fSBen Gras tm.tm_year -= 1900;
143ef01931fSBen Gras ts->tv_sec = mktime(&tm);
144ef01931fSBen Gras if (ts->tv_sec == -1) {
145ef01931fSBen Gras errno = EINVAL;
146ef01931fSBen Gras return -1;
147ef01931fSBen Gras }
148ef01931fSBen Gras return 0;
149ef01931fSBen Gras }
150ef01931fSBen Gras
151ef01931fSBen Gras int
152ef01931fSBen Gras /*ARGSUSED*/
cdf_timespec_to_timestamp(cdf_timestamp_t * t,const struct timespec * ts)153ef01931fSBen Gras cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
154ef01931fSBen Gras {
155ef01931fSBen Gras #ifndef __lint__
156ef01931fSBen Gras (void)&t;
157ef01931fSBen Gras (void)&ts;
158ef01931fSBen Gras #endif
159ef01931fSBen Gras #ifdef notyet
160ef01931fSBen Gras struct tm tm;
161ef01931fSBen Gras if (gmtime_r(&ts->ts_sec, &tm) == NULL) {
162ef01931fSBen Gras errno = EINVAL;
163ef01931fSBen Gras return -1;
164ef01931fSBen Gras }
165ef01931fSBen Gras *t = (ts->ts_nsec / 100) * CDF_TIME_PREC;
166ef01931fSBen Gras *t = tm.tm_sec;
167ef01931fSBen Gras *t += tm.tm_min * 60;
168ef01931fSBen Gras *t += tm.tm_hour * 60 * 60;
169ef01931fSBen Gras *t += tm.tm_mday * 60 * 60 * 24;
170ef01931fSBen Gras #endif
171ef01931fSBen Gras return 0;
172ef01931fSBen Gras }
173ef01931fSBen Gras
174835f6802SDirk Vogt char *
cdf_ctime(const time_t * sec,char * buf)17584d9c625SLionel Sambuc cdf_ctime(const time_t *sec, char *buf)
176835f6802SDirk Vogt {
17784d9c625SLionel Sambuc char *ptr = ctime_r(sec, buf);
178835f6802SDirk Vogt if (ptr != NULL)
17984d9c625SLionel Sambuc return buf;
180*0a6a1f1dSLionel Sambuc (void)snprintf(buf, 26, "*Bad* 0x%16.16" INT64_T_FORMAT "x\n",
181*0a6a1f1dSLionel Sambuc (long long)*sec);
18284d9c625SLionel Sambuc return buf;
183835f6802SDirk Vogt }
184835f6802SDirk Vogt
185ef01931fSBen Gras
186*0a6a1f1dSLionel Sambuc #ifdef TEST_TIME
187ef01931fSBen Gras int
main(int argc,char * argv[])188ef01931fSBen Gras main(int argc, char *argv[])
189ef01931fSBen Gras {
190ef01931fSBen Gras struct timespec ts;
19184d9c625SLionel Sambuc char buf[25];
192ef01931fSBen Gras static const cdf_timestamp_t tst = 0x01A5E403C2D59C00ULL;
193ef01931fSBen Gras static const char *ref = "Sat Apr 23 01:30:00 1977";
194ef01931fSBen Gras char *p, *q;
195ef01931fSBen Gras
196ef01931fSBen Gras cdf_timestamp_to_timespec(&ts, tst);
19784d9c625SLionel Sambuc p = cdf_ctime(&ts.tv_sec, buf);
198ef01931fSBen Gras if ((q = strchr(p, '\n')) != NULL)
199ef01931fSBen Gras *q = '\0';
200ef01931fSBen Gras if (strcmp(ref, p) != 0)
201ef01931fSBen Gras errx(1, "Error date %s != %s\n", ref, p);
202ef01931fSBen Gras return 0;
203ef01931fSBen Gras }
204ef01931fSBen Gras #endif
205