How to format incomplete dates and times in a locale specific manner

流过昼夜 提交于 2019-12-23 16:07:20

问题


How can one format incomplete dates and/or a times in a locale specific manner?

I have a date/time stored in a set of 7 strings:

  • dayOfWeek - The day of the week. ([1,7])
  • dayOfMonth - The day of the month. ([1,31])
  • monthOfYear - The month of the year. ([0,11])
  • year - The year. ([-∞,∞])
  • hourOfDay - The hour of the day. ([0,11])
  • minuteOfHour - The minute of the hour. ([0,59])
  • meridianOfDay - The meridian of the day. ([0,1])

For various reasons, most of these are obtained through use of IR on web data, some of these strings may be empty. The problem is then how to format such incomplete dates/times in a locale specific manner.

What I currently do, using boost.locale, is create a date_time and add to it the various date/time components that are complete, then store the date_time in a locale specific manner in a string. Something like this:

date_time dateTime;
if(!dayOfWeek.empty())
  dateTime = period::day_of_week(lexical_cast<int>(dayOfWeek));
if(!dayOfMonth.empty())
  dateTime = period::day(lexical_cast<int>(dayOfMonth));
if(!monthOfYear.empty())
  dateTime = period::month(lexical_cast<int>(monthOfYear));
if(!year.empty())
  dateTime = period::extended_year(lexical_cast<int>(year));
if(!hourOfDay.empty())
  dateTime = period::hour_12(lexical_cast<int>(hourOfDay));
if(!minuteOfHour.empty())
  dateTime = period::minute(lexical_cast<int>(minuteOfHour));
if(!meridianOfDay.empty())
  dateTime = period::am_pm(lexical_cast<int>(meridianOfDay));

std::stringstream stringStream;
stringStream.imbue(desiredLocale);
stringStream << dateTime;

std::string localeSpecificDateTime = stringStream.str();

This works to some extent. I have a locale formatted date_time. However, the fields that are incomplete are filled with the date/time of now, due to the date_time I start with.

What would be perfect is if one could flag the incomplete fields as to not be included in the formatting of the date_time.

PS: I know I could maintain 127 (27-1) formatting strings for each locale, but creating/finding such strings and then maintaining them seems like a recipe for insanity!

来源:https://stackoverflow.com/questions/13846334/how-to-format-incomplete-dates-and-times-in-a-locale-specific-manner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!