Discussions related to Visual Prolog
billanova
Posts: 5
Joined: 26 Jul 2022 5:50

Date time and hour

Unread post by billanova »

Hi everyone,
How to show datetime in a static text ?

i want to show it on o form with format like dd-mm-yyyy HH:mm:ss
Harrison Pratt
VIP Member
Posts: 412
Joined: 5 Nov 2000 0:01

Re: Date time and hour

Unread post by Harrison Pratt »

How to get formatted time:

Code: Select all

clauses     run() :-         Tnow = time::now(),         TimeStamp = Tnow:formatDateTime("dd-MM-yyyy", " HH:mm:ss"),         stdio::write("\nTime now: ", TimeStamp),         _ = console::readLine().
It's up to you to determine if, how often and how you want to update the text box.

If you are just displaying it once when time the dialog or form is displayed you can do that either in the constructor ( new:( window ParentWin):- ... or in the onShow predicate.

Code: Select all

... myStaticTextCtrl:setText( TimeStamp), ...
billanova
Posts: 5
Joined: 26 Jul 2022 5:50

Re: Date time and hour

Unread post by billanova »

i want to update it every second. but i cannot configure to run infinite on a different thread . i have limited knowledge of visual prolog.
Harrison Pratt
VIP Member
Posts: 412
Joined: 5 Nov 2000 0:01

Re: Date time and hour

Unread post by Harrison Pratt »

This code snippet should do it for you.
On dialog creation you can start a timer with the interval you want, e.g. 1000 milliseconds.
Store the timer's handle in a static fact.
Use the IDE to create an onTimer/2 event that is invoked whenever the timer passes the threshold.
Update the time display in the onTimer/2 event clause.

Code: Select all

facts     dlgTimer : timerHandle := erroneous.   clauses     display(Parent) = Dialog :-         Dialog = new(Parent),         Dialog:show().   clauses     new(Parent) :-         dialog::new(Parent),         generatedInitialize(),         %--         dlgTimer := This:timerSet(1000).   predicates     onTimer : window::timerListener. clauses     onTimer(_Source, dlgTimer) :-         Tnow = time::now(),         TimeStamp = Tnow:formatDateTime("dd-MM-yyyy", " HH:mm:ss"),         staticText_ctl:setText(TimeStamp),         !.     onTimer(_, _).
User avatar
Thomas Linder Puls
VIP Member
Posts: 1351
Joined: 28 Feb 2000 0:01

Re: Date time and hour

Unread post by Thomas Linder Puls »

Using timers like that is somewhat "old fashioned", it has the disadvantage that it "mixes" all your timers in a single handler. Using tickAction's (and delayCall for one shots) has a more direct handler approach:

Code: Select all

clauses     new() :-         applicationWindow::new(),         generatedInitialize(),         _ =             tickAction(1000,                 {  :-                     Tnow = time::now(),                     TimeStamp = Tnow:formatDateTime("dd-MM-yyyy", " HH:mm:ss"),                     staticText_ctl:setText(TimeStamp)                 }).
Here I ignore the returned timerHandle because you only need it for killing (=stopping) the tickAction, which I don't believe is relevant here.
Regards Thomas Linder Puls
PDC
Harrison Pratt
VIP Member
Posts: 412
Joined: 5 Nov 2000 0:01

Re: Date time and hour

Unread post by Harrison Pratt »

Thanks! :-)
Post Reply