A new version of Kernel was added to project The Trunk:
http://source.squeak.org/trunk/Kernel-tfel.235.mcz ==================== Summary ==================== Name: Kernel-tfel.235 Author: tfel Time: 1 September 2009, 11:37:20 am UUID: d2e0bf54-4389-4074-8d85-d98707f39ef1 Ancestors: Kernel-ar.234 Changed the parsing from Stream in Date and Time classes to be more forgiving and esp. in the Date class to allow the ISO8601 format. Also changed the DateAndTime parsing to use the methods of Date and Time to parse their parts of the Stream making the whole method more forgiving if they don't properly keep to the standard. =============== Diff against Kernel-ar.234 =============== Item was changed: ----- Method: DateAndTime class>>readFrom: (in category 'squeak protocol') ----- readFrom: aStream - | bc year month day hour minute second nanos offset buffer ch | - - - aStream peek = $- ifTrue: [ aStream next. bc := -1] ifFalse: [bc := 1]. - year := (aStream upTo: $-) asInteger * bc. - month := (aStream upTo: $-) asInteger. - day := (aStream upTo: $T) asInteger. - hour := (aStream upTo: $:) asInteger. - buffer := '00:' copy. ch := nil. - minute := WriteStream on: buffer. - [ aStream atEnd | (ch = $:) | (ch = $+) | (ch = $-) ] - whileFalse: [ ch := minute nextPut: aStream next. ]. - (ch isNil or: [ch isDigit]) ifTrue: [ ch := $: ]. - minute := ((ReadStream on: buffer) upTo: ch) asInteger. - buffer := '00.' copy. - second := WriteStream on: buffer. - [ aStream atEnd | (ch = $.) | (ch = $+) | (ch = $-) ] - whileFalse: [ ch := second nextPut: aStream next. ]. - (ch isNil or: [ch isDigit]) ifTrue: [ ch := $. ]. - second := ((ReadStream on: buffer) upTo: ch) asInteger. - buffer := '000000000+' copy. - (ch = $.) ifTrue: [ - nanos := WriteStream on: buffer. - [ aStream atEnd | ((ch := aStream next) = $+) | (ch = $-) ] - whileFalse: [ nanos nextPut: ch. ]. - (ch isNil or: [ch isDigit]) ifTrue: [ ch := $+ ]. - ]. - - nanos := buffer asInteger. - aStream atEnd - ifTrue: [ offset := self localOffset ] - - ifFalse: - [offset := Duration fromString: (ch asString, '0:', aStream upToEnd). - - (offset = self localOffset) ifTrue: [ offset := self localOffset ]]. - ^ self - year: year - month: month - day: day - hour: hour - minute: minute - - second: second - nanoSecond: nanos + | offset date time ch | + + date := Date readFrom: aStream. + [aStream peek isDigit] + whileFalse: [aStream next]. + time := Time readFrom: aStream. + aStream atEnd + ifTrue: [ offset := self localOffset ] + ifFalse: [ + ch := aStream next. + ch = $+ ifTrue: [ch := Character space]. + offset := Duration fromString: ch asString, '0:', aStream upToEnd]. + + ^ self + year: date year + month: date monthIndex + day: date dayOfMonth + hour: time hour + minute: time minute + second: time second + nanoSecond: time nanoSecond + offset: offset - offset: offset. " '-1199-01-05T20:33:14.321-05:00' asDateAndTime ' 2002-05-16T17:20:45.1+01:01' asDateAndTime ' 2002-05-16T17:20:45.02+01:01' asDateAndTime ' 2002-05-16T17:20:45.003+01:01' asDateAndTime ' 2002-05-16T17:20:45.0004+01:01' asDateAndTime ' 2002-05-16T17:20:45.00005' asDateAndTime ' 2002-05-16T17:20:45.000006+01:01' asDateAndTime ' 2002-05-16T17:20:45.0000007+01:01' asDateAndTime ' 2002-05-16T17:20:45.00000008-01:01' asDateAndTime ' 2002-05-16T17:20:45.000000009+01:01' asDateAndTime ' 2002-05-16T17:20:45.0000000001+01:01' asDateAndTime ' 2002-05-16T17:20' asDateAndTime ' 2002-05-16T17:20:45' asDateAndTime ' 2002-05-16T17:20:45+01:57' asDateAndTime ' 2002-05-16T17:20:45-02:34' asDateAndTime ' 2002-05-16T17:20:45+00:00' asDateAndTime ' 1997-04-26T01:02:03+01:02:3' asDateAndTime "! Item was changed: ----- Method: Time class>>readFrom: (in category 'smalltalk-80') ----- readFrom: aStream "Read a Time from the stream in the form: <hour>:<minute>:<second> <am/pm> <minute>, <second> or <am/pm> may be omitted. e.g. 1:59:30 pm; 8AM; 15:30" + | hour minute second ampm nanos nanosBuffer | - | hour minute second ampm | hour := Integer readFrom: aStream. minute := 0. second := 0. + nanosBuffer := '000000000' copy. + nanos := ReadWriteStream on: nanosBuffer. + (aStream peekFor: $:) + ifTrue: [ + minute := Integer readFrom: aStream. + (aStream peekFor: $:) + ifTrue: [ + second := Integer readFrom: aStream]. + (aStream peekFor: $.) + ifTrue: [ + [aStream atEnd not and: [aStream peek isDigit]] + whileTrue: [nanos nextPut: aStream next]]]. - (aStream peekFor: $:) ifTrue: - - [ minute := Integer readFrom: aStream. - (aStream peekFor: $:) ifTrue: [ second := Integer readFrom: aStream ]]. aStream skipSeparators. (aStream atEnd not and: [aStream peek isLetter]) ifTrue: [ampm := aStream next asLowercase. (ampm = $p and: [hour < 12]) ifTrue: [hour := hour + 12]. (ampm = $a and: [hour = 12]) ifTrue: [hour := 0]. (aStream peekFor: $m) ifFalse: [aStream peekFor: $M ]]. + ^ self + hour: hour + minute: minute + second: second + nanoSecond: nanosBuffer asInteger - ^ self hour: hour minute: minute second: second - "Time readFrom: (ReadStream on: '2:23:09 pm')"! Item was changed: ----- Method: Date class>>readFrom: (in category 'squeak protocol') ----- readFrom: aStream "Read a Date from the stream in any of the forms: + <day> <month> <year> (15 April 1982; 15-APR-82; 15.4.82; 15APR82) + <month> <day> <year> (April 15, 1982; 4/15/82) + <year>-<month>-<day> (1982-04-15) (ISO8601)" + | day month year parsedNumber prefix | + aStream peek = $- + ifTrue: [prefix := -1] + ifFalse: [prefix := 1]. - - <day> <monthName> <year> (5 April 1982; 5-APR-82) - - <monthName> <day> <year> (April 5, 1982) - - <monthNumber> <day> <year> (4/5/82) - <day><monthName><year> (5APR82)" - | day month year | - aStream peek isDigit - ifTrue: [day := Integer readFrom: aStream]. [aStream peek isAlphaNumeric] whileFalse: [aStream skip: 1]. + aStream peek isDigit + ifTrue: [ + parsedNumber := (Integer readFrom: aStream) * prefix. + (parsedNumber < 0 or: [parsedNumber > 31]) + ifTrue: [year := parsedNumber]]. + [aStream peek isAlphaNumeric] + whileFalse: [aStream skip: 1]. aStream peek isLetter + ifTrue: ["MM-DD-YY or DD-MM-YY or YY-MM-DD" + month := WriteStream on: (String new: 10). - ifTrue: ["number/name... or name..." - month := WriteStream - on: (String new: 10). [aStream peek isLetter] whileTrue: [month nextPut: aStream next]. month := month contents. + [aStream peek isAlphaNumeric] + whileFalse: [aStream skip: 1]. + parsedNumber isNil + ifTrue: ["MM DD YY" + day := Integer readFrom: aStream] + ifFalse: [ + year isNil + ifTrue: ["DD MM YY" + day := parsedNumber]]] + ifFalse: ["MM-DD-YY or DD-MM-YY or YY-MM-DD" + year isNil + ifTrue: ["MM-DD-YY or DD-MM-YY" + parsedNumber > 12 + ifTrue: ["DD-MM-YY" + day := parsedNumber. + month := Month nameOfMonth: (Integer readFrom: aStream)] + ifFalse: ["MM-DD-YY" + month := Month nameOfMonth: parsedNumber. + day := Integer readFrom: aStream]] + ifFalse: ["YY-MM-DD" + month := Month nameOfMonth: (Integer readFrom: aStream)]]. - day isNil - ifTrue: ["name/number..." - [aStream peek isAlphaNumeric] - whileFalse: [aStream skip: 1]. - day := Integer readFrom: aStream]] - ifFalse: ["number/number..." - month := Month nameOfMonth: day. - day := Integer readFrom: aStream]. [aStream peek isAlphaNumeric] whileFalse: [aStream skip: 1]. + year isNil + ifTrue: [year := Integer readFrom: aStream] + ifFalse: [day := Integer readFrom: aStream]. + (year < 100 and: [year >= 0]) + ifTrue: [ + year < 69 + ifTrue: [ year := 2000 + year] + ifFalse: [year := 1900 + year]]. - year := Integer readFrom: aStream. - year < 10 ifTrue: [year := 2000 + year] - ifFalse: [ year < 1900 ifTrue: [ year := 1900 + year]]. ^ self year: year month: month day: day ! |
Free forum by Nabble | Edit this page |