Time.st
author claus
Tue, 15 Aug 1995 20:55:40 +0200
changeset 391 66bb97f4e41f
parent 384 cc3d110ea879
child 394 c17e818d7a59
permissions -rw-r--r--
.

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

'From Smalltalk/X, Version:2.10.4 on 8-feb-1995 at 12:46:41 pm'!

AbstractTime subclass:#Time
	 instanceVariableNames:'timeEncoding'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'Magnitude-General'
!

Time comment:'
COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

$Header: /cvs/stx/stx/libbasic/Time.st,v 1.18 1995-08-15 18:55:28 claus Exp $
'!

!Time class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libbasic/Time.st,v 1.18 1995-08-15 18:55:28 claus Exp $
"
!

documentation
"
    Instances of time represent a particular time-of-day.
    Since they only store hours, minutes and seconds of a day,
    they cannot be used to compare times across midnight (i.e. should
    not be used as timeStamps).
    Use instances of AbsoluteTime (and read the comment there) to do this.

    Examples:
	|t|

	t := Time now.
	Transcript showCr:t.


	|t1 t2|

	t1 := Time now.
	(Delay forSeconds:10) wait.
	t2 := Time now.
	t2 - t1   
"
! !

!Time class methodsFor:'instance creation'!

hour:h minutes:m seconds:s
    "return an instance of Time representing the given values"

    ^ self basicNew setHour:h minutes:m seconds:s

    "
     Time hour:2 minutes:33 seconds:0 
     Time hour:0 minutes:0 seconds:0 
     Time hour:24 minutes:0 seconds:0 
     Time hour:23 minutes:59 seconds:59 
    "
!

readFrom:aStream onError:exceptionBlock
    "return a new Time, reading a printed representation from aStream.
     If no pm follows the time, the string is interpreted as either 24 hour format
     or being am.
     Notice, that this is not the storeString format and 
     is different from the format expected by readFrom:."

    |hour min sec ex|

    ex := [^ exceptionBlock value].
    hour := Integer readFrom:aStream onError:ex.
    (hour between:0 and:24) ifFalse:[ex value].

    [aStream peek isDigit] whileFalse:[aStream next].
    min := Integer readFrom:aStream onError:ex.
    (min between:0 and:59) ifFalse:[ex value].

    [aStream peek isDigit] whileFalse:[aStream next].
    sec := Integer readFrom:aStream onError:ex.
    (sec between:0 and:59) ifFalse:[ex value].

    [aStream peek == Character space] whileTrue:[aStream next].
    (aStream peek == $p) ifTrue:[
	"pm"
	hour := hour + 12
    ].
    ^ self basicNew setHour:hour minutes:min seconds:sec

    "
     Time readFromString:'18:22:00'    
     Time readFromString:'14:00:11'    
     Time readFromString:'7:00:11'     
     Time readFromString:'6:22:00 pm'   
     Time readFromString:'2:00:11 pm'  
     Time readFromString:'7:00:11 am'  
    "
! !

!Time methodsFor:'accessing'!

minutes
    "return the number of minutes within the hour (i.e. 0..59)"

    ^ (timeEncoding \\ 3600) // 60

    "
     Time now minutes
    "
!

seconds
    "return the number of seconds within the minute (i.e. 0..59)"

    ^ (timeEncoding \\ 3600) \\ 60

    "
     Time now seconds
    "
!

hours
    "return the number of hours since midnight (i.e. 0..23)"

    ^ timeEncoding // 3600

    "
     Time now hours
    "
!

day
    "catch day access - Time does not know about it"

    ^ self shouldNotImplement
!

month 
    "catch month access - Time does not know about it"

    ^ self shouldNotImplement
!

year
    "catch year access - Time does not know about it"

    ^ self shouldNotImplement
! !

!Time methodsFor:'printing & storing'!

printOn:aStream
    "append a printed representation of the receiver to aStream.
     Format is hh:mm:ss either in 12-hour or 24-hour format.
     depending on the setting of LanguageTerritory.
     I dont know what ST-80 does here (12-hour format ?)"

    LanguageTerritory = 'usa' ifTrue:[
	self print12HourFormatOn:aStream
    ] ifFalse:[
	self print24HourFormatOn:aStream
    ]

    "
     Time now printOn:Transcript. Transcript cr
    "
!

print24HourFormatOn:aStream
    "append a printed representation of the receiver to aStream.
     Format is hh:mm:ss in 24-hour format."

    |m s|

    self hours printOn:aStream.
    aStream nextPut:$:.
    m := self minutes.
    (m < 10) ifTrue:[aStream nextPut:$0].
    m printOn:aStream.
    aStream nextPut:$:.
    s := self seconds.
    (s < 10) ifTrue:[aStream nextPut:$0].
    s printOn:aStream

    "
     Time now print24HourFormatOn:Transcript. Transcript cr
    "
!

print12HourFormatOn:aStream
    "append a printed representation of the receiver to aStream.
     Format is hh:mm:ss am/pm (i.e. 12-hour american format)."

    |h m s|

    h := self hours.
    h > 12 ifTrue:[
	h := h - 12.
    ] ifFalse:[
	h < 1 ifTrue:[
	    h := 12
	]
    ].
    h printOn:aStream.
    aStream nextPut:$:.
    m := self minutes.
    (m < 10) ifTrue:[aStream nextPut:$0].
    m printOn:aStream.
    aStream nextPut:$:.
    s := self seconds.
    (s < 10) ifTrue:[aStream nextPut:$0].
	s printOn:aStream.
    h >= 12 ifTrue:[
	aStream nextPutAll:' pm'
    ] ifFalse:[
	aStream nextPutAll:' am'
    ]

    "
     Time now print12HourFormatOn:Transcript. Transcript cr
    "
! !

!Time methodsFor:'converting'!

asSeconds
    "return the number of seconds elapsed since midnight"

    ^ timeEncoding

    "
     Time now asSeconds
    "
!

asTime
    "return a Time object from the receiver - thats the receiver."

    ^ self
!

asAbsoluteTime
    "return an AbsoluteTime object from the receiver.
     The date components are taken from today."

    |today|

    today := Date today.
    ^ AbsoluteTime day:today day month:today month year:today year
		   hour:self hours minutes:self minutes seconds:self seconds
    "
     Time now asAbsoluteTime
    "
! !

!Time methodsFor:'comparing'!

> aTime
    "return true if the argument, aTime is after the receiver"

    ^ timeEncoding > aTime timeEncoding
!

< aTime
    "return true if the argument, aTime is before the receiver"

    ^ timeEncoding < aTime timeEncoding
!

= aTime
    "return true if the argument, aTime represents the same timeOfDay"

    aTime class == self class ifTrue:[
	^ timeEncoding == aTime timeEncoding
    ].
    (aTime species == self species) ifFalse:[^ false].
    ^ self asSeconds == aTime asSeconds
!

hash
    "return an integer useful for hashing on times"

    ^ timeEncoding
! !

!Time methodsFor:'private'!

fromOSTimeLow:lowOSTime and:hiOSTime
    "set my time, given operatingSystems time parts"

    |h m s|
    OperatingSystem computeTimePartsOf:lowOSTime and:hiOSTime for:[
	:hours :minutes :secs |

	h := hours.
	m := minutes.
	s := secs.
    ].
    self setHour:h minutes:m seconds:s

!

getSeconds
    ^ timeEncoding
!

setSeconds:secs
    "set my time given seconds since midnight"

    timeEncoding := secs
!

setHour:h minutes:m seconds:s
    "set my time given individual values"

    timeEncoding := ((h\\24) * 60 * 60 ) + (m * 60) + s.
!

timeEncoding:encoding
    "the internal encoding is stricktly private, 
     and should not be used outside."

    timeEncoding := encoding
!

timeEncoding
    "the internal encoding is stricktly private, 
     and should not be used outside."

    ^ timeEncoding
! !