/src/lib/time/microsecond_time.e

http://github.com/tybor/Liberty · Specman e · 197 lines · 140 code · 21 blank · 36 comment · 2 complexity · 0587ea6e253f6e852ed019a37af6f204 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. expanded class MICROSECOND_TIME
  5. --
  6. -- Date and time facilities (like TIME) plus an extra microsecond information.
  7. --
  8. insert
  9. HASHABLE
  10. redefine out
  11. end
  12. COMPARABLE
  13. redefine is_equal, out
  14. end
  15. TIME_HANDLER
  16. redefine is_equal, out
  17. end
  18. feature {ANY}
  19. time: TIME
  20. -- The normal TIME with second accuracy.
  21. microsecond: INTEGER
  22. -- Extra information in number of microseconds in range 0 .. 999999.
  23. -- Note that the accuracy is system dependant.
  24. update
  25. -- Update `Current' with the current system clock.
  26. do
  27. basic_microsecond_update
  28. time.set_time_memory(basic_microsecond_time)
  29. microsecond := basic_microsecond_microsecond
  30. end
  31. set_time (t: like time)
  32. do
  33. time := t
  34. ensure
  35. time = t
  36. end
  37. set_microsecond (microsec: INTEGER)
  38. -- To set `microsecond' in range 0 .. 999 999.
  39. require
  40. microsec.in_range(0, 999999)
  41. do
  42. microsecond := microsec
  43. ensure
  44. microsecond = microsec
  45. end
  46. infix "+" (s: REAL): like Current
  47. -- Add `s' seconds (2.476 is 2 seconds and 476 milliseconds)
  48. require
  49. s >= 0.0
  50. local
  51. a, b: INTEGER
  52. do
  53. a := s.force_to_integer_32
  54. b := ((s - a) * 1000000).force_to_integer_32
  55. Result := Current
  56. Result.add_second(a)
  57. Result.add_microsecond(b)
  58. end
  59. add_second (s: INTEGER)
  60. -- Add `s' seconds to `Current'.
  61. require
  62. s >= 0
  63. do
  64. time.add_second(s)
  65. ensure
  66. Current >= old Current
  67. end
  68. add_millisecond (millisecond: INTEGER)
  69. -- Add `millisecond' milliseconds.
  70. require
  71. millisecond.in_range(0, 999)
  72. do
  73. add_microsecond(millisecond * 1000)
  74. ensure
  75. Current >= old Current
  76. end
  77. add_microsecond (microsec: INTEGER)
  78. -- Add `microsec' microseconds
  79. require
  80. microsec.in_range(0, 999999)
  81. local
  82. a: INTEGER
  83. do
  84. a := microsec + microsecond
  85. if a >= 1000000 then
  86. add_second(1)
  87. a := a - 1000000
  88. end
  89. microsecond := a
  90. ensure
  91. Current >= old Current
  92. end
  93. elapsed_seconds (other: like Current): REAL_64
  94. -- Elapsed time in seconds from `Current' to `other' with sub-second precision.
  95. do
  96. Result := time.elapsed_seconds(other.time)
  97. Result := Result + (other.microsecond - microsecond).to_real_64 / 1000000
  98. end
  99. is_equal (other: like Current): BOOLEAN
  100. do
  101. Result := other.time = time and then other.microsecond = microsecond
  102. end
  103. infix "<" (other: like Current): BOOLEAN
  104. do
  105. Result := time < other.time or else time = other.time and then microsecond < other.microsecond
  106. ensure then
  107. Result implies elapsed_seconds(other) > 0
  108. end
  109. hash_code: INTEGER
  110. do
  111. Result := time.hash_code.bit_xor(microsecond)
  112. end
  113. out: STRING
  114. local
  115. mic: STRING
  116. do
  117. Result := time.out
  118. Result.extend('.')
  119. mic := once "............"
  120. mic.copy(once "000000")
  121. microsecond.append_in(mic)
  122. Result.append_substring(mic, mic.upper - 5, mic.upper)
  123. end
  124. feature {ANY}
  125. timestamp: INTEGER_64
  126. do
  127. Result := time.time_memory * 1000000 + microsecond
  128. end
  129. feature {}
  130. basic_microsecond_time: INTEGER_64
  131. external "plug_in"
  132. alias "{
  133. location: "${sys}/runtime"
  134. module_name: "basic_microsecond"
  135. feature_name: "basic_microsecond_time"
  136. }"
  137. end
  138. basic_microsecond_microsecond: INTEGER
  139. external "plug_in"
  140. alias "{
  141. location: "${sys}/runtime"
  142. module_name: "basic_microsecond"
  143. feature_name: "basic_microsecond_microsecond"
  144. }"
  145. end
  146. basic_microsecond_update
  147. external "plug_in"
  148. alias "{
  149. location: "${sys}/runtime"
  150. module_name: "basic_microsecond"
  151. feature_name: "basic_microsecond_update"
  152. }"
  153. end
  154. invariant
  155. microsecond.in_range(0, 999999)
  156. end -- class MICROSECOND_TIME
  157. --
  158. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  159. --
  160. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  161. -- of this software and associated documentation files (the "Software"), to deal
  162. -- in the Software without restriction, including without limitation the rights
  163. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  164. -- copies of the Software, and to permit persons to whom the Software is
  165. -- furnished to do so, subject to the following conditions:
  166. --
  167. -- The above copyright notice and this permission notice shall be included in
  168. -- all copies or substantial portions of the Software.
  169. --
  170. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  171. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  172. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  173. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  174. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  175. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  176. -- THE SOFTWARE.