VASmalltalk – ICU, UDateFormat

To my surprise is ws not possible to create the original DateAndTime printString format.

It turns out, that ICU is able to create an ISO8601 compatible string for the time zone offset value (e.g. “+0100” is a valid zone description) – but RFC3339 says, that one should use another version of ISO8601: “+01:00”.

Therefore I added an optional block attribute to UDateFormat to be able to build your own string format …

I used this feature to create the VASmalltalk DateAndTime output format. You get this special formater instance via “UDateFormat vaStyle” and you here the source how it is defined:

vaStyle
"^ this creates the original Timestamp format used by VASmalltalk	"
| anUDateFormat |
anUDateFormat :=
  UDateFormat
    openTimeStyle: UDAT_IGNORE
    dateStyle: UDAT_IGNORE
    locale: nil
    zoneName: nil
    pattern: 'YYYY-MM-dd''T''HH:mm:ss.SSS'.
"here is the definition of the block, creating the 'correct' zone offset format"			
anUDateFormat formatBlock: [:anUtf16String :anUCalendar :aUDateFormat | 
  | offsetDuration writeStream |

  offsetDuration := anUCalendar offset.
  writeStream := WriteStream on: String new.
  writeStream
    nextPut: (offsetDuration hours < 0 ifTrue: [$-] ifFalse: [$+]);
    nextPutAll: (offsetDuration hours asString leftPaddedTo: 2 with: $0);
    nextPut: $:;
    nextPutAll: (offsetDuration minutes asString leftPaddedTo: 2 with: $0).
  anUtf16String , writeStream contents].

^anUDateFormat
This entry was posted in Smalltalk and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s