]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/datetime.cpp
Fix line indices translation in wxGrid::DoEndDragResizeLine().
[wxWidgets.git] / src / common / datetime.cpp
index 5de52dd01ec85a7b51cff12d7a4ad061b5cac830..d6e965cb094aef87a2b604802d182e38186388b1 100644 (file)
@@ -130,14 +130,9 @@ wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter<wxDateTime> , wxFromStringCon
         #include <values.h>
         static long wxGetTimeZone()
         {
-            static long timezone = MAXLONG; // invalid timezone
-            if (timezone == MAXLONG)
-            {
-                struct timeb tb;
-                ftime(&tb);
-                timezone = tb.timezone;
-            }
-            return timezone;
+            struct timeb tb;
+            ftime(&tb);
+            return tb.timezone;
         }
         #define WX_TIMEZONE wxGetTimeZone()
     #elif defined(__DARWIN__)
@@ -148,28 +143,18 @@ wxCUSTOM_TYPE_INFO(wxDateTime, wxToStringConverter<wxDateTime> , wxFromStringCon
         // Solution (1): use the function equivalent of _timezone
         static long wxGetTimeZone()
         {
-            static long s_Timezone = MAXLONG; // invalid timezone
-            if (s_Timezone == MAXLONG)
-            {
-                int t;
-                _get_timezone(& t);
-                s_Timezone = (long) t;
-            }
-            return s_Timezone;
+            long t;
+            _get_timezone(& t);
+            return t;
         }
         #define WX_TIMEZONE wxGetTimeZone()
         #elif 1
         // Solution (2): using GetTimeZoneInformation
         static long wxGetTimeZone()
         {
-            static long timezone = MAXLONG; // invalid timezone
-            if (timezone == MAXLONG)
-            {
-                TIME_ZONE_INFORMATION tzi;
-                ::GetTimeZoneInformation(&tzi);
-                timezone = tzi.Bias;
-            }
-            return timezone;
+            TIME_ZONE_INFORMATION tzi;
+            ::GetTimeZoneInformation(&tzi);
+            return tzi.Bias; // x 60
         }
         #define WX_TIMEZONE wxGetTimeZone()
         #else
@@ -296,8 +281,6 @@ static const int SEC_PER_MIN = 60;
 
 static const int MIN_PER_HOUR = 60;
 
-static const int HOURS_PER_DAY = 24;
-
 static const long SECONDS_PER_DAY = 86400l;
 
 static const int DAYS_PER_WEEK = 7;
@@ -338,8 +321,8 @@ const long wxDateTime::TIME_T_FACTOR = 1000l;
 // global data
 // ----------------------------------------------------------------------------
 
-const char *wxDefaultDateTimeFormat = "%c";
-const char *wxDefaultTimeSpanFormat = "%H:%M:%S";
+const char wxDefaultDateTimeFormat[] = "%c";
+const char wxDefaultTimeSpanFormat[] = "%H:%M:%S";
 
 // in the fine tradition of ANSI C we use our equivalent of (time_t)-1 to
 // indicate an invalid wxDateTime object
@@ -371,7 +354,7 @@ wxDateTime::wxDateTime_t GetNumOfDaysInMonth(int year, wxDateTime::Month month)
 {
     // the number of days in month in Julian/Gregorian calendar: the first line
     // is for normal years, the second one is for the leap ones
-    static wxDateTime::wxDateTime_t daysInMonth[2][MONTHS_IN_YEAR] =
+    static const wxDateTime::wxDateTime_t daysInMonth[2][MONTHS_IN_YEAR] =
     {
         { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
         { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
@@ -385,6 +368,7 @@ wxDateTime::wxDateTime_t GetNumOfDaysInMonth(int year, wxDateTime::Month month)
 // NOTE: not static because used by datetimefmt.cpp
 int GetTimeZone()
 {
+#ifdef WX_GMTOFF_IN_TM
     // set to true when the timezone is set
     static bool s_timezoneSet = false;
     static long gmtoffset = LONG_MAX; // invalid timezone
@@ -400,17 +384,15 @@ int GetTimeZone()
         wxLocaltime_r(&t, &tm);
         s_timezoneSet = true;
 
-#ifdef WX_GMTOFF_IN_TM
         // note that GMT offset is the opposite of time zone and so to return
         // consistent results in both WX_GMTOFF_IN_TM and !WX_GMTOFF_IN_TM
         // cases we have to negate it
         gmtoffset = -tm.tm_gmtoff;
-#else // !WX_GMTOFF_IN_TM
-        gmtoffset = WX_TIMEZONE;
-#endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM
     }
-
     return (int)gmtoffset;
+#else // !WX_GMTOFF_IN_TM
+    return WX_TIMEZONE;
+#endif // WX_GMTOFF_IN_TM/!WX_GMTOFF_IN_TM
 }
 
 // return the integral part of the JDN for the midnight of the given date (to
@@ -836,7 +818,7 @@ wxString wxDateTime::GetEnglishMonthName(Month month, NameFlags flags)
 {
     wxCHECK_MSG( month != Inv_Month, wxEmptyString, "invalid month" );
 
-    static const char *monthNames[2][MONTHS_IN_YEAR] =
+    static const char *const monthNames[2][MONTHS_IN_YEAR] =
     {
         { "January", "February", "March", "April", "May", "June",
           "July", "August", "September", "October", "November", "December" },
@@ -875,7 +857,7 @@ wxString wxDateTime::GetEnglishWeekDayName(WeekDay wday, NameFlags flags)
 {
     wxCHECK_MSG( wday != Inv_WeekDay, wxEmptyString, wxT("invalid weekday") );
 
-    static const char *weekdayNames[2][DAYS_PER_400_YEARS] =
+    static const char *const weekdayNames[2][DAYS_PER_WEEK] =
     {
         { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
           "Saturday" },
@@ -2070,16 +2052,22 @@ wxDateTime::wxDateTime_t wxDateTime::GetWeekOfMonth(wxDateTime::WeekFlags flags,
                                                     const TimeZone& tz) const
 {
     Tm tm = GetTm(tz);
-    wxDateTime dtMonthStart = wxDateTime(1, tm.mon, tm.year);
-    int nWeek = GetWeekOfYear(flags) - dtMonthStart.GetWeekOfYear(flags) + 1;
-    if ( nWeek < 0 )
+    const wxDateTime dateFirst = wxDateTime(1, tm.mon, tm.year);
+    const wxDateTime::WeekDay wdFirst = dateFirst.GetWeekDay();
+
+    if ( flags == Default_First )
     {
-        // this may happen for January when Jan, 1 is the last week of the
-        // previous year
-        nWeek += IsLeapYear(tm.year - 1) ? 53 : 52;
+        flags = GetCountry() == USA ? Sunday_First : Monday_First;
     }
 
-    return (wxDateTime::wxDateTime_t)nWeek;
+    // compute offset of dateFirst from the beginning of the week
+    int firstOffset;
+    if ( flags == Sunday_First )
+        firstOffset = wdFirst - Sun;
+    else
+        firstOffset = wdFirst == Sun ? DAYS_PER_WEEK - 1 : wdFirst - Mon;
+
+    return (wxDateTime::wxDateTime_t)((tm.mday - 1 + firstOffset)/7 + 1);
 }
 
 wxDateTime& wxDateTime::SetToYearDay(wxDateTime::wxDateTime_t yday)
@@ -2356,7 +2344,15 @@ wxDateTime& wxDateTime::SetFromMSWSysTime(const SYSTEMTIME& st)
     return Set(st.wDay,
             static_cast<wxDateTime::Month>(wxDateTime::Jan + st.wMonth - 1),
             st.wYear,
-            0, 0, 0);
+            st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
+}
+
+wxDateTime& wxDateTime::SetFromMSWSysDate(const SYSTEMTIME& st)
+{
+    return Set(st.wDay,
+            static_cast<wxDateTime::Month>(wxDateTime::Jan + st.wMonth - 1),
+            st.wYear,
+            0, 0, 0, 0);
 }
 
 void wxDateTime::GetAsMSWSysTime(SYSTEMTIME* st) const
@@ -2367,12 +2363,28 @@ void wxDateTime::GetAsMSWSysTime(SYSTEMTIME* st) const
     st->wMonth = (WXWORD)(tm.mon - wxDateTime::Jan + 1);
     st->wDay = tm.mday;
 
+    st->wDayOfWeek = 0;
+    st->wHour = tm.hour;
+    st->wMinute = tm.min;
+    st->wSecond = tm.sec;
+    st->wMilliseconds = tm.msec;
+}
+
+void wxDateTime::GetAsMSWSysDate(SYSTEMTIME* st) const
+{
+    const wxDateTime::Tm tm(GetTm());
+
+    st->wYear = (WXWORD)tm.year;
+    st->wMonth = (WXWORD)(tm.mon - wxDateTime::Jan + 1);
+    st->wDay = tm.mday;
+
     st->wDayOfWeek =
     st->wHour =
     st->wMinute =
     st->wSecond =
     st->wMilliseconds = 0;
 }
+
 #endif // __WXMSW__
 
 #endif // wxUSE_DATETIME