Time.st
changeset 6165 0ac589c3d92a
parent 6164 8335251d4cf9
child 6467 f200ac48f2a8
equal deleted inserted replaced
6164:8335251d4cf9 6165:0ac589c3d92a
     1 "
     1 "
     2  COPYRIGHT (c) 1989 by Claus Gittinger
     2  COPYRIGHT (c) 1989 by Claus Gittinger
     3               All Rights Reserved
     3 	      All Rights Reserved
     4 
     4 
     5  This software is furnished under a license and may be used
     5  This software is furnished under a license and may be used
     6  only in accordance with the terms of that license and with the
     6  only in accordance with the terms of that license and with the
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 "{ Package: '__NoProject__' }"
    13 "{ Package: 'stx:libbasic' }"
    14 
    14 
    15 Time subclass:#TimeDuration
    15 AbstractTime subclass:#Time
    16         instanceVariableNames:''
    16 	instanceVariableNames:'timeEncoding'
    17         classVariableNames:''
    17 	classVariableNames:''
    18         poolDictionaries:''
    18 	poolDictionaries:''
    19         category:'Magnitude-General'
    19 	category:'Magnitude-General'
    20 !
    20 !
    21 
    21 
    22 !TimeDuration class methodsFor:'documentation'!
    22 !Time class methodsFor:'documentation'!
    23 
    23 
    24 copyright
    24 copyright
    25 "
    25 "
    26  COPYRIGHT (c) 1989 by Claus Gittinger
    26  COPYRIGHT (c) 1989 by Claus Gittinger
    27               All Rights Reserved
    27 	      All Rights Reserved
    28 
    28 
    29  This software is furnished under a license and may be used
    29  This software is furnished under a license and may be used
    30  only in accordance with the terms of that license and with the
    30  only in accordance with the terms of that license and with the
    31  inclusion of the above copyright notice.   This software may not
    31  inclusion of the above copyright notice.   This software may not
    32  be provided or otherwise made available to, or used by, any
    32  be provided or otherwise made available to, or used by, any
    65         Date AbsoluteTime AbstractTime OperatingSystem
    65         Date AbsoluteTime AbstractTime OperatingSystem
    66         Filename
    66         Filename
    67 "
    67 "
    68 ! !
    68 ! !
    69 
    69 
    70 !TimeDuration class methodsFor:'instance creation'!
    70 !Time class methodsFor:'instance creation'!
    71 
    71 
    72 hours:h minutes:m seconds:s millis:millis
    72 fromSeconds:secondsSinceMidnight
       
    73     "return a new Time, which represents the number of seconds since midNight.
       
    74      Be careful, time wraps - if the number of seconds exceeds the number of seconds
       
    75      in a day, a time relative to another days midnight is returned.
       
    76      (i.e. (Time fromSeconds:0) = (Time fromSeconds:24*3600)"
       
    77 
       
    78     |s|
       
    79 
       
    80     s := secondsSinceMidnight \\ (3600 * 24).
       
    81     ^ self
       
    82         hours:(s // 3600)
       
    83         minutes:((s \\ 3600) // 60)
       
    84         seconds:((s \\ 3600) \\ 60)
       
    85 
       
    86     "
       
    87      Time fromSeconds:0     
       
    88      Time fromSeconds:3675     
       
    89      Time fromSeconds:36000     
       
    90      Time fromSeconds:72000     
       
    91      Time fromSeconds:96000      
       
    92     "
       
    93 
       
    94     "Modified: 8.10.1996 / 19:32:11 / cg"
       
    95 
       
    96 
       
    97 
       
    98 !
       
    99 
       
   100 hour:h minutes:m seconds:s
       
   101     "return an instance of Time representing the given time.
       
   102      See also Time now / Date today / AbsoluteTime now.
       
   103      Obsolete: please use #hours:minutes:seconds:"
       
   104 
       
   105     <resource:#obsolete>
       
   106 
       
   107     self obsoleteMethodWarning:'use hours:minutes:seconds:'.
       
   108 
       
   109     ^ self hours:h minutes:m seconds:s
       
   110 
       
   111     "
       
   112      Time hours:2 minutes:33 seconds:0 
       
   113      Time hours:0 minutes:0 seconds:0 
       
   114      Time hours:24 minutes:0 seconds:0 
       
   115      Time hours23 minutes:59 seconds:59 
       
   116     "
       
   117 
       
   118     "Modified: 19.4.1996 / 15:32:40 / cg"
       
   119 !
       
   120 
       
   121 hours:h minutes:m seconds:s
    73     "return an instance of Time representing the given time.
   122     "return an instance of Time representing the given time.
    74      See also Time now / Date today / AbsoluteTime now."
   123      See also Time now / Date today / AbsoluteTime now."
    75 
   124 
    76     ^ self basicNew setHours:h minutes:m seconds:s millis:millis
   125     ^ self basicNew setHours:h minutes:m seconds:s
    77 
   126 
    78     "
   127     "
    79      TimeDuration hours:2 minutes:33 seconds:0 millis:123
   128      Time hours:2 minutes:33 seconds:0 
    80     "
   129      Time hours:0 minutes:0 seconds:0 
    81 ! !
   130      Time hours:24 minutes:0 seconds:0 
    82 
   131      Time hours:23 minutes:59 seconds:59 
    83 !TimeDuration class methodsFor:'format strings'!
   132     "
       
   133 
       
   134     "Modified: 19.4.1996 / 15:32:40 / cg"
       
   135 !
       
   136 
       
   137 readFrom:aStringOrStream onError:exceptionBlock
       
   138     "return a new Time, reading a printed representation from aStream.
       
   139      If no am/pm follows the time, the string is interpreted as 
       
   140      either 24 hour format or being am."
       
   141 
       
   142     |newTime|
       
   143 
       
   144     ErrorSignal handle:[:ex |
       
   145         ^ exceptionBlock value
       
   146     ] do:[
       
   147         |str hour min sec|
       
   148 
       
   149         str := aStringOrStream readStream.
       
   150 
       
   151         hour := Integer readFrom:str.
       
   152         (hour between:0 and:24) ifFalse:[^ exceptionBlock value].
       
   153 
       
   154         min := 0.
       
   155         sec := 0.
       
   156         str atEnd ifFalse:[
       
   157             (str peek == $:) ifTrue:[
       
   158                 str next.
       
   159                 min := Integer readFrom:str.
       
   160                 (min between:0 and:59) ifFalse:[^ exceptionBlock value].
       
   161 
       
   162                 (str peek == $:) ifTrue:[
       
   163                     str next.
       
   164                     sec := Integer readFrom:str.
       
   165                     (sec between:0 and:59) ifFalse:[^ exceptionBlock value].
       
   166                 ].
       
   167             ].
       
   168             [str peek == Character space] whileTrue:[str next].
       
   169             (str peek == $p or:[str peek == $P]) ifTrue:[
       
   170                 str next.
       
   171                 (str peek == $m or:[str peek == $M]) ifTrue:[
       
   172                     str next
       
   173                 ].
       
   174                 (hour <= 0 or:[hour > 12]) ifTrue:[^ exceptionBlock value].
       
   175 
       
   176                 "pm"
       
   177                 hour ~~ 12 ifTrue:[
       
   178                     hour := hour + 12
       
   179                 ]
       
   180             ] ifFalse:[
       
   181                 (str peek == $a or:[str peek == $A]) ifTrue:[
       
   182                     str next.
       
   183                     (str peek == $m or:[str peek == $M]) ifTrue:[
       
   184                         str next
       
   185                     ].
       
   186                     hour >= 12 ifTrue:[^ exceptionBlock value].
       
   187                     hour == 24 ifTrue:[
       
   188                         hour := 0.
       
   189                     ]
       
   190                 ]
       
   191             ]
       
   192         ].
       
   193         newTime := self basicNew setHours:hour minutes:min seconds:sec
       
   194     ].
       
   195     ^ newTime
       
   196 
       
   197     "
       
   198      Time readFrom:'0:00'     
       
   199      Time readFrom:'2:00'     
       
   200      Time readFrom:'12:00'    
       
   201      Time readFrom:'14:00'    
       
   202      Time readFrom:'23:00'    
       
   203      Time readFrom:'24:00'    
       
   204      Time readFrom:'2:30 am'    
       
   205      Time readFrom:'2:30 pm'    
       
   206      Time readFrom:'14'    
       
   207      Time readFrom:'2 am'    
       
   208      Time readFrom:'2 pm'    
       
   209 
       
   210      Time readFrom:'18:22:00'    
       
   211      Time readFrom:'14:00:11'    
       
   212      Time readFrom:'7:00:11'     
       
   213      Time readFrom:'24:00:00'     
       
   214      Time readFrom:'0:00:00'     
       
   215      Time readFrom:'12:00:00'     
       
   216      Time readFrom:'0:00:00'     
       
   217      Time readFrom:'6:22:00 pm'   
       
   218      Time readFrom:'2:00:11 pm'  
       
   219      Time readFrom:'7:00:11 am'  
       
   220      Time readFrom:'12:00:00 am'  
       
   221      Time readFrom:'0:00:00 am'  
       
   222      Time readFrom:'24:00:00 am'  
       
   223      Time readFrom:'12:00:00 pm'  
       
   224      Time readFrom:'0:00:00 pm'   - invalid
       
   225      Time readFrom:'24:00:00 pm'  
       
   226     "
       
   227 
       
   228     "Modified: 8.10.1996 / 19:32:11 / cg"
       
   229 ! !
       
   230 
       
   231 !Time class methodsFor:'format strings'!
    84 
   232 
    85 formatString12us
   233 formatString12us
    86     "return the format string used to format US times (and other areas)"
   234     "return the format string used to format US times (and other areas)"
    87 
   235 
    88     ^ '%u:%m:%s.%i %a'
   236     ^ '%u:%m:%s %a'
    89 !
   237 !
    90 
   238 
    91 formatString24
   239 formatString24
    92     "return the format string used to format european times (and other areas)"
   240     "return the format string used to format european times (and other areas)"
    93 
   241 
   211 !Time methodsFor:'comparing'!
   359 !Time methodsFor:'comparing'!
   212 
   360 
   213 < aTime
   361 < aTime
   214     "return true if the receiver is before the argument"
   362     "return true if the receiver is before the argument"
   215 
   363 
   216     ^ timeEncoding < aTime timeEncoding
   364     ^ self getSeconds < aTime getSeconds
   217 !
   365 !
   218 
   366 
   219 = aTime
   367 = aTime
   220     "return true if the argument, aTime represents the same timeOfDay"
   368     "return true if the argument, aTime represents the same timeOfDay"
   221 
   369 
   222     aTime class == self class ifTrue:[
   370     aTime class == self class ifTrue:[
   223         ^ timeEncoding == aTime timeEncoding
   371         ^ timeEncoding == aTime timeEncoding
   224     ].
   372     ].
   225     (aTime species == self species) ifFalse:[^ false].
   373     (aTime species == self species) ifFalse:[^ false].
   226     ^ self asSeconds == aTime asSeconds
   374     ^ self getSeconds == aTime getSeconds
   227 !
   375 !
   228 
   376 
   229 > aTime
   377 > aTime
   230     "return true if the receiver is before the argument"
   378     "return true if the receiver is before the argument"
   231 
   379 
   232     ^ timeEncoding > aTime timeEncoding
   380     ^ self getSeconds > aTime getSeconds
   233 !
   381 !
   234 
   382 
   235 hash
   383 hash
   236     "return an integer useful for hashing on times"
   384     "return an integer useful for hashing on times"
   237 
   385 
   238     ^ timeEncoding
   386     ^ self getSeconds
   239 ! !
   387 ! !
   240 
   388 
   241 !Time methodsFor:'converting'!
   389 !Time methodsFor:'converting'!
   242 
   390 
   243 asAbsoluteTime
   391 asAbsoluteTime
   420 !
   568 !
   421 
   569 
   422 getMilliseconds
   570 getMilliseconds
   423     "return the number of milliseconds since midnight"
   571     "return the number of milliseconds since midnight"
   424 
   572 
   425     ^ timeEncoding
   573     ^ self getSeconds * 1000
   426 !
   574 !
   427 
   575 
   428 getSeconds
   576 getSeconds
   429     "return the number of seconds since midnight"
   577     "return the number of seconds since midnight"
   430 
   578 
   431     ^ timeEncoding // 1000
   579     ^ timeEncoding
   432 !
   580 !
   433 
   581 
   434 setHours:h minutes:m seconds:s millis:millis
   582 setHours:h minutes:m seconds:s
   435     "set my time given individual values"
   583     "set my time given individual values"
   436 
   584 
   437     self setMilliseconds:(((h\\24) * 60 * 60 ) + (m * 60) + s) * 1000 + millis.
   585     self setSeconds:(((h\\24) * 60 * 60 ) + (m * 60) + s).
   438 !
   586 !
   439 
   587 
   440 setMilliseconds:millis
   588 setMilliseconds:millis
   441     "set my time given milliseconds since midnight"
   589     "set my time given milliseconds since midnight"
   442 
   590 
   443     timeEncoding := millis
   591     self setSeconds:(millis // 1000)
   444 !
   592 !
   445 
   593 
   446 setSeconds:secs
   594 setSeconds:secs
   447     "set my time given seconds since midnight"
   595     "set my time given seconds since midnight"
   448 
   596 
   449     self setMilliseconds:(secs * 1000).
   597     secs < 0 ifTrue:[
   450 ! !
   598         timeEncoding := (24 * 3600) - (secs negated \\ (24 * 3600))
   451 
   599     ] ifFalse:[
   452 !TimeDuration class methodsFor:'documentation'!
   600         timeEncoding := secs
       
   601     ].
       
   602     timeEncoding > (24 * 3600) ifTrue:[
       
   603         timeEncoding := timeEncoding \\ (24 * 3600).
       
   604     ]
       
   605 
       
   606     "
       
   607      Time now seconds
       
   608      Time now timeEncoding
       
   609      (Time now addDays:5) seconds     
       
   610      (Time now addDays:5) timeEncoding
       
   611     "
       
   612 !
       
   613 
       
   614 timeEncoding
       
   615     "the internal encoding is stricktly private, 
       
   616      and should not be used outside."
       
   617 
       
   618     ^ timeEncoding
       
   619 !
       
   620 
       
   621 timeEncoding:encoding
       
   622     "the internal encoding is stricktly private, 
       
   623      and should not be used outside."
       
   624 
       
   625     timeEncoding := encoding
       
   626 ! !
       
   627 
       
   628 !Time class methodsFor:'documentation'!
   453 
   629 
   454 version
   630 version
   455     ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.51 2001-11-08 16:04:46 md Exp $'
   631     ^ '$Header: /cvs/stx/stx/libbasic/Time.st,v 1.52 2001-11-08 16:07:36 md Exp $'
   456 ! !
   632 ! !