wxDateTime::Set() failures 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
raananb
Super wx Problem Solver
Super wx Problem Solver
Posts: 488
Joined: Fri Oct 27, 2006 4:35 pm
Location: Paris, France
Contact:

wxDateTime::Set() failures

Post by raananb »

Under Windows 10 & Visual Studio 2017, using the Trunk, the code below compiles but fails in execution with 'Invalide wxDateTime' at all the Set() functions.

Code: Select all

    long day, month, year;

    wxString date = "2018-01-01";
    date.Mid(8,2).ToLong(&day);
    date.Mid(5,2).ToLong(&month);
    date.Left(4).ToLong(&year);

    wxDateTime* mydate = new wxDateTime();
    mydate->SetDay((wxDateTime::wxDateTime_t) (unsigned short) day);
    mydate->SetMonth(wxDateTime::Month(wxDateTime::Jan + month - 1));
    mydate->SetYear((int)year);
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxDateTime::Set() failures

Post by PB »

Code: Select all

wxDateTime* mydate = new wxDateTime()
creates myDate in an invalid state. The individual Set{Day!Month!Year}() methods do not alter the state of wxDateTime:

Code: Select all

wxDateTime& wxDateTime::SetDay(wxDateTime_t mday)
{
    wxASSERT_MSG( IsValid(), wxT("invalid wxDateTime") );

    Tm tm(GetTm());
    tm.mday = mday;
    Set(tm);

    return *this;
}
Either use a different constructor to create the wxDateTime instance or use one of the overloaded Set() or Parse*() methods.
raananb
Super wx Problem Solver
Super wx Problem Solver
Posts: 488
Joined: Fri Oct 27, 2006 4:35 pm
Location: Paris, France
Contact:

Re: wxDateTime::Set() failures

Post by raananb »

Thanks for the information about the default constructor.

The solution is then to create a valid instance of mydate and apply the Set() functions as needed:

wxDateTime* mydate = new wxDateTime(wxDateTime::Now());

....

mydate->SetDay(day);
mydate->SetMonth(wxDateTime::Month(wxDateTime::Jan + month - 1)
myDate->SetYear(year)
Post Reply