How to convert Unix timestamp to readable date string (in C or C++)?

fritzke
fritzke used Ask the Experts™
on
I am looking for C/C++ code to convert a Unix timestamp (seconds since Jan. 1, 1970) , e.g.

1193060347

to a human-readable date format, e.g.

Mon, 22 Oct 2007 13:39:07 GMT
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jaime OlivaresSoftware Architect
Top Expert 2008

Commented:
Use strftime() standard function
Hi fritzke,

If you want to write the code yourself, all you need is a couple of key numbers.

  86400 -- the number of seconds in a day.

  1461 -- the number of days in 4 years.

Divide your number by 86400.  This gives you the number of elapsed days.
Take the number module 86400.  This gives you the number of seconds into the target day.

Solve the years 4 at a time.  That way the leap year logic plays out the cleanest.


That's about it.

Good Luck,
Kent
Software Architect
Top Expert 2008
Commented:
The easiest way will be ctime() function:
http://msdn2.microsoft.com/en-us/library/59w5xcdy(VS.71).aspx

But if you want full control over formatting, use strftime function, but you will have to convert the timestamp to a struct tm first.

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

jkr
Top Expert 2012
Commented:
The simplest way probably is 'asctime()':

/* ASCTIME.C: This program places the system time
 * in the long integer aclock, translates it into the
 * structure newtime and then converts it to string
 * form for output, using the asctime function.
 */

#include <time.h>
#include <stdio.h>

struct tm *newtime;
time_t aclock;

void main( void )
{
   time( &aclock );                 /* Get time in seconds */

   newtime = localtime( &aclock );  /* Convert time to struct */
                                    /* tm form */

   /* Print local time as a string */
   printf( "The current date and time are: %s", asctime( newtime ) );
}
Jaime is right but you need to convert time_t (seconds since 1/1/1970) to struct tm before using strftime

   time_t tt = 1193060347;
   struct tm * ptm = localtime(tt);
   char buf[30];
   strftime (buf, 30, "%a, %d %b %YYYY %HH:%MM:%SS",  ptm);

Regards, Alex



Jaime OlivaresSoftware Architect
Top Expert 2008

Commented:
>>you need to convert time_t (seconds since 1/1/1970) to struct tm
Yes, I have just stated that.

Notice time_t is in fact a 32-bit integral, so you can cast with easy.
Top Expert 2009

Commented:
>> Use strftime() standard function

>> The easiest way will be ctime() function:

>> The simplest way probably is 'asctime()':

This has to be confusing for fritzke heh. All three of the suggested functions are fine - they just work slightly differently. You can find a reference for these three functions, and the other standard time operations here :

        http://www.cplusplus.com/reference/clibrary/ctime/
>>>> This has to be confusing for fritzke heh
As all these possibilities will  give some (valid) output fritzke most likely can find out what fits best without reading the docs. I would bet on the first of these methods ;-) but I will rely on his judgement.

Author

Commented:
Hello experts,

thanks for enlightening me. Now I feel prepared to answer time-related questions myself 8v).

I did a point split since since jaime definitely was first with a valid hint to relevant functions, but jkr and isme... did very shortly afterwards actually provide code which I specifically did ask for.

I hope everyone is fine with my decision.

Thanks again!

Bernd

Author

Commented:
fyi:

This is what I finally coded according to your suggestions:

std::string timeStampToString(time_t t) {
      //time_t tt = 1193060347;
      struct tm * ptm = localtime(&t);
      char buf[30];
      // Format: Tue, 2007-10-23 10:45:51
      strftime (buf, 30, "%a,%F %H:%M:%S",  ptm);
      std::string result(buf);
      return result;
}

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial