DateTimeExtensions.cs 673 B

1234567891011121314151617181920212223
  1. using System;
  2. using S7.Net.Types;
  3. using DateTime = System.DateTime;
  4. namespace S7.Net.Helper
  5. {
  6. public static class DateTimeExtensions
  7. {
  8. public static ushort GetDaysSinceIecDateStart(this DateTime dateTime)
  9. {
  10. if (dateTime < Date.IecMinDate)
  11. {
  12. throw new ArgumentOutOfRangeException($"DateTime must be at least {Date.IecMinDate:d}");
  13. }
  14. if (dateTime > Date.IecMaxDate)
  15. {
  16. throw new ArgumentOutOfRangeException($"DateTime must be lower than {Date.IecMaxDate:d}");
  17. }
  18. return (ushort)(dateTime - Date.IecMinDate).TotalDays;
  19. }
  20. }
  21. }