"strptime" in SWI-Prolog

How to convert atom '2015-12-15T05 PST'to timestamp or datetime?

I tried parse_time / 3

?- parse_time('2015-12-15T05 PST', '%Y-%m-%dT%H %Z', Stamp)
false.

and format_time / 3

?- format_time('2015-12-15T05 PST', '%Y-%m-%dT%H %Z', date(Y,M,D,H,M,S,O,TZ,DST)).
ERROR: format_time/3: Arguments are not sufficiently instantiated
+4
source share
1 answer

According to the documentation, the mode format_time/3cannot help you because it expects everything to be transmitted:

format_time (+ Out, + Format, + StampOrDateTime)

This means that you specify each of the arguments. You want to see something with a prefix -, which means that it sends something back, which looks like it will mean parse_time/3, but the documentation says there:

Text .

: rfc_1123 iso_8601, . , , , Unix, , , .

: ! :

:- use_module(library(dcg/basics)).

myformat(date(Y,M,D,H,_,_,_,TZ,_)) -->
    integer(Y), "-", integer(M), "-", integer(D),
    "T", integer(H), " ", timezone(TZ).

timezone('UTC') --> "UTC".
timezone('UTC') --> "GMT".
timezone(-18000) --> "PST".
timezone(Secs) -->
    [Sign], digit(H0), digit(H1), digit(M0), digit(M1),
    {
     (Sign = 0'+ ; Sign = 0'-),
     number_codes(Hour, [H0,H1]),
     number_codes(Minutes, [M0, M1]),
     (Sign = 0'+
     -> Secs is Hour * 3600 + Minutes * 60
     ;  Secs is -(Hour * 3600 + Minutes * 60))
    }.

my_time_parse(Atom, Date) :-
    atom_codes(Atom, Codes),
    phrase(myformat(Date), Codes).

, , , . , , , (, ?), , PST, . :

?- my_time_parse('2015-12-15T05 PST', Date).
Date = date(2015, 12, 15, 5, _G3204, _G3205, _G3206, -18000, _G3208) ;
false.

, !

+3

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


All Articles