#FEATURE by cg
class: Time
changed: #readFrom:format:language:onError:
new format: fractional seconds
--- a/Time.st Thu Jun 02 11:26:05 2016 +0200
+++ b/Time.st Thu Jun 02 11:58:03 2016 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
"
COPYRIGHT (c) 1989 by Claus Gittinger
All Rights Reserved
@@ -196,6 +198,7 @@
%m minutes, 00..59 0-padded to length 2
%s seconds, 00..59 0-padded to length 2
%i milliseconds, 000..999 0-padded to length 3
+ %f fractional seconds any length, but only milliseconds are read
%a am/pm
an optional length after the % gives a field length;
@@ -206,7 +209,7 @@
|hour minute second millisecond
utcOffset inStream formatStream error fChar format itemHandler
- len s|
+ len s fraction fractionString|
error := [:msg |
exceptionalValue isBlock ifTrue:[
@@ -236,6 +239,10 @@
] ifFalse:[ ( format = 'i' or:[ format = 'I' ]) ifTrue:[
millisecond := Integer readFrom:input onError:[ error value:'invalid millsecond' ].
+ ] ifFalse:[ ( format = 'f' or:[ format = 'F' ]) ifTrue:[
+ fractionString := input upToMatching:[:ch | ch isDigit not].
+ fraction := FixedPoint readFrom:'0.',fractionString.
+ millisecond := (fraction * 1000) truncated.
] ifFalse:[ ( format = 'tz' ) ifTrue:[
utcOffset := Timestamp utcOffsetFrom:input.
utcOffset isNil ifTrue:[ error value:'invalid timezone' ]
@@ -254,7 +261,7 @@
] ifFalse:[
error value:'unhandled format:',format
- ]]]]]]]
+ ]]]]]]]]
].
hour := 0.
@@ -309,6 +316,12 @@
Time readFrom:'131106' format:'%2h%2m%2s' language:nil onError:[self halt]
Time readFrom:'7:30pm EST' format:'%u:%m%a %tz' language:#en onError:[self halt]
Time readFrom:'7:30pm UTC' format:'%u:%m%a %tz' language:#en onError:[self halt]
+
+ Time readFrom:'13:11:06.111' format:'%h:%m:%s.%i' language:nil onError:[self halt]
+ Time readFrom:'13:11:06.1' format:'%h:%m:%s.%f' language:nil onError:[self halt]
+ Time readFrom:'13:11:06.01' format:'%h:%m:%s.%f' language:nil onError:[self halt]
+ Time readFrom:'13:11:06.001' format:'%h:%m:%s.%f' language:nil onError:[self halt]
+ Time readFrom:'13:11:06.1234567' format:'%h:%m:%s.%f' language:nil onError:[self halt]
"
!