Timestamp file name 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
Earned some good credits
Earned some good credits
Posts: 118
Joined: Fri Nov 21, 2008 2:18 pm

Timestamp file name

Post by JohnD » Mon Dec 05, 2011 5:59 pm

I have a feature where my app auto-saves a screen-grab. I want to name it with a prefix (user-name), then a timestamp string which would mean sorting files by name would put them in the right time-order... e.g year/month/day/hour/min/sec.

It seems pretty likely wx has something like this I can just use... I found this code which works but would rather not reinvent the wheel

Code: Select all

        struct tm *pTime;
        time_t ctTime; time(&ctTime);
        pTime = localtime( &ctTime );
        std::ostringstream oss;
        oss	<< std::setw(2) << std::setfill('0') << (pTime->tm_mon + 1)
            << std::setw(2) << std::setfill('0') << pTime->tm_mday
            << std::setw(2) << std::setfill('0') << (pTime->tm_year + 1900)
            << "_" << std::setw(2) << std::setfill('0') << pTime->tm_hour
            << std::setw(2) << std::setfill('0') << pTime->tm_min
            << std::setw(2) << std::setfill('0') << pTime->tm_sec
            << std::setw(3) << std::setfill('0') << (mTimer->getMilliseconds() % 1000);
Last edited by JohnD on Mon Dec 05, 2011 6:11 pm, edited 1 time in total.

User avatar
Posts: 15293
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Timetamp file name

Post by doublemax » Mon Dec 05, 2011 6:05 pm

Code: Select all

wxString filename = wxString::Format( wxT("file_%s.txt"), wxDateTime::Now().Format(wxT("%Y-%m-%d_%H-%M-%S")) );
For the documentation of the wxDateTime::Format() parameters:
http://www.cplusplus.com/reference/clib ... /strftime/
Use the source, Luke!

Post Reply