Page 1 of 1

Date time and hour

Posted: 10 Mar 2023 8:55
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

Re: Date time and hour

Posted: 11 Mar 2023 14:57
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), ...

Re: Date time and hour

Posted: 12 Mar 2023 15:55
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.

Re: Date time and hour

Posted: 12 Mar 2023 18:01
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(_, _).

Re: Date time and hour

Posted: 13 Mar 2023 9:03
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.

Re: Date time and hour

Posted: 13 Mar 2023 14:18
by Harrison Pratt
Thanks! :-)