Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get current time in Prolog? 1

Status
Not open for further replies.

kamileo

Programmer
Jul 15, 2010
6
PL
Hello to everybody.
I wonder how to get the current time from Prolog. I know how to get current date, but can't do that with the time. I want to get 3 variables (Hour, Minutes, Secs).
Thanks for help.
 
In SWI-Prolog you can do it like this:

Code:
?- get_time(T), stamp_date_time(T, X, 'UTC').

The answer will be:

T = 1.27929e+009
X = date(2010, 7, 16, 14, 15, 5.406, 0, 'UTC', -)

The first 3 arguments of X represent the date (16/07/2010) and the next 3 arguments represent the time (14:15:05.406)
 
To complete your answer:

Code:
?- get_time(T), stamp_date_time(T, date(_, _, _, H, M, S, _, _, _), 'UTC').

T = .....
H = 14
M = 18
S = 55.562

Here are the 3 variables that you need ...
 
Thank you, kahleen, for the answer. I've already found the solution, the same as yours, but I've another problem. I live in Poland and my applications works using my local time (UTC+2) so I set the timezone to value -7200. The problem is that we have, in Polad, the summer (UTC+2) and the winter timezone (UTC+1). For now it's good solution, but when we come into winter time, my application would not take into account this and wouldn't change timezone to -3600. I've no idea how to solve it because the time when we change timezone are not always the same days. Has someone an idea how to solve this problem?
 
Aha, that's my way in which I get current time:
current_time(H,M) :-
get_time(TS),
stamp_date_time(TS,Date9,-7200),
arg(4,Date9,H),
arg(5,Date9,M).
 
Here is an excerpt from SWI-Prolog's help:

Code:
4.34.1.1 Time and date data-structures

We use the following time representations

TimeStamp
    A  TimeStamp  is a  floating  point number  expression the  time  in
    seconds since the Epoch at 1970-1-1.

date(Y,M,D,H,Mn,S,Off,TZ,DST)
    We  call this term a  date-time structure.   The first 5 fields  are
    integers  expressing  the year,  month  (1..12), day  (1..31),  hour
    (0..23),  Minute  (0..59).    The S  field holds  the  seconds as  a
    floating  point number  between 0.0  and 60.0.   Off  is an  integer
    representing  the offset relative to  UTC in seconds where  positive
    values  are west of  Greenwich.  If  converted from local time  (see
    stamp_date_time/3, TZ holds the name of the local timezone.   If the
    timezone  is not known TZ is  the atom -.   DST is true if  daylight
    saving  time applies to the  current time, false if daylight  saving
    time is relevant but not  effective and - if unknown or the timezone
    has no daylight saving time.

date(Y,M.D)
    Date  using the  same values as  described above.   Extracted  using
    date_time_value/3.

time(H,Mn,S)
    Time  using the  same values as  described above.   Extracted  using
    date_time_value/3.

The last argument of the date structure is called DST (Daylight Saving Time) and it has to do with your problem. I don't have time to test it right now, but maybe you can ... I believe that your system defines the timezone and the DST status ... If you check DST in Control Panel Date/Time settings, it should be visible for any application running on your system (untested, though) ... Try using 'local' for the third parameter of stamp_date_time ... If you use -7200 as the third parameter, then it's bye bye daylight saving :)
 
That's solved my problem! I use 'local' as the third parameter of the stamp_date_time and everything works as I wish to. Thank you for help.
Code:
current_time(H,M) :-
	get_time(TS),
	stamp_date_time(TS,Date9,'local'),
	arg(4,Date9,H),
	arg(5,Date9,M).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top