1/*	$OpenBSD: rcstime.c,v 1.6 2016/08/26 09:02:54 guenther Exp $	*/
2/*
3 * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4 * 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 * 2. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <err.h>
28#include <stdlib.h>
29#include <string.h>
30#include <time.h>
31
32#include "rcs.h"
33
34void
35rcs_set_tz(char *tz, struct rcs_delta *rdp, struct tm *tb)
36{
37	int tzone;
38	int pos;
39	char *h, *m;
40	const char *errstr;
41	struct tm *ltb;
42	time_t now;
43
44	if (!strcmp(tz, "LT")) {
45		now = mktime(&rdp->rd_date);
46		ltb = localtime(&now);
47		ltb->tm_hour += ((int)ltb->tm_gmtoff/3600);
48		memcpy(tb, ltb, sizeof(*tb));
49	} else {
50		pos = 0;
51		switch (*tz) {
52		case '-':
53			break;
54		case '+':
55			pos = 1;
56			break;
57		default:
58			errx(1, "%s: not a known time zone", tz);
59		}
60
61		h = (tz + 1);
62		if ((m = strrchr(tz, ':')) != NULL)
63			*(m++) = '\0';
64
65		memcpy(tb, &rdp->rd_date, sizeof(*tb));
66
67		tzone = strtonum(h, -23, 23, &errstr);
68		if (errstr)
69			errx(1, "%s: not a known time zone", tz);
70
71		if (pos) {
72			tb->tm_hour += tzone;
73			tb->tm_gmtoff += (tzone * 3600);
74		} else {
75			tb->tm_hour -= tzone;
76			tb->tm_gmtoff -= (tzone * 3600);
77		}
78
79		if ((tb->tm_hour >= 24) || (tb->tm_hour <= -24))
80			tb->tm_hour = 0;
81
82		if (m != NULL) {
83			tzone = strtonum(m, 0, 59, &errstr);
84			if (errstr)
85				errx(1, "%s: not a known minute", m);
86
87			if ((tb->tm_min + tzone) >= 60) {
88				tb->tm_hour++;
89				tb->tm_min -= (60 - tzone);
90			} else
91				tb->tm_min += tzone;
92
93			tb->tm_gmtoff += (tzone*60);
94		}
95	}
96}
97