forked from S7NetPlus/s7netplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dawid Pągowski
committed
Aug 22, 2023
1 parent
0de9364
commit eb1fad9
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using S7.Net.Types; | ||
using DateTime = System.DateTime; | ||
|
||
namespace S7.Net.Helper | ||
{ | ||
public static class DateTimeExtensions | ||
{ | ||
public static ushort GetDaysSinceIecDateStart(this DateTime dateTime) | ||
{ | ||
if (dateTime < Date.IecMinDate) | ||
{ | ||
throw new ArgumentException($"DateTime must be at least {Date.IecMinDate:d}"); | ||
} | ||
if (dateTime > Date.IecMaxDate) | ||
{ | ||
throw new ArgumentException($"DateTime must be lower than {Date.IecMaxDate:d}"); | ||
} | ||
|
||
return (ushort)(dateTime - Date.IecMinDate).TotalDays; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using S7.Net.Helper; | ||
|
||
namespace S7.Net.Types | ||
{ | ||
/// <summary> | ||
/// Contains the conversion methods to convert Words from S7 plc to C#. | ||
/// </summary> | ||
public static class Date | ||
{ | ||
/// <summary> | ||
/// Minimum allowed date for the IEC date type | ||
/// </summary> | ||
public static readonly System.DateTime IecMinDate = new(year: 1990, month: 01, day: 01); | ||
|
||
/// <summary> | ||
/// Maximum allowed date for the IEC date type | ||
/// </summary> | ||
public static readonly System.DateTime IecMaxDate = new(year: 2168, month: 12, day: 31); | ||
|
||
/// <summary> | ||
/// Converts a word (2 bytes) to IEC date (<see cref="System.DateTime"/>) | ||
/// </summary> | ||
public static System.DateTime FromByteArray(byte[] bytes) | ||
{ | ||
if (bytes.Length != 2) | ||
{ | ||
throw new ArgumentException("Wrong number of bytes. Bytes array must contain 2 bytes."); | ||
} | ||
|
||
var daysSinceDateStart = Word.FromByteArray(bytes); | ||
return IecMinDate.AddDays(daysSinceDateStart); | ||
} | ||
|
||
/// <summary> | ||
/// Converts a <see cref="System.DateTime"/> to word (2 bytes) | ||
/// </summary> | ||
public static byte[] ToByteArray(System.DateTime dateTime) => Word.ToByteArray(dateTime.GetDaysSinceIecDateStart()); | ||
|
||
/// <summary> | ||
/// Converts an array of <see cref="System.DateTime"/>s to an array of bytes | ||
/// </summary> | ||
public static byte[] ToByteArray(System.DateTime[] value) | ||
{ | ||
var arr = new ByteArray(); | ||
foreach (var date in value) | ||
arr.Add(ToByteArray(date)); | ||
return arr.Array; | ||
} | ||
|
||
/// <summary> | ||
/// Converts an array of bytes to an array of <see cref="System.DateTime"/>s | ||
/// </summary> | ||
public static System.DateTime[] ToArray(byte[] bytes) | ||
{ | ||
var values = new System.DateTime[bytes.Length / sizeof(ushort)]; | ||
|
||
for (int i = 0; i < values.Length; i++) | ||
{ | ||
values[i] = FromByteArray( | ||
new[] | ||
{ | ||
bytes[i], bytes[i + 1] | ||
}); | ||
} | ||
|
||
return values; | ||
} | ||
} | ||
} |