summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/rcs/lib/rcstime.c
blob: cfd466096ff7edf72d674d345b49b3d80493f73e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Convert between RCS time format and Posix and/or C formats.  */

/* Copyright 1992, 1993, 1994, 1995 Paul Eggert
   Distributed under license by the Free Software Foundation, Inc.

This file is part of RCS.

RCS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

RCS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with RCS; see the file COPYING.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

Report problems and direct all questions to:

    rcs-bugs@cs.purdue.edu

*/

#include "rcsbase.h"
#include "partime.h"
#include "maketime.h"

libId(rcstimeId, "$FreeBSD$")

static long zone_offset; /* seconds east of UTC, or TM_LOCAL_ZONE */
static int use_zone_offset; /* if zero, use UTC without zone indication */

/*
* Convert Unix time to RCS format.
* For compatibility with older versions of RCS,
* dates from 1900 through 1999 are stored without the leading "19".
*/
	void
time2date(unixtime,date)
	time_t unixtime;
	char date[datesize];
{
	register struct tm const *tm = time2tm(unixtime, RCSversion<VERSION(5));
	VOID sprintf(date,
#		if has_printf_dot
			"%.2d.%.2d.%.2d.%.2d.%.2d.%.2d",
#		else
			"%02d.%02d.%02d.%02d.%02d.%02d",
#		endif
		tm->tm_year  +  ((unsigned)tm->tm_year < 100 ? 0 : 1900),
		tm->tm_mon+1, tm->tm_mday,
		tm->tm_hour, tm->tm_min, tm->tm_sec
	);
}

/* Like str2time, except die if an error was found.  */
static time_t str2time_checked P((char const*,time_t,long));
	static time_t
str2time_checked(source, default_time, default_zone)
	char const *source;
	time_t default_time;
	long default_zone;
{
	time_t t = str2time(source, default_time, default_zone);
	if (t == -1)
		faterror("unknown date/time: %s", source);
	return t;
}

/*
* Parse a free-format date in SOURCE, convert it
* into RCS internal format, and store the result into TARGET.
*/
	void
str2date(source, target)
	char const *source;
	char target[datesize];
{
	time2date(
		str2time_checked(source, now(),
			use_zone_offset ? zone_offset
			: RCSversion<VERSION(5) ? TM_LOCAL_ZONE
			: 0
		),
		target
	);
}

/* Convert an RCS internal format date to time_t.  */
	time_t
date2time(source)
	char const source[datesize];
{
	char s[datesize + zonelenmax];
	return str2time_checked(date2str(source, s), (time_t)0, 0);
}


/* Set the time zone for date2str output.  */
	void
zone_set(s)
	char const *s;
{
	if ((use_zone_offset = *s)) {
		long zone;
		char const *zonetail = parzone(s, &zone);
		if (!zonetail || *zonetail)
			error("%s: not a known time zone", s);
		else
			zone_offset = zone;
	}
}


/*
* Format a user-readable form of the RCS format DATE into the buffer DATEBUF.
* Yield DATEBUF.
*/
	char const *
date2str(date, datebuf)
	char const date[datesize];
	char datebuf[datesize + zonelenmax];
{
	register char const *p = date;

	while (*p++ != '.')
		continue;
	if (!use_zone_offset)
	    VOID sprintf(datebuf,
		"19%.*s/%.2s/%.2s %.2s:%.2s:%s"
			+ (date[2]=='.' && VERSION(5)<=RCSversion  ?  0  :  2),
		(int)(p-date-1), date,
		p, p+3, p+6, p+9, p+12
	    );
	else {
	    struct tm t;
	    struct tm const *z;
	    int non_hour;
	    long zone;
	    char c;

	    t.tm_year = atoi(date) - (date[2]=='.' ? 0 : 1900);
	    t.tm_mon = atoi(p) - 1;
	    t.tm_mday = atoi(p+3);
	    t.tm_hour = atoi(p+6);
	    t.tm_min = atoi(p+9);
	    t.tm_sec = atoi(p+12);
	    t.tm_wday = -1;
	    zone = zone_offset;
	    if (zone == TM_LOCAL_ZONE) {
		time_t u = tm2time(&t, 0), d;
		z = localtime(&u);
		d = difftm(z, &t);
		zone  =  (time_t)-1 < 0 || d < -d  ?  d  :  -(long)-d;
	    } else {
		adjzone(&t, zone);
		z = &t;
	    }
	    c = '+';
	    if (zone < 0) {
		zone = -zone;
		c = '-';
	    }
	    VOID sprintf(datebuf,
#		if has_printf_dot
		    "%.2d-%.2d-%.2d %.2d:%.2d:%.2d%c%.2d",
#		else
		    "%02d-%02d-%02d %02d:%02d:%02d%c%02d",
#		endif
		z->tm_year + 1900,
		z->tm_mon + 1, z->tm_mday, z->tm_hour, z->tm_min, z->tm_sec,
		c, (int) (zone / (60*60))
	    );
	    if ((non_hour = zone % (60*60))) {
#		if has_printf_dot
		    static char const fmt[] = ":%.2d";
#		else
		    static char const fmt[] = ":%02d";
#		endif
		VOID sprintf(datebuf + strlen(datebuf), fmt, non_hour / 60);
		if ((non_hour %= 60))
		    VOID sprintf(datebuf + strlen(datebuf), fmt, non_hour);
	    }
	}
	return datebuf;
}
OpenPOWER on IntegriCloud