Clock.Mod 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. (**
  2. AUTHOR: Alexey Morozov, HighDim GmbH, 2018
  3. PURPOSE: A2 clock with a plugable RTC get/set interface
  4. *)
  5. (* Aos, Copyright 2001, Pieter Muller, ETH Zurich *)
  6. MODULE Clock;
  7. TYPE
  8. (*
  9. Function for getting time from an RTC device
  10. second: seconds \in [0,59]
  11. minute: minutes \in [0,59]
  12. hour: hours \in [0,23]
  13. day: days \in [1,31]
  14. month: months \in [1,12]
  15. year: the actual year minus 1900
  16. *)
  17. GetRtcTimeFunc = PROCEDURE{DELEGATE}(VAR second, minute, hour, day, month: SHORTINT; VAR year: INTEGER): BOOLEAN;
  18. (*
  19. Function for setting up time on an RTC device
  20. second: seconds \in [0,59]
  21. minute: minutes \in [0,59]
  22. hour: hours \in [0,23]
  23. day: days \in [1,31]
  24. month: months \in [1,12]
  25. year: the actual year minus 1900
  26. *)
  27. SetRtcTimeFunc =PROCEDURE{DELEGATE}(second, minute, hour, day, month: SHORTINT; year: INTEGER): BOOLEAN;
  28. VAR
  29. getRtcTime: GetRtcTimeFunc;
  30. setRtcTime: SetRtcTimeFunc;
  31. tz*: LONGINT; (** system time zone offset in minutes (from -720 to 720) *)
  32. starttime*, startdate*: LONGINT; (** time this module was loaded (usually boot time) *)
  33. (** Return the current time and date in Oberon format. *)
  34. PROCEDURE Get*(VAR time, date: LONGINT);
  35. VAR
  36. second, minute, hour, day, month: SHORTINT;
  37. year: INTEGER;
  38. BEGIN{EXCLUSIVE}
  39. IF getRtcTime # NIL THEN
  40. IF getRtcTime(second, minute, hour, day, month, year) THEN
  41. time := LONGINT(hour)*4096 + LONGINT(minute)*64 + second;
  42. date := LONGINT(year)*512 + LONGINT(month)*32 + day;
  43. RETURN;
  44. END;
  45. END;
  46. time := 0;
  47. date := 0;
  48. END Get;
  49. (** Set the current time and date in Oberon format. *)
  50. PROCEDURE Set*(time, date: LONGINT);
  51. VAR
  52. second, minute, hour, day, month: SHORTINT;
  53. year: INTEGER;
  54. BEGIN{EXCLUSIVE}
  55. IF setRtcTime # NIL THEN
  56. second := SHORTINT(time MOD 64);
  57. minute := SHORTINT(time DIV 64 MOD 64);
  58. hour := SHORTINT(time DIV 4096 MOD 32);
  59. day := SHORTINT(date MOD 32);
  60. month := SHORTINT(date DIV 32 MOD 16);
  61. year := INTEGER(date DIV 512);
  62. IF setRtcTime(second, minute, hour, day, month, year) THEN
  63. END;
  64. END;
  65. END Set;
  66. PROCEDURE Install*(get: GetRtcTimeFunc; set: SetRtcTimeFunc);
  67. BEGIN
  68. BEGIN{EXCLUSIVE}
  69. getRtcTime := get;
  70. setRtcTime := set;
  71. END;
  72. Get(starttime, startdate);
  73. END Install;
  74. BEGIN
  75. tz := 2*60; (* fixme: configurable *)
  76. END Clock.
  77. (*
  78. 23.08.1999 pjm Split from Aos.Kernel
  79. *)
  80. (**
  81. Notes
  82. The time and date are that of the real-time clock of the system, which may be set to universal time, or to some local time zone.
  83. The tz variable indicates the system time zone offset from universal time in minutes. It may be updated at any time due to daylight savings time. E.g. MET DST is 2 * 60 = 120.
  84. The time and date are each represented in an encoded LONGINT.
  85. Converting from year, month, day, hour, minute, second to time, date:
  86. time := hour*4096 + minute*64 + second;
  87. date := (year-1900)*512 + month*32 + day;
  88. Converting from time to hour, minute, second:
  89. hour := time DIV 4096 MOD 32;
  90. minute := time DIV 64 MOD 64;
  91. second := time MOD 64;
  92. Converting from date to year, month, day:
  93. year = 1900+date DIV 512;
  94. month = date DIV 32 MOD 16;
  95. day = date MOD 32;
  96. All years in the current millenium can be represented. The 1900 offset is a historical artefact from the Oberon system.
  97. Time and date values (respectively) can be compared with the normal Oberon operators <, <=, =, >=, >, #. Overflow at midnight has to be handled separately.
  98. *)