Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invariant culture

We are creating an application that will be shipped through the whole world, while importing and exporting files of different formats.

We should use the Invariant Culture for these standard files.

However, the invariant culture is just like the EN-US culture, instead of really providing a standard international way of writing.

That being said, I'd like to change at least the date format for this culture. But the following code just throws exceptions:

CultureInfo format = System.Globalization.CultureInfo.InvariantCulture;
format.DateTimeFormat.FullDateTimePattern = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
return format;

What would be the correct way to have an IFormatProvider that formats any kind of variable, but has an international way of writing dates?

like image 729
Schiavini Avatar asked Jan 17 '23 11:01

Schiavini


1 Answers

The invariant culture is just that, invariant. You can't change it.

This culture is intended for .NET applications which need to save data in a way which is portable across cultures.

It is not intended that users are shown or should care about the format this data takes, it should always be converted to their particular culture before seeing the data.

As far as what you could do if you really wanted this DateTime format is to start defining a new culture (possibly based off the Invariant culture if you want).

var myStandardCulture = (CultureInfo)System.Globalization.CultureInfo.InvariantCulture.Clone();
myStandardCulture.DateTimeFormat.FullDateTimePattern = "yyyy'-'MM'-'dd' 'HH':'mm':'ss";
return myStandardCulture;

The downside of defining your own culture, is other .NET applications which try to read your files would also need to be told about all of your new culture rules rather than just accepting the InvariantCulture rules built into .NET

like image 100
MerickOWA Avatar answered Jan 25 '23 13:01

MerickOWA