How to get the current system year in Prolog as a number

How to get the current system year as a number in the prologue. I found this. But it gives the year as a string.

today(Today) :- get_time(X), format_time(atom(Today), '%Y', X).

Thank.

+4
source share
1 answer

As mbratch notes in the comments on my answer, it is format_time/3intended to display date and time values. If I just need the data, I would not use format_time/3, but rather stamp_date_time/3, and date_time_value/3to convert the time stamp in the desired value. The documentation for the corresponding predicates can be found here .

To get the current year, such a predicate will be enough:

year(Year) :-
    get_time(Stamp),
    stamp_date_time(Stamp, DateTime, local),
    date_time_value(year, DateTime, Year).

The following are descriptions of the built-in predicates:

  • get_time/2 , Unix Epoch.
  • stamp_date_time/3 date/9 , . local, , . date/9:

    date (2014, 3, 29, 8, 8, 59.30211305618286, 21600, 'MDT', true)

  • date_time_value/3: date/9 ( , date(Year,_,_,_,_,_,_,_,_), ).

, , , :

today(Today) :-
    get_time(Stamp),
    stamp_date_time(Stamp, DateTime, local),
    date_time_value(date, DateTime, Today).

date date_time_value/3 Today date/3, , : , date(2014, 3, 29).

, , , , , ,

get_date_time_value(Key, Value) :-
    get_time(Stamp),
    stamp_date_time(Stamp, DateTime, local),
    date_time_value(Key, DateTime, Value).

, Key:

?- get_date_time_value(day, X).
X = 29.

?- get_date_time_value(year, X).
X = 2014.

?- get_date_time_value(date, X).
X = date(2014, 3, 29).
+8

Source: https://habr.com/ru/post/1534039/


All Articles