Discussions related to Visual Prolog
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Time conversion

Unread post by Tonton Luc »

Hi,

Is it possible to convert "05:56PM" to "17:56" with VP 7.2 ?
Last edited by Tonton Luc on 3 Feb 2016 0:18, edited 1 time in total.
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Re: Time conversion

Unread post by Tonton Luc »

Hi,

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
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post 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).
Regards Thomas Linder Puls
PDC
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post 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()).
Regards Thomas Linder Puls
PDC
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Unread post 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".

:?:
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post 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>
Regards Thomas Linder Puls
PDC
User avatar
Tonton Luc
VIP Member
Posts: 204
Joined: 16 Oct 2001 23:01

Unread post by Tonton Luc »

Hi,

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

Thanks.
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

Fine.
Regards Thomas Linder Puls
PDC
Post Reply