/tutorial/parking/date.e

http://github.com/tybor/Liberty · Specman e · 123 lines · 100 code · 13 blank · 10 comment · 5 complexity · 1a10666ca1f114993de69f425e484e31 MD5 · raw file

  1. class DATE
  2. --
  3. -- Special DATE class to fit with the PARKING example.
  4. --
  5. creation {ANY}
  6. make
  7. feature {ANY}
  8. display_on (stream: OUTPUT_STREAM) is
  9. -- To display `Current' on `stream'.
  10. local
  11. hour, m: INTEGER
  12. do
  13. if day > 0 then
  14. stream.put_string("day:")
  15. stream.put_integer(day)
  16. stream.put_character(' ')
  17. end
  18. m := min
  19. hour := m // 60
  20. m := m \\ 60
  21. stream.put_string("hour:")
  22. stream.put_integer(hour)
  23. stream.put_string(" minute:")
  24. stream.put_integer(m)
  25. if day_time then
  26. stream.put_string(" (day time)%N")
  27. else
  28. stream.put_string(" (night time)%N")
  29. end
  30. end
  31. minutes_to (after: DATE): INTEGER is
  32. -- Count of minutes to go to `after'.
  33. require
  34. after >= Current
  35. do
  36. Result := (after.day - day) * 24 * 60 + (after.min - min)
  37. ensure
  38. Result >= 0
  39. end
  40. day_night_to (d2: DATE): TUPLE[INTEGER, INTEGER] is
  41. --The `Result' is a couple of INTEGER where:
  42. -- Result.first: Night time.
  43. -- Result.second: Day time.
  44. require
  45. d2 >= Current
  46. local
  47. min_jour, min_nuit, save_day, save_min: INTEGER
  48. do
  49. save_day := day
  50. save_min := min
  51. from
  52. until
  53. Current.is_equal(d2)
  54. loop
  55. if day_time then
  56. min_jour := min_jour + 1
  57. else
  58. min_nuit := min_nuit + 1
  59. end
  60. add_time(1)
  61. end
  62. day := save_day
  63. min := save_min
  64. Result := [min_jour, min_nuit]
  65. ensure
  66. Result.first + Result.second = minutes_to(d2)
  67. end
  68. infix ">=" (d2: like Current): BOOLEAN is
  69. require
  70. d2 /= Void
  71. do
  72. if day > d2.day then
  73. Result := True
  74. elseif day = d2.day then
  75. Result := min >= d2.min
  76. end
  77. end
  78. day_time: BOOLEAN is
  79. -- Is it Sunny ?
  80. do
  81. Result := min >= 6 * 60 and min <= 22 * 60
  82. end
  83. nigth_time: BOOLEAN is
  84. -- Is it the night ?
  85. do
  86. Result := not day_time
  87. end
  88. feature {ANY} -- Modifications:
  89. make (vday, vmin: INTEGER) is
  90. do
  91. day := vday
  92. min := vmin
  93. ensure
  94. day = vday
  95. min = vmin
  96. end
  97. add_time (nb_min: INTEGER) is
  98. do
  99. min := min + nb_min
  100. if min >= 24 * 60 then
  101. day := day + min // (24 * 60)
  102. min := min \\ (24 * 60)
  103. end
  104. end
  105. feature {DATE}
  106. day, min: INTEGER -- (No `hour' attribute because the number of hours is in `min' value.)
  107. invariant
  108. day >= 0
  109. min.in_range(0, 24 * 60 - 1)
  110. end -- class DATE