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