CGoodTime -- A Y2.038K-Safe Replacement for CTime

The MFC CTime class uses the time_t "structure" internally to store its data. This means that it stores time/date as a signed 32-bit count of milliseconds since January 1, 1970. While this makes it safe for the dreaded year 2000, this counter will wrap sometime in early 2038. As a result, CTime stops functioning for any date from January 2038 and up.

Now I expect that all of my users will have upgraded to version 42 of my program by that point, but thinking like that is where the Y2K bugs came from in the first place. Besides, when customers or management press for information on Y2K compliance, it can be embarrassing to have to explain why the year 2038 is in the critical path. It's just easier to fix it.

So without further ado, I introduce my solution. I have re-written the CTime class using the tm structure in place of the time_t structure for internal storage. My new class is CGoodTime.

I have attempted to keep the interface consistent with the original CTime class with the intention that this would be a straight drop-in replacement. I believe I have succeeded in this goal with three exceptions.

  1. Methods that have time_t as parameters or return values in CTime have tm values in their place, since interfacing with time_t's would defeat the purpose.
  2. I dropped support for the operators which return CTimeSpan. I didn't need these, so I leave it as an exercise to you to implement these if you need them.
  3. I dropped support for the '#' character in the Format strings. See 2.

The sample project I've included here uses the CGoodTime class and the CTime class to produce text output of all ways a time can be represented with the the Format function. A radio button controls whether the output is in local time or Greenwich Mean Time. For dates prior to 2038, the two strings should match exactly. For dates 2038 and later, CTime does not produce output.

Download demo project - 19 KB

Download source - 6 KB



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds