Sunday 13 November 2011

What is the best way to store the time? in C programming

What is the best way to store the time?

The best way to store time depends entirely on what you need to store time for, and in what way you intend to manipulate the time values. Take a look at the different uses you might have for time, and how that might influence your choice of storage method.

Suppose that you only need to track events and that you need to track them in “real time.” In other words, you want to ascertain the real-world time of when a given event occurred. The events you want to track might include the creation of a file, the start and completion of a long, complex program, or the time that a book chapter was turned in. In this case, you need to be able to retrieve and store the current time from the computer’s system clock. It is better and simpler to use one of the built-in time functions to retrieve the time and to store it directly in that format. This method requires comparatively little effort on your part.

For various reasons, you might not want to store the time as formatted by the standard C functions. You might want a simpler format, for easier manipulation, or you might want to represent time differently.In this case, it might be a good idea to represent time as an integer value, as demonstrated in the answer to FAQ XIII.2. This technique would enable you to advance through periods of time very simply and quickly and to compare different times to see which is earlier.

As with dates, you might have completely relative measures of time that will be difficult to quantify exactly. Although “half past noon” is not too hard to quantify, “after I eat lunch” is. This, however, might be not only the simplest way to track time but, in some cases, the only way! In these cases, you would simply haveto store the textual string that describes the time. In this case, this is the best way to store time.

Cross Reference:

XIII.2: How can I store time as a single integer? Are there any standards for this?

No comments:

Post a Comment