Sunday 13 November 2011

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

What is the best way to store the date?

To put it simply, there is no best way to store the date. The way you choose to store the date will depend on what exactly you plan to do with it. You might want to store it as an integer (perhaps counting days from a fixed time in history); as a structure containing month, day, year, and other information; or as a textual string. A textual string might seem to be impractical and difficult to handle, but as you shall see, it has its uses.

If you are merely keeping track of numerical dates, the problem is relatively simple. You should use one of  the built-in formats, or represent time as an integer, and so on. You should determine whether you need to store the current date, update the date, check how far apart two dates are, and so on. There are ways to carry out many of these tasks and more using the formats and functions contained in the standard C library. However, you might be restricted if you become “locked into” one format too early in the development of your program. By keeping an open mind and by keeping your code flexible, you can use the most suited functions when the time comes.

However, you might want to represent dates in a more complex fashion. You remember dates in different ways. You don’t always remember the exact date for everything; you might remember an important date in your life as “three days after my 16th birthday party” or remember a historical date as “10 years after the fall of the Ottoman Empire.” Such kinds of dates cannot be stored as a simple numeric or structure. They require a more complex schema. You still might want to store such relative dates along with a reference to a known date that the computer can handle, or with a fixed date. This technique would aid in sorting and manipulating dates.

Cross Reference:

XIII.1: How can I store a date in a single number? Are there any standards for this?

No comments:

Post a Comment