Page 1 of 1

Time conversion

Posted: 2 Feb 2016 18:37
by Tonton Luc

Is it possible to convert "05:56PM" to "17:56" with VP 7.2 ?

Re: Time conversion

Posted: 3 Feb 2016 0:16
by Tonton Luc

Is it possible to convert "05:56PM" to "17:56" with VP 7.2 ?
...or to recover "17:56" from "05:56PM" ?

I've tried this following code without success :

Code: Select all

        GMT = gmtTime::new(),         TF = timeFormatter::new(),         TF:setFromString(GMT,"05:56PM","hh:mmtt"),

Dump: 2016/02/03 01:15:15
Exception: wrongStringFormat (com/visual-prolog/time/time_exception)

Input string does not correspond to format string and therefore cannot be converted

Time as string = 05:56PM
Format picture = hh:mmtt

Posted: 3 Feb 2016 9:33
by Thomas Linder Puls
Well, the problem is that AM/PM is defined in the "locale". And you use (due to your computer settings) a locale that does not have any AM/PM constants.

While parsing the time you must use a locale that does have AM/PM, for example US English (I suggest you restore the locale after parsing):

Code: Select all

clauses     run() :-         T = time::newFromGMT(0),         Locale = T:locale,         T:locale :=             locale_api::makeLcId(                 locale_api::makeLangId(locale_native::lang_english, locale_native::sublang_english_us),                 locale_native::sort_default),         T:setFromString("05:56PM", "hh:mmtt"),         T:locale := Locale,         write(T:formatTime()).
Notice that the locale has much higher influence on things related to weekdays and months.

Also notice that this code uses your time zone (and since it is used both when parsing and printing the "numerical" time does not shift).

Posted: 3 Feb 2016 10:00
by Thomas Linder Puls
You can also create a single timeFormatter that you use for this kind of parsing:

Code: Select all

class facts     amPm : timeFormatter := mk_amPm().   class predicates     mk_amPm : () -> timeFormatter AmPmFormatter. clauses     mk_amPm() = AmPm :-         AmPm = timeFormatter::new(),         AmPm:locale :=             locale_api::makeLcId(                 locale_api::makeLangId(locale_native::lang_english, locale_native::sublang_english_us),                 locale_native::sort_default).   clauses     run() :-         T = time::newFromGMT(0),         amPm:setFromString(T, "05:56PM", "hh:mmtt"),         write(T:formatTime()).

Posted: 3 Feb 2016 12:13
by Tonton Luc
Ok thanks. Works fine.

And from this string "28-12-2016-05:56PM", how to recover "28-12-2016-17:56" ?

This following code works not fine :

Code: Select all

                T = time::newFromGMT(0),                 amPm:setFromString(T, "28-12-2016-05:56PM", "dd-MM-yyyy-hh:mmtt"),  
because the result = "1-02-2016-16:56".


Posted: 3 Feb 2016 13:31
by Thomas Linder Puls
I cannot reproduce that problem:

Code: Select all

clauses     run() :-         T = time::newFromGMT(0),              amPm:setFromString(T, "28-12-2016-05:56PM", "dd-MM-yyyy-hh:mmtt"),              write(T:formatShortDate(), " ", T:formatTime()).
I get this:
<pre>28-12-2016 17:56:00</pre>

Posted: 3 Feb 2016 14:14
by Tonton Luc

Works fine too on my machine.
I've make a mistake about the Result in my previous post. Sorry ! :roll:


Posted: 4 Feb 2016 10:59
by Thomas Linder Puls