"======================================================================
|
|   Time Method Definitions
|
|   $Revision: 1.7.5$
|   $Date: 2000/05/28 16:56:52$
|   $Author: pb$
|
 ======================================================================"


"======================================================================
|
| Copyright 1988-92, 1994-95, 1999, 2000 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.LESSER.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02111-1307, USA.  
|
 ======================================================================"


Magnitude subclass: #Time
	  instanceVariableNames: 'seconds'
	  classVariableNames: 'SecondClockAdjustment ClockOnStartup'
	  poolDictionaries: ''
	  category: 'Language-Data types'
!

Time 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.' !


!Time class methodsFor: 'basic (UTC)'!

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)."
    ^self secondClock - self timezoneBias
!

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

    "\\ rounds towards -infinity, so it is good for negative numbers too"
    ^self new setSeconds: self utcSecondClock \\ 86400
! !


!Time class methodsFor: 'initialization'!

initialize
     "(99 * 365 + 25) * 86400 secs/day."
     SecondClockAdjustment := 86400 * 36159.
!

onStartup
     "Private - Called on startup"
     ClockOnStartup := Time primSecondClock.
! !

!Time class methodsFor: 'basic'!

now
    "Answer a time representing the current time of day"

    "\\ rounds towards -infinity, so it is good for negative numbers too"
    ^self new setSeconds: self primSecondClock \\ 86400
!

new
    "Answer a Time representing midnight"
    ^self basicNew setSeconds: 0
!

fromSeconds: secondCount
    "Answer a Time representing secondCount seconds past midnight"
    ^self new setSeconds: secondCount \\ 86400
!

millisecondClockValue
    "Answer the number of milliseconds since startup"

    | seconds milli |
    milli := self primMillisecondClock \\ 1000.
    seconds := (self primSecondClock - ClockOnStartup) * 1000.
    ^milli + seconds
!

millisecondClock
    "Answer the number of milliseconds since startup"

    | seconds milli |
    milli := self primMillisecondClock \\ 1000.
    seconds := (self primSecondClock - ClockOnStartup) * 1000.
    ^milli + seconds
!

secondClock
    "Answer the number of seconds since the midnight of 1/1/1901"
    ^self primSecondClock + SecondClockAdjustment
!

millisecondsPerDay
    "Answer the number of milliseconds in a day"
    ^86400000
!

millisecondsToRun: timedBlock
    "Answer the number of milliseconds which timedBlock took to run"
    | startTime|
    startTime := self millisecondClock.
    timedBlock value.
    ^self millisecondClock - startTime
! !


!Time methodsFor: 'accessing'!

asSeconds
    ^seconds
!

hours
    "Answer the number of hours in the receiver"
    ^(seconds // 3600) \\ 24
!

minutes
    "Answer the number of minutes in the receiver"
    ^(seconds // 60) \\ 60
!

seconds
    "Answer the number of seconds in the receiver"
    ^seconds \\ 60
! !



!Time methodsFor: 'comparing'!

= aTime
    "Answer whether the receiver is equal to aTime"
    ^self class == aTime class and: [ seconds = aTime asSeconds ]
!

< aTime
    "Answer whether the receiver is less than aTime"
    ^seconds < aTime asSeconds
!

hash
    "Answer an hash value for the receiver"
    ^seconds
! !





!Time methodsFor: 'arithmetic'!

addTime: timeAmount
    "Answer a new Time that is timeAmount seconds after the receiver"
    ^Time new setSeconds: seconds + timeAmount asSeconds
!

subtractTime: timeAmount
    "Answer a new Time that is timeAmount seconds before the receiver"
    ^Time new setSeconds: seconds - timeAmount asSeconds
!

printOn: aStream
    "Print a representation of the receiver on aStream"
    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.
! !



!Time methodsFor: 'private'!

setSeconds: secs
    seconds := secs
! !
