327 "return a new Time, reading a printed representation from aStream. |
327 "return a new Time, reading a printed representation from aStream. |
328 If no am/pm follows the time, the string is interpreted as |
328 If no am/pm follows the time, the string is interpreted as |
329 either 24 hour format or being am." |
329 either 24 hour format or being am." |
330 |
330 |
331 ^ [ |
331 ^ [ |
332 |str hour min sec peekC millis| |
332 |str hour min sec peekC millis| |
333 |
333 |
334 str := aStringOrStream readStream. |
334 str := aStringOrStream readStream. |
335 |
335 |
336 hour := Integer readFrom:str. |
336 hour := Integer readFrom:str. |
337 (hour between:0 and:24) ifFalse:[^ exceptionBlock value]. |
337 (hour between:0 and:24) ifFalse:[^ exceptionBlock value]. |
338 |
338 |
339 min := 0. |
339 min := 0. |
340 sec := 0. |
340 sec := 0. |
341 millis := 0. |
341 millis := 0. |
342 |
342 |
343 str atEnd ifFalse:[ |
343 str atEnd ifFalse:[ |
344 peekC := str peek. |
344 peekC := str peek. |
345 (peekC == $:) ifTrue:[ |
345 (peekC == $:) ifTrue:[ |
346 str next. |
346 str next. |
347 min := Integer readFrom:str. |
347 min := Integer readFrom:str. |
348 (min between:0 and:59) ifFalse:[^ exceptionBlock value]. |
348 (min between:0 and:59) ifFalse:[^ exceptionBlock value]. |
349 |
349 |
350 (str peek == $:) ifTrue:[ |
350 (str peek == $:) ifTrue:[ |
351 str next. |
351 str next. |
352 sec := Integer readFrom:str. |
352 sec := Integer readFrom:str. |
353 (sec between:0 and:59) ifFalse:[^ exceptionBlock value]. |
353 (sec between:0 and:59) ifFalse:[^ exceptionBlock value]. |
354 (str peek == $.) ifTrue:[ |
354 (str peek == $.) ifTrue:[ |
355 str next. |
355 str next. |
356 millis := ((Fraction readDecimalFractionFrom:str onError:[^ exceptionBlock value]) * 1000) asInteger. |
356 millis := ((Fraction readDecimalFractionFrom:str onError:[^ exceptionBlock value]) * 1000) asInteger. |
357 ]. |
357 ]. |
358 ]. |
358 ]. |
359 peekC := str peek. |
359 peekC := str peek. |
360 ]. |
360 ]. |
361 [peekC == Character space] whileTrue:[str next. peekC := str peek]. |
361 [peekC == Character space] whileTrue:[str next. peekC := str peek]. |
362 (peekC == $p or:[peekC == $P]) ifTrue:[ |
362 (peekC == $p or:[peekC == $P]) ifTrue:[ |
363 str next. |
363 str next. |
364 (str peek == $m or:[str peek == $M]) ifTrue:[ |
364 (str peek == $m or:[str peek == $M]) ifTrue:[ |
365 str next |
365 str next |
366 ]. |
366 ]. |
367 (hour <= 0 or:[hour > 12]) ifTrue:[^ exceptionBlock value]. |
367 (hour <= 0 or:[hour > 12]) ifTrue:[^ exceptionBlock value]. |
368 |
368 |
369 "pm" |
369 "pm" |
370 hour ~~ 12 ifTrue:[ |
370 hour ~~ 12 ifTrue:[ |
371 hour := hour + 12 |
371 hour := hour + 12 |
372 ]. |
372 ]. |
373 peekC := str peek |
373 peekC := str peek |
374 ] ifFalse:[ |
374 ] ifFalse:[ |
375 (peekC == $a or:[peekC == $A]) ifTrue:[ |
375 (peekC == $a or:[peekC == $A]) ifTrue:[ |
376 str next. |
376 str next. |
377 (str peek == $m or:[str peek == $M]) ifTrue:[ |
377 (str peek == $m or:[str peek == $M]) ifTrue:[ |
378 str next |
378 str next |
379 ]. |
379 ]. |
380 hour == 12 ifTrue:[ |
380 hour == 12 ifTrue:[ |
381 hour := 0. |
381 hour := 0. |
382 ]. |
382 ]. |
383 hour > 12 ifTrue:[^ exceptionBlock value]. |
383 hour > 12 ifTrue:[^ exceptionBlock value]. |
384 peekC := str peek |
384 peekC := str peek |
385 ] ifFalse:[ |
385 ] ifFalse:[ |
386 "/ cg: dont be too picky here - we do not care, what comes after the |
386 "/ cg: don't be too picky here - we do not care, what comes after the |
387 "/ time string. (Needed to be able to read rfc822 strings where a timezone |
387 "/ time string. (Needed to be able to read rfc822 strings where a timezone |
388 "/ follows (-/+0700 GMT, for example) |
388 "/ follows (-/+0700 GMT, for example) |
389 "/ peekC notNil ifTrue:[ |
389 "/ peekC notNil ifTrue:[ |
390 "/ peekC isSeparator ifFalse:[^ exceptionBlock value]. |
390 "/ peekC isSeparator ifFalse:[^ exceptionBlock value]. |
391 "/ ] |
391 "/ ] |
392 ]. |
392 ]. |
393 ] |
393 ] |
394 ]. |
394 ]. |
395 self basicNew setHours:hour minutes:min seconds:sec milliseconds:millis |
395 self basicNew setHours:hour minutes:min seconds:sec milliseconds:millis |
396 ] on:Error do:exceptionBlock. |
396 ] on:Error do:exceptionBlock. |
397 |
397 |
398 " |
398 " |
399 Time readFrom:'0:00' |
399 Time readFrom:'0:00' |
400 Time readFrom:'2:00' |
400 Time readFrom:'2:00' |