Re: Code Contribution

Paul Morgan <[email protected]> Mon, 15 Sep 2008 14:00:50 -0500
Newsgroups gmane.comp.lang.eiffel.gobo.devel
Message-ID <[email protected]>
On Mon, Sep 15, 2008 at 02:33:47PM +0200, Bernd Schoeller wrote:
> Dear Paul,
---snip---
> Adding an AP_DATE_OPTION class seems very interesting, though it would 
> add a dependency of the argument library to the 'time' library (which - 
> AFAIK - is currently not there).
>
> Parsing dates from strings is also a complex job. Some of this work has  
> been done in the XML libraries, but again we get into trouble of creating 
> dependencies between libraries (there was a discussion between Colin and  
> Eric sometime ago about this issues, but I do not know how this was  
> resolved).
>
> Bernd

Well, I did put together the beginnings of a DATE_OPTION, and it
_does_ depend on another lib. In this case, the ISE libs.

Attached for interest.

-paul

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/

_______________________________________________
gobo-eiffel-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gobo-eiffel-develop
date_option.e (text/plain, 7.1 KB)
indexing
	description: "Objects that ..."
	author: ""
	date: "$Date$"
	revision: "$Revision$"

class
	DATE_OPTION

inherit
	AP_OPTION_WITH_PARAMETER [DATE]
		redefine
			initialize
		end
	DATE_VALIDITY_CHECKER


create
	make,
	make_with_long_form,
	make_with_short_form



feature {NONE} -- Initialization

	initialize is
			-- Perform the common initialization steps.
		do
			Precursor
			date_format_code := default_date_format
			update_parameter_description
		end



feature -- Access

	parameters: DS_LIST [DATE]
			-- All parameters given to the option

	date_format_code: STRING

	extended_semantics_enabled: BOOLEAN
			-- Should non-date parameters be interpreted?
			-- e.g., *, -, `last monday', and so forth.

	all_dates: BOOLEAN
			-- Does parameter mean to include all possible dates?

	null_date: BOOLEAN
			-- Does parameter mean that date is specifically null?

	input_hint: STRING is
			-- A hint for the human user with respect to the correct date format.
		local
			start_index, end_index: INTEGER
		do
			from
				Result := date_format_code.twin
				start_index := Result.index_of ('[', 1)
				end_index := Result.index_of (']', 2)
			until
				start_index >= end_index
			loop
					-- remove any substring such as `[0]'
				Result.remove_substring (start_index, end_index)
				start_index := Result.index_of ('[', 1)
				end_index := Result.index_of (']', 2)
			end
		ensure
			removed_left_brackets: not Result.has ('[')
			removed_right_brackets: not Result.has (']')
		end


feature -- Element Change

	set_date_format_code (a_code: like date_format_code) is
			-- Update with new format.
		require
			a_code_exists: a_code /= Void
			a_code_meaningful: not a_code.is_empty
		do
			date_format_code := a_code.twin
			update_parameter_description
		end

	enable_extended_semantics is
			-- Interpret special values and relate them to dates.
			-- For now this is limited to * (all dates) and - (no date)
			-- which is handy for creating SQL queries based on CLI.
			-- I'd like to add interpretations for "today", "yesterday",
			-- "last week", "now -3d", and so forth.
		do
			extended_semantics_enabled := True
			update_parameter_description
		ensure
			extended_semantics_enabled: extended_semantics_enabled
		end

	disable_extended_semantics is
			-- Do not interpret special values as dates.
		do
			extended_semantics_enabled := False
			update_parameter_description
		ensure
			no_extended_semantics: not extended_semantics_enabled
		end




feature -- Status report

	needs_parameter: BOOLEAN is
			-- Does this option need a parameter ?
		do
			Result := True
		end



feature {AP_PARSER} -- Parser Interface

	reset is
			-- Reset the option to a clean state before parsing.
		do
			create {DS_LINKED_LIST [DATE]} parameters.make
			all_dates := False
			null_date := False
		ensure then
			not_all_dates: not all_dates
			not_null_date: not null_date
		end

	record_occurrence (a_parser: AP_PARSER) is
			-- Record the occurrence of the option with `a_parameter'.
		local
			date: DATE
		do
			if a_parser.last_option_parameter.is_equal ("*") and extended_semantics_enabled then
				interpret_star
				parameters.force_last (today)
			elseif a_parser.last_option_parameter.is_equal ("-") and extended_semantics_enabled then
				interpret_dash
				parameters.force_last (today)
			elseif date_valid (a_parser.last_option_parameter, default_date_format) then
				create date.make_from_string (a_parser.last_option_parameter, default_date_format)
				parameters.force_last (date)
			else
				report_invalid_date_parameter (a_parser)
			end
		end


feature -- Date formats

	default_date_separator: CHARACTER is '-'
	default_time_separator: CHARACTER is ':'

	default_date_format: STRING is
			-- stardard short format for dates: 2008-03-01 (leading zeros)
			-- Use this format when the date but not time is meaningful.
			-- EXAMPLE:
			--                      2008-03-01
		once
			create Result.make_empty
			Result.append_string ("yyyy")
			Result.append_character (default_date_separator)
			Result.append_string ("[0]mm")
			Result.append_character (default_date_separator)
			Result.append_string ("[0]dd")
		ensure
			result_exists: result /= Void and then not result.is_empty
		end

	default_date_time_format: STRING is
			-- standard format with time: 2008-03-01 01:31:59 (leading zeros)
			-- Use this format when both the date and time are meaningful.
			-- EXAMPLE:
			--                      2008-03-01 01:31:59
		once
			create Result.make_from_string (default_date_format)
			Result.append (" [0]hh")
			Result.append_character (default_time_separator)
			Result.append ("[0]mi")
			Result.append_character (default_time_separator)
			Result.append ("[0]ss")
		ensure
			result_exists: result /= Void and then not result.is_empty
		end

	alternative_short_date_format: STRING is
			-- Format sometimes used in databases.
			-- 16-MAY-08
		once
			create Result.make_from_string ("[0]dd")
			Result.append_character (default_date_separator)
			Result.append_string ("mmm")
			Result.append_character (default_date_separator)
			Result.append_string ("yy")
		ensure
			result_exists: result /= Void and then not result.is_empty
		end


feature {NONE} -- Implementation

	update_parameter_description is
			-- Update the param description after changing formats.
		do
			set_parameter_description (input_hint)
		end

	report_invalid_date_parameter (a_parser: AP_PARSER) is
			-- Create an error for the parser
		local
			err: AP_ERROR
			date: DATE
		do
			create date.make_now_utc
			create err.make_invalid_parameter_error (Current, a_parser.last_option_parameter)
			a_parser.error_handler.report_error (err)
			parameters.force_last (date)
		end

	handle_extended_semantics (a_parser: AP_PARSER) is
			-- Attempt to interpret date using extended semantics.
		require
			extended_semantics_enabled: extended_semantics_enabled
		local
			date: DATE
		do
			if date_valid (a_parser.last_option_parameter, default_date_format) then
				create date.make_from_string (a_parser.last_option_parameter, default_date_format)
				parameters.force_last (date)
			elseif a_parser.last_option_parameter.is_equal ("*") then
				interpret_star
				create date.make (1970, 01, 01)
				parameters.force_last (date)
			elseif a_parser.last_option_parameter.is_equal ("-") then
				interpret_dash
				parameters.force_last (today)
			end
		end

	interpret_star is
			-- By default, set all_dates
		do
			all_dates := True
		end

	interpret_dash is
			-- By default, set null_date
		do
			null_date := True
		end



	formatted_today: STRING is
			-- Convenience function with `today' formatted
			-- using the current date_format_code.
		do
			Result := today.formatted_out (date_format_code)
		end

	today: DATE is
			-- Today's date.
		do
			create Result.make_now_utc
		ensure
			now_exists: Result /= Void
		end


invariant

	date_format_code_exists: date_format_code /= Void
	meaningful_date_format_code: not date_format_code.is_empty


		-- extended semantics

	all_dates_is_extended: all_dates implies extended_semantics_enabled
	null_date_is_extended: null_date implies extended_semantics_enabled

	all_dates_means_not_null: all_dates implies not null_date
	null_date_means_not_all: null_date implies not all_dates

end
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFIzrDifSBtZvWed8IRAkKDAJ0ZTG2pqmCRltNjC1w6/5BDwjnj+QCgku3v
XAzPYl42n9eAfnxkZRh1TRk=
=bwKX
-----END PGP SIGNATURE-----