note
	description: "Duration of dates and times"
	legal: "See notice at end of class."
	status: "See notice at end of class."
	date: "$Date: 2017-03-29 12:26:46 +0000 (Wed, 29 Mar 2017) $"
	revision: "$Revision: 100065 $"
	access: date, time

class interface
	DATE_TIME_DURATION

create 
	make,
	make_definite,
	make_fine,
	make_by_date_time,
	make_by_date

feature -- Initialization

	make (y, mo, d, h, mi, s: INTEGER_32)
			-- Set year, month, day to y, mo, d.
			-- Set hour, minute, second to h, mi, s.
		ensure
			date_exists: date /= Void
			time_exists: time /= Void
			year_set: year = y
			month_set: month = mo
			day_set: day = d
			hour_set: hour = h
			minute_set: minute = mi
			second_set: second = s

	make_definite (d, h, m, s: INTEGER_32)
			-- Set day to d.
			-- Set hour, minute, second to h, m, s.
		ensure
			date_exists: date /= Void
			time_exists: time /= Void
			definite_result: definite
			day_set: day = d
			hour_set: hour = h
			minute_set: minute = m
			second_set: second = s

	make_fine (y, mo, d, h, mi: INTEGER_32; s: REAL_64)
			-- set year, month, day to y, mo, d.
			-- set hour, minute, second to h, mi, s.
		ensure
			date_exists: date /= Void
			time_exists: time /= Void
			year_set: year = y
			month_set: month = mo
			day_set: day = d
			hour_set: hour = h
			minute_set: minute = mi
			fine_second_set: fine_second = s

	make_by_date_time (d: DATE_DURATION; t: TIME_DURATION)
			-- Set date to d and time to t.
		require
			date_exists: d /= Void
			time_exists: t /= Void
		ensure
			date_set: date = d
			time_set: time = t

	make_by_date (d: DATE_DURATION)
			-- Set date to d and time to zero.
		require
			d_exists: d /= Void
		ensure
			date_set: date = d
			time_exists: time /= Void
			time_set: time.is_equal (time.zero)
	
feature -- Access

	date: DATE_DURATION
			-- Date part of the current duration

	time: TIME_DURATION
			-- Time part of current duration

	origin_date_time: detachable DATE_TIME
			-- Origin date time of duration

	zero: like Current
			-- Neutral element for "+" and "-"

	seconds_count: INTEGER_64
			-- Total number of seconds of current duration
		require
			has_origin: has_origin_date_time

	fine_seconds_count: REAL_64
			-- Total number of seconds of current duration
		require
			has_origin: has_origin_date_time
	
feature -- Comparison

	is_less alias "<" (other: like Current): BOOLEAN
			-- Is the current duration smaller than other?
			-- False if either is not definite
		ensure then
			non_definite_result: not (definite and other.definite) implies not Result

	is_equal (other: like Current): BOOLEAN
			-- Are the current duration an other equal?
	
feature -- Status report

	definite: BOOLEAN
			-- Is this duration date-independent?
			-- (True if it only uses day, not year and month)
		ensure
			result_definition: Result = (year = 0 and then month = 0)

	canonical (start_date: DATE_TIME): BOOLEAN
			-- Are the time and date parts of the same sign,
			-- and both canonical?
		require
			start_date_not_void: start_date /= Void

	is_positive: BOOLEAN
			-- Is duration positive?

	has_origin_date_time: BOOLEAN
			-- Has an origin_date_time been set?
	
feature -- Status setting

	set_origin_date_time (dt: detachable DATE_TIME)
			-- Set origin_date_time to dt.
		ensure
			origin_date_time_set: origin_date_time = dt
			origin_date_set: dt /= Void implies (dt.date = date.origin_date)
	
feature -- Element Change

	set_date (d: DATE_DURATION)
			-- Set date to d.
		require
			d_exists: d /= Void
		ensure
			date_set: date = d

	set_time (t: TIME_DURATION)
			-- Set time to t.
		require
			t_exists: time /= Void
		ensure
			time_set: time = t
	
feature -- Basic operations

	plus alias "+" (other: like Current): like Current
			-- Sum with other (commutative)
		ensure then
			origin_date_time: equal (origin_date_time, Result.origin_date_time)

	opposite alias "-": like Current
			-- Unary minus
		ensure then
			origin_date_time: equal (origin_date_time, Result.origin_date_time)

	day_add (d: INTEGER_32)
			-- Add d days to the current duration.
		ensure
			result_definition: day = old day + d
	
feature -- Conversion

	to_canonical (start_date: DATE_TIME): like Current
			-- A new duration, equivalent to current one
			-- and canonical for start_date
		ensure
			canonical_set: Result.canonical (start_date)
			duration_not_changed: equal (start_date + Current, start_date + Result)

	time_to_canonical: like Current
			-- A new duration, equivalent to current one
			-- but time is canonical and has the same sign as date
		require
			definite_duration: definite
		ensure
			time_canonical: Result.time.canonical
			same_sign: ((Result.date > date.zero) implies (Result.time >= time.zero)) and then ((Result.date < date.zero) implies (Result.time <= time.zero))
	
invariant
	date_exists: date /= Void
	time_exists: time /= Void
	origin_constraint: (origin_date_time = Void and date.origin_date = Void) or else (attached origin_date_time as l_origin_1 and then l_origin_1.date = date.origin_date)
	same_signs: (has_origin_date_time and then attached origin_date_time as l_origin_2 and then canonical (l_origin_2)) implies ((date.is_positive or date.is_zero) and (time.is_positive or time.is_zero)) or else ((date.is_negative or date.is_zero) and (time.is_negative or time.is_zero))

note
	copyright: "Copyright (c) 1984-2017, Eiffel Software and others"
	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
	source: "[
		Eiffel Software
		5949 Hollister Ave., Goleta, CA 93117 USA
		Telephone 805-685-1006, Fax 805-685-6869
		Website http://www.eiffel.com
		Customer support http://support.eiffel.com
	]"

end -- class DATE_TIME_DURATION

Generated by ISE EiffelStudio