Link to home
Start Free TrialLog in
Avatar of fritzke
fritzke

asked on

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

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
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Use strftime() standard function
Avatar of Kent Olsen
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
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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.
>> 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.
Avatar of fritzke
fritzke

ASKER

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
Avatar of fritzke

ASKER

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;
}