Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build error on DateTime.UtcNow.ToString("o")

Tags:

c#

.net-core

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.

like image 710
silent Avatar asked Dec 06 '25 23:12

silent


1 Answers

Use the static CultureInfo.InvariantCulture property:

var timestamp = DateTime.UtcNow.ToString("o", 
                    System.Globalization.CultureInfo.InvariantCulture);
like image 133
mm8 Avatar answered Dec 08 '25 14:12

mm8