Discussions related to Visual Prolog
Peter Muraya
VIP Member
Posts: 147
Joined: 5 Dec 2012 7:29

(Date) string to local time conversion

Unread post by Peter Muraya »

Hi,
I'm trying to convert a string formatted as a short date, dd/m/yyyy, to a local time value using code that looks like this:-

Code: Select all

/*     Converts the given string to a local time value*/     str_2_localtimevalue(Str)=LocalTimeValue:-           /*           Create a new time object*/           Time = time::new(),           /*           Set the date/time according to given string (short date) format, dd/mm/yyyy.           (THIS SETTING IS NOT BEING EFFECTED. THE CURRENT TIME SET DURING CREATION OF THE TIME           OBJECT DOES NOT CHANGE. WHY?*/           Time:setFromString(Str),           /*           Retrieve and return the local time value*/           Time:getTime()=LocalTimeValue.
The resulting local time variable is that of the current time, contrary to expectation. What am I not doing right?

Many thanks.
Mutall Data Management Technical Support
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

setFromString only updates those parts of the data/time that can be found in the input, other parts are kept as they were. Furthermore, the one argument version expects a time value formatted acording to your locale settings.

time::new() produces a time set to "now".

A time object and a localTimeValue is both a data and a time, so you have to consider which time-of-day a certain date corresponds to:

This implementation will return the localTimeValue corresponding to 0:00:00 on the corresponding date:

Code: Select all

class predicates     str_2_localtimevalue : (string) -> localTimeValue. clauses     str_2_localtimevalue(Str)=LocalTimeValue:-           Time = time::new(1900, 1, 1, 0, 0, 0),           Time:setFromString(Str, "dd/MM/yyyy"),           Time:getTime()=LocalTimeValue.
Regards Thomas Linder Puls
PDC
Post Reply