2

I'm writing a simple web server in C on Linux.

I have to add the last modified time of a file that will be transferred to the client(browser),

I analysed how some websites and find out that they all present the time in format:

Fri, 12 Nov 2010 16:02:54 GMT,

my question is, can I transfer a time_t value to something string in the former format simply? Is there a function to do this? Or is the format unimportant at all?

wong2
  • 34,358
  • 48
  • 134
  • 179

2 Answers2

5

strftime() is the function you need.

The wikipedia article on time_t gives a good example http://en.wikipedia.org/wiki/Time_t

Alexander Gorshenev
  • 2,769
  • 18
  • 33
2

That looks like the format defined in RFC 822, you can convert a time_t to a struct tm, and format it to a using strftime string like

struct tm time_tm;
char http_time[64];
time_t  t = time(NULL);
gmtime_r(&t,&time_tm);
strftime(http_time,sizeof http_time,"%a, %d %b %Y %H:%M:%S +0000",&time_tm);

NOTE; strftime converts the names using the current locale, you might need to change the current locale to get the English names.

The HTTP spec also allow the format given by asctime()

nos
  • 223,662
  • 58
  • 417
  • 506