RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

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



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Date Conversion

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?

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:

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.



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