1
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Magnitude subclass:#Time
|
|
14 |
instanceVariableNames:'secondsLow secondsHi'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Magnitude-General'
|
|
18 |
!
|
|
19 |
|
|
20 |
Time comment:'
|
|
21 |
|
|
22 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
23 |
All Rights Reserved
|
|
24 |
|
|
25 |
time represents a particular second in a day; since we depend on
|
|
26 |
unix, the seconds are counted from 1. Jan 1970 NOT as in Smalltalk-80
|
|
27 |
from 1. Jan 1901; since unix-time is 32 bit which does not fit into
|
|
28 |
a SmallInteger, we keep low and hi 16bit of the time separately.
|
|
29 |
|
|
30 |
%W% %E%
|
|
31 |
'!
|
|
32 |
|
|
33 |
!Time class methodsFor:'instance creation'!
|
|
34 |
|
|
35 |
secondClock
|
|
36 |
"return seconds of now - for GNU-ST compatibility"
|
|
37 |
|
|
38 |
^ OperatingSystem getTime
|
|
39 |
!
|
|
40 |
|
|
41 |
fromUnixTimeLow:low and:hi
|
|
42 |
^ self basicNew setSecondsLow:low and:hi
|
|
43 |
!
|
|
44 |
|
|
45 |
now
|
|
46 |
"return an instance of Time representing this moment"
|
|
47 |
|
|
48 |
^ self basicNew setSecondsLow:(OperatingSystem getTimeLow)
|
|
49 |
and:(OperatingSystem getTimeHi)
|
|
50 |
!
|
|
51 |
|
|
52 |
millisecondClockValue
|
|
53 |
"return the millisecond clock - since this one overruns
|
|
54 |
regularly, use only for short timing deltas"
|
|
55 |
|
|
56 |
^ OperatingSystem getMillisecondTime.
|
|
57 |
! !
|
|
58 |
|
|
59 |
!Time class methodsFor:'timing evaluations'!
|
|
60 |
|
|
61 |
secondsToRun:aBlock
|
|
62 |
"evaluate the argument, aBlock; return the number of seconds it took"
|
|
63 |
|
|
64 |
|startTime endTime|
|
|
65 |
|
|
66 |
startTime := self now.
|
|
67 |
aBlock value.
|
|
68 |
endTime := self now.
|
|
69 |
^ endTime - startTime
|
|
70 |
!
|
|
71 |
|
|
72 |
millisecondsToRun:aBlock
|
|
73 |
"evaluate the argument, aBlock; return the number of milliseconds it took"
|
|
74 |
|
|
75 |
|startTime endTime|
|
|
76 |
|
|
77 |
startTime := OperatingSystem getMillisecondTime.
|
|
78 |
aBlock value.
|
|
79 |
endTime := OperatingSystem getMillisecondTime.
|
|
80 |
^ endTime - startTime
|
|
81 |
! !
|
|
82 |
|
|
83 |
!Time class methodsFor:'ST-80 compatibility'!
|
|
84 |
|
|
85 |
totalSeconds
|
|
86 |
^ self secondClock
|
|
87 |
! !
|
|
88 |
|
|
89 |
!Time methodsFor:'accessing'!
|
|
90 |
|
|
91 |
hourInDay
|
|
92 |
"return the hour-part"
|
|
93 |
|
|
94 |
|hr|
|
|
95 |
|
|
96 |
OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
|
|
97 |
:hours :minutes :secs |
|
|
98 |
|
|
99 |
hr := hours
|
|
100 |
].
|
|
101 |
^ hr
|
|
102 |
!
|
|
103 |
|
|
104 |
minuteInDay
|
|
105 |
"return the minute-part"
|
|
106 |
|
|
107 |
|m|
|
|
108 |
|
|
109 |
OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
|
|
110 |
:hours :minutes :secs |
|
|
111 |
|
|
112 |
m := minutes
|
|
113 |
].
|
|
114 |
^ m
|
|
115 |
!
|
|
116 |
|
|
117 |
secondInDay
|
|
118 |
"return the second-part"
|
|
119 |
|
|
120 |
|s|
|
|
121 |
|
|
122 |
OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
|
|
123 |
:hours :minutes :secs |
|
|
124 |
|
|
125 |
s := secs
|
|
126 |
].
|
|
127 |
^ s
|
|
128 |
! !
|
|
129 |
|
|
130 |
!Time methodsFor:'comparing'!
|
|
131 |
|
|
132 |
> aTime
|
|
133 |
(aTime isMemberOf:Time) ifTrue:[
|
|
134 |
(secondsHi > aTime secondsHi) ifTrue:[^ true].
|
|
135 |
(secondsHi < aTime secondsHi) ifTrue:[^ false].
|
|
136 |
(secondsLow > aTime secondsLow) ifTrue:[^ true].
|
|
137 |
^ false
|
|
138 |
].
|
|
139 |
^ self getSeconds > aTime getSeconds
|
|
140 |
!
|
|
141 |
|
|
142 |
< aTime
|
|
143 |
(aTime isMemberOf:Time) ifTrue:[
|
|
144 |
(secondsHi < aTime secondsHi) ifTrue:[^ true].
|
|
145 |
(secondsHi > aTime secondsHi) ifTrue:[^ false].
|
|
146 |
(secondsLow < aTime secondsLow) ifTrue:[^ true].
|
|
147 |
^ false
|
|
148 |
].
|
|
149 |
^ self getSeconds < aTime getSeconds
|
|
150 |
!
|
|
151 |
|
|
152 |
= aTime
|
|
153 |
(aTime isMemberOf:Time) ifTrue:[
|
|
154 |
^ ((secondsLow == aTime secondsLow) and:[secondsHi == aTime secondsHi])
|
|
155 |
].
|
|
156 |
^ self getSeconds = aTime getSeconds
|
|
157 |
! !
|
|
158 |
|
|
159 |
!Time methodsFor:'arithmetic'!
|
|
160 |
|
|
161 |
- aTime
|
|
162 |
"return delta in seconds between 2 times"
|
|
163 |
|
|
164 |
^ self getSeconds - (aTime getSeconds)
|
|
165 |
!
|
|
166 |
|
|
167 |
addTime:timeAmount
|
|
168 |
"return a new Time timeAmount seconds from myself"
|
|
169 |
|
|
170 |
^Time new setSeconds:(self getSeconds + timeAmount)
|
|
171 |
!
|
|
172 |
|
|
173 |
subtractTime:timeAmount
|
|
174 |
"return a new Time timeAmount seconds before myself"
|
|
175 |
|
|
176 |
^Time new setSeconds:(self getSeconds - timeAmount)
|
|
177 |
! !
|
|
178 |
|
|
179 |
!Time methodsFor:'printing'!
|
|
180 |
|
|
181 |
printString
|
|
182 |
|aString|
|
|
183 |
|
|
184 |
OperatingSystem computeTimePartsOf:secondsLow and:secondsHi for:[
|
|
185 |
:hours :minutes :secs |
|
|
186 |
|
|
187 |
aString := hours printString , ':' .
|
|
188 |
(minutes < 10) ifTrue:[aString := aString , '0'].
|
|
189 |
aString := aString , minutes printString.
|
|
190 |
aString := aString , ':'.
|
|
191 |
(secs < 10) ifTrue:[aString := aString , '0'].
|
|
192 |
aString := aString , secs printString
|
|
193 |
].
|
|
194 |
^ aString
|
|
195 |
! !
|
|
196 |
|
|
197 |
!Time methodsFor:'private'!
|
|
198 |
|
|
199 |
secondsLow
|
|
200 |
^ secondsLow
|
|
201 |
!
|
|
202 |
|
|
203 |
secondsHi
|
|
204 |
^ secondsHi
|
|
205 |
!
|
|
206 |
|
|
207 |
getSeconds
|
|
208 |
^ (secondsHi * 16r10000) + secondsLow
|
|
209 |
!
|
|
210 |
|
|
211 |
setSeconds:secs
|
|
212 |
secondsHi := secs // 16r10000.
|
|
213 |
secondsLow := secs \\ 16r10000
|
|
214 |
!
|
|
215 |
|
|
216 |
setSecondsLow:secsLow and:secsHi
|
|
217 |
secondsHi := secsHi.
|
|
218 |
secondsLow := secsLow
|
|
219 |
! !
|