31

Let's say I have time_t and tm structure. I can't use Boost but MFC. How can I make it a string like following?

Mon Apr 23 17:48:14 2012

Is using sprintf the only way?

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
  • 2
    [Boost.DateTime](http://www.boost.org/libs/date_time) has formatted I/O facilities. – ildjarn Apr 23 '12 at 22:10
  • With a sentence or two more, that could be a (the?) answer, @ildjarn – Jasper Apr 23 '12 at 22:11
  • 2
    @Jasper : While I use the library, I find recommending it to other people difficult because the documentation is so horrible. I just left a comment so the OP could pursue that on their own if they choose (e.g. there are plenty of SO answers demonstrating exactly this, such as [this one](http://stackoverflow.com/a/5697577/636019)). – ildjarn Apr 23 '12 at 22:12

6 Answers6

56

The C library includes strftime specifically for formatting dates/times. The format you're asking for seems to correspond to something like this:

char buffer[256];

strftime(buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y", &your_tm);

I believe std::put_time uses a similar format string, though it does relieve you of having to explicitly deal with a buffer. If you want to write the output to a stream, it's quite convenient, but to get it into a string it's not a lot of help -- you'd have to do something like:

std::stringstream buffer;

buffer << std::put_time(&your_tm, "%a %b %d %H:%M:%S %Y");

// now the result is in `buffer.str()`.

std::put_time is new with C++11, but C++03 has a time_put facet in a locale that can do the same thing. If memory serves, I did manage to make it work once, but after that decided it wasn't worth the trouble, and I haven't done it since.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
16

I'd try std::put_time. See the link here for information on how to use it. It supports full format strings and such.

Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
2

ctime() produces strings in that format. It takes a pointer to a time_t.
There's also asctime() that takes a pointer to a struct tm and does the same.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • `ctime(&curtime)`, where time_t curtime; reference: http://www.tutorialspoint.com/c_standard_library/c_function_ctime.htm – parasrish May 12 '17 at 08:41
2

If you need to worry about formatting on different locales, don't forget to initialize the CRT with the current locale. This affects COleDateTime too.

setlocale(LC_COLLATE,“.OCP”); // sets the sort order

setlocale(LC_MONETARY, “.OCP”); // sets the currency formatting rules

setlocale(LC_NUMERIC, “.OCP”); // sets the formatting of numerals

setlocale(LC_TIME, “.OCP”); // defines the date/time formatting

See my blog post which ties in MSDN articles and other sources. http://gilesey.wordpress.com/2012/12/30/initailizing-mfccrt-for-consumption-of-regional-settings-internationalizationc

GilesDMiddleton
  • 2,279
  • 22
  • 31
1

MFC has COleDateTime which has a contructor that takes time_t (or __time64_t) and has a Format method.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1
CTime obj1(time_tObj);

CString s = obj1.Format( "%a %b %d %H:%M:%S %Y" );
Jeeva
  • 4,585
  • 2
  • 32
  • 56