AbstractTime.st
changeset 6172 a3c88ea5efe9
parent 6048 af4b1127f362
child 6419 5cf00f5e656d
equal deleted inserted replaced
6171:cf2befb8562e 6172:a3c88ea5efe9
    47 
    47 
    48     [See also:]
    48     [See also:]
    49         Time Date AbsoluteTime
    49         Time Date AbsoluteTime
    50         Delay ProcessorScheduler
    50         Delay ProcessorScheduler
    51 "
    51 "
       
    52 !
       
    53 
       
    54 iso8601Format
       
    55 "
       
    56   Abstract
       
    57 
       
    58     This document defines a profile of ISO 8601, the International Standard for the representation of dates and times. ISO
       
    59     8601 describes a large number of date/time formats. To reduce the scope for error and the complexity of software, it is
       
    60     useful to restrict the supported formats to a small number. This profile defines a few date/time formats, likely to satisfy
       
    61     most requirements. 
       
    62 
       
    63 
       
    64   Formats
       
    65 
       
    66     Different standards may need different levels of granularity in the date and time, so this profile defines six levels.
       
    67     Standards that reference this profile should specify one or more of these granularities. If a given standard allows more
       
    68     than one granularity, it should specify the meaning of the dates and times with reduced precision, for example, the result
       
    69     of comparing two dates with different precisions.
       
    70 
       
    71     The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note
       
    72     that the 'T' appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601. 
       
    73 
       
    74        Year:
       
    75           YYYY (eg 1997)
       
    76        Year and month:
       
    77           YYYY-MM (eg 1997-07)
       
    78        Complete date:
       
    79           YYYY-MM-DD (eg 1997-07-16)
       
    80        Complete date plus hours and minutes:
       
    81           YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
       
    82        Complete date plus hours, minutes and seconds:
       
    83           YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
       
    84        Complete date plus hours, minutes, seconds and a decimal fraction of a
       
    85     second
       
    86           YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
       
    87 
       
    88     where:
       
    89 
       
    90          YYYY = four-digit year
       
    91          MM   = two-digit month (01=January, etc.)
       
    92          DD   = two-digit day of month (01 through 31)
       
    93          hh   = two digits of hour (00 through 23) (am/pm NOT allowed)
       
    94          mm   = two digits of minute (00 through 59)
       
    95          ss   = two digits of second (00 through 59)
       
    96          s    = one or more digits representing a decimal fraction of a second
       
    97          TZD  = time zone designator (Z or +hh:mm or -hh:mm)
       
    98 
       
    99     This profile does not specify how many digits may be used to represent the decimal fraction of a second. An adopting
       
   100     standard that permits fractions of a second must specify both the minimum number of digits (a number greater than or
       
   101     equal to one) and the maximum number of digits (the maximum may be stated to be 'unlimited').
       
   102 
       
   103     This profile defines two ways of handling time zone offsets:
       
   104 
       
   105        1.Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ('Z'). 
       
   106        2.Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of
       
   107          '+hh:mm' indicates that the date/time uses a local time zone which is 'hh' hours and 'mm' minutes ahead of
       
   108          UTC. A time zone offset of '-hh:mm' indicates that the date/time uses a local time zone which is 'hh' hours and
       
   109          'mm' minutes behind UTC. 
       
   110 
       
   111     A standard referencing this profile should permit one or both of these ways of handling time zone offsets.
       
   112 
       
   113   The ISO8601 printString are generated with:
       
   114 
       
   115        Year:
       
   116           YYYY (eg 1997)
       
   117                 Date today printStringFormat:'%(year)'
       
   118                 AbsoluteTime now printStringFormat:'%(year)'  
       
   119 
       
   120        Year and month:
       
   121           YYYY-MM (eg 1997-07)
       
   122                 Date today printStringFormat:'%(year)-%(month)'  
       
   123                 AbsoluteTime now printStringFormat:'%(year)-%(month)'  
       
   124 
       
   125        Complete date:
       
   126           YYYY-MM-DD (eg 1997-07-16)
       
   127                 Date today printStringFormat:'%(year)-%(month)-%(day)'    
       
   128                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)'  
       
   129 
       
   130        Complete date plus hours and minutes:
       
   131           YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
       
   132                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)T%h:%m%(TZD)'  
       
   133 
       
   134        Complete date plus hours, minutes and seconds:
       
   135           YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
       
   136                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s%(TZD)'  
       
   137 
       
   138        Complete date plus hours, minutes, seconds and a decimal fraction of a second
       
   139           YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
       
   140                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s.%(milli2)%(TZD)'  
       
   141 
       
   142 
       
   143 "
    52 ! !
   144 ! !
    53 
   145 
    54 !AbstractTime class methodsFor:'instance creation'!
   146 !AbstractTime class methodsFor:'instance creation'!
    55 
   147 
    56 dateAndTimeNow
   148 dateAndTimeNow
   180     "
   272     "
   181      Time secondsToRun:[1000 factorial]  
   273      Time secondsToRun:[1000 factorial]  
   182     "
   274     "
   183 ! !
   275 ! !
   184 
   276 
       
   277 !AbstractTime methodsFor:'abstract'!
       
   278 
       
   279 hours
       
   280     "return the hours (0..23)"
       
   281 
       
   282     ^ self subclassResponsibility
       
   283 
       
   284     "
       
   285      AbsoluteTime now hours 
       
   286      Time now hours 
       
   287     "
       
   288 !
       
   289 
       
   290 milliseconds
       
   291     "return the milliseconds (0..999)"
       
   292 
       
   293     ^ self subclassResponsibility
       
   294 
       
   295     "
       
   296      AbsoluteTime now milliseconds 
       
   297      Time now milliseconds 
       
   298     "
       
   299 !
       
   300 
       
   301 minutes
       
   302     "return the minutes (0..59)"
       
   303 
       
   304     ^ self minutes.
       
   305 
       
   306     "
       
   307      AbsoluteTime now minutes 
       
   308      Time now minutes 
       
   309     "
       
   310 !
       
   311 
       
   312 seconds
       
   313     "return the seconds (0..59)"
       
   314 
       
   315     ^ self subclassResponsibility
       
   316 
       
   317     "
       
   318      AbsoluteTime now seconds 
       
   319      Time now seconds 
       
   320     "
       
   321 ! !
       
   322 
   185 !AbstractTime methodsFor:'accessing'!
   323 !AbstractTime methodsFor:'accessing'!
   186 
   324 
   187 hourInDay
   325 hourInDay
   188     "return the hours (0..23)"
   326     "return the hours (0..23)"
   189 
   327 
   215      AbsoluteTime now secondInDay 
   353      AbsoluteTime now secondInDay 
   216      Time now seconds 
   354      Time now seconds 
   217     "
   355     "
   218 
   356 
   219     "Created: 22.10.1996 / 09:27:47 / stefan"
   357     "Created: 22.10.1996 / 09:27:47 / stefan"
       
   358 !
       
   359 
       
   360 timeZoneDeltaInMinutes
       
   361     ^ 0
   220 !
   362 !
   221 
   363 
   222 timeZoneName
   364 timeZoneName
   223     ^ 'utc'
   365     ^ 'utc'
   224 !
   366 !
   504 !AbstractTime methodsFor:'printing & storing'!
   646 !AbstractTime methodsFor:'printing & storing'!
   505 
   647 
   506 addPrintBindingsTo:aDictionary
   648 addPrintBindingsTo:aDictionary
   507     "add bindings for printing to aDictionary."
   649     "add bindings for printing to aDictionary."
   508 
   650 
   509     |hours minutes seconds millis usHours ampm s zone|
   651     |hours minutes seconds millis usHours ampm s zone tzDelta|
   510 
   652 
   511     hours := self hours.
   653     hours := self hours.
   512     minutes := self minutes.
   654     minutes := self minutes.
   513     seconds := self seconds.
   655     seconds := self seconds.
   514     millis := self milliseconds.
   656     millis := self milliseconds.
   515     zone := self timeZoneName.
   657     zone := self timeZoneName.
       
   658     tzDelta := self timeZoneDeltaInMinutes.
   516 
   659 
   517     hours // 12 == 0 ifTrue:[
   660     hours // 12 == 0 ifTrue:[
   518 	ampm := 'am'.
   661         ampm := 'am'.
   519     ] ifFalse:[
   662     ] ifFalse:[
   520 	ampm := 'pm'.
   663         ampm := 'pm'.
   521     ].
   664     ].
   522     usHours := hours.
   665     usHours := hours.
   523     usHours ~~ 0 ifTrue:[
   666     usHours ~~ 0 ifTrue:[
   524 	usHours := usHours - 1 \\ 12 + 1.
   667         usHours := usHours - 1 \\ 12 + 1.
   525     ].
   668     ].
   526 
   669 
   527     aDictionary at:$H put:(s := hours printString).
   670     aDictionary at:$H put:(s := hours printString).
   528     aDictionary at:$h put:(s leftPaddedTo:2 with:$0).
   671     aDictionary at:$h put:(s leftPaddedTo:2 with:$0).
   529     aDictionary at:$U put:(s := usHours printString).
   672     aDictionary at:$U put:(s := usHours printString).
   540     aDictionary at:$T put:(seconds * minutes * hours) printString.
   683     aDictionary at:$T put:(seconds * minutes * hours) printString.
   541     aDictionary at:$a put:ampm.
   684     aDictionary at:$a put:ampm.
   542     aDictionary at:$A put:ampm asUppercase.
   685     aDictionary at:$A put:ampm asUppercase.
   543     aDictionary at:$z put:zone.
   686     aDictionary at:$z put:zone.
   544     aDictionary at:$Z put:zone asUppercase.
   687     aDictionary at:$Z put:zone asUppercase.
       
   688 
       
   689     s := tzDelta >= 0 ifTrue:[ '+' ] ifFalse:[ '-' ].
       
   690     tzDelta := tzDelta abs.
       
   691     s := s , ((tzDelta // 60) printStringLeftPaddedTo:2 with:$0).
       
   692     s := s , ':'.
       
   693     s := s , ((tzDelta \\ 60) printStringLeftPaddedTo:2 with:$0).
       
   694     aDictionary at:#TZD put:s
   545 !
   695 !
   546 
   696 
   547 printOn:aStream format:aFormatString
   697 printOn:aStream format:aFormatString
   548     "print using a format string;
   698     "print using a format string;
   549      valid format items are:
   699      valid format items are:
   599         %s      seconds, 00..59                0-padded to length 2
   749         %s      seconds, 00..59                0-padded to length 2
   600         %i      milliseconds, 000..999         0-padded to length 3
   750         %i      milliseconds, 000..999         0-padded to length 3
   601         %a      am/pm
   751         %a      am/pm
   602 
   752 
   603      AbsoluteTime only:
   753      AbsoluteTime only:
   604         %day     day, 00..31                    0-padded to length 2
   754         %(day)   day, 00..31                    0-padded to length 2
   605         %month   month, 00..12                  0-padded to length 2
   755         %(month) month, 00..12                  0-padded to length 2
   606         %year    year, 4 digits                 0-padded to length 4
   756         %(year)  year, 4 digits                 0-padded to length 4
   607 
   757 
   608      special:
   758      special:
   609         %H      24-hours - unpadded
   759         %H      24-hours - unpadded
   610         %U      12-hours - unpadded
   760         %U      12-hours - unpadded
   611         %M      minutes - unpadded
   761         %M      minutes - unpadded
   614         %A      AM/PM   - uppercase
   764         %A      AM/PM   - uppercase
   615 
   765 
   616         %t      seconds within hour  (unpadded)
   766         %t      seconds within hour  (unpadded)
   617         %T      seconds from midNight  (unpadded)
   767         %T      seconds from midNight  (unpadded)
   618 
   768 
       
   769         %(TZD)  timeZone delta from UTC in the format +/-hh:mm  
       
   770 
   619         %milli1 milliseconds, truncated to 1/10th of a second 0..9         
   771         %milli1 milliseconds, truncated to 1/10th of a second 0..9         
   620         %milli2 milliseconds, truncated to 1/100th of a second 00..99 0-padded to length 2        
   772         %milli2 milliseconds, truncated to 1/100th of a second 00..99 0-padded to length 2        
   621 
   773 
   622      AbsoluteTime only:
   774      AbsoluteTime only:
   623         %Day    day - unpadded                    
   775         %Day    day - unpadded                    
   624         %Month  month - unpadded                    
   776         %Month  month - unpadded                    
       
   777 
       
   778 
       
   779      The ISO8601 printString are generated with:
       
   780 
       
   781        Year:
       
   782           YYYY (eg 1997)
       
   783                 Date today printStringFormat:'%(year)'
       
   784                 AbsoluteTime now printStringFormat:'%(year)'  
       
   785 
       
   786        Year and month:
       
   787           YYYY-MM (eg 1997-07)
       
   788                 Date today printStringFormat:'%(year)-%(month)'  
       
   789                 AbsoluteTime now printStringFormat:'%(year)-%(month)'  
       
   790 
       
   791        Complete date:
       
   792           YYYY-MM-DD (eg 1997-07-16)
       
   793                 Date today printStringFormat:'%(year)-%(month)-%(day)'    
       
   794                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)'  
       
   795 
       
   796        Complete date plus hours and minutes:
       
   797           YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
       
   798                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)T%h:%m%(TZD)'  
       
   799 
       
   800        Complete date plus hours, minutes and seconds:
       
   801           YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
       
   802                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s%(TZD)'  
       
   803 
       
   804        Complete date plus hours, minutes, seconds and a decimal fraction of a second
       
   805           YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
       
   806                 AbsoluteTime now printStringFormat:'%(year)-%(month)-%(day)T%h:%m:%s.%(milli2)%(TZD)'  
       
   807 
   625     "
   808     "
   626 
   809 
   627     |dict|
   810     |dict|
   628 
   811 
   629     dict := IdentityDictionary new.
   812     dict := IdentityDictionary new.
   702 ! !
   885 ! !
   703 
   886 
   704 !AbstractTime class methodsFor:'documentation'!
   887 !AbstractTime class methodsFor:'documentation'!
   705 
   888 
   706 version
   889 version
   707     ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.33 2001-09-26 13:11:37 cg Exp $'
   890     ^ '$Header: /cvs/stx/stx/libbasic/AbstractTime.st,v 1.34 2001-11-09 15:18:46 cg Exp $'
   708 ! !
   891 ! !