Time.st
changeset 3477 ac8613ab02c9
parent 2706 878723bc99fc
child 3478 3f1a83b35865
equal deleted inserted replaced
3476:f2ebb76a952d 3477:ac8613ab02c9
    94         |str hour min sec ex|
    94         |str hour min sec ex|
    95 
    95 
    96         str := aStringOrStream readStream.
    96         str := aStringOrStream readStream.
    97 
    97 
    98         ex := [^ exceptionBlock value].
    98         ex := [^ exceptionBlock value].
       
    99 
    99         hour := Integer readFrom:str onError:ex.
   100         hour := Integer readFrom:str onError:ex.
   100         (hour between:0 and:24) ifFalse:[ex value].
   101         (hour between:0 and:24) ifFalse:[ex value].
   101 
   102 
   102         [str peek isDigit] whileFalse:[str next].
   103         [str peek isDigit] whileFalse:[str next].
   103         min := Integer readFrom:str onError:ex.
   104         min := Integer readFrom:str onError:ex.
   104         (min between:0 and:59) ifFalse:[ex value].
   105         (min between:0 and:59) ifFalse:[ex value].
   105 
   106 
   106         [str peek isDigit] whileFalse:[str next].
   107         str atEnd ifFalse:[
   107         sec := Integer readFrom:str onError:ex.
   108             [str peek isDigit] whileFalse:[str next].
   108         (sec between:0 and:59) ifFalse:[ex value].
   109             sec := Integer readFrom:str onError:ex.
   109 
   110             (sec between:0 and:59) ifFalse:[ex value].
   110         [str peek == Character space] whileTrue:[str next].
   111 
   111         (str peek == $p) ifTrue:[
   112             [str peek == Character space] whileTrue:[str next].
   112             "pm"
   113             (str peek == $p) ifTrue:[
   113             hour := hour + 12
   114                 "pm"
       
   115                 hour := hour + 12
       
   116             ].
   114         ].
   117         ].
   115         ^ self basicNew setHour:hour minutes:min seconds:sec
   118         ^ self basicNew setHour:hour minutes:min seconds:sec
   116     ]
   119     ]
   117 
   120 
   118     "
   121     "
   245     "append a printed representation of the receiver to aStream.
   248     "append a printed representation of the receiver to aStream.
   246      Format is hh:mm:ss am/pm (i.e. 12-hour american format)."
   249      Format is hh:mm:ss am/pm (i.e. 12-hour american format)."
   247 
   250 
   248     |h m s ampm|
   251     |h m s ampm|
   249 
   252 
       
   253     ampm := ' am'.
   250     h := self hours.
   254     h := self hours.
       
   255 
       
   256     "/ 0 -> 12 am
       
   257     "/ 12 -> 12 pm
       
   258 
   251     h > 12 ifTrue:[
   259     h > 12 ifTrue:[
   252 	h := h - 12.
   260         h := h - 12.
       
   261         ampm := ' pm'.
   253     ] ifFalse:[
   262     ] ifFalse:[
   254 	h < 1 ifTrue:[
   263         h == 0 ifTrue:[
   255 	    h := 12
   264             h := 12
   256 	]
   265         ] ifFalse:[
       
   266             h == 12 ifTrue:[
       
   267                 ampm := ' pm'
       
   268             ]
       
   269         ]
   257     ].
   270     ].
   258     h printOn:aStream.
   271     h printOn:aStream.
   259     aStream nextPut:$:.
   272     aStream nextPut:$:.
   260     m := self minutes.
   273     m := self minutes.
   261     (m < 10) ifTrue:[aStream nextPut:$0].
   274     (m < 10) ifTrue:[aStream nextPut:$0].
   262     m printOn:aStream.
   275     m printOn:aStream.
   263     aStream nextPut:$:.
   276     aStream nextPut:$:.
   264     s := self seconds.
   277     s := self seconds.
   265     (s < 10) ifTrue:[aStream nextPut:$0].
   278     (s < 10) ifTrue:[aStream nextPut:$0].
   266     s printOn:aStream.
   279     s printOn:aStream.
   267     h >= 12 ifTrue:[
       
   268 	ampm := ' am'
       
   269     ] ifFalse:[
       
   270 	ampm := ' pm'
       
   271     ].
       
   272     aStream nextPutAll:ampm
   280     aStream nextPutAll:ampm
   273 
   281 
   274     "
   282     "
   275      Time now print12HourFormatOn:Transcript. Transcript cr
   283      Time now print12HourFormatOn:Transcript. Transcript cr
       
   284      (Time now subtractHours:12) print12HourFormatOn:Transcript. Transcript cr
       
   285      (Time hour:24 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr
       
   286      (Time hour:12 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr
       
   287      (Time hour:0 minutes:0 seconds:0) print12HourFormatOn:Transcript. Transcript cr
   276     "
   288     "
   277 !
   289 !
   278 
   290 
   279 print24HourFormatOn:aStream
   291 print24HourFormatOn:aStream
   280     "append a printed representation of the receiver to aStream.
   292     "append a printed representation of the receiver to aStream.
   343     self setHour:h minutes:m seconds:s
   355     self setHour:h minutes:m seconds:s
   344 
   356 
   345     "Modified: 1.7.1996 / 15:21:06 / cg"
   357     "Modified: 1.7.1996 / 15:21:06 / cg"
   346 !
   358 !
   347 
   359 
       
   360 getMilliseconds
       
   361     ^ self getSeconds * 1000
       
   362 !
       
   363 
   348 getSeconds
   364 getSeconds
   349     ^ timeEncoding
   365     ^ timeEncoding
   350 !
   366 !
   351 
   367 
   352 setHour:h minutes:m seconds:s
   368 setHour:h minutes:m seconds:s
   353     "set my time given individual values"
   369     "set my time given individual values"
   354 
   370 
   355     timeEncoding := ((h\\24) * 60 * 60 ) + (m * 60) + s.
   371     self setSeconds:(((h\\24) * 60 * 60 ) + (m * 60) + s).
       
   372 !
       
   373 
       
   374 setMilliseconds:millis
       
   375     "set my time given milliseconds since midnight"
       
   376 
       
   377     self setSeconds:(millis // 1000)
   356 !
   378 !
   357 
   379 
   358 setSeconds:secs
   380 setSeconds:secs
   359     "set my time given seconds since midnight"
   381     "set my time given seconds since midnight"
   360 
   382 
   361     timeEncoding := secs
   383     secs < 0 ifTrue:[
       
   384         timeEncoding := (24 * 3600) - (secs negated \\ (24 * 3600))
       
   385     ] ifFalse:[
       
   386         timeEncoding := secs
       
   387     ]
   362 !
   388 !
   363 
   389 
   364 timeEncoding
   390 timeEncoding
   365     "the internal encoding is stricktly private, 
   391     "the internal encoding is stricktly private, 
   366      and should not be used outside."
   392      and should not be used outside."
   376 ! !
   402 ! !
   377 
   403 
   378 !Time class methodsFor:'documentation'!
   404 !Time class methodsFor:'documentation'!
   379 
   405 
   380 version
   406 version
   381     ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.31 1997-06-20 15:29:38 cg Exp $'
   407     ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.32 1998-05-22 10:55:12 cg Exp $'
   382 ! !
   408 ! !