Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
date types changed unit form milli to nano see PLC-lang/rusty#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
99NIMI committed Aug 22, 2022
1 parent 405f143 commit df340f6
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 332 deletions.
8 changes: 4 additions & 4 deletions src/date_time_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use chrono::{TimeZone, Timelike};
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn DATE_AND_TIME_TO_DATE(input: i64) -> i64 {
let date_time = chrono::Utc.timestamp_millis(input);
let date_time = chrono::Utc.timestamp_nanos(input);

let new_date_time = date_time.date().and_hms(0, 0, 0);
new_date_time.timestamp_millis()
new_date_time.timestamp_nanos()
}

/// .
Expand All @@ -18,7 +18,7 @@ pub extern "C" fn DATE_AND_TIME_TO_DATE(input: i64) -> i64 {
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn DATE_AND_TIME_TO_TIME_OF_DAY(input: i64) -> i64 {
let date_time = chrono::Utc.timestamp_millis(input);
let date_time = chrono::Utc.timestamp_nanos(input);
let hour = date_time.hour();
let min = date_time.minute();
let sec = date_time.second();
Expand All @@ -27,5 +27,5 @@ pub extern "C" fn DATE_AND_TIME_TO_TIME_OF_DAY(input: i64) -> i64 {
let new_date_time =
chrono::NaiveDate::from_ymd(1970, 1, 1).and_hms_milli(hour, min, sec, milli);

new_date_time.timestamp_millis()
new_date_time.timestamp_nanos()
}
48 changes: 24 additions & 24 deletions src/date_time_extra_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use chrono::{Datelike, TimeZone, Timelike};
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn CONCAT_DATE_TOD(in1: i64, in2: i64) -> i64 {
let date = chrono::Utc.timestamp_millis(in1).date();
let tod = chrono::Utc.timestamp_millis(in2);
let date = chrono::Utc.timestamp_nanos(in1).date();
let tod = chrono::Utc.timestamp_nanos(in2);
let hour = tod.hour();
let min = tod.minute();
let sec = tod.second();
let milli = tod.timestamp_subsec_millis();

date.and_hms_milli(hour, min, sec, milli).timestamp_millis()
date.and_hms_milli(hour, min, sec, milli).timestamp_nanos()
}

/// .
Expand Down Expand Up @@ -78,7 +78,7 @@ pub extern "C" fn CONCAT_DATE__ULINT(in1: u64, in2: u64, in3: u64) -> i64 {
pub extern "C" fn concat_date(in1: i32, in2: u32, in3: u32) -> i64 {
let date = chrono::NaiveDate::from_ymd(in1, in2, in3);
let dt = date.and_hms(0, 0, 0);
dt.timestamp_millis()
dt.timestamp_nanos()
}

/// .
Expand Down Expand Up @@ -161,7 +161,7 @@ pub extern "C" fn CONCAT_TOD__ULINT(in1: u64, in2: u64, in3: u64, in4: u64) -> i
pub extern "C" fn concat_tod(in1: u32, in2: u32, in3: u32, in4: u32) -> i64 {
let date = chrono::NaiveDate::from_ymd(1970, 1, 1);
let dt = date.and_hms_milli(in1, in2, in3, in4);
dt.timestamp_millis()
dt.timestamp_nanos()
}

/// .
Expand All @@ -170,7 +170,7 @@ pub extern "C" fn concat_tod(in1: u32, in2: u32, in3: u32, in4: u32) -> i64 {
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn SPLIT_DATE__INT(in1: i64, out1: &mut i16, out2: &mut i16, out3: &mut i16) -> i16 {
let date = chrono::Utc.timestamp_millis(in1).date();
let date = chrono::Utc.timestamp_nanos(in1).date();
// if year does not fit in target data type -> panic
*out1 = date.year().try_into().unwrap();
*out2 = date.month() as i16;
Expand All @@ -191,7 +191,7 @@ pub extern "C" fn SPLIT_DATE__UINT(
out2: &mut u16,
out3: &mut u16,
) -> i16 {
let date = chrono::Utc.timestamp_millis(in1).date();
let date = chrono::Utc.timestamp_nanos(in1).date();
// if year does not fit in target data type -> panic
*out1 = date.year().try_into().unwrap();
*out2 = date.month() as u16;
Expand All @@ -211,7 +211,7 @@ pub extern "C" fn SPLIT_DATE__DINT(
out2: &mut i32,
out3: &mut i32,
) -> i16 {
let date = chrono::Utc.timestamp_millis(in1).date();
let date = chrono::Utc.timestamp_nanos(in1).date();
*out1 = date.year();
*out2 = date.month() as i32;
*out3 = date.day() as i32;
Expand All @@ -230,7 +230,7 @@ pub extern "C" fn SPLIT_DATE__UDINT(
out2: &mut u32,
out3: &mut u32,
) -> i16 {
let date = chrono::Utc.timestamp_millis(in1).date();
let date = chrono::Utc.timestamp_nanos(in1).date();
// if year does not fit in target data type -> panic
*out1 = date.year().try_into().unwrap();
*out2 = date.month() as u32;
Expand All @@ -250,7 +250,7 @@ pub extern "C" fn SPLIT_DATE__LINT(
out2: &mut i64,
out3: &mut i64,
) -> i16 {
let date = chrono::Utc.timestamp_millis(in1).date();
let date = chrono::Utc.timestamp_nanos(in1).date();
// if year does not fit in target data type -> panic
*out1 = date.year().try_into().unwrap();
*out2 = date.month() as i64;
Expand All @@ -271,7 +271,7 @@ pub extern "C" fn SPLIT_DATE__ULINT(
out2: &mut u64,
out3: &mut u64,
) -> i16 {
let date = chrono::Utc.timestamp_millis(in1).date();
let date = chrono::Utc.timestamp_nanos(in1).date();
// if year does not fit in target data type -> panic
*out1 = date.year().try_into().unwrap();
*out2 = date.month() as u64;
Expand All @@ -292,7 +292,7 @@ pub extern "C" fn SPLIT_TOD__INT(
out3: &mut i16,
out4: &mut i16,
) -> i16 {
let tod = chrono::Utc.timestamp_millis(in1);
let tod = chrono::Utc.timestamp_nanos(in1);
*out1 = tod.hour() as i16;
*out2 = tod.minute() as i16;
*out3 = tod.second() as i16;
Expand All @@ -313,7 +313,7 @@ pub extern "C" fn SPLIT_TOD__UINT(
out3: &mut u16,
out4: &mut u16,
) -> i16 {
let tod = chrono::Utc.timestamp_millis(in1);
let tod = chrono::Utc.timestamp_nanos(in1);
*out1 = tod.hour() as u16;
*out2 = tod.minute() as u16;
*out3 = tod.second() as u16;
Expand All @@ -334,7 +334,7 @@ pub extern "C" fn SPLIT_TOD__DINT(
out3: &mut i32,
out4: &mut i32,
) -> i16 {
let tod = chrono::Utc.timestamp_millis(in1);
let tod = chrono::Utc.timestamp_nanos(in1);
*out1 = tod.hour() as i32;
*out2 = tod.minute() as i32;
*out3 = tod.second() as i32;
Expand All @@ -355,7 +355,7 @@ pub extern "C" fn SPLIT_TOD__UDINT(
out3: &mut u32,
out4: &mut u32,
) -> i16 {
let tod = chrono::Utc.timestamp_millis(in1);
let tod = chrono::Utc.timestamp_nanos(in1);
*out1 = tod.hour() as u32;
*out2 = tod.minute() as u32;
*out3 = tod.second() as u32;
Expand All @@ -376,7 +376,7 @@ pub extern "C" fn SPLIT_TOD__LINT(
out3: &mut i64,
out4: &mut i64,
) -> i16 {
let tod = chrono::Utc.timestamp_millis(in1);
let tod = chrono::Utc.timestamp_nanos(in1);
*out1 = tod.hour() as i64;
*out2 = tod.minute() as i64;
*out3 = tod.second() as i64;
Expand All @@ -397,7 +397,7 @@ pub extern "C" fn SPLIT_TOD__ULINT(
out3: &mut u64,
out4: &mut u64,
) -> i16 {
let tod = chrono::Utc.timestamp_millis(in1);
let tod = chrono::Utc.timestamp_nanos(in1);
*out1 = tod.hour() as u64;
*out2 = tod.minute() as u64;
*out3 = tod.second() as u64;
Expand All @@ -421,7 +421,7 @@ pub extern "C" fn SPLIT_DT__INT(
out6: &mut i16,
out7: &mut i16,
) -> i16 {
let dt = chrono::Utc.timestamp_millis(in1);
let dt = chrono::Utc.timestamp_nanos(in1);
// if year does not fit in target data type -> panic
*out1 = dt.year().try_into().unwrap();
*out2 = dt.month() as i16;
Expand Down Expand Up @@ -449,7 +449,7 @@ pub extern "C" fn SPLIT_DT__UINT(
out6: &mut u16,
out7: &mut u16,
) -> i16 {
let dt = chrono::Utc.timestamp_millis(in1);
let dt = chrono::Utc.timestamp_nanos(in1);
// if year does not fit in target data type -> panic
*out1 = dt.year().try_into().unwrap();
*out2 = dt.month() as u16;
Expand Down Expand Up @@ -477,7 +477,7 @@ pub extern "C" fn SPLIT_DT__DINT(
out6: &mut i32,
out7: &mut i32,
) -> i16 {
let dt = chrono::Utc.timestamp_millis(in1);
let dt = chrono::Utc.timestamp_nanos(in1);
*out1 = dt.year();
*out2 = dt.month() as i32;
*out3 = dt.day() as i32;
Expand All @@ -504,7 +504,7 @@ pub extern "C" fn SPLIT_DT__UDINT(
out6: &mut u32,
out7: &mut u32,
) -> i16 {
let dt = chrono::Utc.timestamp_millis(in1);
let dt = chrono::Utc.timestamp_nanos(in1);
// if year does not fit in target data type -> panic
*out1 = dt.year().try_into().unwrap();
*out2 = dt.month() as u32;
Expand Down Expand Up @@ -532,7 +532,7 @@ pub extern "C" fn SPLIT_DT__LINT(
out6: &mut i64,
out7: &mut i64,
) -> i16 {
let dt = chrono::Utc.timestamp_millis(in1);
let dt = chrono::Utc.timestamp_nanos(in1);
// if year does not fit in target data type -> panic
*out1 = dt.year().try_into().unwrap();
*out2 = dt.month() as i64;
Expand Down Expand Up @@ -560,7 +560,7 @@ pub extern "C" fn SPLIT_DT__ULINT(
out6: &mut u64,
out7: &mut u64,
) -> i16 {
let dt = chrono::Utc.timestamp_millis(in1);
let dt = chrono::Utc.timestamp_nanos(in1);
// if year does not fit in target data type -> panic
*out1 = dt.year().try_into().unwrap();
*out2 = dt.month() as u64;
Expand All @@ -579,6 +579,6 @@ pub extern "C" fn SPLIT_DT__ULINT(
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn DAY_OF_WEEK(in1: i64) -> i8 {
let date = chrono::Utc.timestamp_millis(in1);
let date = chrono::Utc.timestamp_nanos(in1);
date.weekday().num_days_from_sunday() as i8
}
26 changes: 14 additions & 12 deletions src/date_time_numeric_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use chrono::TimeZone;
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn ADD_TIME(in1: i64, in2: i64) -> i64 {
chrono::Duration::milliseconds(in1)
.checked_add(&chrono::Duration::milliseconds(in2))
chrono::Duration::nanoseconds(in1)
.checked_add(&chrono::Duration::nanoseconds(in2))
.unwrap()
.num_nanoseconds()
.unwrap()
.num_milliseconds()
}

/// .
Expand All @@ -35,10 +36,10 @@ pub extern "C" fn ADD_DT_TIME(in1: i64, in2: i64) -> i64 {

fn add_datetime_time(in1: i64, in2: i64) -> i64 {
chrono::Utc
.timestamp_millis(in1)
.timestamp_nanos(in1)
.checked_add_signed(chrono::Duration::nanoseconds(in2))
.unwrap()
.timestamp_millis()
.timestamp_nanos()
}

/// .
Expand All @@ -48,10 +49,11 @@ fn add_datetime_time(in1: i64, in2: i64) -> i64 {
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn SUB_TIME(in1: i64, in2: i64) -> i64 {
chrono::Duration::milliseconds(in1)
.checked_sub(&chrono::Duration::milliseconds(in2))
chrono::Duration::nanoseconds(in1)
.checked_sub(&chrono::Duration::nanoseconds(in2))
.unwrap()
.num_nanoseconds()
.unwrap()
.num_milliseconds()
}

/// .
Expand Down Expand Up @@ -86,8 +88,8 @@ pub extern "C" fn SUB_TOD_TOD(in1: i64, in2: i64) -> i64 {

fn sub_datetimes(in1: i64, in2: i64) -> i64 {
chrono::Utc
.timestamp_millis(in1)
.signed_duration_since(chrono::Utc.timestamp_millis(in2))
.timestamp_nanos(in1)
.signed_duration_since(chrono::Utc.timestamp_nanos(in2))
.num_nanoseconds()
.unwrap()
}
Expand All @@ -104,10 +106,10 @@ pub extern "C" fn SUB_DT_TIME(in1: i64, in2: i64) -> i64 {

fn sub_datetime_duration(in1: i64, in2: i64) -> i64 {
chrono::Utc
.timestamp_millis(in1)
.timestamp_nanos(in1)
.checked_sub_signed(chrono::Duration::nanoseconds(in2))
.unwrap()
.timestamp_millis()
.timestamp_nanos()
}

/// .
Expand Down
Loading

0 comments on commit df340f6

Please sign in to comment.