Skip to content

Commit

Permalink
fix: Culture info to Invariant (#1265)
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Skender <[email protected]>
  • Loading branch information
DaveSkender authored Oct 27, 2024
1 parent 68a1ae7 commit 89fc240
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/_common/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace Skender.Stock.Indicators;
/// the Guide</see> for more information.</summary>
public static partial class Indicator
{
private static readonly CultureInfo EnglishCulture = new("en-US", false);
private static readonly Calendar EnglishCalendar = EnglishCulture.Calendar;
private static readonly CultureInfo invCulture = CultureInfo.InvariantCulture;
private static readonly Calendar invCalendar = invCulture.Calendar;

// Gets the DTFI properties required by GetWeekOfYear.
private static readonly CalendarWeekRule EnglishCalendarWeekRule
= EnglishCulture.DateTimeFormat.CalendarWeekRule;
private static readonly CalendarWeekRule invCalendarWeekRule
= invCulture.DateTimeFormat.CalendarWeekRule;

private static readonly DayOfWeek EnglishFirstDayOfWeek
= EnglishCulture.DateTimeFormat.FirstDayOfWeek;
private static readonly DayOfWeek invFirstDayOfWeek
= invCulture.DateTimeFormat.FirstDayOfWeek;
}
2 changes: 0 additions & 2 deletions src/_common/Quotes/Quote.Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace Skender.Stock.Indicators;

public static partial class QuoteUtility
{
private static readonly CultureInfo NativeCulture = Thread.CurrentThread.CurrentUICulture;

/* STANDARD DECIMAL QUOTES */

// convert TQuotes to basic double tuple list
Expand Down
9 changes: 6 additions & 3 deletions src/_common/Quotes/Quote.Validation.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System.Globalization;

namespace Skender.Stock.Indicators;

// QUOTE UTILITIES

public static partial class QuoteUtility
{
private static readonly CultureInfo invCulture = CultureInfo.InvariantCulture;

// VALIDATION
/// <include file='./info.xml' path='info/type[@name="Validate"]/*' />
///
Expand All @@ -12,17 +16,16 @@ public static IEnumerable<TQuote> Validate<TQuote>(
where TQuote : IQuote
{
// we cannot rely on date consistency when looking back, so we force sort

List<TQuote> quotesList = quotes.ToSortedList();

// check for duplicates
DateTime lastDate = DateTime.MinValue;
foreach (var q in quotesList)
foreach (TQuote q in quotesList)
{
if (lastDate == q.Date)
{
throw new InvalidQuotesException(
string.Format(NativeCulture, "Duplicate date found on {0}.", q.Date));
$"Duplicate date found on {q.Date.ToString("o", invCulture)}.");
}

lastDate = q.Date;
Expand Down
2 changes: 1 addition & 1 deletion src/m-r/MaEnvelopes/MaEnvelopes.Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static IEnumerable<MaEnvelopeResult> CalcMaEnvelopes(
_ => throw new ArgumentOutOfRangeException(
nameof(movingAverageType), movingAverageType,
string.Format(
EnglishCulture,
invCulture,
"Moving Average Envelopes does not support {0}.",
Enum.GetName(typeof(MaType), movingAverageType)))
};
Expand Down
2 changes: 1 addition & 1 deletion src/m-r/ParabolicSar/ParabolicSar.Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private static void ValidateParabolicSar(
if (accelerationStep > maxAccelerationFactor)
{
string message = string.Format(
EnglishCulture,
invCulture,
"Acceleration Step cannot be larger than the Max Acceleration Factor ({0}) for Parabolic SAR.",
maxAccelerationFactor);

Expand Down
4 changes: 2 additions & 2 deletions src/m-r/PivotPoints/PivotPoints.Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ internal static TPivotPoint GetPivotPoint<TPivotPoint>(
private static int GetWindowNumber(DateTime d, PeriodSize windowSize)
=> windowSize switch {
PeriodSize.Month => d.Month,
PeriodSize.Week => EnglishCalendar.GetWeekOfYear(d, EnglishCalendarWeekRule, EnglishFirstDayOfWeek),
PeriodSize.Week => invCalendar.GetWeekOfYear(d, invCalendarWeekRule, invFirstDayOfWeek),
PeriodSize.Day => d.Day,
PeriodSize.OneHour => d.Hour,
_ => throw new ArgumentOutOfRangeException(nameof(windowSize), windowSize,
string.Format(
EnglishCulture,
invCulture,
"Pivot Points does not support PeriodSize of {0}. See documentation for valid options.",
Enum.GetName(typeof(PeriodSize), windowSize)))
};
Expand Down
2 changes: 1 addition & 1 deletion src/m-r/Prs/Prs.Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static void ValidatePriceRelative(
{
string message = "Insufficient quotes provided for Price Relative Strength. " +
string.Format(
EnglishCulture,
invCulture,
"You provided {0} periods of quotes when at least {1} are required.",
qtyHistoryEval, minHistory);

Expand Down
2 changes: 1 addition & 1 deletion src/s-z/Tr/Tr.Series.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static List<TrResult> CalcTr(
{
// initialize
List<TrResult> results = new(qdList.Count);
double prevClose = 0;
double prevClose = double.NaN;

// roll through quotes
for (int i = 0; i < qdList.Count; i++)
Expand Down
1 change: 1 addition & 0 deletions tests/indicators/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
global using FluentAssertions;
global using Microsoft.VisualStudio.TestTools.UnitTesting;
global using Skender.Stock.Indicators;
global using Tests.Common;
1 change: 1 addition & 0 deletions tests/indicators/Tests.Indicators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
Expand Down
8 changes: 5 additions & 3 deletions tests/indicators/_common/Quotes/Quote.Validation.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public void ValidateCut()
}
}

/* BAD QUOTES EXCEPTIONS */
[TestMethod]
[ExpectedException(typeof(InvalidQuotesException), "Duplicate date found.")]
public void DuplicateHistory()
{
List<Quote> badHistory =
Expand All @@ -86,6 +84,10 @@ public void DuplicateHistory()
new Quote { Date = DateTime.ParseExact("2017-01-06", "yyyy-MM-dd", EnglishCulture), Open = 228.97m, High = 231.92m, Low = 228.00m, Close = 231.28m, Volume = 3979484 }
];

badHistory.Validate();
InvalidQuotesException ex =
Assert.ThrowsException<InvalidQuotesException>(()
=> badHistory.Validate());

ex.Message.Should().Contain("Duplicate date found on 2017-01-06T00:00:00.0000000.");
}
}

0 comments on commit 89fc240

Please sign in to comment.