C timestamps in XSB

5 views
Skip to first unread message

Mark Clements

unread,
May 28, 2021, 5:36:36 AM5/28/21
to ErgoAI, Flora-2, and XSB Users Forum
I have written a small module that includes a subset of the C timestamp functions for XSB (https://github.com/mclements/xsb-timestamp). As examples:

| ?- [timestamp].
| ?- TS is 0, to_date(TS, Date).

TS = 0
Date = 1970-01-01

yes
| ?- to_timestamp('1970-01-01','%Y-%m-%d', TS), localdatetime(Now), timestamps_age(Now, TS, Age).

TS = -3600
Now = 1622194336
Age = 51

yes
| ?- localdatetime(Now), from_timestamp(Now, '%Y-%m-%d %H:%M:%S', DateTime).

Now = 1622194350
DateTime = 2021-05-28 11:32:30

yes
| ?- localdatetime(Now), to_date(Now, Date).

Now = 1622194373
Date = 2021-05-28

yes
| ?- localdatetime(Now), to_days(Now, Days).

Now = 1622194379
Days = 18775

yes

Given that XSB already uses timestamps from <time.h> for epoch_seconds(-Seconds) [which is the same as localdatetime(-Timestamp) -- taking the name from Postgresql], is there any interest in extending the timestamp facilities in base XSB?

Sincerely, Mark.

Theresa Swift

unread,
May 28, 2021, 5:27:28 PM5/28/21
to Mark Clements, Michael Kifer, David Warren, ErgoAI, Flora-2, and XSB Users Forum
Thanks -- we'll take a look.  XSB does have a package, iso8601 that contains some functionality with Julian dates, but your module may provide added functionality that our library is missing.

Thanks for your interest in XSB.  I'll use this email as a way to plug our new and very efficient XSB Python interface, which is in pre-release.  If you're interested its available from the git development branch where it is documented in Volume 2 of the manual.

Thanks again
Theresa Swift (for the XSB developers)

--
You received this message because you are subscribed to the Google Groups "ErgoAI, Flora-2, and XSB Users Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ErgoAI-Flora2-XSB...@coherentknowledge.com.
To view this discussion on the web visit https://groups.google.com/a/coherentknowledge.com/d/msgid/ErgoAI-Flora2-XSB-forum/9164cf28-d3f5-4fbb-b07e-60a06ddc4825n%40coherentknowledge.com.

Mark Clements (gmail)

unread,
May 30, 2021, 5:56:46 PM5/30/21
to Theresa Swift, Michael Kifer, David Warren, ErgoAI, Flora-2, and XSB Users Forum

Thank you for your prompt and helpful reply.

The iso8601 package has some very nice functionality. As a potential bug report, the following does not work on a XSB 4.0.0 (Ubuntu 20.04 LTS):

?- [iso8601].
?- date_string('YYYY-MM-DD',[Y,M,D],'2005-10-11').

I get an error "[Existence (No predicate iso8601 : date_string / 3 exists)]". Interestingly, the package is okay when recompiled separately. Am I missing something obvious?

Sincerely, Mark.

Mark Clements (gmail)

unread,
May 30, 2021, 5:56:57 PM5/30/21
to Theresa Swift, Michael Kifer, David Warren, ErgoAI, Flora-2, and XSB Users Forum

Thank you for your prompt and helpful reply.

The iso8601 package has some very nice functionality. As a potential bug report, the following does not work on a XSB 4.0.0 (Ubuntu 20.04 LTS):

?- [iso8601].
?- date_string('YYYY-MM-DD',[Y,M,D],'2005-10-11').

I get an error "[Existence (No predicate iso8601 : date_string / 3 exists)]". Interestingly, the package is okay when recompiled separately. Am I missing something obvious?

Sincerely, Mark.

On 28/05/2021 23:27, Theresa Swift wrote:

Michael Kifer

unread,
May 31, 2021, 2:46:24 AM5/31/21
to Mark Clements (gmail), Theresa Swift, David Warren, ErgoAI, Flora-2, and XSB Users Forum

It should be working now, if you pull and recompile xsb.

One thing is that it should be iso8601:date_string('YYYY-MM-DD',date(Y,M,D),'2005-10-11'), not a list, in arg 2.


I see that an earlier version tried to accommodate lists, but a few days ago Teri took that option out.


--

       --- michael


 

Mark Clements (gmail)

unread,
May 31, 2021, 3:54:44 AM5/31/21
to Michael Kifer, Theresa Swift, David Warren, ErgoAI, Flora-2, and XSB Users Forum

Thank you for this.


This now works: iso8601:date_string('YYYY-MM-DD',date(Y,M,D),'2005-10-11'), but iso8601:date_string('YYYY-MM-DD',date(2005,10,11),Date) gives an error "No predicate iso8601 : atom_codes / 2". I looked at the code but could not see the error.


Another question: what licence do you suggest for the xsb-timestamp package?


Sincerely, Mark.

Michael Kifer

unread,
May 31, 2021, 4:27:56 AM5/31/21
to Mark Clements (gmail), Theresa Swift, David Warren, ErgoAI, Flora-2, and XSB Users Forum


On 5/31/21 3:54 AM, Mark Clements (gmail) wrote:

Thank you for this.


This now works: iso8601:date_string('YYYY-MM-DD',date(Y,M,D),'2005-10-11'), but iso8601:date_string('YYYY-MM-DD',date(2005,10,11),Date) gives an error "No predicate iso8601 : atom_codes / 2". I looked at the code but could not see the error.


try again (after pulling)



Another question: what licence do you suggest for the xsb-timestamp package?


Better to use Apache2

--

       --- michael


 

Mark Clements (gmail)

unread,
Jun 1, 2021, 2:55:11 AM6/1/21
to Michael Kifer, Theresa Swift, David Warren, ErgoAI, Flora-2, and XSB Users Forum

That now works. Thank you for the quick response.


I was going to change the XSB example at http://rosettacode.org/wiki/Merge_and_aggregate_datasets#Prolog, but the Git version is very new -- and the iso8601 package seems to use the same pattern as


number_datecodes(Number, [Y1,Y2,Y3,Y4,45,M1,M2,45,D1,D2]) :-
    number_codes(Number, [Y1,Y2,Y3,Y4,M1,M2,D1,D2]).


which is crude but does provide a strict ordering for dates.


Sincerely, Mark.

Mark Clements (gmail)

unread,
Jun 1, 2021, 9:36:29 AM6/1/21
to Michael Kifer, Theresa Swift, David Warren, ErgoAI, Flora-2, and XSB Users Forum

Continuing the date theme, is there an example using add_cvt_type_hook/2 to read in a date from a CSV file? I tried:


:- import load_csv/2, add_cvt_type_hook/2 from proc_files.
:- import date_string/3 from iso8601.
date_converter(Atom,Date) :- date_string('YYYY-MM-DD',Date,Atom).

test :-
    load_csv('Visit.csv',visit(integer,atom,float)), % read date as atom
    visit(Id,Atom,Value),
    date_converter(Atom,Date),
    writeln(tuple(Id,Date,Value)), fail. %% ok

add_cvt_type_hook(date,date_converter(_,_)). %% error

test :-
    load_csv('Visit.csv',visit(integer,date,float)), % now, read as date
    visit(Id,Date,Value),
    writeln(tuple(Id,Date,Value)), fail. %% ok

The error is: "Permission (Operation) redefine on imported predicate: proc_files : add_cvt_type_hook / 2".


Sincerely, Mark.


On 31/05/2021 10:27, Michael Kifer wrote:
Visit.csv

Mark Clements (gmail)

unread,
Jun 1, 2021, 10:35:35 AM6/1/21
to David Warren, Michael Kifer, Theresa Swift, ErgoAI, Flora-2, and XSB Users Forum

Nice - this works better than I expected, where the null date is converted to an atom (with a warning 'Cannot convert  to date: ""').

Thank you for your help with this.

-- Mark

On 01/06/2021 16:21, David Warren wrote:
Hi Mark,
add_cvt_type_hook / 2 is a predicate that must be called to have an effect.  You have given it a definition with a fact, which isn't accepted since the definition is already given by the import.  I presume you want to execute that predicate.  To do that you should change the line indicated as error to:
:-  add_cvt_type_hook(date,date_converter(_,_)).
Which will cause it to be called when the file is loaded.
(Actually, it is better form, I think, (even though I rarely use it) to write:
?-  add_cvt_type_hook(date,date_converter(_,_)).
)
Thanks for your work on dates.
-David

Mark Clements (gmail)

unread,
Jun 8, 2021, 2:51:45 AM6/8/21
to David Warren, Michael Kifer, Theresa Swift, ErgoAI, Flora-2, and XSB Users Forum

As a follow-up: is there a simple way to match on NaN values that are input from a csv file? I have written an example using XSB at http://rosettacode.org/wiki/Merge_and_aggregate_datasets#Prolog that uses

is_nan(Number) :- number_codes(Number, [110,97,110|_]). %% "nan"
is_nan(Number) :- number_codes(Number, [78,97,78|_]).   %% "NaN"

but that seems awkward and slow. Any other improvements on the code would also be welcome.

Sincerely, Mark.

On 01/06/2021 16:21, David Warren wrote:
Hi Mark,
add_cvt_type_hook / 2 is a predicate that must be called to have an effect.  You have given it a definition with a fact, which isn't accepted since the definition is already given by the import.  I presume you want to execute that predicate.  To do that you should change the line indicated as error to:
:-  add_cvt_type_hook(date,date_converter(_,_)).
Which will cause it to be called when the file is loaded.
(Actually, it is better form, I think, (even though I rarely use it) to write:
?-  add_cvt_type_hook(date,date_converter(_,_)).
)
Thanks for your work on dates.
-David

On Tue, Jun 1, 2021 at 9:36 AM Mark Clements (gmail) <samuel....@gmail.com> wrote:

Mark Clements (gmail)

unread,
Jun 8, 2021, 10:08:07 AM6/8/21
to David Warren, Michael Kifer, Theresa Swift, ErgoAI, Flora-2, and XSB Users Forum

Nice - the following seems to work.

is_nan(Number) :- X is Number, X =\= Number.

Kindly, Mark.

On 08/06/2021 15:55, David Warren wrote:
I certainly don't know much about NaN's, but I did try:

| ?- X is sqrt(-27.0), Y is X, X =:= Y.

no
| ?-
So this tries to take a NaN and compare it to itself to see if the comparison fails, under the assumption that they are the only values for which equality with themselves is false.  The issue is how to get that copy, and it seems the is/2 does it.  And you need to use a numeric comparison =:=.
This hack seems to work here, but I have no idea how robust it is.  I did note that:

| ?- X is 5.0/0.0, Y is X, X =:= Y.

X = inf.0
Y = inf.0

yes
| ?-
which means it doesn't include inf's.

I hope this helps more than hurts.
Best,
David
Reply all
Reply to author
Forward
0 new messages