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: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds