2
0

Dates.txt 3.3 KB

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