Microsoft Year 2000 Readiness Disclosure
& Resource Center |
|
|
|
Visual C++ and the Year
2000 |
|
Year 2000
Examples in C/C++
The following list of "examples" is only suggestive of
the various ways code can have Year 2000 issues. The list should not
be assumed to be comprehensive.
EXAMPLE: writing 2-bytes that may unexpectedly expand to 3
For
example:
time_t
now; time(&now); //
get current time struct tm* pWhen =
localtime(&now); // convert to
local time zone char DateBuf[9]; sprintf(DateBuf,
"%02d/%02d/%2d", pWhen->tm_month, pWhen->tm_day,
pWhen->tm_year);
The
"tm_year" member of struct tm is the number of years since 1900.
From 1910 until 1999 this code fragment will generate 2-digit years.
In the year 2000, years will be 3-digits long ("01/01/100"), and the
character buffer "DateBuf" will overflow.
EXAMPLE: hard-coded century
int year =
2000; // used to be <
2000 printf("%d/%d/19%02d", m, d, y
- 1900);
Notice
the hard-coded century "19" in the format string. If uncorrected,
this code will display the year 2000 as "19100".
Suggested Practice: Use GetDateFormat() with the
DATE_LONGDATE flag. In addition to correctly handling Year 2000
aspects of dates, GetDateFormat() also handles locale specific
issues.
EXAMPLE: Date-like formats
"%d/%d/%d", "%2d/%02d/%02d"
The
above is an example of a common format for writing dates (that the
relative ordering of the day, month and year varies across locales).
A pattern of this form occurring in a string is probably worth
examining both for Y2K and localization issues.
Suggested Practice: Use GetDateFormat() and
GetTimeFormat(). In addition to correctly handling Year 2000 aspects
of dates, they also handle locale specific issues.
EXAMPLE: 2-digit comparisons
Calling
a string comparison library function with one of the arguments being
a two-digit string indicates a section of code that may have Y2K
issues.
strcmp(x, "98");
// If this is an abbreviated
"year", //
the code around this may have a problemà
EXAMPLE: Constants 1900, 1970, 1980 in your code
int iYear = x + 1900;
Although
year arithmetic like this may be OK in your application, searching
for common offsets for years such as 1900, 1970 and 1980 may help
you find code where date arithmetic is occurring and review is
advisable.
Resources
A good
place to start looking is the Web:
Other
articles:
Gothard,
W and Rodner, L., "Strategies for Solving the Y2K Problem," Dr.
DobbÆs Journal, May 1998.
Moore,
R.L. and Foley, D.G., "Date Compression and Year 2000 Challenges,"
Dr. DobbÆs Journal, May 1998.
Pleas,
K., "Y2K for VB Developers: We take a look at some of the problem
areas you may encounter in your systems and applications," PC
Magazine, June 1998.
|