PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

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