Extract common code in a new wxTryGetTm() helper function
Replace exactly the same code occurring in both wxDateTime::GetTm() and Format() with a single version in a new wxTryGetTm() function. No real changes yet, except for removing of some useless asserts.
This commit is contained in:
@@ -1430,6 +1430,24 @@ unsigned long wxDateTime::GetAsDOS() const
|
|||||||
// time_t <-> broken down time conversions
|
// time_t <-> broken down time conversions
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
const tm* wxTryGetTm(tm& tmstruct, time_t t, const wxDateTime::TimeZone& tz)
|
||||||
|
{
|
||||||
|
if ( tz.GetOffset() == -wxGetTimeZone() )
|
||||||
|
{
|
||||||
|
// we are working with local time
|
||||||
|
return wxLocaltime_r(&t, &tmstruct);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
t += (time_t)tz.GetOffset();
|
||||||
|
#if !defined(__VMS__) // time is unsigned so avoid warning
|
||||||
|
if ( t < 0 )
|
||||||
|
return NULL;
|
||||||
|
#endif
|
||||||
|
return wxGmtime_r(&t, &tmstruct);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const
|
wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const
|
||||||
{
|
{
|
||||||
wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
|
wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );
|
||||||
@@ -1437,39 +1455,9 @@ wxDateTime::Tm wxDateTime::GetTm(const TimeZone& tz) const
|
|||||||
time_t time = GetTicks();
|
time_t time = GetTicks();
|
||||||
if ( time != (time_t)-1 )
|
if ( time != (time_t)-1 )
|
||||||
{
|
{
|
||||||
// use C RTL functions
|
// Try to use the RTL.
|
||||||
struct tm tmstruct;
|
struct tm tmstruct;
|
||||||
tm *tm;
|
if ( const tm* tm = wxTryGetTm(tmstruct, time, tz) )
|
||||||
if ( tz.GetOffset() == -wxGetTimeZone() )
|
|
||||||
{
|
|
||||||
// we are working with local time
|
|
||||||
tm = wxLocaltime_r(&time, &tmstruct);
|
|
||||||
|
|
||||||
// should never happen
|
|
||||||
wxCHECK_MSG( tm, Tm(), wxT("wxLocaltime_r() failed") );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
time += (time_t)tz.GetOffset();
|
|
||||||
#if defined(__VMS__) // time is unsigned so avoid warning
|
|
||||||
int time2 = (int) time;
|
|
||||||
if ( time2 >= 0 )
|
|
||||||
#else
|
|
||||||
if ( time >= 0 )
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
tm = wxGmtime_r(&time, &tmstruct);
|
|
||||||
|
|
||||||
// should never happen
|
|
||||||
wxCHECK_MSG( tm, Tm(), wxT("wxGmtime_r() failed") );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tm = (struct tm *)NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( tm )
|
|
||||||
{
|
{
|
||||||
// adjust the milliseconds
|
// adjust the milliseconds
|
||||||
Tm tm2(*tm, tz);
|
Tm tm2(*tm, tz);
|
||||||
|
@@ -65,6 +65,7 @@
|
|||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
extern void wxInitTm(struct tm& tm);
|
extern void wxInitTm(struct tm& tm);
|
||||||
|
extern const tm* wxTryGetTm(tm& tmstruct, time_t t, const wxDateTime::TimeZone& tz);
|
||||||
|
|
||||||
extern wxString wxCallStrftime(const wxString& format, const tm* tm);
|
extern wxString wxCallStrftime(const wxString& format, const tm* tm);
|
||||||
|
|
||||||
@@ -367,40 +368,9 @@ wxString wxDateTime::Format(const wxString& formatp, const TimeZone& tz) const
|
|||||||
|
|
||||||
if ( canUseStrftime )
|
if ( canUseStrftime )
|
||||||
{
|
{
|
||||||
// use strftime()
|
// Try using strftime()
|
||||||
struct tm tmstruct;
|
struct tm tmstruct;
|
||||||
struct tm *tm;
|
if ( const tm* tm = wxTryGetTm(tmstruct, time, tz) )
|
||||||
if ( tz.GetOffset() == -wxGetTimeZone() )
|
|
||||||
{
|
|
||||||
// we are working with local time
|
|
||||||
tm = wxLocaltime_r(&time, &tmstruct);
|
|
||||||
|
|
||||||
// should never happen
|
|
||||||
wxCHECK_MSG( tm, wxEmptyString, wxT("wxLocaltime_r() failed") );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
time += (int)tz.GetOffset();
|
|
||||||
|
|
||||||
#if defined(__VMS__) // time is unsigned so avoid warning
|
|
||||||
int time2 = (int) time;
|
|
||||||
if ( time2 >= 0 )
|
|
||||||
#else
|
|
||||||
if ( time >= 0 )
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
tm = wxGmtime_r(&time, &tmstruct);
|
|
||||||
|
|
||||||
// should never happen
|
|
||||||
wxCHECK_MSG( tm, wxEmptyString, wxT("wxGmtime_r() failed") );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
tm = (struct tm *)NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( tm )
|
|
||||||
{
|
{
|
||||||
return wxCallStrftime(format, tm);
|
return wxCallStrftime(format, tm);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user