Subversion Repositories Kolibri OS

Rev

Rev 7597 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7696 akron1 1
(*
7597 akron1 2
    Copyright 2016, 2018 Anton Krotov
6613 leency 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 .
16
*)
17
 
18
MODULE DateTime;
19
 
20
IMPORT KOSAPI;
21
 
7597 akron1 22
CONST ERR* = -7.0E5;
6613 leency 23
 
7597 akron1 24
PROCEDURE Encode*(Year, Month, Day, Hour, Min, Sec: INTEGER): REAL;
25
VAR d, i: INTEGER; M: ARRAY 14 OF CHAR; Res: REAL;
6613 leency 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
7597 akron1 39
        d := d + ORD(M[i]) - ORD("0") + 28
6613 leency 40
      END;
7597 akron1 41
      Res := FLT(d) + FLT(Hour * 3600000 + Min * 60000 + Sec * 1000) / 86400000.0
6613 leency 42
    END
43
  END
44
  RETURN Res
45
END Encode;
46
 
7597 akron1 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;
6613 leency 49
 
7597 akron1 50
  PROCEDURE MonthDay(n: INTEGER; VAR d, Month: INTEGER; M: ARRAY OF CHAR): BOOLEAN;
6613 leency 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
7597 akron1 63
  IF (Date >= -693593.0) & (Date < 2958466.0) THEN
6613 leency 64
    d := FLOOR(Date);
7597 akron1 65
    t := FLOOR((Date - FLT(d)) * 86400000.0);
6613 leency 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
7597 akron1 85
      flag := MonthDay(i, d, Month, M);
6613 leency 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
 
7597 akron1 101
PROCEDURE Now*(VAR Year, Month, Day, Hour, Min, Sec, Msec: INTEGER);
6613 leency 102
VAR date, time: INTEGER;
103
BEGIN
7597 akron1 104
  date  := KOSAPI.sysfunc1(29);
105
  time  := KOSAPI.sysfunc1(3);
6613 leency 106
 
7597 akron1 107
  Year  := date MOD 16;
108
  date  := date DIV 16;
109
  Year  := (date MOD 16) * 10 + Year;
110
  date  := date DIV 16;
6613 leency 111
 
112
  Month := date MOD 16;
7597 akron1 113
  date  := date DIV 16;
6613 leency 114
  Month := (date MOD 16) * 10 + Month;
7597 akron1 115
  date  := date DIV 16;
6613 leency 116
 
117
  Day := date MOD 16;
7597 akron1 118
  date  := date DIV 16;
6613 leency 119
  Day := (date MOD 16) * 10 + Day;
7597 akron1 120
  date  := date DIV 16;
6613 leency 121
 
7597 akron1 122
  Hour  := time MOD 16;
123
  time  := time DIV 16;
124
  Hour  := (time MOD 16) * 10 + Hour;
125
  time  := time DIV 16;
6613 leency 126
 
127
  Min := time MOD 16;
7597 akron1 128
  time  := time DIV 16;
6613 leency 129
  Min := (time MOD 16) * 10 + Min;
7597 akron1 130
  time  := time DIV 16;
6613 leency 131
 
132
  Sec := time MOD 16;
7597 akron1 133
  time  := time DIV 16;
6613 leency 134
  Sec := (time MOD 16) * 10 + Sec;
7597 akron1 135
  time  := time DIV 16;
6613 leency 136
 
7597 akron1 137
  Year := Year + 2000;
138
  Msec := 0
6613 leency 139
END Now;
140
 
7696 akron1 141
END DateTime.