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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
implement Daytime;
#
# These routines convert time as follows:
#
# The epoch is 0000 Jan 1 1970 GMT.
# The argument time is in microseconds since then.
# The local(t) entry returns a reference to an ADT
# containing
#
# seconds (0-59)
# minutes (0-59)
# hours (0-23)
# day of month (1-31)
# month (0-11)
# year-1900
# weekday (0-6, Sun is 0)
# day of the year
# daylight savings flag
#
# The routine gets the daylight savings time from the file /locale/timezone.
#
# text(tvec)
# where tvec is produced by local
# returns a string that has the time in the form
#
# Thu Jan 01 00:00:00 GMT 1970n0
# 012345678901234567890123456789
# 0 1 2
#
# time() just reads the time from /dev/time
# and then calls localtime, then asctime.
#
# The sign bit of second times will turn on 68 years from the epoch ->2038
#
include "sys.m";
include "string.m";
include "daytime.m";
S: String;
sys: Sys;
dmsize := array[] of {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
ldmsize := array[] of {
31, 29, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
Timezone: adt
{
stname: string;
dlname: string;
stdiff: int;
dldiff: int;
dlpairs: array of int;
};
timezone: ref Timezone;
now(): int
{
if(sys == nil)
sys = load Sys Sys->PATH;
fd := sys->open("/dev/time", sys->OREAD);
if(fd == nil)
return 0;
buf := array[128] of byte;
n := sys->read(fd, buf, len buf);
if(n < 0)
return 0;
t := (big string buf[0:n]) / big 1000000;
return int t;
}
time(): string
{
t := now();
tm := local(t);
return text(tm);
}
local(tim: int): ref Tm
{
ct: ref Tm;
if(timezone == nil)
timezone = readtimezone(nil);
t := tim + timezone.stdiff;
dlflag := 0;
for(i := 0; i+1 < len timezone.dlpairs; i += 2) {
if(t >= timezone.dlpairs[i] && t < timezone.dlpairs[i+1]) {
t = tim + timezone.dldiff;
dlflag++;
break;
}
}
ct = gmt(t);
if(dlflag) {
ct.zone = timezone.dlname;
ct.tzoff = timezone.dldiff;
}
else {
ct.zone = timezone.stname;
ct.tzoff = timezone.stdiff;
}
return ct;
}
gmt(tim: int): ref Tm
{
xtime := ref Tm;
# break initial number into days
hms := tim % 86400;
day := tim / 86400;
if(hms < 0) {
hms += 86400;
day -= 1;
}
# generate hours:minutes:seconds
xtime.sec = hms % 60;
d1 := hms / 60;
xtime.min = d1 % 60;
d1 /= 60;
xtime.hour = d1;
# day is the day number.
# generate day of the week.
# The addend is 4 mod 7 (1/1/1970 was Thursday)
xtime.wday = (day + 7340036) % 7;
# year number
if(day >= 0)
for(d1 = 70; day >= dysize(d1+1900); d1++)
day -= dysize(d1+1900);
else
for (d1 = 70; day < 0; d1--)
day += dysize(d1+1900-1);
xtime.year = d1;
d0 := day;
xtime.yday = d0;
# generate month
if(dysize(d1+1900) == 366)
dmsz := ldmsize;
else
dmsz = dmsize;
for(d1 = 0; d0 >= dmsz[d1]; d1++)
d0 -= dmsz[d1];
xtime.mday = d0 + 1;
xtime.mon = d1;
xtime.zone = "GMT";
xtime.tzoff = 0;
return xtime;
}
wkday := array[] of {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
weekday := array[] of {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
month := array[] of {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
text(t: ref Tm): string
{
if(sys == nil)
sys = load Sys Sys->PATH;
year := 1900+t.year;
return sys->sprint("%s %s %.2d %.2d:%.2d:%.2d %s %d",
wkday[t.wday],
month[t.mon],
t.mday,
t.hour,
t.min,
t.sec,
t.zone,
year);
}
filet(now: int, file: int): string
{
if(sys == nil)
sys = load Sys Sys->PATH;
t := local(file);
if(now - file < 6*30*24*3600)
return sys->sprint("%s %.2d %.2d:%.2d",
month[t.mon], t.mday, t.hour, t.min);
year := 1900+t.year;
return sys->sprint("%s %.2d %d", month[t.mon], t.mday, year);
}
dysize(y: int): int
{
if(y%4 == 0 && (y%100 != 0 || y%400 == 0))
return 366;
return 365;
}
readtimezone(fname: string): ref Timezone
{
if(sys == nil)
sys = load Sys Sys->PATH;
tz := ref Timezone;
tz.stdiff = 0;
tz.stname = "GMT";
fd: ref Sys->FD;
if(fname == nil){
fd = sys->open("/env/timezone", Sys->OREAD);
if(fd == nil)
fd = sys->open("/locale/timezone", Sys->OREAD);
}else
fd = sys->open("/locale/" + fname, sys->OREAD);
if(fd == nil)
return tz;
buf := array[2048] of byte;
cnt := sys->read(fd, buf, len buf);
if(cnt <= 0)
return tz;
(n, val) := sys->tokenize(string buf[0:cnt], "\t \n\r");
if(n < 4)
return tz;
tz.stname = hd val;
val = tl val;
tz.stdiff = int hd val;
val = tl val;
tz.dlname = hd val;
val = tl val;
tz.dldiff = int hd val;
val = tl val;
tz.dlpairs = array[n-4] of {* => 0};
for(j := 0; val != nil; val = tl val)
tz.dlpairs[j++] = int hd val;
return tz;
}
SEC2MIN: con 60;
SEC2HOUR: con 60*SEC2MIN;
SEC2DAY: con 24*SEC2HOUR;
tm2epoch(tm: ref Tm): int
{
secs := 0;
#
# seconds per year
#
yr := tm.year + 1900;
if(yr < 1970)
for(i := yr; i < 1970; i++)
secs -= dysize(i) * SEC2DAY;
else
for(i = 1970; i < yr; i++)
secs += dysize(i) * SEC2DAY;
#
# seconds per month
#
if(dysize(yr) == 366)
dmsz := ldmsize;
else
dmsz = dmsize;
for(i = 0; i < tm.mon; i++)
secs += dmsz[i] * SEC2DAY;
#
# secs in last month
#
secs += (tm.mday-1) * SEC2DAY;
#
# hours, minutes, seconds
#
secs += tm.hour * SEC2HOUR;
secs += tm.min * SEC2MIN;
secs += tm.sec;
#
# time zone offset includes daylight savings time
#
return secs - tm.tzoff;
}
# handle three formats (we'll be a bit more tolerant)
# Sun, 06 Nov 1994 08:49:37 TZ (rfc822+rfc1123)
# Sunday, 06-Nov-94 08:49:37 TZ (rfc850, obsoleted by rfc1036)
# Sun Nov 6 08:49:37 1994 (ANSI C's asctime() format, assume GMT)
#
# return nil on parsing error
#
string2tm(date: string): ref Tm
{
buf: string;
ok: int;
tm := ref Tm;
if(S == nil)
S = load String String->PATH;
# Weekday|Wday
(date, buf) = dateword(date);
tm.wday = strlookup(wkday, buf);
if(tm.wday < 0)
tm.wday = strlookup(weekday, buf);
if(tm.wday < 0)
return nil;
# Try Mon
odate := date;
(date, buf) = dateword(date);
tm.mon = strlookup(month, buf);
if(tm.mon >= 0) {
# Mon was OK, so asctime() format
# DD
(date, tm.mday) = datenum(date);
if(tm.mday < 1 || tm.mday > 31)
return nil;
# HH:MM:SS
(ok, date) = hhmmss(date, tm);
if(!ok)
return nil;
# optional time zone
while(date != nil && date[0] == ' ')
date = date[1:];
if(date != nil && !(date[0] >= '0' && date[0] <= '9')){
for(i := 0; i < len date; i++)
if(date[i] == ' '){
(tm.zone, tm.tzoff) = tzinfo(date[0: i]);
date = date[i:];
break;
}
}
# YY|YYYY
(nil, tm.year) = datenum(date);
if(tm.year > 1900)
tm.year -= 1900;
if(tm.zone == ""){
tm.zone = "GMT";
tm.tzoff = 0;
}
} else {
# Mon was not OK
date = odate;
# DD Mon YYYY or DD-Mon-(YY|YYYY)
(date, tm.mday) = datenum(date);
if(tm.mday < 1 || tm.mday > 31)
return nil;
(date, buf) = dateword(date);
tm.mon = strlookup(month, buf);
if(tm.mon < 0 || tm.mon >= 12)
return nil;
(date, tm.year) = datenum(date);
if(tm.year > 1900)
tm.year -= 1900;
# HH:MM:SS
(ok, buf) = hhmmss(date, tm);
if(!ok)
return nil;
(tm.zone, tm.tzoff) = tzinfo(buf);
if(tm.zone == "")
return nil;
}
return tm;
}
dateword(date: string): (string, string)
{
notalnum: con "^A-Za-z0-9";
date = S->drop(date, notalnum);
(w, rest) := S->splitl(date, notalnum);
return (rest, w);
}
datenum(date: string): (string, int)
{
notdig: con "^0-9";
date = S->drop(date, notdig);
(num, rest) := S->splitl(date, notdig);
return (rest, int num);
}
strlookup(a: array of string, s: string): int
{
n := len a;
for(i := 0; i < n; i++) {
if(s == a[i])
return i;
}
return -1;
}
hhmmss(date: string, tm: ref Tm): (int, string)
{
err := (0, "");
(date, tm.hour) = datenum(date);
if(tm.hour < 0 || tm.hour >= 24)
return err;
(date, tm.min) = datenum(date);
if(tm.min < 0 || tm.min >= 60)
return err;
(date, tm.sec) = datenum(date);
if(tm.sec < 0 || tm.sec >= 60)
return err;
return (1, date);
}
tzinfo(tz: string): (string, int)
{
# strip leading and trailing whitespace
WS: con " \t";
tz = S->drop(tz, WS);
for(n := len tz; n > 0; n--) {
if(S->in(tz[n-1], WS) == 0)
break;
}
if(n < len tz)
tz = tz[:n];
# if no timezone, default to GMT
if(tz == nil)
return ("GMT", 0);
# GMT aliases
case tz {
"GMT" or
"UT" or
"UTC" or
"Z" =>
return ("GMT", 0);
}
# [+-]hhmm (hours and minutes offset from GMT)
if(len tz == 5 && (tz[0] == '+' || tz[0] == '-')) {
h := int tz[1:3];
m := int tz[3:5];
if(h > 23 || m > 59)
return ("", 0);
tzoff := h*SEC2HOUR + m*SEC2MIN;
if(tz[0] == '-')
tzoff = -tzoff;
return ("GMT", tzoff);
}
# try continental US timezones
filename: string;
case tz {
"CST" or "CDT" =>
filename = "CST.CDT";
"EST" or "EDT" =>
filename = "EST.EDT";
"MST" or "MDT" =>
filename = "MST.MDT";
"PST" or "PDT" =>
filename = "PST.PDT";
* =>
; # default to local timezone
}
tzdata := readtimezone(filename);
if(tzdata.stname == tz)
return (tzdata.stname, tzdata.stdiff);
if(tzdata.dlname == tz)
return (tzdata.dlname, tzdata.dldiff);
return ("", 0);
}
|