Timestamp.st
branchjv
changeset 17728 bbc5fa73dfab
parent 17711 39faaaf888b4
child 17732 a1892eeca6c0
--- a/Timestamp.st	Sun Aug 16 18:14:23 2009 +0100
+++ b/Timestamp.st	Wed Aug 19 17:14:36 2009 +0100
@@ -417,6 +417,81 @@
         onError:exceptionValue
 !
 
+readRFC1123FormatFrom:rfc1123String onError:exceptionBlock
+    |parts indexModifier utcOffsetString utcOffset day year time monthName month|
+
+"/    All HTTP/1.0 date/time stamps must be represented in Universal Time (UT), 
+"/    also known as Greenwich Mean Time (GMT), without exception. 
+"/    This is indicated in the first two formats by the inclusion of "GMT" as the three-letter abbreviation for time zone, 
+"/    and should be assumed when reading the asctime format.
+"/
+"/    HTTP-date      = rfc1123-date | rfc850-date | asctime-date
+"/
+"/    rfc1123-date   = wkday "," SP date1 SP time SP "GMT"
+"/    rfc850-date    = weekday "," SP date2 SP time SP "GMT"
+"/    asctime-date   = wkday SP date3 SP time SP 4DIGIT
+"/
+"/    date1          = 2DIGIT SP month SP 4DIGIT
+"/                     ; day month year (e.g., 02 Jun 1982)
+"/    date2          = 2DIGIT "-" month "-" 2DIGIT
+"/                     ; day-month-year (e.g., 02-Jun-82)
+"/    date3          = month SP ( 2DIGIT | ( SP 1DIGIT ))
+"/                     ; month day (e.g., Jun  2)
+"/
+"/    time           = 2DIGIT ":" 2DIGIT ":" 2DIGIT
+"/                     ; 00:00:00 - 23:59:59
+"/
+"/    wkday          = "Mon" | "Tue" | "Wed"
+"/                   | "Thu" | "Fri" | "Sat" | "Sun"
+"/
+"/    weekday        = "Monday" | "Tuesday" | "Wednesday"
+"/                   | "Thursday" | "Friday" | "Saturday" | "Sunday"
+"/
+"/    month          = "Jan" | "Feb" | "Mar" | "Apr"
+"/                   | "May" | "Jun" | "Jul" | "Aug"
+"/                   | "Sep" | "Oct" | "Nov" | "Dec"
+"/
+"/    Mon, 17 Aug 2009 11:11:15 GMT
+
+    rfc1123String isEmptyOrNil ifTrue:[^ exceptionBlock value].
+
+    parts := rfc1123String subStrings:Character space.
+    parts size == 6 ifTrue:[
+        indexModifier := 0.
+    ] ifFalse:[
+        parts size == 5 ifTrue:[
+            indexModifier := -1.
+        ] ifFalse:[
+            ^ exceptionBlock value
+        ].
+    ].
+
+    utcOffsetString := (parts at:6 + indexModifier).
+    (utcOffsetString = 'GMT' or:[utcOffsetString = 'UTC']) ifFalse:[ 
+        self assert:utcOffsetString size == 5.
+
+        utcOffset := (utcOffsetString from:4 to:5) asString asNumber * 60.
+        utcOffset := utcOffset + ((utcOffsetString from:2 to:3) asString asNumber * 60 * 60).
+
+        (utcOffsetString at:1) asSymbol == #- ifTrue:[
+            utcOffset := -1 * utcOffset.
+        ].
+    ].
+
+    day := Integer readFrom:(parts at:2 + indexModifier) onError:[^ exceptionBlock].
+    year := Integer readFrom:(parts at:4 + indexModifier) onError:[^ exceptionBlock].
+    time := Time readFrom:(parts at:5 + indexModifier) onError:[^ exceptionBlock].
+    monthName := parts at:3 + indexModifier.
+
+    month := (1 to:12) asOrderedCollection detect:[:i | 
+        (Date abbreviatedNameOfMonth:i language:#en) sameAs:monthName 
+    ] ifNone:[^ exceptionBlock].    
+
+    ^ (Timestamp 
+        fromDate:(Date newDay:day monthIndex:month year:year) 
+        andTime:time) + utcOffset       
+!
+
 secondsSince1970:sec
     "set time from elapsed seconds since 1-1-1970, 0:0:0.
      This is the format used in the UNIX world"
@@ -477,6 +552,12 @@
     "Modified: / 13.7.1999 / 12:37:57 / stefan"
 ! !
 
+!Timestamp class methodsFor:'Compatibility-Squeak'!
+
+current
+    ^ self now
+! !
+
 !Timestamp class methodsFor:'private'!
 
 basicReadFrom:aStream
@@ -1406,6 +1487,7 @@
     "
 ! !
 
+
 !Timestamp methodsFor:'visiting'!
 
 acceptVisitor:aVisitor with:aParameter
@@ -1416,7 +1498,7 @@
 !Timestamp class methodsFor:'documentation'!
 
 version
-    ^ '$Id: Timestamp.st 10447 2009-06-14 13:09:55Z vranyj1 $'
+    ^ '$Id: Timestamp.st 10467 2009-08-19 16:14:36Z vranyj1 $'
 ! !
 
 Timestamp initialize!