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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS