xref: /inferno-os/appl/charon/date.b (revision 54041ca415f1fdfa1c3dcd82d524223cfa890940)
1implement Date;
2
3include "common.m";
4include "date.m";
5include "daytime.m";
6	daytime: Daytime;
7	Tm: import daytime;
8
9sys: Sys;
10CU: CharonUtils;
11
12wdayname := array[] of {
13	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
14};
15
16monname := array[] of {
17	"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
18};
19
20init(cu: CharonUtils)
21{
22	sys = load Sys Sys->PATH;
23	CU = cu;
24	daytime = load Daytime Daytime->PATH;
25	if (daytime==nil)
26		raise sys->sprint("EXInternal: can't load Daytime: %r");
27}
28
29# print dates in the format
30# Wkd, DD Mon YYYY HH:MM:SS GMT
31
32dateconv(t: int): string
33{
34	tm : ref Tm;
35	tm = daytime->gmt(t);
36	return sys->sprint("%s, %.2d %s %.4d %.2d:%.2d:%.2d GMT",
37		wdayname[tm.wday], tm.mday, monname[tm.mon], tm.year+1900,
38		tm.hour, tm.min, tm.sec);
39}
40
41# parse a date and return the seconds since the epoch
42# return 0 for a failure
43#
44# need to handle three formats (we'll be a bit more tolerant)
45#  Sun, 06 Nov 1994 08:49:37 GMT  (rfc822+rfc1123; preferred)
46#  Sunday, 06-Nov-94 08:49:37 GMT (rfc850, obsoleted by rfc1036)
47#  Sun Nov  6 08:49:37 1994	  (ANSI C's asctime() format; GMT assumed)
48
49date2sec(date : string): int
50{
51	tm := daytime->string2tm(date);
52	if(tm == nil || tm.year < 70 || tm.zone != "GMT")
53		t := 0;
54	else
55		t = daytime->tm2epoch(tm);
56	return t;
57}
58
59now(): int
60{
61	return daytime->now();
62}
63