https://gcc.gnu.org/bugzilla/show_bug.cgi?id=9635

--- Comment #12 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #11)
> We could also have a custom facet derived from time_get that we store in the
> std::locale when it is created by name from a C locale, and store a
> dateorder enum in that derived type on construction. That would allow
> std::time_get facets that are associated with a C locale to return a correct
> dateorder for the locale (but only if the locale's D_FMT happens to map to
> one of the four distinct dateorder formats!)

Like so ...

--- a/libstdc++-v3/src/c++11/localename.cc
+++ b/libstdc++-v3/src/c++11/localename.cc
@@ -36,6 +36,43 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION

   using namespace __gnu_cxx;

+  template<typename C>
+    struct __time_get : std::time_get<C>
+    {
+      explicit
+      __time_get(time_base::dateorder ord, size_t refs = 0)
+      : time_get<C>(refs), _M_order(ord)
+      { }
+
+      time_base::dateorder do_date_order() const override { return _M_order; }
+
+      time_base::dateorder _M_order;
+    };
+
+  inline template class __time_get<char>;
+#ifdef _GLIBCXX_USE_WCHAR_T
+  inline template class __time_get<wchar_t>;
+#endif
+
+  static inline time_base::dateorder
+  get_date_order(__timepunct<char>* tp)
+  {
+    const char*  dates[2];
+    tp->_M_date_formats(dates);
+    if (const char* d = dates[0])
+      {
+       if (!strcmp(d, "%d/%m/%y"))
+         return time_base::dmy;
+       if (!strcmp(d, "%m/%d/%y"))
+         return time_base::mdy;
+       if (!strcmp(d, "%y/%m/%d"))
+         return time_base::ymd;
+       if (!strcmp(d, "%y/%d/%m"))
+         return time_base::ydm;
+      }
+    return time_base::no_order;
+  }
+
   static inline bool
   is_C_locale(const char* s)
   {
@@ -266,8 +303,10 @@ const int num_facets = (
        _M_init_facet(new moneypunct<char, true>(__cloc, 0));
        _M_init_facet(new money_get<char>);
        _M_init_facet(new money_put<char>);
-       _M_init_facet(new __timepunct<char>(__cloc, __s));
-       _M_init_facet(new time_get<char>);
+       auto tp = new __timepunct<char>(__cloc, __s);
+       _M_init_facet(tp);
+       auto ord = get_date_order(tp);
+       _M_init_facet(new __time_get<char>(ord));
        _M_init_facet(new time_put<char>);
        _M_init_facet(new std::messages<char>(__cloc, __s));

@@ -283,7 +322,7 @@ const int num_facets = (
        _M_init_facet(new money_get<wchar_t>);
        _M_init_facet(new money_put<wchar_t>);
        _M_init_facet(new __timepunct<wchar_t>(__cloc, __s));
-       _M_init_facet(new time_get<wchar_t>);
+       _M_init_facet(new time_get<wchar_t>(ord));
        _M_init_facet(new time_put<wchar_t>);
        _M_init_facet(new std::messages<wchar_t>(__cloc, __s));
 #endif

Reply via email to