author | Claus Gittinger <cg@exept.de> |
Tue, 26 Feb 2008 11:29:14 +0100 | |
changeset 1930 | 935b2870be2e |
parent 1905 | ec30ff95406a |
child 1989 | fbaeabdda6bd |
permissions | -rw-r--r-- |
989 | 1 |
" |
2 |
COPYRIGHT (c) 1989 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 |
"{ Package: 'stx:libbasic2' }" |
|
13 |
||
14 |
Time subclass:#TimeDuration |
|
15 |
instanceVariableNames:'' |
|
1902 | 16 |
classVariableNames:'TimeDurationZero' |
989 | 17 |
poolDictionaries:'' |
1332 | 18 |
category:'Magnitude-Time' |
989 | 19 |
! |
20 |
||
21 |
!TimeDuration class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
26 |
All Rights Reserved |
|
27 |
||
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
" |
|
40 |
! ! |
|
41 |
||
42 |
!TimeDuration class methodsFor:'instance creation'! |
|
43 |
||
1555 | 44 |
days:d |
45 |
"return a new TimeDuration representing a duration of d days." |
|
46 |
||
47 |
^ self hours:(d*24) minutes:0 |
|
48 |
||
49 |
" |
|
50 |
TimeDuration days:1 |
|
51 |
" |
|
52 |
! |
|
53 |
||
1891
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
54 |
fromMilliseconds:millisecondsInterval |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
55 |
"redefined to disable wrapping at 24hours." |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
56 |
|
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
57 |
^ self new setMilliseconds:millisecondsInterval |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
58 |
|
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
59 |
" |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
60 |
TimeDuration fromMilliseconds:500 |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
61 |
" |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
62 |
|
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
63 |
"Created: / 18-07-2007 / 13:56:25 / cg" |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
64 |
! |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
65 |
|
1507 | 66 |
fromMinutes:minutesInterval |
67 |
^ self new setSeconds:(minutesInterval * 60) |
|
68 |
||
69 |
" |
|
70 |
TimeDuration fromMinutes:120 |
|
71 |
" |
|
72 |
! |
|
73 |
||
1028 | 74 |
fromSeconds:secondsInterval |
75 |
"redefined to disable wrapping at 24hours." |
|
76 |
||
77 |
^ self new setSeconds:secondsInterval |
|
78 |
||
79 |
" |
|
1506 | 80 |
TimeDuration fromSeconds:3600 |
81 |
" |
|
82 |
! |
|
83 |
||
1889 | 84 |
hours:h |
85 |
"return a new TimeDuration representing a duration of h hours. |
|
86 |
See also Time now / Date today / Timestamp now." |
|
87 |
||
88 |
^ self basicNew setHours:h minutes:0 seconds:0 milliseconds:0 |
|
89 |
||
90 |
" |
|
91 |
TimeDuration hours:2 |
|
92 |
TimeDuration hours:100 |
|
93 |
" |
|
94 |
||
95 |
"Created: / 14-07-2007 / 18:15:51 / cg" |
|
96 |
! |
|
97 |
||
1506 | 98 |
hours:h minutes:m |
99 |
"return a new TimeDuration representing a duration of h hours and m minutes. |
|
100 |
See also Time now / Date today / Timestamp now." |
|
101 |
||
1545
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
102 |
^ self basicNew setHours:h minutes:m seconds:0 milliseconds:0 |
1506 | 103 |
|
104 |
" |
|
105 |
TimeDuration hours:2 minutes:33 |
|
106 |
TimeDuration hours:100 minutes:33 |
|
1028 | 107 |
" |
108 |
! |
|
109 |
||
989 | 110 |
hours:h minutes:m seconds:s millis:millis |
1506 | 111 |
"return a new TimeDuration representing a duration of h hours, m minutes, s seconds and millis milliseconds. |
1433
47729d4283ef
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
1419
diff
changeset
|
112 |
See also Time now / Date today / Timestamp now." |
989 | 113 |
|
1545
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
114 |
self obsoleteMethodWarning:'use hours:minutes:seconds:milliseconds:'. |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
115 |
^ self hours:h minutes:m seconds:s milliseconds:millis |
989 | 116 |
|
117 |
" |
|
1545
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
118 |
TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123 |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
119 |
TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123 |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
120 |
" |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
121 |
! |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
122 |
|
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
123 |
hours:h minutes:m seconds:s milliseconds:millis |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
124 |
"return a new TimeDuration representing a duration of h hours, m minutes, s seconds and millis milliseconds. |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
125 |
See also Time now / Date today / Timestamp now." |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
126 |
|
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
127 |
^ self basicNew |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
128 |
setHours:h minutes:m seconds:s milliseconds:millis |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
129 |
|
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
130 |
" |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
131 |
TimeDuration hours:2 minutes:33 seconds:0 milliseconds:123 |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
132 |
TimeDuration hours:100 minutes:33 seconds:0 milliseconds:123 |
989 | 133 |
" |
1508 | 134 |
! |
135 |
||
1892 | 136 |
minutes:m |
137 |
"return a new TimeDuration representing a duration of m minutes. |
|
138 |
See also Time now / Date today / Timestamp now." |
|
139 |
||
140 |
^ self basicNew setHours:0 minutes:m seconds:0 milliseconds:0 |
|
141 |
||
142 |
" |
|
143 |
TimeDuration minutes:2 |
|
144 |
" |
|
145 |
||
146 |
"Created: / 06-08-2007 / 15:32:42 / cg" |
|
147 |
! |
|
148 |
||
1508 | 149 |
readFrom:aStringOrStream onError:exceptionBlock |
150 |
"return a new TimeDuration, reading a printed representation from aStream. |
|
151 |
The format is [n 'd'] [n 'h'] [n 'm'] [n 's']" |
|
152 |
||
153 |
^ [ |
|
1890 | 154 |
|seconds millis str val fraction uIdx unit unitChar1 unitChar2| |
1508 | 155 |
|
156 |
str := aStringOrStream readStream. |
|
157 |
seconds := 0. |
|
1890 | 158 |
millis := 0. |
1508 | 159 |
[ |
1897
d13a5cbd9250
reading from empty string fixed
Claus Gittinger <cg@exept.de>
parents:
1896
diff
changeset
|
160 |
val := Integer readFrom:str onError:nil. |
d13a5cbd9250
reading from empty string fixed
Claus Gittinger <cg@exept.de>
parents:
1896
diff
changeset
|
161 |
val isNil ifTrue:[^ exceptionBlock value]. |
1508 | 162 |
str peek == $. ifTrue:[ |
163 |
str next. |
|
164 |
fraction := Number readMantissaFrom:str radix:10. |
|
165 |
val := val + fraction. |
|
166 |
]. |
|
167 |
str skipSeparators. |
|
1894
8bf137acc445
if no unit is given in the readString, assume seconds.
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
168 |
str atEnd ifTrue:[ |
8bf137acc445
if no unit is given in the readString, assume seconds.
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
169 |
"/ no unit given - assume seconds |
8bf137acc445
if no unit is given in the readString, assume seconds.
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
170 |
^ self fromSeconds:val. |
8bf137acc445
if no unit is given in the readString, assume seconds.
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
171 |
"/ ^ exceptionBlock value |
8bf137acc445
if no unit is given in the readString, assume seconds.
Claus Gittinger <cg@exept.de>
parents:
1893
diff
changeset
|
172 |
]. |
1508 | 173 |
|
1890 | 174 |
unitChar1 := str next. |
175 |
uIdx := #($d $h $m $s) indexOf:unitChar1. |
|
1508 | 176 |
uIdx == 0 ifTrue:[^ exceptionBlock value]. |
1890 | 177 |
(unitChar1 == $m and:[str peek == $s]) ifTrue:[ |
178 |
str next. |
|
179 |
millis := millis + val. |
|
180 |
] ifFalse:[ |
|
181 |
unit := #(86400 "24*60*60" |
|
182 |
3600 |
|
183 |
60 |
|
184 |
1 ) at:uIdx. |
|
185 |
seconds := seconds + (unit * val). |
|
186 |
]. |
|
1508 | 187 |
str skipSeparators. |
188 |
str atEnd |
|
189 |
] whileFalse. |
|
1891
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
190 |
self fromMilliseconds:(seconds*1000+millis) rounded asInteger. |
1508 | 191 |
] on:Error do:exceptionBlock. |
192 |
||
193 |
" |
|
194 |
TimeDuration readFrom:'1h' |
|
195 |
TimeDuration readFrom:'1h 35m' |
|
196 |
TimeDuration readFrom:'25h' |
|
197 |
TimeDuration readFrom:'3d' |
|
1890 | 198 |
TimeDuration readFrom:'120s' |
199 |
TimeDuration readFrom:'1500ms |
|
200 |
TimeDuration readFrom:'3ms' |
|
1508 | 201 |
" |
1890 | 202 |
|
1897
d13a5cbd9250
reading from empty string fixed
Claus Gittinger <cg@exept.de>
parents:
1896
diff
changeset
|
203 |
"Modified: / 08-10-2007 / 16:41:48 / cg" |
1892 | 204 |
! |
205 |
||
206 |
seconds:s |
|
207 |
"return a new TimeDuration representing a duration of s seconds. |
|
208 |
See also Time now / Date today / Timestamp now." |
|
209 |
||
210 |
^ self basicNew setHours:0 minutes:0 seconds:s milliseconds:0 |
|
211 |
||
212 |
" |
|
213 |
TimeDuration seconds:2 |
|
214 |
" |
|
215 |
||
216 |
"Created: / 06-08-2007 / 15:32:21 / cg" |
|
989 | 217 |
! ! |
218 |
||
1902 | 219 |
!TimeDuration class methodsFor:'class initialization'! |
220 |
||
221 |
initialize |
|
222 |
TimeDurationZero isNil ifTrue:[ |
|
223 |
TimeDurationZero := self seconds:0. |
|
224 |
] |
|
225 |
! ! |
|
226 |
||
227 |
!TimeDuration class methodsFor:'constants'! |
|
228 |
||
229 |
zero |
|
230 |
"return the neutral element for addition (0s)" |
|
231 |
||
232 |
^ TimeDurationZero |
|
233 |
||
234 |
"Modified: 18.7.1996 / 12:26:12 / cg" |
|
235 |
! ! |
|
236 |
||
989 | 237 |
!TimeDuration class methodsFor:'format strings'! |
238 |
||
239 |
formatString12us |
|
240 |
"return the format string used to format US times (and other areas)" |
|
241 |
||
1506 | 242 |
^ '%h:%m:%s.%i' |
989 | 243 |
! |
244 |
||
245 |
formatString24 |
|
246 |
"return the format string used to format european times (and other areas)" |
|
247 |
||
248 |
^ '%h:%m:%s.%i' |
|
249 |
! ! |
|
250 |
||
251 |
!TimeDuration methodsFor:'accessing'! |
|
252 |
||
1028 | 253 |
days |
254 |
^ timeEncoding // 1000 // 3600 // 24 |
|
255 |
! |
|
256 |
||
989 | 257 |
milliseconds |
258 |
^ timeEncoding \\ 1000 |
|
259 |
! ! |
|
260 |
||
1896
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
261 |
!TimeDuration methodsFor:'arithmetic'! |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
262 |
|
1900 | 263 |
* aNumber |
264 |
"return a new timeDuration" |
|
265 |
||
266 |
^ self species basicNew |
|
267 |
setMilliseconds:(self getMilliseconds * aNumber) asInteger |
|
268 |
||
269 |
" |
|
270 |
(TimeDuration fromString:'10s') * 5 |
|
271 |
" |
|
272 |
! |
|
273 |
||
1896
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
274 |
+ aTimeDurationOrNumberOfSeconds |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
275 |
"return a new timeDuration" |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
276 |
|
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
277 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[ |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
278 |
^ self species basicNew |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
279 |
setMilliseconds:(self getMilliseconds + (aTimeDurationOrNumberOfSeconds * 1000) asInteger) |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
280 |
]. |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
281 |
^ self species basicNew |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
282 |
setMilliseconds:(self getMilliseconds + aTimeDurationOrNumberOfSeconds getMilliseconds) |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
283 |
|
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
284 |
"Created: / 04-10-2007 / 14:12:40 / cg" |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
285 |
! |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
286 |
|
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
287 |
- aTimeDurationOrNumberOfSeconds |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
288 |
"return a new timeDuration" |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
289 |
|
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
290 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[ |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
291 |
^ self species basicNew |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
292 |
setMilliseconds:(self getMilliseconds - (aTimeDurationOrNumberOfSeconds * 1000) asInteger) |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
293 |
]. |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
294 |
^ self species basicNew |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
295 |
setMilliseconds:(self getMilliseconds - aTimeDurationOrNumberOfSeconds getMilliseconds) |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
296 |
|
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
297 |
"Created: / 04-10-2007 / 14:12:29 / cg" |
1900 | 298 |
! |
299 |
||
300 |
/ aTimeDurationOrNumberOfSeconds |
|
301 |
"return a new timeDuration" |
|
302 |
||
303 |
aTimeDurationOrNumberOfSeconds isNumber ifTrue:[ |
|
304 |
^ self species basicNew |
|
305 |
setMilliseconds:(self getMilliseconds / aTimeDurationOrNumberOfSeconds) asInteger |
|
306 |
]. |
|
307 |
^ (self getMilliseconds / aTimeDurationOrNumberOfSeconds getMilliseconds) |
|
308 |
||
309 |
" |
|
310 |
(TimeDuration fromString:'10s') / (TimeDuration fromString:'5s') |
|
311 |
(TimeDuration fromString:'10s') / 5 |
|
312 |
" |
|
1901 | 313 |
! |
314 |
||
315 |
productFromFraction:aNumber |
|
316 |
"return a new timeDuration" |
|
317 |
||
318 |
^ self productFromNumber:aNumber |
|
319 |
||
320 |
" |
|
321 |
(TimeDuration fromString:'10s') * 5 |
|
322 |
" |
|
323 |
! |
|
324 |
||
325 |
productFromNumber:aNumber |
|
326 |
"return a new timeDuration" |
|
327 |
||
328 |
^ self species basicNew |
|
329 |
setMilliseconds:(self getMilliseconds * aNumber) asInteger |
|
330 |
||
331 |
" |
|
332 |
(TimeDuration fromString:'10s') * 5 |
|
333 |
" |
|
1896
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
334 |
! ! |
fc107aed9bb9
arithmetic on time-durations
Claus Gittinger <cg@exept.de>
parents:
1894
diff
changeset
|
335 |
|
989 | 336 |
!TimeDuration methodsFor:'converting'! |
337 |
||
338 |
asTime |
|
339 |
"return a Time object from the receiver - thats the receiver." |
|
340 |
||
341 |
^ Time hours:(self hours) minutes:(self minutes) seconds:(self seconds) |
|
342 |
! ! |
|
343 |
||
1893 | 344 |
!TimeDuration methodsFor:'double dispatching'! |
345 |
||
346 |
sumFromTimestamp:aTimestamp |
|
347 |
^ aTimestamp addMilliseconds:(self getMilliseconds) |
|
348 |
! ! |
|
349 |
||
1028 | 350 |
!TimeDuration methodsFor:'printing'! |
351 |
||
1419
cde809955208
print bindings (care for language specifics)
Claus Gittinger <cg@exept.de>
parents:
1332
diff
changeset
|
352 |
addPrintBindingsTo:aDictionary language:languageOrNil |
cde809955208
print bindings (care for language specifics)
Claus Gittinger <cg@exept.de>
parents:
1332
diff
changeset
|
353 |
"private print support: add bindings for printing to aDictionary. |
cde809955208
print bindings (care for language specifics)
Claus Gittinger <cg@exept.de>
parents:
1332
diff
changeset
|
354 |
languageOrNil can only be #en or nil for the current language." |
1028 | 355 |
|
356 |
|hoursInDay s| |
|
357 |
||
1419
cde809955208
print bindings (care for language specifics)
Claus Gittinger <cg@exept.de>
parents:
1332
diff
changeset
|
358 |
super addPrintBindingsTo:aDictionary language:languageOrNil. |
1028 | 359 |
aDictionary at:$d put:self days. |
360 |
||
361 |
hoursInDay := self hours \\ 24. |
|
362 |
aDictionary at:#Hd put:(s := hoursInDay printString). |
|
363 |
aDictionary at:#hd put:(s leftPaddedTo:2 with:$0). |
|
364 |
! |
|
365 |
||
1904 | 366 |
formatForPrinting |
367 |
"Return a format which is suitable for a human - not meant to be read back." |
|
1028 | 368 |
|
1905 | 369 |
^ self formatForPrinting:false |
370 |
! |
|
371 |
||
372 |
formatForPrinting:shortFlag |
|
373 |
"Return a format which is suitable for a human - not meant to be read back. |
|
374 |
If shortFlag is true, some millisecond-info is ommitted for longer times." |
|
375 |
||
1904 | 376 |
|fmt hours mins secs overAllSeconds millis| |
1028 | 377 |
|
1506 | 378 |
hours := self hours. |
379 |
mins := self minutes. |
|
380 |
secs := self seconds. |
|
381 |
millis := self milliseconds. |
|
1904 | 382 |
|
1506 | 383 |
hours >= 24 ifTrue:[ |
384 |
fmt := '%dd %(Hd)h %Mm'. |
|
1510 | 385 |
secs = 0 ifTrue:[ |
1506 | 386 |
fmt := '%dd %(Hd)h %Mm'. |
1510 | 387 |
mins = 0 ifTrue:[ |
1506 | 388 |
fmt := '%dd %(Hd)h'. |
1511 | 389 |
(hours \\ 24) = 0 ifTrue:[ |
1509 | 390 |
fmt := '%dd'. |
391 |
]. |
|
1506 | 392 |
]. |
393 |
]. |
|
1028 | 394 |
] ifFalse:[ |
1506 | 395 |
hours > 0 ifTrue:[ |
396 |
fmt := '%Hh %Mm'. |
|
1510 | 397 |
secs = 0 ifTrue:[ |
1506 | 398 |
fmt := '%Hh %Mm'. |
1510 | 399 |
mins = 0 ifTrue:[ |
1506 | 400 |
fmt := '%Hh'. |
401 |
]. |
|
402 |
]. |
|
1133 | 403 |
] ifFalse:[ |
1506 | 404 |
mins > 0 ifTrue:[ |
405 |
fmt := '%Mm'. |
|
1510 | 406 |
secs = 0 ifTrue:[ |
1506 | 407 |
fmt := '%Mm' |
408 |
]. |
|
1133 | 409 |
] ifFalse:[ |
1506 | 410 |
fmt := '' |
1133 | 411 |
]. |
412 |
]. |
|
413 |
]. |
|
1510 | 414 |
((secs ~= 0) or:[millis ~= 0])ifTrue:[ |
1506 | 415 |
fmt size ~~ 0 ifTrue:[ |
416 |
fmt := fmt , ' ' |
|
417 |
]. |
|
1510 | 418 |
(millis = 0) ifTrue:[ |
1506 | 419 |
fmt := fmt , '%Ss' |
420 |
] ifFalse:[ |
|
1891
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
421 |
secs = 0 ifTrue:[ |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
422 |
fmt := fmt , '%Ims' |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
423 |
] ifFalse:[ |
1905 | 424 |
shortFlag ifTrue:[ |
425 |
overAllSeconds := self asSeconds. |
|
426 |
overAllSeconds > 2 ifTrue:[ |
|
427 |
overAllSeconds > 10 ifTrue:[ |
|
428 |
overAllSeconds > 300 ifTrue:[ |
|
429 |
fmt := fmt , '%Ss' |
|
430 |
] ifFalse:[ |
|
431 |
fmt := fmt , '%S.%(milli1)s' |
|
432 |
] |
|
1904 | 433 |
] ifFalse:[ |
1905 | 434 |
fmt := fmt , '%S.%(milli2)s' |
1904 | 435 |
] |
436 |
] ifFalse:[ |
|
1905 | 437 |
fmt := fmt , '%S.%is' |
1904 | 438 |
] |
439 |
] ifFalse:[ |
|
440 |
fmt := fmt , '%S.%is' |
|
441 |
] |
|
1891
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
442 |
] |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
443 |
]. |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
444 |
] ifFalse:[ |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
445 |
fmt isEmpty ifTrue:[ |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
446 |
fmt := '%Ss' |
1506 | 447 |
]. |
1028 | 448 |
]. |
1891
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
449 |
|
1904 | 450 |
^ fmt. |
451 |
||
452 |
" |
|
453 |
TimeDuration hours:0 minutes:0 seconds:0 millis:12 |
|
454 |
||
455 |
TimeDuration hours:0 minutes:0 seconds:0 millis:123 |
|
456 |
TimeDuration hours:0 minutes:0 seconds:10 millis:123 |
|
457 |
TimeDuration hours:0 minutes:33 seconds:0 millis:123 |
|
458 |
TimeDuration hours:2 minutes:0 seconds:0 millis:123 |
|
459 |
TimeDuration hours:2 minutes:33 seconds:0 millis:123 |
|
460 |
TimeDuration hours:100 minutes:33 seconds:0 millis:123 |
|
461 |
TimeDuration hours:10000 minutes:33 seconds:0 millis:123 |
|
462 |
TimeDuration hours:1000000 minutes:33 seconds:0 millis:123 |
|
463 |
||
464 |
TimeDuration hours:2 minutes:33 seconds:0 millis:0 |
|
465 |
TimeDuration hours:2 minutes:0 seconds:0 millis:0 |
|
466 |
TimeDuration hours:24 minutes:0 seconds:0 millis:0 |
|
467 |
||
468 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
469 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringFormat:'%h:%m:%s' |
|
470 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
471 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
472 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
473 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
474 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
475 |
" |
|
476 |
||
477 |
"Modified: / 18-07-2007 / 14:06:17 / cg" |
|
478 |
! |
|
479 |
||
480 |
printOn:aStream |
|
481 |
"append a user printed representation of the receiver to aStream. |
|
482 |
The format is suitable for a human - not meant to be read back." |
|
483 |
||
1028 | 484 |
^ self |
485 |
printOn:aStream |
|
1904 | 486 |
format:(self formatForPrinting). |
1028 | 487 |
|
488 |
" |
|
1891
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
489 |
TimeDuration hours:0 minutes:0 seconds:0 millis:12 |
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
490 |
|
1133 | 491 |
TimeDuration hours:0 minutes:0 seconds:0 millis:123 |
492 |
TimeDuration hours:0 minutes:0 seconds:10 millis:123 |
|
493 |
TimeDuration hours:0 minutes:33 seconds:0 millis:123 |
|
1506 | 494 |
TimeDuration hours:2 minutes:0 seconds:0 millis:123 |
1133 | 495 |
TimeDuration hours:2 minutes:33 seconds:0 millis:123 |
1028 | 496 |
TimeDuration hours:100 minutes:33 seconds:0 millis:123 |
497 |
TimeDuration hours:10000 minutes:33 seconds:0 millis:123 |
|
498 |
TimeDuration hours:1000000 minutes:33 seconds:0 millis:123 |
|
1133 | 499 |
|
1506 | 500 |
TimeDuration hours:2 minutes:33 seconds:0 millis:0 |
501 |
TimeDuration hours:2 minutes:0 seconds:0 millis:0 |
|
502 |
TimeDuration hours:24 minutes:0 seconds:0 millis:0 |
|
503 |
||
1133 | 504 |
(TimeDuration hours:0 minutes:0 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
505 |
(TimeDuration hours:0 minutes:0 seconds:10 millis:123) printStringFormat:'%h:%m:%s' |
|
506 |
(TimeDuration hours:0 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
507 |
(TimeDuration hours:2 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
508 |
(TimeDuration hours:100 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
509 |
(TimeDuration hours:10000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
510 |
(TimeDuration hours:1000000 minutes:33 seconds:0 millis:123) printStringFormat:'%h:%m:%s' |
|
1028 | 511 |
" |
1891
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
512 |
|
af37298a4b38
printing/storing; ms handling
Claus Gittinger <cg@exept.de>
parents:
1890
diff
changeset
|
513 |
"Modified: / 18-07-2007 / 14:06:17 / cg" |
1028 | 514 |
! ! |
515 |
||
989 | 516 |
!TimeDuration methodsFor:'private'! |
517 |
||
518 |
getMilliseconds |
|
1890 | 519 |
"return the number of milliseconds" |
989 | 520 |
|
521 |
^ timeEncoding |
|
1890 | 522 |
|
523 |
"Modified: / 18-07-2007 / 13:44:33 / cg" |
|
989 | 524 |
! |
525 |
||
526 |
getSeconds |
|
1890 | 527 |
"return the number of seconds" |
989 | 528 |
|
529 |
^ timeEncoding // 1000 |
|
1890 | 530 |
|
531 |
"Modified: / 18-07-2007 / 13:44:37 / cg" |
|
989 | 532 |
! |
533 |
||
534 |
setHours:h minutes:m seconds:s millis:millis |
|
535 |
"set my time given individual values" |
|
536 |
||
1545
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
537 |
self obsoleteMethodWarning:'use setHours:minutes:seconds:milliseconds:'. |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
538 |
self setHours:h minutes:m seconds:s milliseconds:millis. |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
539 |
! |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
540 |
|
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
541 |
setHours:h minutes:m seconds:s milliseconds:millis |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
542 |
"set my time given individual values" |
36cf17510eaa
*millis: interface is obsolete
Claus Gittinger <cg@exept.de>
parents:
1511
diff
changeset
|
543 |
|
1028 | 544 |
self setMilliseconds:((h * 60 * 60 ) + (m * 60) + s) * 1000 + millis. |
989 | 545 |
! |
546 |
||
547 |
setMilliseconds:millis |
|
1890 | 548 |
"set my duration given milliseconds. |
549 |
Notice that (in contrast to Time), there is no modulu operation here. |
|
550 |
Duration can be longer than a day" |
|
989 | 551 |
|
552 |
timeEncoding := millis |
|
1890 | 553 |
|
554 |
"Modified: / 18-07-2007 / 13:44:16 / cg" |
|
989 | 555 |
! |
556 |
||
557 |
setSeconds:secs |
|
1123 | 558 |
"set my timeduration given seconds. |
1890 | 559 |
Notice that (in contrast to Time), there is no modulu operation here. |
1123 | 560 |
Duration can be longer than a day" |
989 | 561 |
|
562 |
self setMilliseconds:(secs * 1000). |
|
1890 | 563 |
|
564 |
"Modified: / 18-07-2007 / 13:44:24 / cg" |
|
989 | 565 |
! ! |
566 |
||
1902 | 567 |
!TimeDuration methodsFor:'testing'! |
568 |
||
569 |
isZero |
|
570 |
^ self = self class zero |
|
571 |
! ! |
|
572 |
||
989 | 573 |
!TimeDuration class methodsFor:'documentation'! |
574 |
||
575 |
version |
|
1905 | 576 |
^ '$Header: /cvs/stx/stx/libbasic2/Attic/TimeDuration.st,v 1.28 2007-10-26 14:07:25 cg Exp $' |
989 | 577 |
! ! |
1902 | 578 |
|
579 |
TimeDuration initialize! |