wyatt
wyatt

Reputation: 3246

Converting a time_t to string

I have a time_t variable containing a timestamp which I'd like to store in a database, so I need it as a string. How would I convert it?

Also, on the subject, how would I convert a timestamp string into a time_t variable?

Thanks, Wyatt

Upvotes: 1

Views: 4095

Answers (2)

mathon12
mathon12

Reputation: 181

Since it's tagged C++, I'd recommend this: istringstream http://www.cplusplus.com/reference/iostream/istringstream/

Works like a charm. Might require ostringstream as well.

Upvotes: 0

nc3b
nc3b

Reputation: 16250

Look at ctime, it takes a time_t and returns a string. To make a timestamp from a string, look at mktime. Populate the fields of a struct tm and call mktime. It should return a time_t.

Upvotes: 2

Related Questions