1175261Sobrien/*-
2175261Sobrien * Copyright (c) 2008 Christos Zoulas
3175261Sobrien * All rights reserved.
4175261Sobrien *
5175261Sobrien * Redistribution and use in source and binary forms, with or without
6175261Sobrien * modification, are permitted provided that the following conditions
7175261Sobrien * are met:
8175261Sobrien * 1. Redistributions of source code must retain the above copyright
9175261Sobrien *    notice, this list of conditions and the following disclaimer.
10175261Sobrien * 2. Redistributions in binary form must reproduce the above copyright
11175261Sobrien *    notice, this list of conditions and the following disclaimer in the
12175261Sobrien *    documentation and/or other materials provided with the distribution.
13175261Sobrien *
14175261Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1517721Speter * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1617721Speter * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1717721Speter * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1817721Speter * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1917721Speter * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2017721Speter * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2117721Speter * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2217721Speter * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2317721Speter * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2417721Speter * POSSIBILITY OF SUCH DAMAGE.
2517721Speter */
2617721Speter
2717721Speter#include "file.h"
2817721Speter
2917721Speter#ifndef lint
3017721SpeterFILE_RCSID("@(#)$File: cdf_time.c,v 1.15 2014/05/14 23:15:42 christos Exp $")
3117721Speter#endif
3217721Speter
3317721Speter#include <time.h>
3417721Speter#ifdef TEST
3517721Speter#include <err.h>
3617721Speter#endif
3717721Speter#include <string.h>
3817721Speter
3917721Speter#include "cdf.h"
4017721Speter
4117721Speter#define isleap(y) ((((y) % 4) == 0) && \
4217721Speter    ((((y) % 100) != 0) || (((y) % 400) == 0)))
4317721Speter
4417721Speterstatic const int mdays[] = {
4517721Speter    31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
4617721Speter};
4717721Speter
4817721Speter/*
4917721Speter * Return the number of days between jan 01 1601 and jan 01 of year.
5017721Speter */
5117721Speterstatic int
5217721Spetercdf_getdays(int year)
5317721Speter{
5417721Speter	int days = 0;
5517721Speter	int y;
5617721Speter
5717721Speter	for (y = CDF_BASE_YEAR; y < year; y++)
5817721Speter		days += isleap(y) + 365;
5917721Speter
60	return days;
61}
62
63/*
64 * Return the day within the month
65 */
66static int
67cdf_getday(int year, int days)
68{
69	size_t m;
70
71	for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) {
72		int sub = mdays[m] + (m == 1 && isleap(year));
73		if (days < sub)
74			return days;
75		days -= sub;
76	}
77	return days;
78}
79
80/*
81 * Return the 0...11 month number.
82 */
83static int
84cdf_getmonth(int year, int days)
85{
86	size_t m;
87
88	for (m = 0; m < sizeof(mdays) / sizeof(mdays[0]); m++) {
89		days -= mdays[m];
90		if (m == 1 && isleap(year))
91			days--;
92		if (days <= 0)
93			return (int)m;
94	}
95	return (int)m;
96}
97
98int
99cdf_timestamp_to_timespec(struct timespec *ts, cdf_timestamp_t t)
100{
101	struct tm tm;
102#ifdef HAVE_STRUCT_TM_TM_ZONE
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 = (int)(t % 60);
112	t /= 60;
113
114	tm.tm_min = (int)(t % 60);
115	t /= 60;
116
117	tm.tm_hour = (int)(t % 24);
118	t /= 24;
119
120	/* XXX: Approx */
121	tm.tm_year = (int)(CDF_BASE_YEAR + (t / 365));
122
123	rdays = cdf_getdays(tm.tm_year);
124	t -= rdays - 1;
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
146/*ARGSUSED*/
147cdf_timespec_to_timestamp(cdf_timestamp_t *t, const struct timespec *ts)
148{
149#ifndef __lint__
150	(void)&t;
151	(void)&ts;
152#endif
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, char *buf)
170{
171	char *ptr = ctime_r(sec, buf);
172	if (ptr != NULL)
173		return buf;
174	(void)snprintf(buf, 26, "*Bad* 0x%16.16" INT64_T_FORMAT "x\n",
175	    (long long)*sec);
176	return buf;
177}
178
179
180#ifdef TEST_TIME
181int
182main(int argc, char *argv[])
183{
184	struct timespec ts;
185	char buf[25];
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);
191	p = cdf_ctime(&ts.tv_sec, buf);
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
199