----- Original Message -----
From: "mauro" <mauro@xxxxxxxxxxx>
To: <prolog@xxxxxxxxxxxxxx>
Sent: Wednesday, December 14, 2005 2:07 PM
Subject: [SWIPL] strtime equivalent any hint ?
Mauro,
>
> I was searching for a strtime equivalent in swi prolog the
> convert_time/2 or /8 is one way. Am i missing something ?
>
AFAIK there is no built-in 'strftime' [or equivalent] predicate, merely the
means for obtaining the UNIX Epoch Time [via 'get_time/1'], and for
splitting that value into component parts [via the 'convert_time'
predicates].
Of course it is possible to obtain such functionality, though the degree of
effort needed to do so, varies:
* Via the 'pipe/1' predicate to access the UNIX 'date' utility
* Via the Foreign Function Interface to the C Library's 'strftime'
function
* Write your own formatting / conversion routines in Prolog
The first approach is probably the simplest, though the least efficient
[because you have to spawn a new process to effect the conversion], and
least elegant [because you have to direct output to a file then read that
file in, so as to make effective use of it]. A very crude example of
executing, then trapping a single line of output, is shown:
trap(Cmd, Output) :-
open(pipe(Cmd), read, Input),
read_line_to_codes(Input, Output),
close(Input).
You could then use the UNIX 'date' utility as follows:
trap('date "+%c"', Output), atom_codes(A, Output).
Of course, this could be greatly expanded and streamlined. Hopefully,
though, the general strategy is clear.
The second approach is probably both the most efficient and elegant, but
also, probably, the most work as it requires you to get your hands dirty
using the C compiler and linker. Space doesn't permit inclusion of an
example. The 'Foreign Language Interface' section of the Help file does,
however, offer in-depth commentary and a reasonably comprehensive example.
The last approach is quite straighforward: write your own conversion
predicates :) ! Not really as difficult as you might think since these break
down into two categories:
* Format routines i.e. basically present easily-obtainable values
in different layouts
* Specialist conversion routines e.g. Julian date conversion
The first category is pretty easy to implement, especially once you become
familiar with some of the SWI built-in predicates like 'atom_concat',
'atom_codes', 'string_concat', and 'sformat' [not to mention many others!].
All I can add is: read throught the Help, and get busy doodling :) !
You could try Googling for implementations of items in the second category.
If you don't find any suitable Prolog code you could try converting across
examples in other languages, though this is perhaps best done by a more
experienced person [it *is*, however, a pretty interesting learning exercise
!].
>
> Please any help would be appreciated.
>
Well, I hoped that helped :) !
>
> Otherwise a pointer to some explanation of how to convert
> an iso time representition to a timestamp could be useful.
>
Which ISO time format are you wishing to obtain ? You could use the UNIX
'date' utility to do this:
trap('date "+%Y-%m-%d"', Output), atom_codes(YMD, Output).
trap('date "+%Y-%j"', Output), atom_codes(YDD, Output).
trap('date "+%Y-W-%V-%u"', Output), atom_codes(YWDN, Output).
trap('date "+%H:%M:%S"', Output), atom_codes(HMS, Output).
or just concatenate the component values obtained from 'convert_time/8', or
your own conversion predicates [assumes the following items have been
instantiated]:
get_time(T), convert_time(T, Yr, Mo, Da, Hr, Mi, Se, Milli), ...
..., concat_atom([Yr, Mo, Da], '-', YMD).
..., concat_atom([Ho, Mi, Se], ':', HMS).
Hopefully enough has been said to get you started :) !
Cheers,
Anthony Borla
|