From c9ce82662ef4c1fcbc26acaba51985ff2413d4b2 Mon Sep 17 00:00:00 2001 From: Grant Birchmeier Date: Mon, 20 May 2024 18:37:49 -0500 Subject: [PATCH] deprecate DateTimeConverter.ParseToTimeOnly(str,precision) second param is unnecessary --- .../Fields/Converters/DateTimeConverter.cs | 17 ++++++++++++----- .../Fields/Converters/DateTimeConverterTests.cs | 15 ++++++--------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/QuickFIXn/Fields/Converters/DateTimeConverter.cs b/QuickFIXn/Fields/Converters/DateTimeConverter.cs index 3fe1fb852..5bd9d6062 100644 --- a/QuickFIXn/Fields/Converters/DateTimeConverter.cs +++ b/QuickFIXn/Fields/Converters/DateTimeConverter.cs @@ -26,11 +26,15 @@ public static class DateTimeConverter public const string DATE_TIME_WITH_MICROSECONDS = "yyyyMMdd-HH:mm:ss.ffffff"; public static int DATE_TIME_MAXLENGTH_WITHOUT_NANOSECONDS = DATE_TIME_WITH_MICROSECONDS.Length; + public const string TIME_ONLY_WITH_MICROSECONDS = "HH:mm:ss.ffffff"; + public static int TIME_ONLY_MAXLENGTH_WITHOUT_NANOSECONDS = TIME_ONLY_WITH_MICROSECONDS.Length; + public static string[] DATE_TIME_FORMATS = { DATE_TIME_WITH_MICROSECONDS, "yyyyMMdd-HH:mm:ss.fff", "yyyyMMdd-HH:mm:ss" }; public static string[] DATE_ONLY_FORMATS = { "yyyyMMdd" }; - public static string[] TIME_ONLY_FORMATS = { "HH:mm:ss.ffffff", "HH:mm:ss.fff", "HH:mm:ss" }; + public static string[] TIME_ONLY_FORMATS = { TIME_ONLY_WITH_MICROSECONDS, "HH:mm:ss.fff", "HH:mm:ss" }; public static DateTimeStyles DATE_TIME_STYLES = DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal; public static CultureInfo DATE_TIME_CULTURE_INFO = CultureInfo.InvariantCulture; + private static IDictionary DATE_TIME_PRECISION_TO_FORMAT = new Dictionary { {TimeStampPrecision.Second, DATE_TIME_FORMAT_WITHOUT_MILLISECONDS}, @@ -110,7 +114,6 @@ public static DateTime ParseToDateTime(string str) { try { - //Avoid NanoString Path parsing if not possible from string length return (str.Length > DATE_TIME_MAXLENGTH_WITHOUT_NANOSECONDS) ? DateTimeFromNanoString(str) : DateTime.ParseExact(str, DATE_TIME_FORMATS, DATE_TIME_CULTURE_INFO, DATE_TIME_STYLES); @@ -143,13 +146,12 @@ public static DateTime ParseToDateOnly(string str) /// Check if string is TimeOnly and, if yes, convert to DateTime. /// /// - /// /// /// - public static DateTime ParseToTimeOnly(string str, TimeStampPrecision precision = TimeStampPrecision.Millisecond) + public static DateTime ParseToTimeOnly(string str) { try { - DateTime d = (precision == TimeStampPrecision.Nanosecond) + DateTime d = (str.Length > TIME_ONLY_MAXLENGTH_WITHOUT_NANOSECONDS) ? TimeOnlyFromNanoString(str) : DateTime.ParseExact(str, TIME_ONLY_FORMATS, DATE_TIME_CULTURE_INFO, DATE_TIME_STYLES); @@ -199,6 +201,11 @@ public static string ToFIXTimeOnly(DateTime dt, TimeStampPrecision precision) : string.Format(TIME_ONLY_PRECISION_TO_FORMAT[precision], dt); } + [Obsolete("Use ParseToTimeOnly(str). The 'precision' parameter is unnecessary.")] + public static DateTime ParseToTimeOnly(string str, TimeStampPrecision precision = TimeStampPrecision.Millisecond) { + return ParseToTimeOnly(str); + } + [Obsolete("Renamed to ParseToDateTime(str) - the precision parameter was unused internally, so discard it")] public static DateTime ConvertToDateTime(string str, TimeStampPrecision precision = TimeStampPrecision.Millisecond) { return ParseToDateTime(str); diff --git a/UnitTests/Fields/Converters/DateTimeConverterTests.cs b/UnitTests/Fields/Converters/DateTimeConverterTests.cs index a5db16881..81dc229bf 100644 --- a/UnitTests/Fields/Converters/DateTimeConverterTests.cs +++ b/UnitTests/Fields/Converters/DateTimeConverterTests.cs @@ -74,19 +74,16 @@ public void ParseToTimeOnlyTest_Nano() { var targetTicks = new DateTime(1980, 1, 1, 4, 22, 1, 0, DateTimeKind.Utc).Ticks; Assert.That(targetTicks, Is.EqualTo(624511453210000000)); // for human reader reference - Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700", TimeStampPrecision.Nanosecond).Ticks, Is.EqualTo(targetTicks + 1234567)); - Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700Z", TimeStampPrecision.Nanosecond).Ticks, Is.EqualTo(targetTicks + 1234567)); + Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700").Ticks, Is.EqualTo(targetTicks + 1234567)); + Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700Z").Ticks, Is.EqualTo(targetTicks + 1234567)); var ticksInHour = 36000000000; - Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700+01", TimeStampPrecision.Nanosecond).Ticks, Is.EqualTo(targetTicks + 1234567 - ticksInHour)); - Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700-01", TimeStampPrecision.Nanosecond).Ticks, Is.EqualTo(targetTicks + 1234567 + ticksInHour)); + Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700+01").Ticks, Is.EqualTo(targetTicks + 1234567 - ticksInHour)); + Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700-01").Ticks, Is.EqualTo(targetTicks + 1234567 + ticksInHour)); var ticksIn90mins = 54000000000; - Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700+01:30", TimeStampPrecision.Nanosecond).Ticks, Is.EqualTo(targetTicks + 1234567 - ticksIn90mins)); - Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700-01:30", TimeStampPrecision.Nanosecond).Ticks, Is.EqualTo(targetTicks + 1234567 + ticksIn90mins)); - - // TODO: without second param - //Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700").Ticks, Is.EqualTo(targetTicks + 1234567)); + Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700+01:30").Ticks, Is.EqualTo(targetTicks + 1234567 - ticksIn90mins)); + Assert.That(DateTimeConverter.ParseToTimeOnly("04:22:01.123456700-01:30").Ticks, Is.EqualTo(targetTicks + 1234567 + ticksIn90mins)); } [Test]