This page has been designed specifically for the printed screen. It may look different than the page you were viewing on the web.
Please recycle it when you're done reading.

The URI for this page is { http://cc.byexamples.com }

create your own time stamp Posted on January 26th

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,

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()

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.

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.

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

Trackback URL

Some Responses to “create your own time stamp” :

  1. [...] create your own time stamp [...]

    Commented create a timer on March 14th, 2007.
  2. Good Article

    Commented puneet on May 29th, 2007.
  3. [...] my previous post, create your own time stamp 2, I have introduced a way to construct your own time [...]

    Commented create your own time stamp 2 on September 27th, 2007.
  4. Really helpful. Thank you very much.

    Commented senthil on March 17th, 2009.
Leave your own comments about this post: