Skip to content

Commit

Permalink
PLC-lang#539 changed any date precision to nanoseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
99NIMI committed Aug 17, 2022
1 parent ab04c99 commit 085b7b3
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 95 deletions.
202 changes: 149 additions & 53 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions book/src/datatypes.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ Examples
| Type name | Size | Internally stored as |
|-----------------|--------|------------------------------------|
| TIME | 64 bit | Timespan in nanoseconds |
| TIME\_OF\_DAY | 64 bit | Milliseconds since Jan 1, 1970 UTC |
| DATE | 64 bit | Milliseconds since Jan 1, 1970 UTC |
| DATE\_AND\_TIME | 64 bit | Milliseconds since Jan 1, 1970 UTC |
| TIME\_OF\_DAY | 64 bit | Nanoseconds since Jan 1, 1970 UTC |
| DATE | 64 bit | Nanoseconds since Jan 1, 1970 UTC |
| DATE\_AND\_TIME | 64 bit | Nanoseconds since Jan 1, 1970 UTC |

Note that the internal representation and sizes of these types are specific
to RuSTy and not defined by the IEC61131 standard.

### DATE
The `DATE` datatype is used to represent a Date in the Gregorian Calendar. Such a value is
stored as an i64 with a precision in milliseconds and denotes the number of milliseconds
stored as an i64 with a precision in nanoseconds and denotes the number of nanoseconds
that have elapsed since January 1, 1970 UTC not counting leap seconds. DATE literals start
with `DATE#` or `D#` followed by a date in the format of `yyyy-mm-dd`.

Expand All @@ -96,8 +96,8 @@ Examples

### DATE_AND_TIME
The `DATE_AND_TIME` datatype is used to represent a certain point in time in the Gregorian Calendar.
Such a value is stored as an `i64` with a precision in milliseconds and denotes the
number of milliseconds that have elapsed since January 1, 1970 UTC not counting leap seconds.
Such a value is stored as an `i64` with a precision in nanoseconds and denotes the
number of nanoseconds that have elapsed since January 1, 1970 UTC not counting leap seconds.
DATE_AND_TIME literals start with `DATE_AND_TIME#` or `DT#` followed by a date and time in the
format of `yyyy-mm-dd-hh:mm:ss`.

Expand All @@ -110,8 +110,8 @@ Examples

### TIME_OF_DAY
The `TIME_OF_DAY` datatype is used to represent a specific moment in time in a day.
Such a value is stored as an `i64` value with a precision in milliseconds and denotes the
number of milliseconds that have elapsed since January 1, 1970 UTC not counting leap seconds.
Such a value is stored as an `i64` value with a precision in nanoseconds and denotes the
number of nanoseconds that have elapsed since January 1, 1970 UTC not counting leap seconds.
Hence this value is stored as a `DATE_AND_TIME` with the day fixed to 1970-01-01.
`TIME_OF_DAY` literals start with `TIME_OF_DAY#` or `TOD#` followed by a time in the
format of `hh:mm:ss`.
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/generators/date_time_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn calculate_time_nano(negative: bool, sec: f64, milli: f64, micro: f64, nan
}
}

/// calculates the milliseconds since 1970-01-01-00:00:00 for the given
/// calculates the nanoseconds since 1970-01-01-00:00:00 for the given
/// point in time
pub fn calculate_date_time(
year: i32,
Expand All @@ -37,7 +37,7 @@ pub fn calculate_date_time(
.ymd_opt(year, month, day)
.and_hms_milli_opt(hour, min, sec, milli)
{
return Ok(date_time.timestamp_millis());
return Ok(date_time.timestamp_nanos());
}
Err(format!(
"Invalid Date {}-{}-{}-{}:{}:{}.{}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: src/codegen/tests/code_gen_tests.rs
assertion_line: 493
expression: result

---
; ModuleID = 'main'
source_filename = "main"
Expand All @@ -17,13 +17,13 @@ entry:
%c = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 2
%d = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 3
%load_a = load i64, i64* %a, align 4
%tmpVar = icmp sgt i64 %load_a, 1619827200000
%tmpVar = icmp sgt i64 %load_a, 1619827200000000000
%load_b = load i64, i64* %b, align 4
%tmpVar1 = icmp sgt i64 %load_b, 1619897357000
%tmpVar1 = icmp sgt i64 %load_b, 1619897357000000000
%load_c = load i64, i64* %c, align 4
%tmpVar2 = icmp sgt i64 %load_c, 156557000000000
%load_d = load i64, i64* %d, align 4
%tmpVar3 = icmp sgt i64 %load_d, 70157000
%tmpVar3 = icmp sgt i64 %load_d, 70157000000000
ret void
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ entry:
%x = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 1
%y = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 2
%z = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 3
store i64 56190123, i64* %w, align 4
store i64 56190123, i64* %w, align 4
store i64 56190123000000, i64* %w, align 4
store i64 56190123000000, i64* %w, align 4
store i64 100012000000, i64* %x, align 4
store i64 100012000000, i64* %x, align 4
store i64 465436800000, i64* %y, align 4
store i64 465436800000000000, i64* %y, align 4
store i64 0, i64* %y, align 4
store i64 465509714000, i64* %z, align 4
store i64 58804123, i64* %z, align 4
store i64 58804123, i64* %z, align 4
store i64 946757700000, i64* %z, align 4
store i64 946757700000, i64* %z, align 4
store i64 946757708123, i64* %z, align 4
store i64 465509714000000000, i64* %z, align 4
store i64 58804123000000, i64* %z, align 4
store i64 58804123000000, i64* %z, align 4
store i64 946757700000000000, i64* %z, align 4
store i64 946757700000000000, i64* %z, align 4
store i64 946757708123000000, i64* %z, align 4
ret void
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ entry:
%x = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 1
%y = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 2
%z = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 3
store i64 56190123, i64* %w, align 4
store i64 56190123, i64* %w, align 4
store i64 56190123000000, i64* %w, align 4
store i64 56190123000000, i64* %w, align 4
store i64 100012000000, i64* %x, align 4
store i64 100012000000, i64* %x, align 4
store i64 465436800000, i64* %y, align 4
store i64 465436800000000000, i64* %y, align 4
store i64 0, i64* %y, align 4
store i64 465509700000, i64* %z, align 4
store i64 58808123, i64* %z, align 4
store i64 58804123, i64* %z, align 4
store i64 465509700000000000, i64* %z, align 4
store i64 58808123000000, i64* %z, align 4
store i64 58804123000000, i64* %z, align 4
ret void
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ entry:
%z = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 3
store i64 100012000000, i64* %w, align 4
store i64 100012000000, i64* %w, align 4
store i64 465436800000, i64* %x, align 4
store i64 465436800000000000, i64* %x, align 4
store i64 0, i64* %x, align 4
store i64 465509714000, i64* %y, align 4
store i64 58804123, i64* %y, align 4
store i64 56190123, i64* %z, align 4
store i64 56190123, i64* %z, align 4
store i64 465509714000000000, i64* %y, align 4
store i64 58804123000000, i64* %y, align 4
store i64 56190123000000, i64* %z, align 4
store i64 56190123000000, i64* %z, align 4
ret void
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: src/codegen/tests/code_gen_tests.rs
assertion_line: 454
expression: result

---
; ModuleID = 'main'
source_filename = "main"
Expand All @@ -14,13 +14,13 @@ define void @prg(%prg_interface* %0) {
entry:
%y = getelementptr inbounds %prg_interface, %prg_interface* %0, i32 0, i32 0
store i64 0, i64* %y, align 4
store i64 3600000, i64* %y, align 4
store i64 3600001, i64* %y, align 4
store i64 3661000, i64* %y, align 4
store i64 72900000, i64* %y, align 4
store i64 72900000, i64* %y, align 4
store i64 40260000, i64* %y, align 4
store i64 40260000, i64* %y, align 4
store i64 3600000000000, i64* %y, align 4
store i64 3600001000000, i64* %y, align 4
store i64 3661000000000, i64* %y, align 4
store i64 72900000000000, i64* %y, align 4
store i64 72900000000000, i64* %y, align 4
store i64 40260000000000, i64* %y, align 4
store i64 40260000000000, i64* %y, align 4
ret void
}

0 comments on commit 085b7b3

Please sign in to comment.