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