DateTime.ob07 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. (*
  2. Copyright 2016, 2018 Anton Krotov
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. *)
  14. MODULE DateTime;
  15. IMPORT KOSAPI;
  16. CONST ERR* = -7.0E5;
  17. PROCEDURE Encode*(Year, Month, Day, Hour, Min, Sec: INTEGER): REAL;
  18. VAR d, i: INTEGER; M: ARRAY 14 OF CHAR; Res: REAL;
  19. BEGIN
  20. Res := ERR;
  21. IF (Year >= 1) & (Year <= 9999) & (Month >= 1) & (Month <= 12) &
  22. (Day >= 1) & (Day <= 31) & (Hour >= 0) & (Hour <= 23) &
  23. (Min >= 0) & (Min <= 59) & (Sec >= 0) & (Sec <= 59) THEN
  24. M := "_303232332323";
  25. IF (Year MOD 4 = 0) & (Year MOD 100 # 0) OR (Year MOD 400 = 0) THEN
  26. M[2] := "1"
  27. END;
  28. IF Day <= ORD(M[Month]) - ORD("0") + 28 THEN
  29. DEC(Year);
  30. d := Year * 365 + (Year DIV 4) - (Year DIV 100) + (Year DIV 400) + Day - 693594;
  31. FOR i := 1 TO Month - 1 DO
  32. d := d + ORD(M[i]) - ORD("0") + 28
  33. END;
  34. Res := FLT(d) + FLT(Hour * 3600000 + Min * 60000 + Sec * 1000) / 86400000.0
  35. END
  36. END
  37. RETURN Res
  38. END Encode;
  39. PROCEDURE Decode*(Date: REAL; VAR Year, Month, Day, Hour, Min, Sec: INTEGER): BOOLEAN;
  40. VAR Res, flag: BOOLEAN; d, t, i: INTEGER; M: ARRAY 14 OF CHAR;
  41. PROCEDURE MonthDay(n: INTEGER; VAR d, Month: INTEGER; M: ARRAY OF CHAR): BOOLEAN;
  42. VAR Res: BOOLEAN;
  43. BEGIN
  44. Res := FALSE;
  45. IF d > ORD(M[n]) - ORD("0") + 28 THEN
  46. d := d - ORD(M[n]) + ORD("0") - 28;
  47. INC(Month);
  48. Res := TRUE
  49. END
  50. RETURN Res
  51. END MonthDay;
  52. BEGIN
  53. IF (Date >= -693593.0) & (Date < 2958466.0) THEN
  54. d := FLOOR(Date);
  55. t := FLOOR((Date - FLT(d)) * 86400000.0);
  56. d := d + 693593;
  57. Year := 1;
  58. Month := 1;
  59. WHILE d > 0 DO
  60. d := d - 365 - ORD((Year MOD 4 = 0) & (Year MOD 100 # 0) OR (Year MOD 400 = 0));
  61. INC(Year)
  62. END;
  63. IF d < 0 THEN
  64. DEC(Year);
  65. d := d + 365 + ORD((Year MOD 4 = 0) & (Year MOD 100 # 0) OR (Year MOD 400 = 0))
  66. END;
  67. INC(d);
  68. M := "_303232332323";
  69. IF (Year MOD 4 = 0) & (Year MOD 100 # 0) OR (Year MOD 400 = 0) THEN
  70. M[2] := "1"
  71. END;
  72. i := 1;
  73. flag := TRUE;
  74. WHILE flag & (i <= 12) DO
  75. flag := MonthDay(i, d, Month, M);
  76. INC(i)
  77. END;
  78. Day := d;
  79. Hour := t DIV 3600000;
  80. t := t MOD 3600000;
  81. Min := t DIV 60000;
  82. t := t MOD 60000;
  83. Sec := t DIV 1000;
  84. Res := TRUE
  85. ELSE
  86. Res := FALSE
  87. END
  88. RETURN Res
  89. END Decode;
  90. PROCEDURE Now*(VAR Year, Month, Day, Hour, Min, Sec, Msec: INTEGER);
  91. VAR date, time: INTEGER;
  92. BEGIN
  93. date := KOSAPI.sysfunc1(29);
  94. time := KOSAPI.sysfunc1(3);
  95. Year := date MOD 16;
  96. date := date DIV 16;
  97. Year := (date MOD 16) * 10 + Year;
  98. date := date DIV 16;
  99. Month := date MOD 16;
  100. date := date DIV 16;
  101. Month := (date MOD 16) * 10 + Month;
  102. date := date DIV 16;
  103. Day := date MOD 16;
  104. date := date DIV 16;
  105. Day := (date MOD 16) * 10 + Day;
  106. date := date DIV 16;
  107. Hour := time MOD 16;
  108. time := time DIV 16;
  109. Hour := (time MOD 16) * 10 + Hour;
  110. time := time DIV 16;
  111. Min := time MOD 16;
  112. time := time DIV 16;
  113. Min := (time MOD 16) * 10 + Min;
  114. time := time DIV 16;
  115. Sec := time MOD 16;
  116. time := time DIV 16;
  117. Sec := (time MOD 16) * 10 + Sec;
  118. time := time DIV 16;
  119. Year := Year + 2000;
  120. Msec := 0
  121. END Now;
  122. END DateTime.