[squeak-dev] encoding challenge

Robert Withers robert.w.withers at gmail.com
Sat Dec 19 06:09:45 UTC 2015


A core part of communications, whether online or to persistence, is 
encoding, or marshalling.  Let's do an example! Thank you for your 
thoughtful, earnest participation, as God wills it.

Here are 2 methods that encode and decode a DateAndTime object, but 
rather poorly. I've been going without much sleep for a bit and I can't 
figure it out. Here is the code in hopes that some will find this an 
interesting exercise in team and community building. For that intention, 
as a beginning.  What's the best solution?

Let's show them how to code when given the space in the environment.  
Tnks! :)

In class ASN1UTCTimeType..

encodeValue: anObject withDERStream: derStream

     | yy mo dd hh mm ss utcDateTime |
     yy :=  anObject year asString copyFrom: 3 to: 4.
     mo :=  anObject month asString padded: #left to: 2 with: $0.
     dd :=  anObject dayOfMonth asString padded: #left to: 2 with: $0.
     hh :=  anObject hour asString padded: #left to: 2 with: $0.
     mm :=  anObject minute asString padded: #left to: 2 with: $0.
     ss :=  anObject seconds asString padded: #left to: 2 with: $0.
     utcDateTime := (yy, mo, dd, hh, mm, ss, 'Z') asByteArray.
     derStream nextPutAll: utcDateTime.



decodeValueWithDERStream: derStream length: length

     | aUTCDateTime |
     aUTCDateTime := (derStream next: length) asByteArray asString.
     ^ (DateAndTime readFromString: (
         ((aUTCDateTime copyFrom: 1 to: 2) asInteger > 50 ifTrue: ['19'] 
ifFalse: ['20']),
         (aUTCDateTime copyFrom: 1 to: 2), '-',
         (aUTCDateTime copyFrom: 3 to: 4), '-',
         (aUTCDateTime copyFrom: 5 to: 6), 'T',
         (aUTCDateTime copyFrom: 7 to: 8), ':',
         (aUTCDateTime copyFrom: 9 to: 10), ':',
         (aUTCDateTime copyFrom: 11 to: 12)))
             offset: 0


-- 
. .. .. ^,^ robert


More information about the Squeak-dev mailing list