1 /* $NetBSD: rcstime.c,v 1.1.1.1 1996/10/13 21:56:51 veego Exp $ */
3 /* Convert between RCS time format and Posix and/or C formats. */
5 /* Copyright 1992, 1993, 1994, 1995 Paul Eggert
6 Distributed under license by the Free Software Foundation, Inc.
8 This file is part of RCS.
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)
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.
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.
25 Report problems and direct all questions to:
27 rcs-bugs@cs.purdue.edu
35 libId(rcstimeId
, "Id: rcstime.c,v 1.4 1995/06/16 06:19:24 eggert Exp")
37 static long zone_offset
; /* seconds east of UTC, or TM_LOCAL_ZONE */
38 static int use_zone_offset
; /* if zero, use UTC without zone indication */
41 * Convert Unix time to RCS format.
42 * For compatibility with older versions of RCS,
43 * dates from 1900 through 1999 are stored without the leading "19".
46 time2date(unixtime
,date
)
50 register struct tm
const *tm
= time2tm(unixtime
, RCSversion
<VERSION(5));
53 "%.2d.%.2d.%.2d.%.2d.%.2d.%.2d",
55 "%02d.%02d.%02d.%02d.%02d.%02d",
57 tm
->tm_year
+ ((unsigned)tm
->tm_year
< 100 ? 0 : 1900),
58 tm
->tm_mon
+1, tm
->tm_mday
,
59 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
63 /* Like str2time, except die if an error was found. */
64 static time_t str2time_checked
P((char const*,time_t,long));
66 str2time_checked(source
, default_time
, default_zone
)
71 time_t t
= str2time(source
, default_time
, default_zone
);
73 faterror("unknown date/time: %s", source
);
78 * Parse a free-format date in SOURCE, convert it
79 * into RCS internal format, and store the result into TARGET.
82 str2date(source
, target
)
84 char target
[datesize
];
87 str2time_checked(source
, now(),
88 use_zone_offset
? zone_offset
89 : RCSversion
<VERSION(5) ? TM_LOCAL_ZONE
96 /* Convert an RCS internal format date to time_t. */
99 char const source
[datesize
];
101 char s
[datesize
+ zonelenmax
];
102 return str2time_checked(date2str(source
, s
), (time_t)0, 0);
106 /* Set the time zone for date2str output. */
111 if ((use_zone_offset
= *s
)) {
113 char const *zonetail
= parzone(s
, &zone
);
114 if (!zonetail
|| *zonetail
)
115 error("%s: not a known time zone", s
);
123 * Format a user-readable form of the RCS format DATE into the buffer DATEBUF.
127 date2str(date
, datebuf
)
128 char const date
[datesize
];
129 char datebuf
[datesize
+ zonelenmax
];
131 register char const *p
= date
;
135 if (!use_zone_offset
)
136 VOID
sprintf(datebuf
,
137 "19%.*s/%.2s/%.2s %.2s:%.2s:%s"
138 + (date
[2]=='.' && VERSION(5)<=RCSversion
? 0 : 2),
139 (int)(p
-date
-1), date
,
140 p
, p
+3, p
+6, p
+9, p
+12
149 t
.tm_year
= atoi(date
) - (date
[2]=='.' ? 0 : 1900);
150 t
.tm_mon
= atoi(p
) - 1;
151 t
.tm_mday
= atoi(p
+3);
152 t
.tm_hour
= atoi(p
+6);
153 t
.tm_min
= atoi(p
+9);
154 t
.tm_sec
= atoi(p
+12);
157 if (zone
== TM_LOCAL_ZONE
) {
158 time_t u
= tm2time(&t
, 0), d
;
161 zone
= (time_t)-1 < 0 || d
< -d
? d
: -(long)-d
;
171 VOID
sprintf(datebuf
,
173 "%.2d-%.2d-%.2d %.2d:%.2d:%.2d%c%.2d",
175 "%02d-%02d-%02d %02d:%02d:%02d%c%02d",
178 z
->tm_mon
+ 1, z
->tm_mday
, z
->tm_hour
, z
->tm_min
, z
->tm_sec
,
179 c
, (int) (zone
/ (60*60))
181 if ((non_hour
= zone
% (60*60))) {
183 static char const fmt
[] = ":%.2d";
185 static char const fmt
[] = ":%02d";
187 VOID
sprintf(datebuf
+ strlen(datebuf
), fmt
, non_hour
/ 60);
188 if ((non_hour
%= 60))
189 VOID
sprintf(datebuf
+ strlen(datebuf
), fmt
, non_hour
);