1 /* $MirOS: src/gnu/usr.bin/rcs/src/rcstime.c,v 1.4 2011/11/20 19:58:01 tg Exp $ */
2 
3 /* Convert between RCS time format and Posix and/or C formats.  */
4 
5 /* Copyright 1992, 1993, 1994, 1995 Paul Eggert
6    Distributed under license by the Free Software Foundation, Inc.
7 
8 This file is part of RCS.
9 
10 RCS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14 
15 RCS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with RCS; see the file COPYING.
22 If not, write to the Free Software Foundation,
23 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 
25 Report problems and direct all questions to:
26 
27     rcs-bugs@cs.purdue.edu
28 
29 */
30 
31 #include "rcsbase.h"
32 #include "getdate.h"
33 
34 __RCSID("$MirOS: src/gnu/usr.bin/rcs/src/rcstime.c,v 1.4 2011/11/20 19:58:01 tg Exp $");
35 
36 /*
37 * Convert Unix time to RCS format.
38 * For compatibility with older versions of RCS,
39 * dates from 1900 through 1999 are stored without the leading "19".
40 */
41 	void
time2date(time_t unixtime,char date[datesize])42 time2date(time_t unixtime, char date[datesize])
43 {
44 	struct tm tm;
45 
46 	timet2tm(&tm, unixtime);
47 	snprintf(date, datesize,
48 	    "%.2d.%.2d.%.2d.%.2d.%.2d.%.2d",
49 	    (int)(tm.tm_year + ((unsigned)tm.tm_year < 100 ? 0 : 1900)),
50 	    tm.tm_mon + 1, tm.tm_mday,
51 	    tm.tm_hour, tm.tm_min, tm.tm_sec);
52 }
53 
54 static time_t
str2time_checked(char const * source,time_t default_time)55 str2time_checked(char const *source, time_t default_time)
56 {
57 	struct timespec ts, td;
58 
59 	td.tv_sec = default_time;
60 	td.tv_nsec = 0;
61 
62 	if (!get_date(&ts, source, &td))
63 		faterror("unknown date/time: %s", source);
64 	return ts.tv_sec;
65 }
66 
67 /*
68 * Parse a free-format date in SOURCE, convert it
69 * into RCS internal format, and store the result into TARGET.
70 */
71 	void
str2date(char const * source,char target[datesize])72 str2date(char const *source, char target[datesize])
73 {
74 	time2date(str2time_checked(source, now()), target);
75 }
76 
77 /* Convert an RCS internal format date to time_t.  */
78 	time_t
date2time(char const source[datesize])79 date2time(char const source[datesize])
80 {
81 	char s[datesize + zonelenmax];
82 	return str2time_checked(date2str(source, s), 0);
83 }
84 
85 /*
86 * Format a user-readable form of the RCS format DATE into the buffer DATEBUF.
87 * Yield DATEBUF.
88 */
89 	char const *
date2str(char const date[datesize],char datebuf[datesize+zonelenmax])90 date2str(char const date[datesize], char datebuf[datesize + zonelenmax])
91 {
92 	char const *p = date;
93 	char *b = datebuf;
94 
95 	while (*p++ != '.')
96 		continue;
97 
98 	if (date[2]=='.' && VERSION(5)<=RCSversion) {
99 		strlcpy(b, "19", datesize + zonelenmax);
100 		b += 2;
101 	}
102 
103 	snprintf(b, datesize + zonelenmax - (b - datebuf),
104 	    "%.*s/%.2s/%.2s %.2s:%.2s:%s",
105 	    (int)(p-date-1), date, p, p+3, p+6, p+9, p+12);
106 	return datebuf;
107 }
108