home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / gnusmalltalk / time.st < prev    next >
Text File  |  1992-02-15  |  4KB  |  164 lines

  1. "======================================================================
  2. |
  3. |   Time Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991, 1992 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbb         12 Jul 91      Added methods for comparing, including #= and #hash.
  34. |
  35. | sbb         16 Mar 91      Class creation now separate statement.
  36. |
  37. | sbyrne     24 Apr 90      Fixed printOn: to print the instance time, instead of
  38. |              now, and added printOn: for Time class to print now.
  39. |
  40. | sbyrne     19 Sep 89      Changed to use real method categories.
  41. |
  42. | sbyrne     12 Aug 89      Implemented many methods.  The book is exceptionally
  43. |              vague here, so please feel free to change the
  44. |              behavior to something which is more correct.
  45. |
  46. | sbyrne     25 Apr 89      created.
  47. |
  48. "
  49.  
  50. Magnitude subclass: #Time
  51.       instanceVariableNames: 'seconds'
  52.       classVariableNames: ''
  53.       poolDictionaries: ''
  54.       category: nil
  55. !
  56.  
  57. Time comment: 
  58. 'My instances represent times of the day.  I provide methods for instance 
  59. creation, methods that access components (hours, minutes, and seconds) of a 
  60. time value, and a block execution timing facility.' !
  61.  
  62. !Time class methodsFor: 'basic'!
  63.  
  64. now
  65.     ^self new setSeconds: Time secondClock
  66. !
  67.  
  68. fromSeconds: secondCount
  69.     ^self new setSeconds: (Time secondClock \\ (24*60*60)) + secondCount
  70. !
  71.  
  72. millisecondClockValue
  73.     ^self millisecondClock
  74. !
  75.  
  76. millisecondsToRun: timedBlock
  77.     | startTime|
  78.     startTime _ self millisecondClock.
  79.     timedBlock value.
  80.     ^self millisecondClock - startTime
  81. !!
  82.  
  83.  
  84.  
  85. !Time methodsFor: 'accessing'!
  86.  
  87. hours
  88.     ^(seconds // (60*60)) \\ 24
  89. !
  90.  
  91. minutes
  92.     ^(seconds // 60) \\ 60
  93. !
  94.  
  95. seconds
  96.     ^seconds \\ 60
  97. !!
  98.  
  99.  
  100.  
  101. !Time methodsFor: 'comparing'!
  102.  
  103. = aTime
  104.     ^seconds = aTime time
  105. !
  106.  
  107. < aTime
  108.     ^seconds < aTime time
  109. !
  110.  
  111. > aTime
  112.     ^seconds > aTime time
  113. !
  114.     
  115. <= aTime
  116.     ^seconds <= aTime time
  117. !
  118.  
  119. >= aTime
  120.     ^seconds >= aTime time
  121. !
  122.  
  123. hash
  124.     ^seconds
  125. !!
  126.  
  127.  
  128.  
  129.  
  130.  
  131. !Time methodsFor: 'arithmetic'!
  132.  
  133. addTime: timeAmount
  134.     ^Time new setSeconds: seconds + timeAmount
  135. !
  136.  
  137. subtractTime: timeAmount
  138.     ^Time new setSeconds: seconds - timeAmount
  139. !
  140.  
  141. printOn: aStream
  142.     self hours printOn: aStream.
  143.     aStream nextPut: $:.
  144.     self minutes < 10 ifTrue: [ aStream nextPut: $0 ].
  145.     self minutes printOn: aStream.
  146.     aStream nextPut: $:.
  147.     self seconds < 10 ifTrue: [ aStream nextPut: $0 ].
  148.     self seconds printOn: aStream
  149. !!
  150.  
  151.  
  152.  
  153. !Time methodsFor: 'private'!
  154.  
  155. setSeconds: secs
  156.     seconds _ secs
  157. !
  158.  
  159. time
  160.     ^seconds
  161. !!
  162.