File: Time.st

package info (click to toggle)
gnu-smalltalk 3.1-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 33,300 kB
  • ctags: 13,999
  • sloc: ansic: 88,106; sh: 23,223; asm: 7,889; perl: 4,493; cpp: 3,539; awk: 1,483; yacc: 1,355; xml: 1,272; makefile: 1,192; lex: 843; sed: 258; objc: 124
file content (412 lines) | stat: -rw-r--r-- 10,485 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"======================================================================
|
|   Time Method Definitions
|
|
 ======================================================================"

"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2008
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
| 
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
| General Public License for more details.
| 
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.  
|
 ======================================================================"



Magnitude subclass: Time [
    | seconds |
    
    <category: 'Language-Data types'>
    <comment: 'My instances represent times of the day.  I provide methods for instance 
creation, methods that access components (hours, minutes, and seconds) of a 
time value, and a block execution timing facility.'>

    SecondClockAdjustment := nil.
    ClockOnStartup := nil.

    Time class >> utcSecondClock [
	"Answer the number of seconds since the midnight of 1/1/1901 (unlike
	 #secondClock, the reference time is here expressed as UTC, that is
	 as Coordinated Universal Time)."

	<category: 'basic (UTC)'>
	^self secondClock - self timezoneBias
    ]

    Time class >> utcNow [
	"Answer a time representing the current time of day in Coordinated
	 Universal Time (UTC)"

	<category: 'basic (UTC)'>
	^self new setSeconds: self utcSecondClock
    ]

    Time class >> midnight [
	"Answer a time representing midnight in Coordinated
	 Universal Time (UTC)"

	<category: 'basic (UTC)'>
	^self new setSeconds: 0
    ]

    Time class >> timezoneBias [
	"Specifies the current bias, in minutes, for local time translation for
	 the current time. The bias is the difference, in seconds, between
	 Coordinated Universal Time (UTC) and local time; a positive bias
	 indicates that the local timezone is to the east of Greenwich (e.g.
	 Europe, Asia), while a negative bias indicates that it is to the
	 west (e.g. America)"

	<category: 'builtins'>
	<primitive: VMpr_Time_timezoneBias>
	^self primitiveFailed
    ]

    Time class >> timezone [
	"Answer a String associated with the current timezone (either standard
	 or daylight-saving) on this operating system. For example, the answer
	 could be `EST' to indicate Eastern Standard Time; the answer can be
	 empty and can't be assumed to be a three-character code such as `EST'."

	<category: 'builtins'>
	<primitive: VMpr_Time_timezone>
	^self primitiveFailed
    ]

    Time class >> secondClock [
	"Answer the number of seconds since the midnight of 1/1/1901"

	<category: 'clocks'>
	^self primSecondClock + SecondClockAdjustment
    ]

    Time class >> primSecondClock [
	"Returns the number of seconds to/from 1/1/2000."

	<category: 'builtins'>
	<primitive: VMpr_Time_secondClock>
	^self primitiveFailed
    ]

    Time class >> millisecondClock [
	"Answer the number of milliseconds since startup."

	<category: 'clocks'>
	| second milli |
	
	[second := self primSecondClock.
	milli := self primMillisecondClock \\ 1000.
	second = self primSecondClock and: [milli > 10]] 
		whileFalse.
	^(second - ClockOnStartup) * 1000 + milli
    ]

    Time class >> primMillisecondClock [
	"Returns the number of milliseconds since midnight."

	<category: 'builtins'>
	<primitive: VMpr_Time_millisecondClock>
	^self primitiveFailed
    ]

    Time class >> initialize [
	"Initialize the Time class after the image has been bootstrapped"

	"(99 * 365 + 25) * 86400 secs/day."

	<category: 'initialization'>
	SecondClockAdjustment := 86400 * 36159.
	ObjectMemory addDependent: self
    ]

    Time class >> update: aspect [
	"Private - Initialize the receiver's instance variables"

	<category: 'initialization'>
	aspect == #returnFromSnapshot 
	    ifTrue: [ClockOnStartup := Time primSecondClock]
    ]

    Time class >> now [
	"Answer a time representing the current time of day"

	"\\ rounds towards -infinity, so it is good for negative numbers too"

	<category: 'instance creation'>
	^self new setSeconds: self primSecondClock
    ]

    Time class >> new [
	"Answer a Time representing midnight"

	<category: 'instance creation'>
	^self basicNew setSeconds: 0
    ]

    Time class >> fromSeconds: secondCount [
	"Answer a Time representing secondCount seconds past midnight"

	<category: 'instance creation'>
	^self new setSeconds: secondCount
    ]

    Time class >> hour: h [
	"Answer a Time that is the given number of hours past midnight"

	<category: 'instance creation'>
	^self fromSeconds: h * 3600
    ]

    Time class >> hour: h minute: m second: s [
	"Answer a Time that is the given number of hours, minutes and
	 seconds past midnight"

	<category: 'instance creation'>
	^self fromSeconds: (h * 60 + m) * 60 + s
    ]

    Time class >> minute: m [
	"Answer a Time that is the given number of minutes past midnight"

	<category: 'instance creation'>
	^self fromSeconds: m * 60
    ]

    Time class >> second: s [
	"Answer a Time that is the given number of seconds past midnight"

	<category: 'instance creation'>
	^self fromSeconds: s
    ]

    Time class >> hours: h [
	"Answer a Time that is the given number of hours past midnight"

	<category: 'instance creation'>
	^self fromSeconds: h * 3600
    ]

    Time class >> hours: h minutes: m seconds: s [
	"Answer a Time that is the given number of hours, minutes and
	 seconds past midnight"

	<category: 'instance creation'>
	^self fromSeconds: (h * 60 + m) * 60 + s
    ]

    Time class >> minutes: m [
	"Answer a Time that is the given number of minutes past midnight"

	<category: 'instance creation'>
	^self fromSeconds: m * 60
    ]

    Time class >> seconds: s [
	"Answer a Time that is the given number of seconds past midnight"

	<category: 'instance creation'>
	^self fromSeconds: s
    ]

    Time class >> readFrom: aStream [
	"Parse an instance of the receiver (hours/minutes/seconds) from
	 aStream"

	<category: 'instance creation'>
	| t1 t2 t3 ch ws |
	t1 := t2 := t3 := 0.
	ch := $:.
	3 timesRepeat: 
		[t1 := t2.
		t2 := t3.
		t3 := 0.
		ch isSeparator 
		    ifFalse: 
			[ws := WriteStream on: (String new: 10).
			ch := $:.
			[aStream atEnd not and: [(ch := aStream next) isDigit not]] whileTrue.
			ch isDigit 
			    ifTrue: 
				[
				[ws nextPut: ch.
				aStream atEnd not and: [(ch := aStream next) isDigit]] 
					whileTrue].
			t3 := ws contents asNumber]].
	^self fromSeconds: t1 * 3600 + (t2 * 60) + t3
    ]

    Time class >> millisecondClockValue [
	"Answer the number of milliseconds since startup"

	<category: 'clocks'>
	| second milli |
	
	[second := self primSecondClock.
	milli := self primMillisecondClock \\ 1000.
	second = self primSecondClock and: [milli > 10]] 
		whileFalse.
	^(second - ClockOnStartup) * 1000 + milli
    ]

    Time class >> millisecondsPerDay [
	"Answer the number of milliseconds in a day"

	<category: 'clocks'>
	^86400000
    ]

    Time class >> millisecondsToRun: timedBlock [
	"Answer the number of milliseconds which timedBlock took to run"

	<category: 'clocks'>
	| startTime |
	startTime := self millisecondClock.
	timedBlock value.
	^self millisecondClock - startTime
    ]

    asSeconds [
	<category: 'accessing (non ANSI & for Durations)'>
	^seconds
    ]

    hours [
	"Answer the number of hours in the receiver"

	<category: 'accessing (non ANSI & for Durations)'>
	^(seconds quo: 3600) rem: 24
    ]

    minutes [
	"Answer the number of minutes in the receiver"

	<category: 'accessing (non ANSI & for Durations)'>
	^(seconds quo: 60) rem: 60
    ]

    seconds [
	"Answer the number of seconds in the receiver"

	<category: 'accessing (non ANSI & for Durations)'>
	^seconds rem: 60
    ]

    hour [
	"Answer the number of hours in the receiver"

	<category: 'accessing (ANSI for DateAndTimes)'>
	^seconds // 3600 \\ 24
    ]

    hour12 [
	"Answer the hour in a 12-hour clock"

	<category: 'accessing (ANSI for DateAndTimes)'>
	| h |
	h := self hour \\ 12.
	^h = 0 ifTrue: [12] ifFalse: [h]
    ]

    hour24 [
	"Answer the hour in a 24-hour clock"

	<category: 'accessing (ANSI for DateAndTimes)'>
	^self hour
    ]

    minute [
	"Answer the number of minutes in the receiver"

	<category: 'accessing (ANSI for DateAndTimes)'>
	^seconds // 60 \\ 60
    ]

    second [
	"Answer the number of seconds in the receiver"

	<category: 'accessing (ANSI for DateAndTimes)'>
	^seconds \\ 60
    ]

    = aTime [
	"Answer whether the receiver is equal to aTime"

	<category: 'comparing'>
	^self class == aTime class and: [seconds = aTime asSeconds]
    ]

    < aTime [
	"Answer whether the receiver is less than aTime"

	<category: 'comparing'>
	^seconds < aTime asSeconds
    ]

    hash [
	"Answer an hash value for the receiver"

	<category: 'comparing'>
	^seconds
    ]

    addSeconds: timeAmount [
	"Answer a new Time that is timeAmount seconds after the receiver"

	<category: 'arithmetic'>
	^Time new setSeconds: seconds + timeAmount
    ]

    addTime: timeAmount [
	"Answer a new Time that is timeAmount seconds after the receiver;
	 timeAmount is a Time."

	<category: 'arithmetic'>
	^Time new setSeconds: seconds + timeAmount asSeconds
    ]

    subtractTime: timeAmount [
	"Answer a new Time that is timeAmount seconds before the receiver;
	 timeAmount is a Time."

	<category: 'arithmetic'>
	^Time new setSeconds: seconds - timeAmount asSeconds
    ]

    printOn: aStream [
	"Print a representation of the receiver on aStream"

	<category: 'arithmetic'>
	self hours printOn: aStream.
	aStream nextPut: $:.
	self minutes < 10 ifTrue: [aStream nextPut: $0].
	self minutes printOn: aStream.
	aStream nextPut: $:.
	self seconds < 10 ifTrue: [aStream nextPut: $0].
	self seconds printOn: aStream
    ]

    setSeconds: secs [
	<category: 'private'>
	seconds := secs \\ 86400
    ]
]