Why is .NET Core (version 2.1) throwing a error (not just a warning) on dotnet build when I have this in my code
var timestamp = DateTime.UtcNow.ToString("o");
Program.cs(78,8): error CA1305: The behavior of 'DateTime.ToString(string)' could vary based on the current user's locale settings. Replace this call in 'Program.SendMessagesForever(ModuleClient, CancellationToken)' with a call to 'DateTime.ToString(string, IFormatProvider)'.
My understanding was that "o" is not local-sensitive anyway?!
Is this desired behavior? If so, how would you work around this? Instantiating a FormatProvider just for this meaningless task seems very unnecessary to me.
Use the static CultureInfo.InvariantCulture property:
var timestamp = DateTime.UtcNow.ToString("o",
System.Globalization.CultureInfo.InvariantCulture);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With