AbstractTime.st
changeset 4649 3eaf75dd8973
parent 3476 f2ebb76a952d
child 4650 0a8d9f7a38ef
--- a/AbstractTime.st	Wed Sep 01 17:10:07 1999 +0200
+++ b/AbstractTime.st	Wed Sep 01 20:18:31 1999 +0200
@@ -406,6 +406,107 @@
 
 ! !
 
+!AbstractTime methodsFor:'printing & storing'!
+
+printOn:aStream format:aFormatString
+    "print using a format string;
+     valid format items are:
+        %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
+        %a      am/pm
+
+     special:
+        %H      24-hours - unpadded
+        %U      12-hours - unpadded
+        %M      minutes - unpadded
+        %S      seconds - unpadded
+        %A      AM/PM   - uppercase
+    "
+
+    aStream nextPutAll:(self printStringFormat:aFormatString)
+
+    "
+     Time now printOn:Transcript format:'%h:%m:%s'   . Transcript cr.      
+     Time now printOn:Transcript format:'%H:%m:%s'   . Transcript cr.      
+     Time now printOn:Transcript format:'%u:%m:%s %a'. Transcript cr.   
+     Time now printOn:Transcript format:'%h:%m'      . Transcript cr. 
+     Time now printOn:Transcript format:'%H:%m %A'   . Transcript cr.
+     Time now printOn:Transcript format:'minutes:%M seconds:%S'   . Transcript cr.
+    "
+
+    "Modified: 22.2.1996 / 16:58:30 / cg"
+!
+
+printStringFormat:aFormatString
+    "print using a format string;
+     valid format items are:
+        %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
+        %a      am/pm
+
+     special:
+        %H      24-hours - unpadded
+        %U      12-hours - unpadded
+        %M      minutes - unpadded
+        %S      seconds - unpadded
+        %A      AM/PM   - uppercase
+
+        %t      seconds within hour  (unpadded)
+        %T      seconds from midNight  (unpadded)
+    "
+
+    |dict hours minutes seconds usHours ampm s|
+
+    hours := self hours.
+    minutes := self minutes.
+    seconds := self seconds.
+
+    hours // 12 == 0 ifTrue:[
+        ampm := 'am'.
+    ] ifFalse:[
+        ampm := 'pm'.
+    ].
+    usHours := hours.
+    usHours ~~ 0 ifTrue:[
+        usHours := usHours - 1 \\ 12 + 1.
+    ].
+
+    dict := IdentityDictionary new.
+    dict at:$H put:(s := hours printString).
+    dict at:$h put:(s leftPaddedTo:2 with:$0).
+    dict at:$U put:(s := usHours printString).
+    dict at:$u put:(s leftPaddedTo:2 with:$0).
+    dict at:$M put:(s := minutes printString).
+    dict at:$m put:(s leftPaddedTo:2 with:$0).
+    dict at:$S put:(s := seconds printString).
+    dict at:$s put:(s leftPaddedTo:2 with:$0).
+    dict at:$t put:(seconds * minutes) printString.
+    dict at:$T put:(seconds * minutes * hours) printString.
+    dict at:$a put:ampm.
+    dict at:$A put:ampm asUppercase.
+
+    ^ (aFormatString expandPlaceholdersWith:dict)
+
+    "
+     Time now printStringFormat:'%U:%m:%s %a'   
+
+     Time now printStringFormat:'%h:%m:%s'     
+     Time now printStringFormat:'%H:%m:%s'      
+     Time now printStringFormat:'%u:%m:%s %a'   
+     Time now printStringFormat:'%h:%m'         
+     Time now printStringFormat:'%H:%m %A'     
+     Time now printStringFormat:'%m minutes after %U %a'     
+     Time now printStringFormat:'%t seconds after %U %a'     
+     Time now printStringFormat:'%T seconds from midNight'     
+    "
+
+    "Modified: 22.2.1996 / 16:58:30 / cg"
+! !
+
 !AbstractTime methodsFor:'private'!
 
 fromOSTime:osTime
@@ -455,5 +556,5 @@
 !AbstractTime class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.15 1998-05-22 10:47:19 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.16 1999-09-01 18:18:03 stefan Exp $'
 ! !