create your own time stamp 2

From my previous post, create your own time stamp, I have introduced a way to construct your own time stamp.

Recently I found a better way, and with this function, you can create a better time stamp, yet the way of construct the time stamp is far more simple compared to my previous post.

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


int main()
{
    char timestamp[100];
    time_t mytime;
    struct tm *mytm;
    mytime=time(NULL);
    mytm=localtime(&mytime);

    strftime(timestamp,sizeof timestamp,"%a, %d %b %Y %H:%M:%S %z",mytm);

    printf("%s\n",timestamp);

    return 0;
}

Yes, that’s it, and the output is show as below:

Wed, 26 Sep 2007 23:54:50 +0800