///<summary> ///Convert date time to DLMS bytes. ///</summary> ///<param name="buff"> ///Byte buffer where data is write. ///</param> ///<param name="value"> ///Added value. ///</param> private static void SetDateTime(GXDLMSSettings settings, GXByteBuffer buff, object value) { GXDateTime dt; if (value is GXDateTime) { dt = (GXDateTime)value; } else if (value is DateTime) { dt = new GXDateTime((DateTime)value); dt.Skip = dt.Skip | DateTimeSkips.Ms; } else if (value is string) { dt = new GXDateTime(DateTime.Parse((string)value)); dt.Skip = dt.Skip | DateTimeSkips.Ms; } else { throw new Exception("Invalid date format."); } if (dt.Value.UtcDateTime == DateTime.MinValue) { dt.Value = DateTime.SpecifyKind(new DateTime(2000, 1, 1).Date, DateTimeKind.Utc); } else if (dt.Value.UtcDateTime == DateTime.MaxValue) { dt.Value = DateTime.SpecifyKind(DateTime.Now.AddYears(1).Date, DateTimeKind.Utc); } DateTimeOffset tm = dt.Value; if ((dt.Skip & DateTimeSkips.Year) == 0) { buff.SetUInt16((ushort)tm.Year); } else { buff.SetUInt16(0xFFFF); } if ((dt.Skip & DateTimeSkips.Month) == 0) { if (dt.DaylightSavingsBegin) { buff.SetUInt8(0xFE); } else if (dt.DaylightSavingsEnd) { buff.SetUInt8(0xFD); } else { buff.SetUInt8((byte)tm.Month); } } else { buff.SetUInt8(0xFF); } if ((dt.Skip & DateTimeSkips.Day) == 0) { buff.SetUInt8((byte)tm.Day); } else { buff.SetUInt8(0xFF); } // Add week day if ((dt.Skip & DateTimeSkips.DayOfWeek) != 0) { buff.SetUInt8(0xFF); } else { if (dt.Value.DayOfWeek == DayOfWeek.Sunday) { buff.SetUInt8(7); } else { buff.SetUInt8((byte)(dt.Value.DayOfWeek)); } } //Add time. if ((dt.Skip & DateTimeSkips.Hour) == 0) { buff.SetUInt8((byte)tm.Hour); } else { buff.SetUInt8(0xFF); } if ((dt.Skip & DateTimeSkips.Minute) == 0) { buff.SetUInt8((byte)tm.Minute); } else { buff.SetUInt8(0xFF); } if ((dt.Skip & DateTimeSkips.Second) == 0) { buff.SetUInt8((byte)tm.Second); } else { buff.SetUInt8(0xFF); } if ((dt.Skip & DateTimeSkips.Ms) == 0) { buff.SetUInt8((byte)(tm.Millisecond / 10)); } else { buff.SetUInt8((byte)0xFF); //Hundredths of second is not used. } //Add deviation. if ((dt.Skip & DateTimeSkips.Devitation) == 0) { if (settings != null && settings.UtcTimeZone) { buff.SetInt16((Int16)(dt.Value.Offset.TotalMinutes)); } else { buff.SetInt16((Int16)(-dt.Value.Offset.TotalMinutes)); } } else //deviation not used. { buff.SetUInt16(0x8000); } //Add clock_status if (dt.Value.LocalDateTime.IsDaylightSavingTime()) { buff.SetUInt8((byte)(dt.Status | ClockStatus.DaylightSavingActive)); } else { buff.SetUInt8((byte)dt.Status); } }