Initial revision
[bpt/emacs.git] / src / vmstime.c
CommitLineData
d415c517
JB
1#include "config.h"
2#include "vmstime.h"
3
4long timezone=0;
5int daylight=0;
6
7static char tzname_default[20]="";
8static char tzname_dst[20]="";
9
10char *tzname[2] = { tzname_default, tzname_dst };
11
12static long internal_daylight=0;
13static char daylight_set=0;
14
15static long read_time(const char *nptr, const char **endptr,
16 int sign_allowed_p)
17{
18 int t;
19
20 *endptr = nptr;
21
22 /* This routine trusts the user very much, and does no checks!
23 The only exception is this: */
24 if (!sign_allowed_p && (*nptr == '-' || *nptr == '+'))
25 return 0;
26
27 t = strtol(*endptr, endptr, 10) * 3600;
28 if (**endptr != ':' || **endptr == '+' || **endptr == '-')
29 return t;
30 (*endptr)++;
31
32 t = t + strtol(*endptr, endptr, 10) * 60;
33 if (**endptr != ':' || **endptr == '+' || **endptr == '-')
34 return t;
35 (*endptr)++;
36
37 return t + strtol(*endptr, endptr, 10);
38}
39
40static void read_dst_time(const char *nptr, const char **endptr,
41 int *m, int *n, int *d,
42 int *leap_p)
43{
44 time_t bintim = time(0);
45 struct tm *lc = localtime(&bintim);
46
47 *leap_p = 1;
48 *m = 0; /* When m and n are 0, a Julian */
49 *n = 0; /* date has been inserted in d */
50
51 switch(*nptr)
52 {
53 case 'M':
54 {
55 /* This routine counts on the user to have specified "Mm.n.d",
56 where 1 <= n <= 5, 1 <= m <= 12, 0 <= d <= 6 */
57
58 *m = strtol(++nptr, endptr, 10);
59 (*endptr)++; /* Skip the dot */
60 *n = strtol(*endptr, endptr, 10);
61 (*endptr)++; /* Skip the dot */
62 *d = strtol(*endptr, endptr, 10);
63
64 return;
65 }
66 case 'J':
67 *leap_p = 0; /* Never count with leap years */
68 default: /* trust the user to have inserted a number! */
69 *d = strtol(++nptr, endptr, 10);
70 return;
71 }
72}
73
74struct vms_vectim
75{
76 short year, month, day, hour, minute, second, centi_second;
77};
78static void find_dst_time(int m, int n, long d,
79 int hour, int minute, int second,
80 int leap_p,
81 long vms_internal_time[2])
82{
83 long status = SYS$GETTIM(vms_internal_time);
84 struct vms_vectim vms_vectime;
85 status = SYS$NUMTIM(&vms_vectime, vms_internal_time);
86
87 if (m == 0 && n == 0)
88 {
89 long tmp_vms_internal_time[2][2];
90 long day_of_year;
91 long tmp_operation = LIB$K_DAY_OF_YEAR;
92
93 status = LIB$CVT_FROM_INTERNAL_TIME(&tmp_operation, &day_of_year,
94 vms_internal_time);
95
96 vms_vectime.month = 2;
97 vms_vectime.day = 29;
98 status = LIB$CVT_VECTIM(&vms_vectime, tmp_vms_internal_time[0]);
99 if (status & 1) /* This is a leap year */
100 {
101 if (!leap_p && d > 59)
102 d ++; /* If we don't count with 29th Feb,
103 and this is a leap year, count up,
104 to make day 60 really become the
105 1st March. */
106 }
107 /* 1st January, at midnight */
108 vms_vectime.month = 1;
109 vms_vectime.day = 1;
110 vms_vectime.hour = hour;
111 vms_vectime.minute = minute;
112 vms_vectime.second = second;
113 vms_vectime.centi_second = 0;
114 status = LIB$CVT_VECTIM(&vms_vectime, tmp_vms_internal_time[0]);
115 tmp_operation = LIB$K_DELTA_DAYS;
116 status = LIB$CVT_TO_INTERNAL_TIME(&tmp_operation, &d,
117 tmp_vms_internal_time[1]);
118 /* now, tmp_vms_interval_time[0] contains 1st Jan, 00:00:00,
119 and tmp_vms_interval_time[1] contains delta time +d days.
120 Let's just add them together */
121 status = LIB$ADD_TIMES(tmp_vms_internal_time[0],
122 tmp_vms_internal_time[1],
123 vms_internal_time);
124 }
125 else
126 {
127 long tmp_vms_internal_time[2];
128 long day_of_week;
129 long tmp_operation = LIB$K_DAY_OF_YEAR;
130
131 if (d == 0) /* 0 is Sunday, which isn't compatible with VMS,
132 where day_of_week is 1 -- 7, and 1 is Monday */
133 {
134 d = 7; /* So a simple conversion is required */
135 }
136 vms_vectime.month = m;
137 vms_vectime.day = 1;
138 vms_vectime.hour = hour;
139 vms_vectime.minute = minute;
140 vms_vectime.second = second;
141 vms_vectime.centi_second = 0;
142 status = LIB$CVT_VECTIM(&vms_vectime, tmp_vms_internal_time);
143 tmp_operation = LIB$K_DAY_OF_WEEK;
144 status = LIB$CVT_FROM_INTERNAL_TIME(&tmp_operation, &day_of_week,
145 tmp_vms_internal_time);
146 d -= day_of_week;
147 if (d < 0)
148 {
149 d += 7;
150 }
151 vms_vectime.day += (n-1)*7 + d;
152 status = LIB$CVT_VECTIM(&vms_vectime, vms_internal_time);
153 if (!(status & 1))
154 {
155 vms_vectime.day -= 7; /* n was probably 5 */
156 status = LIB$CVT_VECTIM(&vms_vectime, vms_internal_time);
157 }
158 }
159}
160
161static cmp_vms_internal_times(long vms_internal_time1[2],
162 long vms_internal_time2[2])
163{
164 if (vms_internal_time1[1] < vms_internal_time2[1])
165 return -1;
166 else
167 if (vms_internal_time1[1] > vms_internal_time2[1])
168 return 1;
169
170 if (vms_internal_time1[0] < vms_internal_time2[0])
171 return -1;
172 else
173 if (vms_internal_time1[0] > vms_internal_time2[0])
174 return 1;
175
176 return 0;
177}
178
179/* -------------------------- Global routines ------------------------------ */
180
181#ifdef tzset
182#undef tzset
183#endif
184void sys_tzset()
185{
186 char *TZ;
187 char *p, *q;
188
189 if (daylight_set)
190 return;
191
192 daylight = 0;
193
194 if ((TZ = getenv("TZ")) == 0)
195 return;
196
197 p = TZ;
198 q = tzname[0];
199
200 while(*p != '\0'
201 && (*p <'0' || *p > '9') && *p != '-' && *p != '+' && *p != ',')
202 *q++ = *p++;
203 *q = '\0';
204
205 /* This is special for VMS, so I don't care if it doesn't exist anywhere
206 else */
207
208 timezone = read_time(p, &p, 1);
209
210 q = tzname[1];
211
212 while(*p != '\0'
213 && (*p <'0' || *p > '9') && *p != '-' && *p != '+' && *p != ',')
214 *q++ = *p++;
215 *q = '\0';
216
217 if (*p != '-' && *p != '+' && !(*p >='0' && *p <= '9'))
218 internal_daylight = timezone - 3600;
219 else
220 internal_daylight = read_time(p, &p, 1);
221
222 if (*p == ',')
223 {
224 int start_m;
225 int start_n;
226 int start_d;
227 int start_leap_p;
228 int start_hour=2, start_minute=0, start_second=0;
229
230 p++;
231 read_dst_time(p, &p, &start_m, &start_n, &start_d, &start_leap_p);
232 if (*p == '/')
233 {
234 long tmp = read_time (++p, &p, 0);
235 start_hour = tmp / 3600;
236 start_minute = (tmp % 3600) / 60;
237 start_second = tmp % 60;
238 }
239 if (*p == ',')
240 {
241 int end_m;
242 int end_n;
243 int end_d;
244 int end_leap_p;
245 int end_hour=2, end_minute=0, end_second=0;
246
247 p++;
248 read_dst_time(p, &p, &end_m, &end_n, &end_d, &end_leap_p);
249 if (*p == '/')
250 {
251 long tmp = read_time (++p, &p, 0);
252 end_hour = tmp / 3600;
253 end_minute = (tmp % 3600) / 60;
254 end_second = tmp % 60;
255 }
256 {
257 long vms_internal_time[3][2];
258 find_dst_time(start_m, start_n, start_d,
259 start_hour, start_minute, start_second,
260 start_leap_p,
261 vms_internal_time[0]);
262 SYS$GETTIM(&vms_internal_time[1]);
263 find_dst_time(end_m, end_n, end_d,
264 end_hour, end_minute, end_second,
265 end_leap_p,
266 vms_internal_time[2]);
267 if (cmp_vms_internal_times(vms_internal_time[0],
268 vms_internal_time[1]) < 0
269 && cmp_vms_internal_times(vms_internal_time[1],
270 vms_internal_time[2]) < 0)
271 daylight = 1;
272 }
273 }
274 }
275}
276
277#ifdef localtime
278#undef localtime
279#endif
280struct tm *sys_localtime(time_t *clock)
281{
282 struct tm *tmp = localtime(clock);
283
284 sys_tzset();
285 tmp->tm_isdst = daylight;
286
287 return tmp;
288}
289
290#ifdef gmtime
291#undef gmtime
292#endif
293struct tm *sys_gmtime(time_t *clock)
294{
295 static struct tm gmt;
296 struct vms_vectim tmp_vectime;
297 long vms_internal_time[3][2];
298 long tmp_operation = LIB$K_DELTA_SECONDS;
299 long status;
300 long tmp_offset;
301 char tmp_o_sign;
302
303 sys_tzset();
304
305 if (daylight)
306 tmp_offset = internal_daylight;
307 else
308 tmp_offset = timezone;
309
310 if (tmp_offset < 0)
311 {
312 tmp_o_sign = -1;
313 tmp_offset = -tmp_offset;
314 }
315 else
316 tmp_o_sign = 1;
317
318 status = LIB$CVT_TO_INTERNAL_TIME(&tmp_operation, &tmp_offset,
319 vms_internal_time[1]);
320 status = SYS$GETTIM(vms_internal_time[0]);
321 if (tmp_o_sign < 0)
322 {
323 status = LIB$SUB_TIMES(vms_internal_time[0],
324 vms_internal_time[1],
325 vms_internal_time[2]);
326 }
327 else
328 {
329 status = LIB$ADD_TIMES(vms_internal_time[0],
330 vms_internal_time[1],
331 vms_internal_time[2]);
332 }
333
334 status = SYS$NUMTIM(&tmp_vectime, vms_internal_time[2]);
335 gmt.tm_sec = tmp_vectime.second;
336 gmt.tm_min = tmp_vectime.minute;
337 gmt.tm_hour = tmp_vectime.hour;
338 gmt.tm_mday = tmp_vectime.day;
339 gmt.tm_mon = tmp_vectime.month - 1;
340 gmt.tm_year = tmp_vectime.year % 100;
341
342 tmp_operation = LIB$K_DAY_OF_WEEK;
343 status = LIB$CVT_FROM_INTERNAL_TIME(&tmp_operation,
344 &gmt.tm_wday,
345 vms_internal_time[2]);
346 if (gmt.tm_wday == 7) gmt.tm_wday = 0;
347
348 tmp_operation = LIB$K_DAY_OF_YEAR;
349 status = LIB$CVT_FROM_INTERNAL_TIME(&tmp_operation,
350 &gmt.tm_yday,
351 vms_internal_time[2]);
352 gmt.tm_yday--;
353 gmt.tm_isdst = daylight;
354
355 return &gmt;
356}
357