From: Vadim Zeitlin Date: Sun, 25 Mar 2012 23:17:01 +0000 (+0000) Subject: Added wxTimePickerCtrl::GetTime() and SetTime(). X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/c0795ce8ea8a3f50ce32a2b9aad1cc3bcc458205 Added wxTimePickerCtrl::GetTime() and SetTime(). These methods, taking broken down time representation, avoid the problems arising due to DST complications when using wxDateTime to represent the time as special care needs to be taken in this case to avoid using the date part corresponding to a DST change date at which time is discontinuous. Document the problem with the old functions and use the new ones in the sample. See #14137. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71004 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 396dc4240f..99810742a2 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -494,6 +494,7 @@ All (GUI): - Added EVT_AUI_PANE_ACTIVATED event (Ronny Krüger). - Added wxSplitterWindow::SetSashInvisible() (Armel Asselin). - Enable/disable "Window" menu items in AUI MDI correctly (wsu). +- Added wxTimePickerCtrl::Get/SetTime(). GTK: diff --git a/include/wx/timectrl.h b/include/wx/timectrl.h index 4672675cee..14c98f7327 100644 --- a/include/wx/timectrl.h +++ b/include/wx/timectrl.h @@ -50,9 +50,42 @@ public: /* We also inherit Set/GetValue() methods from the base class which define our public API. Notice that the date portion of the date passed as - input is ignored and for the result date it's always today, but only - the time part of wxDateTime objects is really significant here. + input or received as output is or should be ignored, only the time part + of wxDateTime objects is really significant here. Use Set/GetTime() + below for possibly simpler interface. */ + + // Set the given time. + bool SetTime(int hour, int min, int sec) + { + // Notice that we should use a date on which DST doesn't change to + // avoid any problems with time discontinuity so use a fixed date (on + // which nobody changes DST) instead of e.g. today. + wxDateTime dt(1, wxDateTime::Jan, 2012, hour, min, sec); + if ( !dt.IsValid() ) + { + // No need to assert here, wxDateTime already does it for us. + return false; + } + + SetValue(dt); + + return true; + } + + // Get the current time components. All pointers must be non-NULL. + bool GetTime(int* hour, int* min, int* sec) const + { + wxCHECK_MSG( hour && min && sec, false, + wxS("Time component pointers must be non-NULL") ); + + const wxDateTime::Tm tm = GetValue().GetTm(); + *hour = tm.hour; + *min = tm.min; + *sec = tm.sec; + + return true; + } }; #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) diff --git a/interface/wx/timectrl.h b/interface/wx/timectrl.h index 41bbc54217..42a7e18a2b 100644 --- a/interface/wx/timectrl.h +++ b/interface/wx/timectrl.h @@ -88,6 +88,18 @@ public: const wxValidator& validator = wxDefaultValidator, const wxString& name = "timectrl"); + /** + Returns the currently entered time as hours, minutes and seconds. + + All the arguments must be non-@NULL, @false is returned otherwise and + none of them is modified. + + @see SetTime() + + @since 2.9.4 + */ + bool GetTime(int* hour, int* min, int* sec) const; + /** Returns the currently entered time. @@ -96,12 +108,38 @@ public: */ virtual wxDateTime GetValue() const = 0; + /** + Changes the current time of the control. + + Calling this method does not result in a time change event. + + @param hour The new hour value in 0..23 interval. + @param min The new minute value in 0..59 interval. + @param sec The new second value in 0..59 interval. + @return @true if the time was changed or @false on failure, e.g. if the + time components were invalid. + + @see GetTime() + + @since 2.9.4 + */ + bool SetTime(int hour, int min, int sec); + /** Changes the current value of the control. The date part of @a dt is ignored, only the time part is displayed in the control. The @a dt object must however be valid. + In particular notice that it is a bad idea to use default wxDateTime + constructor from hour, minute and second values as it uses the today + date for the date part which means that some times can be invalid if + today happens to be the day of DST change. For example, when switching + to summer time the time 2:00 typically doesn't exist as the clocks jump + directly to 3:00. To avoid this problem, use a fixed date on which DST + is known not to change (e.g. Jan 1, 2012) for the date part of the + argument or use SetTime(). + Calling this method does not result in a time change event. */ virtual void SetValue(const wxDateTime& dt) = 0; diff --git a/samples/widgets/timepick.cpp b/samples/widgets/timepick.cpp index 9100980e35..05cdd84b7c 100644 --- a/samples/widgets/timepick.cpp +++ b/samples/widgets/timepick.cpp @@ -217,21 +217,23 @@ void TimePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event)) void TimePickerWidgetsPage::OnButtonSet(wxCommandEvent& WXUNUSED(event)) { - wxDateTime dt; - if ( !dt.ParseISOTime(m_textCur->GetValue()) ) + int h, m, s; + if ( wxSscanf(m_textCur->GetValue(), "%d:%d:%d", &h, &m, &s) != 3 ) { wxLogError("Invalid time, please use HH:MM:SS format."); return; } - m_timePicker->SetValue(dt); + m_timePicker->SetTime(h, m, s); } void TimePickerWidgetsPage::OnTimeChanged(wxDateEvent& event) { - wxLogMessage("Time changed, now is %s (control value is %s).", - event.GetDate().FormatISOTime(), - m_timePicker->GetValue().FormatISOTime()); + int h, m, s; + m_timePicker->GetTime(&h, &m, &s); + + wxLogMessage("Time changed, now is %s (control value is %02d:%02d:%02d).", + event.GetDate().FormatISOTime(), h, m, s); } #endif // wxUSE_TIMEPICKCTRL