using System; namespace DataModel.Helpers { /// /// JDE date/time conversion helpers /// public static class DateTimeHelpers { /// /// Strips time component from datetime /// /// Datetime to strip time component from /// Date component of source datetime public static int ToJDEDate(this DateTime source) { return (source.Year < 2000 ? 0 : 100000) + (source.Year % 100) * 1000 + source.DayOfYear; } public static DateTime FromJDEDate(this int sourceDate) { if (sourceDate == 0) { return new DateTime(1900, 1, 1); } DateTime baseDate = new DateTime(1900, 1, 1); try { string strSource = sourceDate.ToString(); if (strSource.Length < 5 || strSource.Length > 6) { throw new Exception($"invalid source date length ({strSource.Length})"); } if (strSource.StartsWith("1")) { baseDate = new DateTime(2000, 1, 1); } baseDate = baseDate.AddYears(int.Parse(strSource.Substring(1, 2))); baseDate = baseDate.AddDays(int.Parse(strSource.Substring(3)) - 1); } catch { //Do nothing } return baseDate; } /// /// Strips date component from datetime /// /// Datetime to strip date component from /// Time component of source datetime public static int ToJDETime(this DateTime source) { return source.Hour * 10000 + source.Minute * 100 + source.Second; } /// /// Converts the JDE date + time components into a datetime /// /// JDE date component /// JDE time component /// Combined datetime from source JDE date/time components public static DateTime FromJDEDateTime(this DateTime sourceDate, int sourceTime) { try { int hours = sourceTime / 10000; int minutes = (sourceTime % 10000) / 100; int seconds = sourceTime % 100; return sourceDate.Date.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds); } catch { return sourceDate; } } } }