Date Conversion

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:

#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.

See also  Does It Make Sense to Splurge on a Laptop?

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist