AbstractTime.st
branchjv
changeset 18120 e3a375d5f6a8
parent 18091 abbcac10730e
parent 17400 2aad82c1d66c
child 18457 214d760f8247
--- a/AbstractTime.st	Tue Feb 04 21:09:59 2014 +0100
+++ b/AbstractTime.st	Wed Apr 01 10:20:10 2015 +0100
@@ -11,6 +11,8 @@
 "
 "{ Package: 'stx:libbasic' }"
 
+"{ NameSpace: Smalltalk }"
+
 Magnitude subclass:#AbstractTime
 	instanceVariableNames:''
 	classVariableNames:''
@@ -144,13 +146,14 @@
 
 dateAndTimeNow
     "return an array filled with the current date and time.
+     As these provide no timezone info, this should be only used for user interface purposes.
      See also: Date today / Time now / Timestamp now."
 
     ^ Array with:(Date today) with:(Time now)
 
     "
-     Time dateAndTimeNow
-     Date dateAndTimeNow
+     Time dateAndTimeNow       
+     Date dateAndTimeNow       
     "
 
     "Modified: 19.4.1996 / 15:23:37 / cg"
@@ -162,13 +165,14 @@
     ^ self new setSeconds:0
 
     "
-        Timestamp epoch
-        Time epoch
+     Timestamp epoch
+     Time epoch     
     "
 !
 
 now
-    "return an instance of myself representing this moment."
+    "return an instance of myself representing this moment with second precision.
+     Timestamps will redefine this to always return millisecond precision."
 
     ^ self basicNew fromOSTime:(OperatingSystem getOSTime)
 
@@ -180,6 +184,20 @@
     "Modified: 1.7.1996 / 15:20:10 / cg"
 !
 
+nowWithMilliseconds
+    "return an instance of myself representing this moment with millisecond precision."
+
+    ^ self basicNew fromOSTimeWithMilliseconds:(OperatingSystem getOSTime)
+
+    "
+     Timestamp now   
+     Time now
+     Time nowWithMilliseconds
+    "
+
+    "Modified: 1.7.1996 / 15:20:10 / cg"
+!
+
 utcNow
     "return an instance of myself representing this momentin the UTC timezone."
 
@@ -354,7 +372,7 @@
 !AbstractTime class methodsFor:'timing evaluations'!
 
 microsecondsToRun:aBlock
-    "evaluate the argument, aBlock; return the number of milliseconds it took"
+    "evaluate the argument, aBlock; return the number of microseconds it took"
 
     |startTime endTime|
 
@@ -366,6 +384,8 @@
     "
      Time microsecondsToRun:[1000 factorial]  
     "
+
+    "Modified (comment): / 01-07-2014 / 15:57:13 / az"
 !
 
 millisecondsToRun:aBlock
@@ -560,7 +580,11 @@
 !
 
 timeZoneName
-    ^ #utc
+    ^ 'UTC'
+!
+
+utcOffset
+    ^ 0
 ! !
 
 !AbstractTime methodsFor:'arithmetic'!
@@ -570,7 +594,7 @@
 
     ^ aNumberOrTimeDuration sumFromTimestamp:self.
 
-"/    ^ self species basicNew 
+"/    ^ self speciesNew 
 "/        setMilliseconds:(self getMilliseconds + (aNumberOrTimeDuration * 1000) asInteger)
 
     "
@@ -588,11 +612,6 @@
 - aTimeOrNumberOfSeconds
     "return the delta in seconds between 2 times or subtract a number of seconds."
 
-"/    aTimeOrNumberOfSeconds isNumber ifTrue:[
-"/        ^ self species basicNew 
-"/            setMilliseconds:(self getMilliseconds - (aTimeOrNumberOfSeconds * 1000) asInteger)
-"/    ].
-"/    ^ self getSeconds - aTimeOrNumberOfSeconds getSeconds
     ^ aTimeOrNumberOfSeconds differenceFromTimestamp:self.
 
     "
@@ -605,7 +624,7 @@
      |t1 t2|
 
      t1 := Timestamp now.
-     (Delay forSeconds:5) wait.
+     Delay waitFor:10 seconds.
      t2 := Timestamp now.
      Transcript showCR:('seconds passed: ' , (t2 - t1) printString).
     "
@@ -650,7 +669,7 @@
 addMilliseconds:numberOfMilliSeconds
     "return a new instance of myself, numberOfMilliSeconds afterwards."
 
-    ^ self species basicNew 
+    ^ self speciesNew 
         setMilliseconds:(self getMilliseconds + numberOfMilliSeconds)
 
     "
@@ -695,7 +714,7 @@
 addSeconds:numberOfSeconds
     "return a new instance of myself, numberOfSeconds afterwards."
 
-    ^ self species basicNew 
+    ^ self speciesNew 
         setMilliseconds:(self getMilliseconds + (numberOfSeconds * 1000))
 
     "
@@ -729,6 +748,46 @@
     ^ self addSeconds:timeAmount
 !
 
+deltaFrom:aTimeOrInteger
+    "return the delta as a timeDuration between 2 timeStamps.
+     The argument is supposed to be BEFORE the receiver,
+        computes self - aTimestamp"
+
+    ^ aTimeOrInteger differenceFromTimestamp:self
+
+    "
+        Time now deltaFrom:10 minutes.
+        Time now deltaFrom:3600.
+        Time now deltaFrom:3600 seconds.
+
+        Timestamp now deltaFrom:10 minutes.
+        Timestamp now deltaFrom:3600.
+        Timestamp now deltaFrom:3600 seconds.
+    "
+
+    "
+     |t1 t2|
+
+     t1 := Timestamp now.
+     Delay waitForSeconds:0.5.
+     t2 := Timestamp now.
+     t2 deltaFrom:t1   
+    "
+
+    "Created: / 04-10-2007 / 13:34:28 / cg"
+    "Modified: / 10-07-2010 / 09:37:01 / cg"
+!
+
+roundTo:aTimeDuration
+     ^ self class new setMilliseconds:(self getMilliseconds roundTo:aTimeDuration getMilliseconds)
+
+    "
+      (TimeDuration fromMilliseconds:25234) roundTo:2 seconds
+      Time now roundTo:10 minutes
+      Timestamp now roundTo:10 minutes
+    "
+!
+
 subtractDays:numberOfDays
     "return a new instance of myself, numberOfDays before."
 
@@ -768,7 +827,7 @@
 subtractMilliseconds:numberOfMilliSeconds
     "return a new instance of myself, numberOfMilliSeconds before."
 
-    ^ self species basicNew 
+    ^ self speciesNew 
         setMilliseconds:(self getMilliseconds - numberOfMilliSeconds)
 
     "
@@ -813,7 +872,7 @@
 subtractSeconds:numberOfSeconds
     "return a new instance of myself, numberOfSeconds before."
 
-    ^ self species basicNew 
+    ^ self speciesNew 
         setMilliseconds:(self getMilliseconds - (numberOfSeconds * 1000))
 
     "
@@ -846,6 +905,16 @@
     ].
     ^ self subtractSeconds:timeAmount
 
+!
+
+truncateTo:aTimeDuration
+     ^ self class new setMilliseconds:(self getMilliseconds truncateTo:aTimeDuration getMilliseconds)
+
+    "
+      (TimeDuration fromMilliseconds:25234) truncateTo:2 seconds
+      Time now truncateTo:10 minutes
+      Timestamp now truncateTo:10 minutes
+    "
 ! !
 
 !AbstractTime methodsFor:'comparing'!
@@ -882,6 +951,33 @@
     ^ self subclassResponsibility
 !
 
+asMilliseconds
+    "return the number of milliseconds elapsed since midnight"
+
+    ^ self getMilliseconds
+
+    "
+     Time now asMilliseconds
+     Timestamp now asMilliseconds
+     (TimeDuration days:1) asMilliseconds
+     (TimeDuration hours:1) asMilliseconds
+    "
+
+    "Created: / 05-09-2011 / 10:40:15 / cg"
+!
+
+asSeconds
+    "return the number of seconds elapsed since midnight"
+
+    ^ self getSeconds
+
+    "
+     Timestamp now asSeconds
+     Time now asSeconds
+     (TimeDuration days:1) asSeconds
+    "
+!
+
 asTimestamp
     "represent myself as a Timestamp"
 
@@ -897,28 +993,33 @@
 !AbstractTime methodsFor:'double dispatching'!
 
 differenceFromTimestamp:aTimestamp
-    ^ aTimestamp getSeconds - self getSeconds
+    "/ the correct thing to do (and I will, in the future) is to
+    "/ return a TimeDuration:
+    "/
+    ^ TimeDuration fromMilliseconds:(aTimestamp getMilliseconds - self getMilliseconds).
+
+    "/ which is the same as: deltaFrom:aTimestamp
+    "/
+    "/ however, there might be old code around, which is not be prepared for
+    "/ getting a non-number (the seconds). Therefore, for the meantime,
+    "/ we return:
+
+    "/ ^ aTimestamp getSeconds - self getSeconds
 ! !
 
 !AbstractTime methodsFor:'printing & storing'!
 
-addPrintBindingsTo:aDictionary
-    "private print support: add bindings for printing to aDictionary."
-
-    self addPrintBindingsTo:aDictionary language:nil
-!
-
-addPrintBindingsTo:aDictionary language:languageOrNil
+addBasicPrintBindingsTo:aDictionary language:languageOrNil
     "private print support: add bindings for printing to aDictionary.
      languageOrNil can be #en, #fr, #de or nil for the current language.
+     Here only basic bindings are added - no timezone am am/pm stuff,
+     which doesn't make sense for TimeDuration.
 
      bindings:
         %h      hours, 00..23 (i.e. european)  0-padded to length 2
-        %u      hours, 00..12 (i.e. us)        0-padded to length 2
         %m      minutes, 00..59                0-padded to length 2
         %s      seconds, 00..59                0-padded to length 2
         %i      milliseconds, 000..999         0-padded to length 3
-        %a      am/pm
 
      Timestamp only:
         %(day)   day, 00..31                    0-padded to length 2
@@ -927,17 +1028,13 @@
 
      special:
         %H      24-hours - unpadded
-        %U      12-hours - unpadded
         %M      minutes - unpadded
         %S      seconds - unpadded
         %I      milliseconds, unpadded
-        %A      AM/PM   - uppercase
 
         %t      seconds within hour  (unpadded)
         %T      seconds from midNight  (unpadded)
 
-        %(TZD)  timeZone delta from UTC in the format +/-hh:mm  
-
         %(milli1) milliseconds, truncated to 1/10th of a second 0..9         
         %(milli2) milliseconds, truncated to 1/100th of a second 00..99 0-padded to length 2        
         %(milli3) milliseconds, same as %i for convenience
@@ -1002,24 +1099,17 @@
 
     "
 
-    |hours minutes seconds millis usHours ampm s t zone tzDelta|
+    |time hours minutes seconds millis s t|
 
-    hours := self hours.
-    minutes := self minutes.
-    seconds := self seconds.
+    time := self asTime.
+    hours := time hours.
+    minutes := time minutes.
+    seconds := time seconds.
     millis := self milliseconds.
-    zone := self timeZoneName.
-    tzDelta := self timeZoneDeltaInMinutes.
-
-    ampm := self meridianAbbreviation.
-    usHours := self hour12.
 
     aDictionary at:$H put:(s := hours printString).
     aDictionary at:$h put:(s leftPaddedTo:2 with:$0).
 
-    aDictionary at:$U put:(s := usHours printString).
-    aDictionary at:$u put:(s leftPaddedTo:2 with:$0).
-
     aDictionary at:$M put:(s := minutes printString).
     aDictionary at:$m put:(s leftPaddedTo:2 with:$0).
 
@@ -1036,6 +1126,131 @@
     aDictionary at:$t put:(seconds * minutes) printString.
     aDictionary at:$T put:(seconds * minutes * hours) printString.
 
+
+    "
+      |dict|
+      dict := Dictionary new.
+      Timestamp now addBasicPrintBindingsTo:dict language:#en.
+      dict inspect
+    "
+!
+
+addPrintBindingsTo:aDictionary
+    <resource: #obsolete>
+
+    self obsoleteMethodWarning:'use #addPrintBindingsTo:language:'.
+    self addPrintBindingsTo:aDictionary language:nil
+!
+
+addPrintBindingsTo:aDictionary language:languageOrNil
+    "private print support: add bindings for printing to aDictionary.
+     languageOrNil can be #en, #fr, #de or nil for the current language.
+
+     bindings:
+        %h      hours, 00..23 (i.e. european)  0-padded to length 2
+        %u      hours, 00..12 (i.e. us)        0-padded to length 2
+        %m      minutes, 00..59                0-padded to length 2
+        %s      seconds, 00..59                0-padded to length 2
+        %i      milliseconds, 000..999         0-padded to length 3
+        %a      am/pm
+
+     Timestamp only:
+        %(day)   day, 00..31                    0-padded to length 2
+        %(month) month, 00..12                  0-padded to length 2
+        %(year)  year, 4 digits                 0-padded to length 4
+
+     special:
+        %H      24-hours - unpadded
+        %U      12-hours - unpadded
+        %M      minutes - unpadded
+        %S      seconds - unpadded
+        %I      milliseconds, unpadded
+        %A      AM/PM   - uppercase
+
+        %t      seconds within hour  (unpadded)
+        %T      seconds from midNight  (unpadded)
+
+        %(TZD)  timeZone delta of the receiver from UTC in the format +/-hh:mm  
+
+        %(milli1) milliseconds, truncated to 1/10th of a second 0..9         
+        %(milli2) milliseconds, truncated to 1/100th of a second 00..99 0-padded to length 2        
+        %(milli3) milliseconds, same as %i for convenience
+
+     Timestamp only:
+        %(Day)         - day - unpadded                    
+        %(Month)       - month - unpadded                    
+        %(yearOrTime)  - year or time 5 digits    as in unix-ls:
+                                                  year if it is not the current year;
+                                                  time otherwise
+        %(weekDay)      - day in week (1->monday, 2->tuesday, ... ,7->sunday)
+
+        %(dayName)      - full day name     
+        %(DayName)      - full day name, first character uppercase      
+        %(DAYNAME)      - full day name, all uppercase       
+
+        %(monthName)    - full month name     
+        %(MonthName)    - full month name, first character uppercase      
+        %(MONTHNAME)    - full month name, all uppercase       
+
+        %(shortDayName) - short (abbreviated) day name     
+        %(ShortDayName) - short (abbreviated) day name, first character uppercase      
+        %(SHORTDAYNAME) - short (abbreviated) day name, all uppercase       
+
+        %(shortMonthName) - short (abbreviated) month name     
+        %(ShortMonthName) - short (abbreviated) month name, first character uppercase      
+        %(SHORTMONTHNAME) - short (abbreviated) month name, all uppercase       
+
+        %(nth)          - counting day-in-month (1->'st'; 2->'nd'; 3->'rd'; 4...->'th')      
+        %(weekDayNth)   - counting day-in-week (1->'st'; 2->'nd'; 3->'rd'; 4...->'th')      
+        %(weekNth)      - counting week-in-year (1->'st'; 2->'nd'; 3->'rd'; 4...->'th')
+
+        %(yearRoman)    - year, in roman letters                    
+        %(monthRoman)   - month, in roman letters                    
+
+
+     The ISO8601 printString are generated with:
+
+       Year:
+          YYYY (eg 1997)
+                Date today printStringFormat:'%(year)'
+                Timestamp now printStringFormat:'%(year)'  
+
+       Year and month:
+          YYYY-MM (eg 1997-07)
+                Date today printStringFormat:'%(year)-%(month)'  
+                Timestamp now printStringFormat:'%(year)-%(month)'  
+
+       Complete date:
+          YYYY-MM-DD (eg 1997-07-16)
+                Date today printStringFormat:'%(year)-%(month)-%(day)'    
+                Timestamp now printStringFormat:'%(year)-%(month)-%(day)'  
+
+       Complete date plus hours and minutes:
+          YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
+                Timestamp now printStringFormat:'%(year)-%(month)-%(day)T%h:%m%(TZD)'  
+
+       Complete date plus hours, minutes and seconds:
+          YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
+                Timestamp now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s%(TZD)'  
+
+       Complete date plus hours, minutes, seconds and a decimal fraction of a second
+          YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
+                Timestamp now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s.%(milli2)%(TZD)'  
+
+    "
+
+    |usHours ampm s zone tzDelta|
+
+    self addBasicPrintBindingsTo:aDictionary language:languageOrNil.
+    zone := self timeZoneName.
+    tzDelta := self timeZoneDeltaInMinutes.
+
+    ampm := self meridianAbbreviation.
+    usHours := self hour12 printString.
+    aDictionary at:$U put:usHours.
+    aDictionary at:$u put:(usHours leftPaddedTo:2 with:$0).
+
+
     aDictionary at:$a put:ampm.
     aDictionary at:$A put:ampm asUppercase.
     aDictionary at:$z put:zone.
@@ -1110,8 +1325,10 @@
     ^ self printStringFormat:aFormatString language:nil.
 
     "
-     Timestamp now printStringFormat:'%U:%m:%s %a'   
+     Timestamp now printStringFormat:'%U:%m:%s %a'        
+     Timestamp now printStringFormat:'%u:%m:%s %a'        
      Time now printStringFormat:'%U:%m:%s %a'   
+     Time now printStringFormat:'%u:%m:%s %a'   
 
      Time now printStringFormat:'%h:%m:%s'      
      Time now printStringFormat:'%H:%m:%s'      
@@ -1139,7 +1356,7 @@
 
     |s|
 
-    s := WriteStream on:(String new:20).
+    s := CharacterWriteStream on:(String new:20).
     self printOn:s format:aFormatString language:languageString.
     ^ s contents.
 
@@ -1213,15 +1430,19 @@
      represented), this must be done by a concrete class."
 
     ^ self subclassResponsibility
+!
+
+speciesNew
+    ^ self species basicNew
 ! !
 
 !AbstractTime class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.77 2013-08-23 00:19:41 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.91 2015-02-03 19:01:47 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.77 2013-08-23 00:19:41 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.91 2015-02-03 19:01:47 cg Exp $'
 ! !