Skip to content

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

Categories: Uncategorized.

Comment Feed

3 Responses

  1. Nice example. But i get warnings on its compilation:
    warning: assignment makes pointer from integer without a cast
    warning: incompatible implicit declaration of built-in function ‘strftime’.

    GCC version is gcc (GCC) 4.1.1 (Gentoo 4.1.1-r1 p1.7.1).

    What is wrong?

  2. it’s most probably due to a cast problem.
    The strftime’s prototype is :
    size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timptr);

    Hope this will help.

    rouiskAugust 21, 2008 @ 9:08 pm
  3. This example is good, but i want display milliseconds along with this.
    pls send me the code for displating Date – Hr:Min:Sec:Milliseconds format.



Some HTML is OK

or, reply to this post via trackback.