Need help with wxDateTime Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
starBuck
In need of some credit
In need of some credit
Posts: 7
Joined: Thu Feb 01, 2018 5:57 pm

Need help with wxDateTime

Post by starBuck »

Hello, can someone point me in the direction of wxDateTime.

I have one that I save to disk as a string using wxDateTime::Format() via wxConfig. It is stored as mm/dd/yy hh:mm:ss (which is default I think). Now when the program starts back up I wish to put that value back into a wxDateTime variable via a wxConfig::Read() call.

Can someone explain how to accomplish this please? What I'm doing apparently isn't correct because my days/hours/minutes/seconds are all off.

Thanks
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Need help with wxDateTime

Post by doublemax »

Save the value of wxDateTime::GetTicks() into the config and use that value to recreate the wxDateTime object after loading.
Use the source, Luke!
starBuck
In need of some credit
In need of some credit
Posts: 7
Joined: Thu Feb 01, 2018 5:57 pm

Re: Need help with wxDateTime

Post by starBuck »

Hi Doublemaxx, thanks for the reply. I think I need to elaborate more and show you the code in question.

Code: Select all

void networkMonitorFrame::processTimeSinceLastFailure(wxTimerEvent& WXUNUSED(event)) // Timer called once a second.
{
    int resVal;

    if((resVal = lastFailureIndicator->GetLabel().compare("N/A")) != 0) {	// will be N/A until a failure.
        wxDateTime currentTime = wxDateTime::Now();
        wxDateTime timeSinceLastFailure = wxDateTime();
        timeSinceLastFailure.ParseDateTime(lastFailureIndicator->GetLabel());
        wxTimeSpan span = currentTime.Subtract(timeSinceLastFailure);
        wxString str;
        if(span.GetDays() > 0)
            str.sprintf("[%02d] - %02d:%02d:%02llu", span.GetDays(), (span.GetHours() % 24), (span.GetMinutes() %60), (span.GetSeconds() %60));
        else str.sprintf("%02d:%02d:%02d", (span.GetHours() % 24), (span.GetMinutes() %60), (span.GetSeconds() %60));
        wxLogDebug(str);    // Will eventually update another label
    }
}
Failed time is 02/05/18 15:22:04, current time is 02/05/18 15:53 and this is what is being logged:

[730485] - 00:31:19
[730485] - 00:31:20
[730485] - 00:31:21
[730485] - 00:31:22


I'm so close, I just cannot figure out the GetDays() issue. Can you save GetTicks with a predetermined string?

Thanks
starBuck
In need of some credit
In need of some credit
Posts: 7
Joined: Thu Feb 01, 2018 5:57 pm

Re: Need help with wxDateTime

Post by starBuck »

I think I figured it out, now I just need to figure out how to fix it.

wxDateTime::Now() returns a date with the year in 4 digits where as wxDateTime::Format() returns a date with the year in 2 digits. Thus is why get days is several hundred thousand days off.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Need help with wxDateTime

Post by doublemax »

wxDateTime::Now() returns a date with the year in 4 digits where as wxDateTime::Format() returns a date with the year in 2 digits.
No. wxDateTime::Now() just returns a wxDateTime object that at the current date/time. How it's formatted into a string is independent of the internal storage.

In addition to lastFailureIndicator, i would store another wxDataTime object or just an integer with Ticks for the last error time. This will make everything much easier and more reliable.
Use the source, Luke!
starBuck
In need of some credit
In need of some credit
Posts: 7
Joined: Thu Feb 01, 2018 5:57 pm

Re: Need help with wxDateTime

Post by starBuck »

Agreed, and I will probably do that, but isn't it cool to know that in the past 2000 years there has been 730485 days? Quick fix was to % the GetDays() with 730485 which returned the proper value. I'm re-writing the code now to use ticks instead of the way I was doing it.

Thanks
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: Need help with wxDateTime

Post by PB »

FWIW, I store and load a datetime with wxConfig as a string using wxDateTime::FormatISOCombined() and wxDateTime::ParseISOCombined().
Post Reply