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

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Today's "average" business in general is ever more reliant on technology and the Internet. Mobility is the most often cited business trend that has transformed the way many of us work and communicate. From an IT security perspective, this means that protection methods and tools from even a few years ago are rapidly becoming "unfit for purpose." This guide provides crucial facts to assist you in building a robust business case, meeting the demands of your business, and protecting against threats now and in the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds