[ENH] sub-second local time
Lex Spoon
lex at cc.gatech.edu
Thu Dec 9 03:04:09 UTC 1999
Some people are talking about scientific applications that might require
very precise time measurements. I have no idea if Squeak will ever be
the tool of choice for this, but it seems easy enough to make the
get-current-time primitive support this: just add a field to report
microseconds as well as seconds back to the image.
Here's an update that does just this: the primitive now fills in a
4-element array instead of a 2-element one, where the first two are
(seconds, microseconds) for the current time, and the second two are
(seconds, microseconds) for the offset from UTC (just to be consistent).
I hacked David Lewis's plugin again to return 0's in the microsecond
fields. The wrapper for the primitive, #localSecondsClockWithOffset,
adds up each pair of numbers and returns a two-element array as before;
thus if you say "Time utcSecondsClock", you might get a fractional
result if your VM reports time that precisely.
If sub-second measurement of time might one day be useful in Squeak,
then this changeset is probably better than the last. If sub-second
time measures sound useless in the face of .01 second garbage collects,
then ignore this :)
Lex
=================================
'From Squeak2.7alpha of 25 October 1999 [latest update: #1671] on 8 December 1999 at 10:01:21 pm'!
"Change Set: Time
Date: 1 December 1999
Author: Lex Spoon
adds basic support for determining both local time from UTC time"!
!Time commentStamp: 'ls 12/6/1999 22:56' prior: 0!
I represent the time of day.
Two primitives, localSecondsClock and utcSecondsClock, return a time encoded as:
days*86400 + hours*3600 + minutes*60 + seconds
For UTC time, this is close to the number of seconds since the beginning of Jan 1, 1901, as of December 1999, although it is off by <60 leap seconds.!
!Time class methodsFor: 'general inquiries' stamp: 'ls 12/1/1999 22:39'!
dateAndTimeNow
"Answer a two-element Array of (Date today, Time now) in local time"
^self localDateAndTimeNow! !
!Time class methodsFor: 'general inquiries' stamp: 'ls 12/8/1999 21:14'!
dateAndTimeNowWithOffset
"Answer a three-element Array of (Date today, Time now, offset from UTC)"
| secondsCount offset secondsWithOffset |
secondsWithOffset _ self localSecondsClockWithOffset.
secondsCount _ secondsWithOffset first.
offset _ secondsWithOffset third.
^(self dateAndTimeFromSeconds: secondsCount) copyWith: offset! !
!Time class methodsFor: 'general inquiries' stamp: 'ls 12/1/1999 22:37'!
localDateAndTimeNow
"Answer a two-element Array of (Date today, Time now) in local time"
| secondCount |
secondCount _ self localSecondsClock.
^self dateAndTimeFromSeconds: secondCount! !
!Time class methodsFor: 'general inquiries' stamp: 'ls 12/6/1999 22:54'!
localSecondsClock
"return the local time, encoded into a single integer (see class comment)"
^self localSecondsClockWithOffset at: 1! !
!Time class methodsFor: 'general inquiries' stamp: 'ls 12/8/1999 21:45'!
localSecondsClockWithOffset
"return the local time along with its offset from UTC. See class comment for the encoding"
| details |
details _ Array new: 4.
self primLocalSecondsClockWithOffset: details.
^Array
with: (details first + (details second / 1000000000))
with: (details third + (details fourth / 1000000000))! !
!Time class methodsFor: 'general inquiries' stamp: 'ls 12/1/1999 22:39'!
utcDateAndTimeNow
"Answer a two-element Array of (Date today, Time now) in UTC time"
| secondCount |
secondCount _ self utcSecondsClock.
^self dateAndTimeFromSeconds: secondCount! !
!Time class methodsFor: 'general inquiries' stamp: 'ls 12/6/1999 23:31'!
utcSecondsClock
"return the date and time in UTC, encoded as an integer (see class comment)"
| clockWithOffset |
clockWithOffset _ self localSecondsClockWithOffset.
^clockWithOffset first - clockWithOffset second! !
!Time class methodsFor: 'private' stamp: 'ls 12/8/1999 21:47'!
primLocalSecondsClockWithOffset: array
"calculate the local time, and its ofset from UTC"
<primitive: 'primitiveLocalSecondsClockWithOffset' module:'time'>
"if failed, assume UTC time (modify this if you want it to assume some
other time zone by default"
array at: 1 put: self primSecondsClock. "local seconds"
array at: 2 put: 0. "local microseconds"
array at: 3 put: 0. "offset from UTC in seconds"
array at: 4 put: 0. "additional offset from UTC in microseconds"! !
More information about the Squeak-dev
mailing list
|