Debian Codegraph @

gcc/m2/gm2-libs-log/TimeDate.mod

raw
1 (* TimeDate.mod provides a Logitech-3.0 compatible library module.
2
3 Copyright (C) 2005-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE TimeDate ;
28
29
30 FROM SYSTEM IMPORT ADDRESS, ADR, TSIZE ;
31 FROM libc IMPORT tm, time, time_t, memcpy, localtime ;
32 FROM DynamicStrings IMPORT String, Mark, ConCat, InitString, KillString, CopyOut ;
33 FROM FormatStrings IMPORT Sprintf3 ;
34
35 IMPORT Selective ;
36
37
38 (*
39 TimeToString - convert time, t, to a string.
40 The string, s, should be at least 19 characters
41 long and the returned string will be
42
43 yyyy-mm-dd hh:mm:ss
44 *)
45
46 PROCEDURE TimeToString (t: Time; VAR s: ARRAY OF CHAR) ;
47 VAR
48 q : String ;
49 y, m, d, h, sec: CARDINAL ;
50 BEGIN
51 WITH t DO
52 y := day DIV 512 + 1900 ;
53 m := (day DIV 32) MOD 16 ;
54 d := day MOD 32 ;
55 q := Sprintf3(Mark(InitString('%04d-%02d-%02d')), y, m, d) ;
56 h := minute DIV 60 ;
57 m := minute MOD 60 ;
58 sec := millisec DIV 1000 ;
59 q := ConCat(q, Mark(Sprintf3(Mark(InitString(' %02d:%02d:%02d')), h, m, sec))) ;
60 CopyOut(s, q) ;
61 q := KillString(q) ;
62 END
63 END TimeToString ;
64
65
66 PROCEDURE TimeToZero (VAR t: Time) ;
67 BEGIN
68 WITH t DO
69 day := 0 ;
70 minute := 0 ;
71 millisec := 0
72 END
73 END TimeToZero ;
74
75
76 PROCEDURE CompareTime (t1, t2: Time) : INTEGER ;
77 BEGIN
78 IF t1.day<t2.day
79 THEN
80 RETURN -1
81 ELSIF t1.day>t2.day
82 THEN
83 RETURN 1
84 ELSE
85 IF t1.minute<t2.minute
86 THEN
87 RETURN -1
88 ELSIF t1.minute>t2.minute
89 THEN
90 RETURN 1
91 ELSE
92 IF t1.millisec<t2.millisec
93 THEN
94 RETURN -1
95 ELSIF t1.millisec>t2.millisec
96 THEN
97 RETURN 1
98 ELSE
99 RETURN 0
100 END
101 END
102 END
103 END CompareTime ;
104
105
106 PROCEDURE SetTime (curTime: Time) ;
107 BEGIN
108 (* does nothing *)
109 END SetTime ;
110
111
112 PROCEDURE GetTime (VAR curTime: Time) ;
113 VAR
114 l : time_t ;
115 r : INTEGER ;
116 t : tm ;
117 a : ADDRESS ;
118 tv : Selective.Timeval ;
119 s, u: CARDINAL ;
120 BEGIN
121 tv := Selective.InitTime(0, 0) ;
122 r := Selective.GetTimeOfDay(tv) ;
123 l := time(NIL) ;
124 IF l#-1
125 THEN
126 a := localtime(l) ;
127 a := memcpy(ADR(t), a, TSIZE(t)) ;
128 WITH curTime DO
129 day := t.tm_mday+(t.tm_mon+1)*32+t.tm_year*512 ;
130 minute := t.tm_min+t.tm_hour*60 ;
131 Selective.GetTime(tv, s, u) ;
132 (* s MOD 61, to allow for leap seconds *)
133 millisec := (u DIV 1000) MOD (60 * 1000) + ((s MOD 61) * 1000)
134 END
135 END ;
136 tv := Selective.KillTime(tv)
137 END GetTime ;
138
139
140 END TimeDate.