:- module(datetime,[get_current_datetime/1]).

:- [prolog_detect].

:- prolog_detect:prolog(Prolog),
	(	(	Prolog=quintus,
			use_module(library(date))
		);
		(	Prolog=sicstus,
			use_module(library(system))
		);
		(	Prolog=pfdm,
			use_module(library(system))
		);
		(	Prolog=eclipse,
			use_module(library(calendar))	%use ECLiPSe's calendar library
		);
		true
	).

%
% depending on the Prolog system, call the appropriate predicate
%
get_current_datetime(Date_time) :-
	prolog_detect:prolog(Prolog),
	get_date_time(Prolog,Date_time).

%
% Quintus version
%
get_date_time(quintus,time_stamp(date(Day,Correct_month,Correct_year),time(Hour,Min,Sec))) :-
	date(date(Year,Month,Day)),
	Correct_year is Year+1900,
	Correct_month is Month+1,
	time(time(Hour,Min,Sec)).

%
% SICStus version
%
get_date_time(sicstus,time_stamp(date(Day,Month,Year),time(Hour,Min,Sec))) :-
	datime(datime(Year,Month,Day,Hour,Min,Sec)).

%
% SWI version
%
get_date_time(swi,time_stamp(date(Day,Month,Year),time(Hour,Min,Sec))) :-
	get_time(Time),
	convert_time(Time,Year,Month,Day,Hour,Min,Sec,_).

%
% ECLiPSe version
%
get_date_time(eclipse,time_stamp(date(Day,Month,Year),time(Hour,Min,Sec))) :-
	mjd_now(Now_mjd),
	mjd_to_date(Now_mjd,Day/Month/Year),
	mjd_to_time(Now_mjd,Hour:Min:Sec_in_float),	%sec is in float
	fix(Sec_in_float,Sec).				%truncate it to integer

