Timestamp.st
changeset 9619 6f1fc553757f
parent 9449 6ba774be6aa1
child 9620 85969e75395d
equal deleted inserted replaced
9618:adc627ef4702 9619:6f1fc553757f
   240     "return a new Timestamp, reading a printed representation from aStream.
   240     "return a new Timestamp, reading a printed representation from aStream.
   241      The string is interpreted as 24 hour format, as printed.
   241      The string is interpreted as 24 hour format, as printed.
   242 
   242 
   243      Notice, that this is not the storeString format and 
   243      Notice, that this is not the storeString format and 
   244      is different from the format expected by readFrom:.
   244      is different from the format expected by readFrom:.
   245      The format read here is yyyymmddHHMMSS.iii+uuuu,
   245      The format read here is either
   246      which is the ASN1 GeneralizedTime format."
   246         yyyymmddHHMMSS.iii+uuuu, which is the ASN1 GeneralizedTime format.
       
   247      or:
       
   248         yyyy-mm-dd HH:MM:SS.iii +uuuu.
       
   249     "
   247 
   250 
   248     |newTime|
   251     |newTime|
   249 
   252 
   250     ^ [
   253     ^ [
   251         |str day month year hour min sec millis c|
   254         |str day month year hour min sec millis c tzDelta|
   252 
   255 
   253         sec := millis := 0.
   256         sec := millis := 0.
   254         str := aStringOrStream readStream.
   257         str := aStringOrStream readStream.
   255 
   258 
   256         year := Integer readFrom:(str next:4).
   259         year := Integer readFrom:(str next:4).
       
   260         str peek == $- ifTrue:[ str next].
   257         month := Integer readFrom:(str next:2).
   261         month := Integer readFrom:(str next:2).
   258         (month between:1 and:12) ifFalse:[^ exceptionBlock value].
   262         (month between:1 and:12) ifFalse:[^ exceptionBlock value].
       
   263         str peek == $- ifTrue:[ str next].
   259         day := Integer readFrom:(str next:2).
   264         day := Integer readFrom:(str next:2).
   260         (day between:1 and:31) ifFalse:[^ exceptionBlock value].
   265         (day between:1 and:31) ifFalse:[^ exceptionBlock value].
       
   266 
       
   267         str skipSeparators.
   261         hour:= Integer readFrom:(str next:2).
   268         hour:= Integer readFrom:(str next:2).
   262         (hour between:0 and:24) ifFalse:[^ exceptionBlock value].
   269         (hour between:0 and:24) ifFalse:[^ exceptionBlock value].
       
   270         str peek == $: ifTrue:[ str next].
   263         min:= Integer readFrom:(str next:2).
   271         min:= Integer readFrom:(str next:2).
   264         (min between:0 and:59) ifFalse:[^ exceptionBlock value].
   272         (min between:0 and:59) ifFalse:[^ exceptionBlock value].
   265         str atEnd ifFalse:[
   273         str atEnd ifFalse:[
       
   274             str peek == $: ifTrue:[ str next].
   266             sec := Integer readFrom:(str next:2).
   275             sec := Integer readFrom:(str next:2).
   267             (sec between:0 and:59) ifFalse:[^ exceptionBlock value].
   276             (sec between:0 and:59) ifFalse:[^ exceptionBlock value].
   268             str peek == $. ifTrue:[
   277             str atEnd ifFalse:[
   269                 str next.
   278                 str peek == $. ifTrue:[
   270                 millis := Integer readFrom:str.
   279                     str next.
       
   280                     millis := Integer readFrom:str.
       
   281                 ].
       
   282                 str skipSeparators.
   271             ].
   283             ].
   272         ]. 
   284         ].
       
   285 
   273         str atEnd ifTrue:[
   286         str atEnd ifTrue:[
   274             "/ this is local time
   287             "/ this is local time
   275             newTime := self year:year month:month day:day 
   288             newTime := self year:year month:month day:day 
   276                               hour:hour minute:min second:sec millisecond:millis.
   289                             hour:hour minute:min second:sec millisecond:millis.
   277         ] ifFalse:[
   290         ] ifFalse:[
   278             c := str next.
   291             c := str next.
   279             c ~~ $Z ifTrue:[ |tzh tzmin|
   292             c ~~ $Z ifTrue:[ 
       
   293                 |tzh tzmin|
   280                 tzh := Integer readFrom:(str next:2).
   294                 tzh := Integer readFrom:(str next:2).
   281                 tzmin := Integer readFrom:(str next:2).
   295                 tzmin := Integer readFrom:(str next:2).
   282                 c == $+ ifTrue:[
   296                 c == $+ ifTrue:[
   283                     "the timezone is ahead of UTC or EAST from Greenwich: subtract hours and minutes"
   297                     "the timezone is ahead of UTC or EAST from Greenwich: subtract hours and minutes"
   284                     hour := hour - tzh.
   298                     hour := hour - tzh.
   321      Timestamp readGeneralizedFrom:'199502201311' onError:[]    
   335      Timestamp readGeneralizedFrom:'199502201311' onError:[]    
   322      Timestamp readGeneralizedFrom:'19950220131106.100' onError:[]    
   336      Timestamp readGeneralizedFrom:'19950220131106.100' onError:[]    
   323      Timestamp readGeneralizedFrom:'19950232131106.100' onError:'wrong'    
   337      Timestamp readGeneralizedFrom:'19950232131106.100' onError:'wrong'    
   324      Timestamp readGeneralizedFrom:'19950fo2131106.100' onError:'wrong'    
   338      Timestamp readGeneralizedFrom:'19950fo2131106.100' onError:'wrong'    
   325      Timestamp readGeneralizedFrom:'foo' onError:'wrong'                     
   339      Timestamp readGeneralizedFrom:'foo' onError:'wrong'                     
   326     "
   340 
   327 
   341      Timestamp readGeneralizedFrom:'2000-02-02 12:00:00' onError:[]   
   328     "Modified: / 8.10.1996 / 19:25:59 / cg"
   342      Timestamp readGeneralizedFrom:'2000-02-02 12:00:00.100' onError:[]
   329     "Modified: / 13.7.1999 / 12:31:14 / stefan"
   343      Timestamp readGeneralizedFrom:'2000-02-02 12:00:00.100 +0100' onError:[] 
       
   344      Timestamp readGeneralizedFrom:'2000-02-02 12:00:00 -0100' onError:[]
       
   345      Timestamp readGeneralizedFrom:'2000-02-02 12:00:00 +0000' onError:[] 
       
   346     "
       
   347 
       
   348     "Modified: / 13-07-1999 / 12:31:14 / stefan"
       
   349     "Modified: / 22-08-2006 / 12:30:11 / cg"
   330 !
   350 !
   331 
   351 
   332 readIso8601FormatFrom:aStringOrStream
   352 readIso8601FormatFrom:aStringOrStream
   333     "return a new Timestamp, reading an iso8601 UTC representation from aStream.
   353     "return a new Timestamp, reading an iso8601 UTC representation from aStream.
   334      Missing month/day values are replaced with 1; i.e. 1999T24:00
   354      Missing month/day values are replaced with 1; i.e. 1999T24:00
  1322     "
  1342     "
  1323      Timestamp now utcSecondsSince1970
  1343      Timestamp now utcSecondsSince1970
  1324     "
  1344     "
  1325 ! !
  1345 ! !
  1326 
  1346 
  1327 
       
  1328 !Timestamp methodsFor:'visiting'!
  1347 !Timestamp methodsFor:'visiting'!
  1329 
  1348 
  1330 acceptVisitor:aVisitor with:aParameter
  1349 acceptVisitor:aVisitor with:aParameter
  1331 
  1350 
  1332     ^ aVisitor visitTimestamp:self with:aParameter
  1351     ^ aVisitor visitTimestamp:self with:aParameter
  1333 ! !
  1352 ! !
  1334 
  1353 
  1335 !Timestamp class methodsFor:'documentation'!
  1354 !Timestamp class methodsFor:'documentation'!
  1336 
  1355 
  1337 version
  1356 version
  1338     ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.109 2006-07-18 16:48:24 stefan Exp $'
  1357     ^ '$Header: /cvs/stx/stx/libbasic/Timestamp.st,v 1.110 2006-08-22 10:30:37 cg Exp $'
  1339 ! !
  1358 ! !
  1340 
  1359 
  1341 Timestamp initialize!
  1360 Timestamp initialize!