PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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