tags:

views:

96

answers:

3

I need to generate time-stamp in this format yyyymmdd. Basically I want to create a filename with current date extension. (for example: log.20100817)

+13  A: 

http://www.cplusplus.com/reference/clibrary/ctime/strftime/

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

int main()
{
  char date[9];
  time_t t = time(0);
  struct tm *tm;

  tm = gmtime(&t);
  strftime(date, sizeof(date), "%Y%m%d", tm);
  printf("log.%s\n", date);
  return EXIT_SUCCESS;
}
Brandon Horsley
caf
A: 

Use strftime.

Ken Bloom
+1  A: 

Another alternative: Boost.Date_Time.

decimus phostle