Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generate a date string in HTTP response date format in C

Tags:

c

date

I'm trying to generate a date string from current time to put into HTTP response header. It looks like this:

Date: Tue, 15 Nov 2010 08:12:31 GMT

I only have the default C library to work with. How do I do this?

like image 863
Wei Shi Avatar asked Sep 25 '11 21:09

Wei Shi


1 Answers

Use strftime(), declared in <time.h>.

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

int main(void) {
  char buf[1000];
  time_t now = time(0);
  struct tm tm = *gmtime(&now);
  strftime(buf, sizeof buf, "%a, %d %b %Y %H:%M:%S %Z", &tm);
  printf("Time is: [%s]\n", buf);
  return 0;
}

See the code "running" at codepad.

like image 141
pmg Avatar answered Sep 28 '22 08:09

pmg