PLC-lang / StandardFunctions

Standard functions for PLC as described by IEC61131
GNU Lesser General Public License v2.1
10 stars 2 forks source link

Loss of information when converting LDT to LTOD #55

Closed mhasel closed 1 year ago

mhasel commented 2 years ago

In the following function, timestamp_subsec_millis should be timestamp_subsec_nanos and consequently .and_hms_milli needs to be and_hms_nano. Otherwise the trailing 6 digits are always going to be 0, regardless of the input.

pub extern "C" fn DATE_AND_TIME_TO_TIME_OF_DAY(input: i64) -> i64 {
    let date_time = chrono::Utc.timestamp_nanos(input);

    // --snip
    let milli = date_time.timestamp_subsec_millis();

    let new_date_time =
        chrono::NaiveDate::from_ymd(1970, 1, 1).and_hms_milli(hour, min, sec, milli);

    new_date_time.timestamp_nanos()
}