]> git.saurik.com Git - wxWidgets.git/commitdiff
added GetEnglish{Month,WeekDay}Name()
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 24 Mar 2009 16:51:10 +0000 (16:51 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 24 Mar 2009 16:51:10 +0000 (16:51 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59811 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/datetime.h
interface/wx/datetime.h
src/common/datetime.cpp
tests/datetime/datetimetest.cpp

index 95a38f562ab66553cdb48488dc9a36c697517db0..bf1b58444c46b2b694cd83686ceeaa71bd4e18e0 100644 (file)
@@ -562,16 +562,25 @@ public:
                                         int year = Inv_Year,
                                         Calendar cal = Gregorian);
 
+
         // get the full (default) or abbreviated month name in the current
         // locale, returns empty string on error
     static wxString GetMonthName(Month month,
                                  NameFlags flags = Name_Full);
 
+        // get the standard English full (default) or abbreviated month name
+    static wxString GetEnglishMonthName(Month month,
+                                        NameFlags flags = Name_Full);
+
         // get the full (default) or abbreviated weekday name in the current
         // locale, returns empty string on error
     static wxString GetWeekDayName(WeekDay weekday,
                                    NameFlags flags = Name_Full);
 
+        // get the standard English full (default) or abbreviated weekday name
+    static wxString GetEnglishWeekDayName(WeekDay weekday,
+                                          NameFlags flags = Name_Full);
+
         // get the AM and PM strings in the current locale (may be empty)
     static void GetAmPmStrings(wxString *am, wxString *pm);
 
index f28fd2abb349f490e34aead97d61dd25f3a76c16..02f28dc731154568f04210371b3de2f106d3dd83 100644 (file)
@@ -1281,8 +1281,53 @@ public:
     static int GetCurrentYear(Calendar cal = Gregorian);
 
     /**
-        Gets the full (default) or abbreviated (specify @c Name_Abbr name of
-        the given month.
+        Return the standard English name of the given month.
+
+        This function always returns "January" or "Jan" for January, use
+        GetMonthName() to retrieve the name of the month in the users current
+        locale.
+
+        @param month
+            One of wxDateTime::Jan, ..., wxDateTime::Dec values.
+        @param flags
+            Either Name_Full (default) or Name_Abbr.
+
+        @see GetEnglishWeekDayName()
+
+        @since 2.9.0
+     */
+    static wxString GetEnglishMonthName(Month month,
+                                        NameFlags flags = Name_Full);
+
+    /**
+        Return the standard English name of the given week day.
+
+        This function always returns "Monday" or "Mon" for Monday, use
+        GetWeekDayName() to retrieve the name of the month in the users current
+        locale.
+
+        @param weekday
+            One of wxDateTime::Sun, ..., wxDateTime::Sat values.
+        @param flags
+            Either Name_Full (default) or Name_Abbr.
+
+        @see GetEnglishMonthName()
+
+        @since 2.9.0
+     */
+    static wxString GetEnglishWeekDayName(WeekDay weekday,
+                                          NameFlags flags = Name_Full);
+
+    /**
+        Gets the full (default) or abbreviated name of the given month.
+
+        This function returns the name in the current locale, use
+        GetEnglishMonthName() to get the untranslated name if necessary.
+
+        @param month
+            One of wxDateTime::Jan, ..., wxDateTime::Dec values.
+        @param flags
+            Either Name_Full (default) or Name_Abbr.
 
         @see GetWeekDayName()
     */
@@ -1330,13 +1375,20 @@ public:
     static tm* GetTmNow();
 
     /**
-        Gets the full (default) or abbreviated (specify @c Name_Abbr) name of
-        the given week day.
+        Gets the full (default) or abbreviated name of the given week day.
+
+        This function returns the name in the current locale, use
+        GetEnglishWeekDayName() to get the untranslated name if necessary.
+
+        @param weekday
+            One of wxDateTime::Sun, ..., wxDateTime::Sat values.
+        @param flags
+            Either Name_Full (default) or Name_Abbr.
 
         @see GetMonthName()
     */
     static wxString GetWeekDayName(WeekDay weekday,
-                                    NameFlags flags = Name_Full);
+                                   NameFlags flags = Name_Full);
 
     /**
         Returns @true if DST was used in the given year (the current one by
index 3889adfdc5ceb1fb31622bc704b18a2adfa7f948..90ac0aedf1c45e8280d59b8e1f86a0ddba8137f3 100644 (file)
@@ -803,12 +803,61 @@ wxDateTime::wxDateTime_t wxDateTime::GetNumberOfDays(wxDateTime::Month month,
     }
 }
 
+namespace
+{
+
+// helper function used by GetEnglish/WeekDayName(): returns 0 if flags is
+// Name_Full and 1 if it is Name_Abbr or -1 if the flags is incorrect (and
+// asserts in this case)
+//
+// the return value of this function is used as an index into 2D array
+// containing full names in its first row and abbreviated ones in the 2nd one
+int NameArrayIndexFromFlag(wxDateTime::NameFlags flags)
+{
+    switch ( flags )
+    {
+        case wxDateTime::Name_Full:
+            return 0;
+
+        case wxDateTime::Name_Abbr:
+            return 1;
+
+        default:
+            wxFAIL_MSG( "unknown wxDateTime::NameFlags value" );
+    }
+
+    return -1;
+}
+
+} // anonymous namespace
+
+/* static */
+wxString wxDateTime::GetEnglishMonthName(Month month, NameFlags flags)
+{
+    wxCHECK_MSG( month != Inv_Month, wxEmptyString, "invalid month" );
+
+    static const char *monthNames[2][MONTHS_IN_YEAR] =
+    {
+        { "January", "February", "March", "April", "May", "June",
+          "July", "August", "September", "October", "November", "December" },
+        { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }
+    };
+
+    const int idx = NameArrayIndexFromFlag(flags);
+    if ( idx == -1 )
+        return wxString();
+
+    return monthNames[idx][month];
+}
+
 /* static */
 wxString wxDateTime::GetMonthName(wxDateTime::Month month,
                                   wxDateTime::NameFlags flags)
 {
-    wxCHECK_MSG( month != Inv_Month, wxEmptyString, _T("invalid month") );
 #ifdef HAVE_STRFTIME
+    wxCHECK_MSG( month != Inv_Month, wxEmptyString, _T("invalid month") );
+
     // notice that we must set all the fields to avoid confusing libc (GNU one
     // gets confused to a crash if we don't do this)
     tm tm;
@@ -817,56 +866,36 @@ wxString wxDateTime::GetMonthName(wxDateTime::Month month,
 
     return CallStrftime(flags == Name_Abbr ? _T("%b") : _T("%B"), &tm);
 #else // !HAVE_STRFTIME
-    wxString ret;
-    switch(month)
-    {
-        case Jan:
-            ret = (flags == Name_Abbr ? wxT("Jan"): wxT("January"));
-            break;
-        case Feb:
-            ret = (flags == Name_Abbr ? wxT("Feb"): wxT("Febuary"));
-            break;
-        case Mar:
-            ret = (flags == Name_Abbr ? wxT("Mar"): wxT("March"));
-            break;
-        case Apr:
-            ret = (flags == Name_Abbr ? wxT("Apr"): wxT("April"));
-            break;
-        case May:
-            ret = (flags == Name_Abbr ? wxT("May"): wxT("May"));
-            break;
-        case Jun:
-            ret = (flags == Name_Abbr ? wxT("Jun"): wxT("June"));
-            break;
-        case Jul:
-            ret = (flags == Name_Abbr ? wxT("Jul"): wxT("July"));
-            break;
-        case Aug:
-            ret = (flags == Name_Abbr ? wxT("Aug"): wxT("August"));
-            break;
-        case Sep:
-            ret = (flags == Name_Abbr ? wxT("Sep"): wxT("September"));
-            break;
-        case Oct:
-            ret = (flags == Name_Abbr ? wxT("Oct"): wxT("October"));
-            break;
-        case Nov:
-            ret = (flags == Name_Abbr ? wxT("Nov"): wxT("November"));
-            break;
-        case Dec:
-            ret = (flags == Name_Abbr ? wxT("Dec"): wxT("December"));
-            break;
-    }
-    return ret;
+    return GetEnglishMonthName(month, flags);
 #endif // HAVE_STRFTIME/!HAVE_STRFTIME
 }
 
+/* static */
+wxString wxDateTime::GetEnglishWeekDayName(WeekDay wday, NameFlags flags)
+{
+    wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, _T("invalid weekday") );
+
+    static const char *weekdayNames[2][DAYS_PER_400_YEARS] =
+    {
+        { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
+          "Saturday" },
+        { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
+    };
+
+    const int idx = NameArrayIndexFromFlag(flags);
+    if ( idx == -1 )
+        return wxString();
+
+    return weekdayNames[idx][wday];
+}
+
 /* static */
 wxString wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday,
                                     wxDateTime::NameFlags flags)
 {
-    wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, _T("invalid weekday") );
 #ifdef HAVE_STRFTIME
+    wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, _T("invalid weekday") );
+
     // take some arbitrary Sunday (but notice that the day should be such that
     // after adding wday to it below we still have a valid date, e.g. don't
     // take 28 here!)
@@ -885,32 +914,7 @@ wxString wxDateTime::GetWeekDayName(wxDateTime::WeekDay wday,
     // ... and call strftime()
     return CallStrftime(flags == Name_Abbr ? _T("%a") : _T("%A"), &tm);
 #else // !HAVE_STRFTIME
-    wxString ret;
-    switch(wday)
-    {
-        case Sun:
-            ret = (flags == Name_Abbr ? wxT("Sun") : wxT("Sunday"));
-            break;
-        case Mon:
-            ret = (flags == Name_Abbr ? wxT("Mon") : wxT("Monday"));
-            break;
-        case Tue:
-            ret = (flags == Name_Abbr ? wxT("Tue") : wxT("Tuesday"));
-            break;
-        case Wed:
-            ret = (flags == Name_Abbr ? wxT("Wed") : wxT("Wednesday"));
-            break;
-        case Thu:
-            ret = (flags == Name_Abbr ? wxT("Thu") : wxT("Thursday"));
-            break;
-        case Fri:
-            ret = (flags == Name_Abbr ? wxT("Fri") : wxT("Friday"));
-            break;
-        case Sat:
-            ret = (flags == Name_Abbr ? wxT("Sat") : wxT("Saturday"));
-            break;
-    }
-    return ret;
+    return GetEnglishWeekDayName(wday, flags);
 #endif // HAVE_STRFTIME/!HAVE_STRFTIME
 }
 
index 685b9b35ebfca27e3c366f866eab51ef8eae67cc..a9992e71bd9f8b81c16c39b07f939d380d30d3a8 100644 (file)
@@ -113,36 +113,24 @@ static wxString TranslateDate(const wxString& str)
 
     wxString trans(str);
 
-    static const char *weekdays[] =
-    {
-        "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
-    };
-
     for ( wxDateTime::WeekDay wd = wxDateTime::Sun;
           wd < wxDateTime::Inv_WeekDay;
           wxNextWDay(wd) )
     {
         trans.Replace
               (
-                weekdays[wd],
+                wxDateTime::GetEnglishWeekDayName(wd, wxDateTime::Name_Abbr),
                 wxDateTime::GetWeekDayName(wd, wxDateTime::Name_Abbr)
               );
     }
 
-
-    static const char *months[] =
-    {
-        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
-        "Nov", "Dec"
-    };
-
     for ( wxDateTime::Month mon = wxDateTime::Jan;
           mon < wxDateTime::Inv_Month;
           wxNextMonth(mon) )
     {
         trans.Replace
               (
-                months[mon],
+                wxDateTime::GetEnglishMonthName(mon, wxDateTime::Name_Abbr),
                 wxDateTime::GetMonthName(mon, wxDateTime::Name_Abbr)
               );
     }