From 689e7ffd96d9ca377760f862d105fb2eaebb1278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20P=C4=85gowski?= Date: Wed, 23 Aug 2023 00:53:29 +0200 Subject: [PATCH] Increase the maximum date The spec goes up to a full 2168 year, but the PLC date type goes up to 2169 June 06 which is represented by 65535 (max ushort value). --- S7.Net/Types/Date.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/S7.Net/Types/Date.cs b/S7.Net/Types/Date.cs index 980742e..ef6cc79 100644 --- a/S7.Net/Types/Date.cs +++ b/S7.Net/Types/Date.cs @@ -15,8 +15,14 @@ namespace S7.Net.Types /// /// Maximum allowed date for the IEC date type + /// + /// Although the spec allows only a max date of 31-12-2168, the PLC IEC date goes up to 06-06-2169 (which is the actual + /// WORD max value - 65535) + /// /// - public static readonly System.DateTime IecMaxDate = new(year: 2168, month: 12, day: 31); + public static readonly System.DateTime IecMaxDate = new(year: 2169, month: 06, day: 06); + + private static readonly ushort MaxNumberOfDays = (ushort)(IecMaxDate - IecMinDate).TotalDays; /// /// Converts a word (2 bytes) to IEC date () @@ -29,6 +35,12 @@ namespace S7.Net.Types } var daysSinceDateStart = Word.FromByteArray(bytes); + if (daysSinceDateStart > MaxNumberOfDays) + { + throw new ArgumentException($"Read number exceeded the number of maximum days in the IEC date (read: {daysSinceDateStart}, max: {MaxNumberOfDays})", + nameof(bytes)); + } + return IecMinDate.AddDays(daysSinceDateStart); }