Files
jdescopingtool/OLD/DataModel/Helpers/DateTimeHelpers.cs
T
Joseph Doherty 26ff8d9b4f Initial commit: JDE Scoping Tool migration project
Set up repository with legacy .NET Framework 4.8 source (OLD/),
new .NET 10 Blazor solution (NEW/), OpenSpec specifications,
documentation, and project configuration.
2026-01-02 07:43:29 -05:00

81 lines
2.7 KiB
C#
Executable File

using System;
namespace DataModel.Helpers
{
/// <summary>
/// JDE date/time conversion helpers
/// </summary>
public static class DateTimeHelpers
{
/// <summary>
/// Strips time component from datetime
/// </summary>
/// <param name="source">Datetime to strip time component from</param>
/// <returns>Date component of source datetime</returns>
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;
}
/// <summary>
/// Strips date component from datetime
/// </summary>
/// <param name="source">Datetime to strip date component from</param>
/// <returns>Time component of source datetime</returns>
public static int ToJDETime(this DateTime source)
{
return source.Hour * 10000 + source.Minute * 100 + source.Second;
}
/// <summary>
/// Converts the JDE date + time components into a datetime
/// </summary>
/// <param name="sourceDate">JDE date component</param>
/// <param name="sourceTime">JDE time component</param>
/// <returns>Combined datetime from source JDE date/time components</returns>
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;
}
}
}
}