Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

portable way to create a timestamp in c/c++

Tags:

c++

c

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)

like image 675
vehomzzz Avatar asked Nov 29 '22 18:11

vehomzzz


2 Answers

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;
}
like image 83
Brandon Horsley Avatar answered Dec 10 '22 16:12

Brandon Horsley


Another alternative: Boost.Date_Time.

like image 27
decimus phostle Avatar answered Dec 10 '22 16:12

decimus phostle