Does the formatting of a date depend on both the u

2019-07-09 20:22发布

问题:

I feel this question is very general for all programmers on all languages. This is somewhat blurry. First, there are locales, which encapsulate cultural information and other stuff. I believe these locale objects also encapsulate information about date and time formatting, i.e. how to display a date, in Unicode Standard. Like so, for example: "yyyy-MM-dd'T'HH:mm:ssZ". Every country may have it's own date+time formatting rules, so we are all happy about locales, and users of our apps get date+time formatted to what is usual in their region.

But now, the really ugly part. There are calendars. Different calendars. The Gregorian like in the US and Europe and some other countries, but there are also Hebrew, Jewish and Muslim calendars and many more. Now, these calendars are of course different. They devide the time period of a year into units of different length, some may have 13 months, other may have just moon phases instead of Monday, Tuesday, Wednesday, and so on. I don't really know, but I know some of them appear very strange to someone who is used to the Gregorian calendar.

But the blurry thing now: Who does affect date+time formatting really? The locale? The calendar system? Both? And does the locale tell what calendar system to use, usually? How does the calendar system affect the locale and reverse?

I've been searching now almost the whole day on this topic, and it seems hard like a huge brick.

回答1:

In a typical OS there will be date routines using the Gregorian calendar by international convention.

The locale will decide how date information is to be formatted, and which language.

This doesn't stop the use of other calendar systems independently of the OS date functions, but doesn't international commerce require that we agree on a common calendar?



回答2:

A user could be from one area where he likes to use a certain calendar system but his locale is different due to being on a remote job site or whatever.

There are hundreds of other cases like this that people need to address if they need bulletproof calendar formatting.



回答3:

I'll try to explain how it works in the .Net framewrk

The value stored in a DateTime variable is the number of ticks since a specific day. This is indepent of calendar used. i.e. today will have the same value regardless of calendar.

The culture contains information about both the format string and the calendar to use when printing a date.

When you ask the system to format the string it will first do something like:

int year = culture.Calendar.GetYear(dt);
int month = culture.Calendar.GetMonth(dt);
int day = culture.Calendar.GetDay(dt);

year will be 2009 in the gregorian calendar but something else for other calendars.

After that, the format string is used to output the values in correct order etc.

Because of that the following code will not output a correct ISO date with a Hebrew locale.

dt.ToString("yyyy-MM-dd'T'HH:mm:ssZ");

The correct way is either

dt.ToString("yyyy-MM-dd'T'HH:mm:ssZ", CultureInfo.InvariantCulture);

or

dt.ToString("s");

The latter works because the framework automatically ignore the culture for the "s" format specifier (and a few others).