note
	description: "Representation of a date at C level"
	legal: "See notice at end of class."
	status: "See notice at end of class."
	date: "$Date: 2016-11-28 03:57:18 +0000 (Mon, 28 Nov 2016) $"
	revision: "$Revision: 99541 $"

class 
	C_DATE

create 
	default_create,
	make_utc

feature {NONE} -- Initialization

	default_create
			-- Create an instance of C_DATA using current local time.
		do
			is_utc := False
			update
		end

	make_utc
			-- Create an instance of C_DATE holding UTC values.
		do
			is_utc := True
			update
		ensure
			is_utc: is_utc
		end
	
feature -- Access

	generating_type: TYPE [detachable C_DATE]
			-- Type of current object
			-- (type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generating_type_not_void: Result /= Void
		end

	generator: STRING_8
			-- Name of current object's generating class
			-- (base class of the type of which it is a direct instance)
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			generator_not_void: Result /= Void
			generator_not_empty: not Result.is_empty
		end

	is_utc: BOOLEAN
			-- Is Current holding value in UTC format?
	
feature -- Comparison

	frozen deep_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void
			-- or attached to isomorphic object structures?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_deep_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			shallow_implies_deep: standard_equal (a, b) implies Result
			both_or_none_void: (a = Void) implies (Result = (b = Void))
			same_type: (Result and (a /= Void)) implies (b /= Void and then a.same_type (b))
			symmetric: Result implies deep_equal (b, a)
		end

	frozen equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached
			-- to objects considered equal?
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.is_equal (b))
		end

	frozen is_deep_equal (other: C_DATE): BOOLEAN
			-- Are Current and other attached to isomorphic object structures?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			shallow_implies_deep: standard_is_equal (other) implies Result
			same_type: Result implies same_type (other)
			symmetric: Result implies other.is_deep_equal (Current)
		end

	is_equal (other: C_DATE): BOOLEAN
			-- Is other attached to an object considered
			-- equal to current object?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			symmetric: Result implies other ~ Current
			consistent: standard_is_equal (other) implies Result
		end

	frozen standard_equal (a: detachable ANY; b: like arg #1): BOOLEAN
			-- Are a and b either both void or attached to
			-- field-by-field identical objects of the same type?
			-- Always uses default object comparison criterion.
			-- (from ANY)
		do
			if a = Void then
				Result := b = Void
			else
				Result := b /= Void and then a.standard_is_equal (b)
			end
		ensure -- from ANY
			instance_free: class
			definition: Result = (a = Void and b = Void) or else ((a /= Void and b /= Void) and then a.standard_is_equal (b))
		end

	frozen standard_is_equal (other: C_DATE): BOOLEAN
			-- Is other attached to an object of the same type
			-- as current object, and field-by-field identical to it?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			same_type: Result implies same_type (other)
			symmetric: Result implies other.standard_is_equal (Current)
		end
	
feature -- Status report

	conforms_to (other: ANY): BOOLEAN
			-- Does type of current object conform to type
			-- of other (as per Eiffel: The Language, chapter 13)?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		end

	same_type (other: ANY): BOOLEAN
			-- Is type of current object identical to type of other?
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		external
			"built_in"
		ensure -- from ANY
			definition: Result = (conforms_to (other) and other.conforms_to (Current))
		end
	
feature -- Duplication

	frozen clone (other: detachable ANY): like other
		obsolete "Use `twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- equal to other
			--
			-- For non-void other, clone calls copy;
			-- to change copying/cloning semantics, redefine copy.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.twin
			end
		ensure -- from ANY
			instance_free: class
			equal: Result ~ other
		end

	copy (other: C_DATE)
			-- Update current object using fields of object attached
			-- to other, so as to yield equal objects.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_equal: Current ~ other
		end

	frozen deep_clone (other: detachable ANY): like other
		obsolete "Use `deep_twin' instead. [2017-05-31]"
			-- Void if other is void: otherwise, new object structure
			-- recursively duplicated from the one attached to other
			-- (from ANY)
		do
			if other /= Void then
				Result := other.deep_twin
			end
		ensure -- from ANY
			instance_free: class
			deep_equal: deep_equal (other, Result)
		end

	frozen deep_copy (other: C_DATE)
			-- Effect equivalent to that of:
			--		copy (other . deep_twin)
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
		do
			copy (other.deep_twin)
		ensure -- from ANY
			deep_equal: deep_equal (Current, other)
		end

	frozen deep_twin: C_DATE
			-- New object structure recursively duplicated from Current.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			deep_twin_not_void: Result /= Void
			deep_equal: deep_equal (Current, Result)
		end

	frozen standard_clone (other: detachable ANY): like other
		obsolete "Use `standard_twin' instead. [2017-05-31]"
			-- Void if other is void; otherwise new object
			-- field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		do
			if other /= Void then
				Result := other.standard_twin
			end
		ensure -- from ANY
			instance_free: class
			equal: standard_equal (Result, other)
		end

	frozen standard_copy (other: C_DATE)
			-- Copy every field of other onto corresponding field
			-- of current object.
			-- (from ANY)
		require -- from ANY
			other_not_void: other /= Void
			type_identity: same_type (other)
		external
			"built_in"
		ensure -- from ANY
			is_standard_equal: standard_is_equal (other)
		end

	frozen standard_twin: C_DATE
			-- New object field-by-field identical to other.
			-- Always uses default copying semantics.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			standard_twin_not_void: Result /= Void
			equal: standard_equal (Result, Current)
		end

	frozen twin: C_DATE
			-- New object equal to Current
			-- twin calls copy; to change copying/twinning semantics, redefine copy.
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			twin_not_void: Result /= Void
			is_equal: Result ~ Current
		end
	
feature -- Basic operations

	frozen as_attached: attached C_DATE
		obsolete "Remove calls to this feature. [2017-05-31]"
			-- Attached version of Current.
			-- (Can be used during transitional period to convert
			-- non-void-safe classes to void-safe ones.)
			-- (from ANY)
		do
			Result := Current
		end

	frozen default: detachable C_DATE
			-- Default value of object's type
			-- (from ANY)
		do
		end

	frozen default_pointer: POINTER
			-- Default value of type POINTER
			-- (Avoid the need to write p.default for
			-- some p of type POINTER.)
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end

	default_rescue
			-- Process exception for routines with no Rescue clause.
			-- (Default: do nothing.)
			-- (from ANY)
		do
		end

	frozen do_nothing
			-- Execute a null action.
			-- (from ANY)
		do
		ensure -- from ANY
			instance_free: class
		end
	
feature {NONE} -- Externals

	gettimeofday (p: POINTER)
			-- Set current date and time in p, pointer to a struct timeval area.
		external
			"C inline use %"eif_time.h%""
		alias
			"[
							#ifdef EIF_WINDOWS
				    			static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
				
								SYSTEMTIME  system_time;
								FILETIME    file_time;
								uint64_t    time;
								struct timeval *tp = (struct timeval *) $p;
				
								GetSystemTime (&system_time );
								SystemTimeToFileTime (&system_time, &file_time );
								time = ((uint64_t) file_time.dwLowDateTime );
								time += ((uint64_t) file_time.dwHighDateTime) << 32;
				
								tp->tv_sec  = (long) ((time - EPOCH) / 10000000L);
								tp->tv_usec = (long) (system_time.wMilliseconds * 1000);
							#else
								gettimeofday((struct timeval *) $p, NULL);
							#endif
			]"
		end
	
feature -- Output

	Io: STD_FILES
			-- Handle to standard file setup
			-- (from ANY)
		once
			create Result
			Result.set_output_default
		ensure -- from ANY
			instance_free: class
			io_not_void: Result /= Void
		end

	out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		do
			Result := tagged_out
		ensure -- from ANY
			out_not_void: Result /= Void
		end

	print (o: detachable ANY)
			-- Write terse external representation of o
			-- on standard output.
			-- (from ANY)
		do
			if o /= Void then
				Io.put_string (o.out)
			end
		ensure -- from ANY
			instance_free: class
		end

	frozen tagged_out: STRING_8
			-- New string containing terse printable representation
			-- of current object
			-- (from ANY)
		external
			"built_in"
		ensure -- from ANY
			tagged_out_not_void: Result /= Void
		end
	
feature -- Platform

	Operating_environment: OPERATING_ENVIRONMENT
			-- Objects available from the operating system
			-- (from ANY)
		once
			create Result
		ensure -- from ANY
			instance_free: class
			operating_environment_not_void: Result /= Void
		end
	
feature {NONE} -- Retrieval

	frozen internal_correct_mismatch
			-- Called from runtime to perform a proper dynamic dispatch on correct_mismatch
			-- from MISMATCH_CORRECTOR.
			-- (from ANY)
		local
			l_msg: STRING_8
			l_exc: EXCEPTIONS
		do
			if attached {MISMATCH_CORRECTOR} Current as l_corrector then
				l_corrector.correct_mismatch
			else
				create l_msg.make_from_string ("Mismatch: ")
				create l_exc
				l_msg.append (generating_type.name)
				l_exc.raise_retrieval_exception (l_msg)
			end
		end
	
feature -- Status

	day_now: INTEGER_32
			-- Current day at creation time or after last call to update.
		do
			Result := get_tm_mday (internal_item.item)
		ensure
			day_valid: Result >= 1 and Result <= 31
		end

	hour_now: INTEGER_32
			-- Current hour at creation time or after last call to update.
		do
			Result := get_tm_hour (internal_item.item)
		ensure
			hour_valid: Result >= 0 and Result <= 23
		end

	microseconds_now: INTEGER_32
			-- Current microseconds (includes milliseconds)

	millisecond_now: INTEGER_32
			-- Current millisecond at creation time or after last call to update.
		do
			Result := microseconds_now // 1000
		end

	minute_now: INTEGER_32
			-- Current minute at creation time or after last call to update.
		do
			Result := get_tm_min (internal_item.item)
		ensure
			minute_valid: Result >= 0 and Result <= 59
		end

	month_now: INTEGER_32
			-- Current month at creation time or after last call to update.
		do
			Result := get_tm_mon (internal_item.item) + 1
		ensure
			month_valid: Result >= 1 and Result <= 12
		end

	second_now: INTEGER_32
			-- Current second at creation time or after last call to update.
		do
			Result := get_tm_sec (internal_item.item)
			if Result > 59 then
				Result := 59
			end
		ensure
			second_valid: Result >= 0 and Result <= 59
		end

	year_now: INTEGER_32
			-- Current year at creation time or after last call to update.
		do
			Result := 1900 + get_tm_year (internal_item.item)
		ensure
			year_valid: Result >= 1900
		end
	
feature -- Update

	update
			-- Pointer to struct tm area.
		local
			l_timeval, l_tm, l_time: POINTER
			l_micro: INTEGER_32
		do
			l_timeval := l_timeval.memory_alloc (timeval_structure_size)
			l_time := l_time.memory_alloc (time_t_structure_size)
			gettimeofday (l_timeval)
			get_time (l_timeval, l_time)
			if is_utc then
				l_tm := gmtime (l_time)
			else
				l_tm := localtime (l_time)
			end
			create internal_item.make_from_pointer (l_tm, tm_structure_size)
			l_micro := get_micro (l_timeval)
			if l_micro < 0 or l_micro > 999999 then
				microseconds_now := 0
			else
				microseconds_now := l_micro
			end
			l_time.memory_free
			l_timeval.memory_free
		end
	
feature {NONE} -- struct timeval encapsulation

	get_micro (p: POINTER): INTEGER_32
			-- get p->tv_usec (which has to be an integer type -1 to 1000000 according to POSIX)
		external
			"C struct struct timeval access tv_usec use <time.h>"
		end

	get_time (p, t: POINTER)
			-- Get p->tv_sec.
		external
			"C inline use <time.h>"
		alias
			"*(time_t *) $t = (((struct timeval *)$p)->tv_sec);"
		end

	time_t_structure_size: INTEGER_32
			-- Size of struct time.
		external
			"C macro use <time.h>"
		alias
			"sizeof(time_t)"
		end

	timeval_structure_size: INTEGER_32
			-- Size of struct timeval.
		external
			"C macro use %"eif_time.h%""
		alias
			"sizeof(struct timeval)"
		end

	tm_structure_size: INTEGER_32
			-- Size of struct tm.
		external
			"C macro use <time.h>"
		alias
			"sizeof(struct tm)"
		end
	
feature {NONE} -- struct tm encapsulation

	get_tm_hour (p: POINTER): INTEGER_32
			-- Get p->tm_hour.
		external
			"C struct struct tm access tm_hour use <time.h>"
		end

	get_tm_mday (p: POINTER): INTEGER_32
			-- Get p->tm_mday.
		external
			"C struct struct tm access tm_mday use <time.h>"
		end

	get_tm_min (p: POINTER): INTEGER_32
			-- Get p->tm_min.
		external
			"C struct struct tm access tm_min use <time.h>"
		end

	get_tm_mon (p: POINTER): INTEGER_32
			-- Get p->tm_mon.
		external
			"C struct struct tm access tm_mon use <time.h>"
		end

	get_tm_sec (p: POINTER): INTEGER_32
			-- Get p->tm_sec.
		external
			"C struct struct tm access tm_sec use <time.h>"
		end

	get_tm_year (p: POINTER): INTEGER_32
			-- Get p->tm_year, number of years since 1900.
		external
			"C struct struct tm access tm_year use <time.h>"
		end

	gmtime (t: POINTER): POINTER
			-- Pointer to struct tm area in UTC.
		external
			"C inline use <time.h>"
		alias
			"gmtime ((time_t *) $t)"
		end

	internal_item: MANAGED_POINTER
			-- Pointer to struct tm area.

	localtime (t: POINTER): POINTER
			-- Pointer to struct tm area.
		external
			"C inline use <time.h>"
		alias
			"localtime ((time_t *) $t)"
		end
	
invariant
		-- from ANY
	reflexive_equality: standard_is_equal (Current)
	reflexive_conformance: conforms_to (Current)

note
	copyright: "Copyright (c) 1984-2016, 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 C_DATE

Generated by ISE EiffelStudio