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.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
namespace JdeScoping.Core.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Converts between JDE date (CYYDDD) and time (HHMMSS) formats and DateTime.
|
||||
/// </summary>
|
||||
public static class JdeDateConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts DateTime to JDE date format (CYYDDD).
|
||||
/// </summary>
|
||||
/// <param name="dateTime">DateTime to convert.</param>
|
||||
/// <returns>JDE date in CYYDDD format.</returns>
|
||||
public static int ToJdeDate(this DateTime dateTime)
|
||||
{
|
||||
// CYYDDD format: C = century (0=1900s, 1=2000s), YY = year, DDD = day of year
|
||||
int century = (dateTime.Year - 1900) / 100;
|
||||
int yearInCentury = (dateTime.Year - 1900) % 100;
|
||||
int dayOfYear = dateTime.DayOfYear;
|
||||
|
||||
return (century * 100000) + (yearInCentury * 1000) + dayOfYear;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts DateTime to JDE time format (HHMMSS).
|
||||
/// </summary>
|
||||
/// <param name="dateTime">DateTime to convert.</param>
|
||||
/// <returns>JDE time in HHMMSS format.</returns>
|
||||
public static int ToJdeTime(this DateTime dateTime)
|
||||
{
|
||||
return (dateTime.Hour * 10000) + (dateTime.Minute * 100) + dateTime.Second;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts JDE date (CYYDDD) to DateTime.
|
||||
/// Returns null for zero or invalid values.
|
||||
/// </summary>
|
||||
/// <param name="jdeDate">JDE date in CYYDDD format (C=century, YY=year, DDD=day of year)</param>
|
||||
/// <returns>DateTime or null if invalid</returns>
|
||||
public static DateTime? ToDateTime(int jdeDate)
|
||||
{
|
||||
return ToDateTime(jdeDate, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts JDE date (CYYDDD) and time (HHMMSS) to DateTime.
|
||||
/// Returns null for zero or invalid values (changed from legacy 1900-01-01).
|
||||
/// </summary>
|
||||
/// <param name="jdeDate">JDE date in CYYDDD format (C=century, YY=year, DDD=day of year)</param>
|
||||
/// <param name="jdeTime">JDE time in HHMMSS format</param>
|
||||
/// <returns>DateTime or null if invalid</returns>
|
||||
public static DateTime? ToDateTime(int jdeDate, int jdeTime)
|
||||
{
|
||||
if (jdeDate <= 0)
|
||||
return null;
|
||||
|
||||
try
|
||||
{
|
||||
// CYYDDD format: C = century (0=1900s, 1=2000s), YY = year, DDD = day of year
|
||||
int century = jdeDate / 100000;
|
||||
int yearInCentury = (jdeDate / 1000) % 100;
|
||||
int dayOfYear = jdeDate % 1000;
|
||||
|
||||
int year = 1900 + (century * 100) + yearInCentury;
|
||||
|
||||
if (dayOfYear < 1 || dayOfYear > 366)
|
||||
return null;
|
||||
|
||||
var date = new DateTime(year, 1, 1).AddDays(dayOfYear - 1);
|
||||
|
||||
// Validate the resulting year matches what we calculated
|
||||
// (handles leap year edge case where day 366 might push to next year)
|
||||
if (date.Year != year)
|
||||
return null;
|
||||
|
||||
// Add time component if provided
|
||||
if (jdeTime > 0)
|
||||
{
|
||||
int hours = jdeTime / 10000;
|
||||
int minutes = (jdeTime / 100) % 100;
|
||||
int seconds = jdeTime % 100;
|
||||
|
||||
if (hours >= 0 && hours < 24 && minutes >= 0 && minutes < 60 && seconds >= 0 && seconds < 60)
|
||||
{
|
||||
date = date.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds);
|
||||
}
|
||||
}
|
||||
|
||||
return date;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user