Subversion Repositories Kolibri OS

Rev

Rev 6613 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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