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.

memset, clean your container before you put your stuff in

It is very important to clean your container before you put your stuff in. It might dirty your stuff if you do not do so. This happen to memory storage.

By declaring a variable such as objects of class is just preserved you a bulk of memory space. It doesn’t come with the memory clean up. We tends to forget to reset all to zero first before we use. Because, usually it is okay, it didn’t cause any problem.

MY_CLASS myClass
for (i=0;i<Len;i++)
     myClass.FeedData(i,szData[i]);

You are just lucky, if it doesn’t cause any problem yet. Doing that is injecting a hidden bug to your code. I have this bad habits too and I suffered from it. And Now, I gonna put a full stop on this. It wasted me an hour to find the bug yesterday.

Clean it up, before it poison your data. A tiny dust can make you sick and it sometimes leads you to death. So reset it to all zero.

void *memset(void *s, int c, size_t n);

Just a line of code that will safe you from many hours of debugging.

MY_CLASS myClass

memset(&myClass,0,sizeof(myClass));

for (i=0;i&lt;Len;i++)
     myClass.FeedData(i,szData[i]);

fprintf to print text on standard output screen, why?

We can actually do this to print text on screen,

fprintf(stdout,"Hello %s\n","World");

But why?

Try to imagine that you are doing a debug log file function, which to print “debug string” to a file with specified FILE such as this

int DebugLog(FILE *fp, char *str)
{
    if (fp==NULL)
        return -1;

    fprintf(fp,"%d : [debug] %s\n",str);
    return 1;
}

So when you want to write the debug string to file, pass the “file output stream”, print on screen? pass “stdout”.

int main()
{
    FILE *fp=fopen("any.txt","w");
    //write to file
    DebugLog(fp,"Things failed");
    fclose(fp);
    //write to screen
    DebugLog(stdout,"Things failed");

    return 1;
}

FILE is a stream object, when you create a stream pointer, you can point to stdout. stdout is a standard output defined when you initiate main(). You can do something fun, look at another example bellow

    FILE *fp=stdout;

    stdout=fopen("out-snap","w");
    printf("Hello hello");
    fclose(stdout);

    stdout=fp;
    printf("Hello Hello Hello");

The code above redirect the standard output to a file “out-snap” and I uses fp to preserved the stdout address. Later I do fclose(stdout), I was actually closing the file stream and point back the stdout to standard output.

Posted in Uncategorized | 1 Reply