Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Beginner
Jun 6, 2000

Date Conversion

Question:
Can you point me at or come up with a generic class that allows date manipulation and storage without running into OS-specific or February 29 or Y2K issues?

Answer:
The Standard Library's date and time functions are declared in the header. All these functions are Y2K ready. C and C++ represent time as a signed integer that has at least 32 bits (64-bit environments often use a 64-bit datatype) and holds the number of seconds elapsed since the "epoch" (e.g., 1/1/1970 00:00:00). A 32-bit unit can safely represent any date between the epoch and January 18, 2038 10:14:08.

If you're relying on the Standard Library's functions, your code is immune to OS-specific, time-related bugs such as Feb29 or Y2K.

If you wish to compare two dates, use the difftime() function. For example:

#include 
time_t now = time(0); // get current time 
time_t yestderday = now-(24*60*60);
double diff_in_sec = difftime(now, yesterday);
You can either write a class that wraps these functions or use them directly. My advice is to avoid any non-standard date and time functions and classes because they tend to be inefficient and buggy.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date