PageRenderTime 32ms CodeModel.GetById 23ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/time/time.e

http://github.com/tybor/Liberty
Specman e | 406 lines | 311 code | 44 blank | 51 comment | 6 complexity | 6abc08d9dd44c4251a1cf95fa4270b57 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4expanded class TIME
  5--
  6-- Time and date facilities: year, month, day, hour and seconds.
  7--
  8
  9insert
 10   HASHABLE
 11      redefine
 12         out
 13      end
 14   COMPARABLE
 15      redefine
 16         is_equal, out
 17      end
 18   TIME_HANDLER
 19      redefine
 20         is_equal, out
 21      end
 22
 23feature {ANY}
 24   out: STRING
 25      do
 26         create Result.with_capacity(21)
 27         year.append_in(Result); Result.extend('/')
 28         if month < 10 then Result.extend('0') end
 29         month.append_in(Result); Result.extend('/')
 30         if day < 10 then Result.extend('0') end
 31         day.append_in(Result); Result.extend(' ')
 32         if hour < 10 then Result.extend('0') end
 33         hour.append_in(Result); Result.extend(':')
 34         if minute < 10 then Result.extend('0') end
 35         minute.append_in(Result); Result.extend(':')
 36         if second < 10 then Result.extend('0') end
 37         second.append_in(Result)
 38      end
 39
 40feature {ANY}
 41   is_local_time: BOOLEAN
 42         -- Is the local time in use? This information applies to all objects
 43         -- of class TIME and MICROSECOND_TIME.
 44      do
 45         Result := time_mode = 0
 46      ensure
 47         Result implies not is_universal_time
 48      end
 49
 50   is_universal_time: BOOLEAN
 51         -- Is Universal Time in use?  This information applies to all objects
 52         -- of class TIME and MICROSECOND_TIME.
 53      do
 54         Result := time_mode /= 0
 55      ensure
 56         Result implies not is_local_time
 57      end
 58
 59   year: INTEGER
 60         -- Number of the year.
 61      do
 62         Result := time_getyear(time_memory, time_mode)
 63      end
 64
 65   month: INTEGER
 66         -- Number of the month (1 for January, 2 for February, ...
 67         -- 12 for December).
 68      do
 69         Result := time_getmonth(time_memory, time_mode)
 70      ensure
 71         Result.in_range(1, 12)
 72      end
 73
 74   day: INTEGER
 75         -- Day of the `month' in range 1 .. 31.
 76      do
 77         Result := time_getday(time_memory, time_mode)
 78      ensure
 79         Result.in_range(1, 31)
 80      end
 81
 82   hour: INTEGER
 83         -- Hour in range 0..23.
 84      do
 85         Result := time_gethour(time_memory, time_mode)
 86      ensure
 87         Result.in_range(0, 23)
 88      end
 89
 90   minute: INTEGER
 91         -- Minute in range 0 .. 59.
 92      do
 93         Result := time_getminute(time_memory, time_mode)
 94      ensure
 95         Result.in_range(0, 59)
 96      end
 97
 98   second: INTEGER
 99         -- Second in range 0 .. 59.
100      do
101         Result := time_getsecond(time_memory, time_mode)
102      ensure
103         Result.in_range(0, 59)
104      end
105
106   week_day: INTEGER
107         -- Number of the day in the week (Sunday is 0, Monday is 1, etc.).
108      do
109         Result := time_getwday(time_memory, time_mode)
110      ensure
111         Result.in_range(0, 6)
112      end
113
114   year_day: INTEGER
115         -- Number of the day in the year in range 0 .. 365.
116      do
117         Result := time_getyday(time_memory, time_mode)
118      end
119
120   is_summer_time_used: BOOLEAN
121         -- Is summer time in effect?
122      do
123         Result := time_is_summer_time_used(time_memory)
124      end
125
126   to_microsecond_time: MICROSECOND_TIME
127      do
128         Result.set_time(Current)
129      ensure
130         Result.time = Current
131         Result.microsecond = 0
132      end
133
134feature {ANY} -- Setting:
135   update
136         -- Update `Current' with the current system clock.
137      do
138         time_memory := basic_time
139      end
140
141   set (a_year, a_month, a_day, a_hour, a_min, sec: INTEGER): BOOLEAN
142         -- Try to set `Current' using the given information. If this input
143         -- is not a valid date, the `Result' is False and `Current' is not updated.
144      require
145         valid_month: a_month.in_range(1, 12)
146         valid_day: a_day.in_range(1, 31)
147         valid_hour: a_hour.in_range(0, 23)
148         valid_minute: a_min.in_range(0, 59)
149         valid_second: sec.in_range(0, 59)
150      local
151         tm: like time_memory
152      do
153         tm := time_mktime(a_year, a_month, a_day, a_hour, a_min, sec)
154         if tm /= -1 then
155            time_memory := tm
156            Result := True
157         end
158      end
159
160   add_second (s: INTEGER)
161         -- Add `s' seconds to `Current'.
162      require
163         s >= 0
164      do
165         time_add_second($time_memory, s)
166      ensure
167         Current >= old Current
168      end
169
170   add_minute (m: INTEGER)
171         -- Add `m' minutes to `Current'.
172      require
173         m >= 0
174      do
175         time_add_second($time_memory, m * 60)
176      ensure
177         Current >= old Current
178      end
179
180   add_hour (h: INTEGER)
181         -- Add `h' hours to `Current'.
182      require
183         h >= 0
184      do
185         time_add_second($time_memory, h * 3600)
186      ensure
187         Current >= old Current
188      end
189
190   add_day (d: INTEGER)
191         -- Add `d' days to `Current'.
192      require
193         d >= 0
194      do
195         time_add_second($time_memory, d * 86400)
196      ensure
197         Current >= old Current
198      end
199
200feature {ANY}
201   elapsed_seconds (other: like Current): REAL
202         -- Elapsed time in seconds from `Current' to `other'.
203      require
204         Current <= other
205      do
206         Result := time_difftime(other.time_memory, time_memory)
207      ensure
208         Result >= 0
209      end
210
211   is_equal (other: like Current): BOOLEAN
212      do
213         Result := other.time_memory = time_memory
214      end
215
216   infix "<" (other: like Current): BOOLEAN
217      do
218         Result := time_difftime(other.time_memory, time_memory) > 0
219      end
220
221feature {ANY} -- Setting common time mode (local or universal):
222   set_universal_time
223      do
224         time_mode_memo.set_item(1)
225      ensure
226         is_universal_time
227      end
228
229   set_local_time
230      do
231         time_mode_memo.set_item(0)
232      ensure
233         is_local_time
234      end
235
236feature {ANY} -- Hashing:
237   hash_code: INTEGER
238      do
239         Result := time_memory.hash_code
240      end
241
242feature {TIME, FILE_TOOLS, TIME_FORMATTER, MICROSECOND_TIME}
243   set_time_memory (tm: like time_memory)
244      do
245         time_memory := tm
246      ensure
247         time_memory = tm
248      end
249
250feature {TIME_HANDLER}
251   time_memory: INTEGER_64
252         -- The current time value of `Current'. As far as we know, this
253         -- kind of information should always fit into an INTEGER_64.
254
255feature {}
256   time_mode_memo: REFERENCE[INTEGER]
257         -- The global default `time_mode' memory.
258      once
259         create Result
260      end
261
262   time_mode: INTEGER
263      do
264         Result := time_mode_memo.item
265      ensure
266         Result = 0 or Result = 1
267      end
268
269   basic_time: INTEGER_64
270      external "plug_in"
271      alias "{
272         location: "${sys}/plugins"
273         module_name: "time"
274         feature_name: "basic_time"
275         }"
276      end
277
278   time_difftime (time2, time1: INTEGER_64): REAL
279      external "plug_in"
280      alias "{
281         location: "${sys}/plugins"
282         module_name: "time"
283         feature_name: "time_difftime"
284         }"
285      end
286
287   time_getyear (tm: INTEGER_64; mode: INTEGER): INTEGER
288      external "plug_in"
289      alias "{
290         location: "${sys}/plugins"
291         module_name: "time"
292         feature_name: "time_getyear"
293         }"
294      end
295
296   time_getmonth (tm: INTEGER_64; mode: INTEGER): INTEGER
297      external "plug_in"
298      alias "{
299         location: "${sys}/plugins"
300         module_name: "time"
301         feature_name: "time_getmonth"
302         }"
303      end
304
305   time_getday (tm: INTEGER_64; mode: INTEGER): INTEGER
306      external "plug_in"
307      alias "{
308         location: "${sys}/plugins"
309         module_name: "time"
310         feature_name: "time_getday"
311         }"
312      end
313
314   time_gethour (tm: INTEGER_64; mode: INTEGER): INTEGER
315      external "plug_in"
316      alias "{
317         location: "${sys}/plugins"
318         module_name: "time"
319         feature_name: "time_gethour"
320         }"
321      end
322
323   time_getminute (tm: INTEGER_64; mode: INTEGER): INTEGER
324      external "plug_in"
325      alias "{
326         location: "${sys}/plugins"
327         module_name: "time"
328         feature_name: "time_getminute"
329         }"
330      end
331
332   time_getsecond (tm: INTEGER_64; mode: INTEGER): INTEGER
333      external "plug_in"
334      alias "{
335         location: "${sys}/plugins"
336         module_name: "time"
337         feature_name: "time_getsecond"
338         }"
339      end
340
341   time_is_summer_time_used (tm: INTEGER_64): BOOLEAN
342      external "plug_in"
343      alias "{
344         location: "${sys}/plugins"
345         module_name: "time"
346         feature_name: "time_is_summer_time_used"
347         }"
348      end
349
350   time_getyday (tm: INTEGER_64; mode: INTEGER): INTEGER
351      external "plug_in"
352      alias "{
353         location: "${sys}/plugins"
354         module_name: "time"
355         feature_name: "time_getyday"
356         }"
357      end
358
359   time_getwday (tm: INTEGER_64; mode: INTEGER): INTEGER
360      external "plug_in"
361      alias "{
362         location: "${sys}/plugins"
363         module_name: "time"
364         feature_name: "time_getwday"
365         }"
366      end
367
368   time_mktime (a_year, a_mon, a_day, a_hour, a_min, a_sec: INTEGER): INTEGER_64
369      external "plug_in"
370      alias "{
371         location: "${sys}/plugins"
372         module_name: "time"
373         feature_name: "time_mktime"
374         }"
375      end
376
377   time_add_second (time: POINTER; s: INTEGER)
378      external "plug_in"
379      alias "{
380         location: "${sys}/plugins"
381         module_name: "time"
382         feature_name: "time_add_second"
383         }"
384      end
385
386end -- class TIME
387--
388-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
389--
390-- Permission is hereby granted, free of charge, to any person obtaining a copy
391-- of this software and associated documentation files (the "Software"), to deal
392-- in the Software without restriction, including without limitation the rights
393-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
394-- copies of the Software, and to permit persons to whom the Software is
395-- furnished to do so, subject to the following conditions:
396--
397-- The above copyright notice and this permission notice shall be included in
398-- all copies or substantial portions of the Software.
399--
400-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
401-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
402-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
403-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
404-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
405-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
406-- THE SOFTWARE.