Page 1 of 1

(Date) string to local time conversion

Posted: 2 Dec 2014 11:30
by Peter Muraya
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.

Posted: 2 Dec 2014 12:27
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.