Skip to content

Commit

Permalink
deprecate DateTimeConverter.ParseToTimeOnly(str,precision)
Browse files Browse the repository at this point in the history
second param is unnecessary
  • Loading branch information
gbirchmeier committed May 20, 2024
1 parent b9f8620 commit c9ce826
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
17 changes: 12 additions & 5 deletions QuickFIXn/Fields/Converters/DateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TimeStampPrecision, string> DATE_TIME_PRECISION_TO_FORMAT = new Dictionary<TimeStampPrecision, string>
{
{TimeStampPrecision.Second, DATE_TIME_FORMAT_WITHOUT_MILLISECONDS},
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -143,13 +146,12 @@ public static DateTime ParseToDateOnly(string str)
/// Check if string is TimeOnly and, if yes, convert to DateTime.
/// </summary>
/// <param name="str"></param>
/// <param name="precision"></param>
/// <returns></returns>
/// <exception cref="FieldConvertError"/>
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);

Expand Down Expand Up @@ -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);
Expand Down
15 changes: 6 additions & 9 deletions UnitTests/Fields/Converters/DateTimeConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit c9ce826

Please sign in to comment.