Hi,
Is it possible to convert "05:56PM" to "17:56" with VP 7.2 ?
-
- VIP Member
- Posts: 204
- Joined: 16 Oct 2001 23:01
Time conversion
Last edited by Tonton Luc on 3 Feb 2016 0:18, edited 1 time in total.
-
- VIP Member
- Posts: 204
- Joined: 16 Oct 2001 23:01
Re: Time conversion
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 :
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
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01
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):
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).
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()).
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
PDC
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01
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
PDC
-
- VIP Member
- Posts: 204
- Joined: 16 Oct 2001 23:01
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 :because the result = "1-02-2016-16:56".

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"),

-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01
I cannot reproduce that problem:
I get this:
<pre>28-12-2016 17:56:00</pre>
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()).
<pre>28-12-2016 17:56:00</pre>
Regards Thomas Linder Puls
PDC
PDC
-
- VIP Member
- Posts: 204
- Joined: 16 Oct 2001 23:01
-
- VIP Member
- Posts: 1466
- Joined: 28 Feb 2000 0:01