namespace JdeScoping.DataSync.Services;
///
/// Converts parameter values to JDE-specific formats with timezone support.
///
public class ParameterFormatConverter
{
private readonly TimeZoneInfo _timezone;
///
/// Initializes a new instance of the class.
///
///
/// The timezone to use for conversions.
/// Accepts "UTC", "LOCAL", or a valid timezone ID (e.g., "Eastern Standard Time").
///
public ParameterFormatConverter(string timezone)
{
_timezone = timezone.ToUpperInvariant() switch
{
"UTC" => TimeZoneInfo.Utc,
"LOCAL" => TimeZoneInfo.Local,
_ => TimeZoneInfo.FindSystemTimeZoneById(timezone)
};
}
///
/// Converts a DateTime value to the specified format.
///
/// The DateTime value to convert.
///
/// The format to convert to. Supported formats:
/// - "jdejulian": JDE Julian date (CYYDDD format)
/// - "jdetime": JDE time (HHMMSS as integer)
/// - null: Returns the timezone-adjusted DateTime
///
/// The converted value.
/// Thrown when an unknown format is specified.
public object Convert(DateTime value, string? format)
{
var adjusted = TimeZoneInfo.ConvertTime(value, _timezone);
return format?.ToLowerInvariant() switch
{
"jdejulian" => ToJdeJulianDate(adjusted),
"jdetime" => ToJdeTime(adjusted),
null => adjusted,
_ => throw new ArgumentException($"Unknown format: {format}")
};
}
///
/// Converts a DateTime to JDE Julian date format (CYYDDD).
///
/// The date to convert.
///
/// The JDE Julian date as an integer where:
/// - C is the century bit (0 for 1900s, 1 for 2000s)
/// - YY is the two-digit year
/// - DDD is the day of year (001-366)
///
public static int ToJdeJulianDate(DateTime date)
{
int century = date.Year >= 2000 ? 1 : 0;
int year = date.Year % 100;
int dayOfYear = date.DayOfYear;
return century * 100000 + year * 1000 + dayOfYear;
}
///
/// Converts a DateTime to JDE time format (HHMMSS).
///
/// The time to convert.
/// The JDE time as an integer in HHMMSS format.
public static int ToJdeTime(DateTime time)
{
return time.Hour * 10000 + time.Minute * 100 + time.Second;
}
}