DEADSOFTWARE

Port, TODO
[bbcp.git] / new / _Linux_ / Host / Mod / Dates.txt
1 MODULE HostDates;
3 (* THIS IS TEXT COPY OF Dates.odc *)
4 (* DO NOT EDIT *)
6 IMPORT
7 SYSTEM, LinLibc, Dates;
9 (* Dates Hook *)
11 TYPE
12 DatesHook = POINTER TO RECORD (Dates.Hook) END;
14 (*
16 Some conversions are needed between the Linux and the BlackBox representations of dates. The following
17 table shows the differences:
19 (!) Linux BlackBox
20 year from year 1900 from year 0000
21 month range 0-11 range 1-12
22 weekday 0:sunday - 6:satruday 0:monday - 6:sunday
23 (!) *)
25 PROCEDURE (h: DatesHook) DateToString (d: Dates.Date; format: INTEGER; OUT str: ARRAY OF CHAR);
26 VAR tm: LinLibc.tmDesc; sstr: ARRAY 64 OF SHORTCHAR; res: LinLibc.size_t;
27 BEGIN
28 ASSERT(format IN {Dates.short, Dates.abbreviated, Dates.long, Dates.plainAbbreviated, Dates.plainLong}, 20);
29 tm.tm_year := d.year - 1900; (* Linux counts years from 1900 but BlackBox from 0000 *)
30 tm.tm_mon := d.month - 1; tm.tm_mday := d.day;
31 tm.tm_wday := (Dates.DayOfWeek(d) + 1) MOD 7;
32 IF format = Dates.short THEN
33 res := LinLibc.strftime(sstr, LEN(sstr), "%x", SYSTEM.VAL(LinLibc.tm, SYSTEM.ADR(tm)))
34 ELSIF format = Dates.abbreviated THEN
35 res := LinLibc.strftime(sstr, LEN(sstr), "%a, %b %d, %Y", SYSTEM.VAL(LinLibc.tm, SYSTEM.ADR(tm)))
36 ELSIF format = Dates.long THEN
37 res := LinLibc.strftime(sstr, LEN(sstr), "%A, %B %d, %Y", SYSTEM.VAL(LinLibc.tm, SYSTEM.ADR(tm)))
38 ELSIF format = Dates.plainAbbreviated THEN
39 res := LinLibc.strftime(sstr, LEN(sstr), "%b %d, %Y", SYSTEM.VAL(LinLibc.tm, SYSTEM.ADR(tm)))
40 ELSE (* format = Dates.plainLong *)
41 res := LinLibc.strftime(sstr, LEN(sstr), "%B %d, %Y", SYSTEM.VAL(LinLibc.tm, SYSTEM.ADR(tm)))
42 END;
43 IF res > 0 THEN str := sstr$ELSE str := "invalid date" END
44 END DateToString;
46 PROCEDURE (h: DatesHook) GetTime (OUT d: Dates.Date; OUT t: Dates.Time);
47 VAR time: LinLibc.time_t; tm: LinLibc.tm;
48 BEGIN
49 time := LinLibc.time(NIL);
50 tm := LinLibc.localtime(time);
51 d.year := tm.tm_year + 1900; (* Linux counts years from 1900 but BlackBox from 0000 *)
52 d.month := tm.tm_mon + 1; d.day := tm.tm_mday;
53 t.hour := tm.tm_hour; t.minute := tm.tm_min; t.second := tm.tm_sec
54 END GetTime;
56 PROCEDURE (h: DatesHook) GetUTCBias (OUT bias: INTEGER);
57 VAR time: LinLibc.time_t; tm: LinLibc.tm;
58 BEGIN
59 time := LinLibc.time(NIL);
60 tm := LinLibc.localtime(time); (* call to localtime needed to make sure that timezone is set *)
61 bias := LinLibc.timezone DIV 60;
62 END GetUTCBias;
64 PROCEDURE (h: DatesHook) GetUTCTime (OUT d: Dates.Date; OUT t: Dates.Time);
65 VAR time: LinLibc.time_t; tm: LinLibc.tm;
66 BEGIN
67 time := LinLibc.time(NIL);
68 tm := LinLibc.gmtime(time);
69 d.year := tm.tm_year + 1900; (* Linux counts years from 1900 but BlackBox from 0000 *)
70 d.month := tm.tm_mon + 1; d.day := tm.tm_mday;
71 t.hour := tm.tm_hour; t.minute := tm.tm_min; t.second := tm.tm_sec
72 END GetUTCTime;
74 PROCEDURE (h: DatesHook) TimeToString (t: Dates.Time; OUT str: ARRAY OF CHAR);
75 VAR tm: LinLibc.tmDesc; sstr: ARRAY 64 OF SHORTCHAR; res: LinLibc.size_t;
76 BEGIN
77 tm.tm_hour := t.hour; tm.tm_min := t.minute; tm.tm_sec := t.second;
78 res := LinLibc.strftime(sstr, LEN(sstr), "%X", SYSTEM.VAL(LinLibc.tm, SYSTEM.ADR(tm)));
79 IF res > 0 THEN str := sstr$ELSE str := "invalid time" END
80 END TimeToString;
83 PROCEDURE Init;
84 VAR
85 datesHook: DatesHook;
86 BEGIN
87 NEW(datesHook); Dates.SetHook(datesHook);
88 END Init;
90 BEGIN
91 Init
92 END HostDates.