create your own time stamp

If you wanna create a time stamp that display date and time, you can simply uses asctime() and localtime(), time() and a time_t variable to do that. time_t will declare a variable to store calendar time.

Calendar time, an absolute time value, it represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).

time() will returns the current calendar time to store at time_t variable. Next we uses localtime() to extract info from calendar time and store into a time structure (struct tm). At last asctime() takes in the time structure and turns the entire thing into string.

It is more easy to understand through example,

void timestamp()
{
    time_t ltime; /* calendar time */
    ltime=time(NULL); /* get current cal time */
    printf("%s",asctime( localtime(&ltime) ) );
}

This will print out :

Sat Jan 27 10:32:12 2007

If you want to customize the time stamp, you have to do more work, you need to adjusting calendar time value from the time structure before you print out.

Lets me redefine the timestamp()

int timestamp()
{
    time_t ltime;
    struct tm *Tm;

    ltime=time(NULL);
    Tm=localtime(&ltime);

    printf("[%d] %d %d %d, %d:%d:%d",
            Tm->tm_wday, /* Mon - Sun */
            Tm->tm_mday,
            Tm->tm_mon,
            Tm->tm_year,
            Tm->tm_hour,
            Tm->tm_min,
            Tm->tm_sec);
}

The result seems to be not what we are looking for,

[6] 27 0 107, 10:32:12

[6] means Saturday, that is Day of the Week.
27 0 107, its calendar time, so you need to adjust it.

    printf("[%d] %d %d %d, %d:%d:%d",
            Tm->tm_wday, /* Mon - Sun */
            Tm->tm_mday,
            Tm->tm_mon+1,
            Tm->tm_year+1900,
            Tm->tm_hour,
            Tm->tm_min,
            Tm->tm_sec);

You need milliseconds and microseconds too? The method of getting it is platform dependent, for win32, you may check out struct timeb and ftime(). For unix based system, let stake a look at gettimeofday() and struct timeval.

   struct timeval detail_time;
   gettimeofday(&detail_time,NULL);
   printf("%d %d", 
   detail_time.tv_usec /1000,  /* milliseconds */
   detail_time.tv_usec); /* microseconds */

For gettimeofday(), you need to include sys/time.h header.

16 thoughts on “create your own time stamp

  1. Pingback: create a timer

  2. Pingback: create your own time stamp 2

  3. Excuse me , im trying to do it with microsecond and milisencond but it doesnt work. I need to do a homework for calculating the performance of the different codes and the localtime is not helpful. please help me!!

  4. If you didn’t want to print it out straight away, but wanted to store it in a string (or something similar) to be used later on how would you do this?

  5. #include
    #include
    #include

    /*
    Returns the current time.
    */
    char *time_stamp(){

    char *timestamp = (char *)malloc(sizeof(char) * 16);
    time_t ltime;
    ltime=time(NULL);
    struct tm *tm;
    tm=localtime(&ltime);

    sprintf(timestamp,”%04d%02d%02d%02d%02d%02d”, tm->tm_year+1900, tm->tm_mon,
    tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
    return timestamp;
    }

    int main(){

    printf(” Timestamp: %s\n”,time_stamp());
    return 0;

    }

    Output: 20110912130940 // 2011 Sep 12 13:09:40

  6. When I try using the code I get error: conflicting types for ‘time_stamp’

Leave a Reply

Your email address will not be published. Required fields are marked *