Hi All:

I have downloaded C++ 2005 Express from Microsoft.
I then run a date + time program, the compiler says the
functions I am using are deprecated, and I should use
localtime_s.
So You go to Microsoft, search for localtime_s.
You throw the example code into your compiler.
Bingo FORD, Found on Road Dead.
It won't compile.
Why, they are using the exit function
and forgot to include <stdlib.h>.
I DON'T think it will work!
How they got it to work without this library is
totally amazing and also impossible.
If you are totalled bored and have nothing to
do. Go to Microsoft and search for localtime_s.
Read the entry Hi All.
or read the Microsoft entry for localtime_s.
http://msdn2.microsoft.com/en-us/library/a442x3ye.aspx
Microsoft help
http://support.microsoft.com/contactus/?ws=support

Code Sample that will work!

// REM LOCALTIME.CPP 7:29 AM 8/22/2006
/// / Source From http://msdn2.microsoft.com// /en-us/library/a442x3ye.aspx
// crt_loca// ltime_s.c// _getch ( ) added 8:56 PM 8/30/2006
/* this program uses _time64 to get the current time * and then uses _localtime64_s() to convert this time to a structure * representing the local time. */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h> // MUST HAVE for exit to work??
#include <conio.h> // for _getch( )
int main( void ) 
{
 struct tm newtime;
 char am_pm [ ] = "AM";
 __time64_t long_time;
 char timebuf[26];
 errno_t err; 
// Get time as 64-bit integer.
 _time64 ( &long_time );
 // Convert to local time.
 err = _localtime64_s ( &newtime, &long_time );
 if ( err )
 { printf( "Invalid argument to _localtime64_s." ); 
  exit ( 1 ); }
 
if ( newtime.tm_hour > 12 )
// Set up extension.
 strcpy_s ( am_pm, sizeof ( am_pm ), "PM" );
 if ( newtime.tm_hour > 12 )
// Convert from 24-hour 
newtime.tm_hour -= 12;// to 12-hour clock.
 if ( newtime.tm_hour == 0 )
// Set hour to 12 if midnight.
 newtime.tm_hour = 12; // Convert to an ASCII representation.
err = asctime_s ( timebuf, 26, &newtime ); 
if ( err ) { printf ( "Invalid argument to asctime_s." );
 exit ( 1 ); }
 
printf ( "%.19s %s\n", timebuf, am_pm );
 _getch(); // Pause, Wait for Keyboard
 }
 // Sample Output 
// Fri Apr 25 01:19:27
// PM // F:\PROGRA~1\MID05A~1\VC\bin>cl 
// altime 
// Microsoft (R) 32-bit C/C++ Optimizing 
// Compiler Version 14.00.50727.42 for 80x8 
// 6 
// Copyright (C) Microsoft Corporation. 
// All rights reserved. 
// cl : Command line warning D9024 : unr 
// ecognized source file type 'localtime', 
// obj
// ect file assumed 
// Microsoft (R) Incremental Linker Vers 
// ion 8.00.50727.42 
// Copyright (C) Microsoft Corporation
// All rights reserved. 
// /out:localtime.exe
// localtime 
// F:\PROGRA~1\MID05A~1\VC\bin>localt 
// ime 
// Wed Aug 30 09:00:34 PM 
// F:\PROGRA~1\MID05A~1\VC\bin>snap 
//

I even complained to Microsoft back in June 5, 2006.
So it's taken over a hundred days to insert one line
of code. Which they have not done yet.

#include <stdlib.h>

Do you know of any reliable sites about C++ 2005,
Microsoft does not have reliable documentation.
I usually find that after reading a help document you
actually know less about it after reading it!
These guys are good?

The original complaint:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=450901&SiteID=1

Enjoy!

Yours Truly: Herge

Its true most sites have bad examples, and Microsoft is no exception. Your best bet if you are just learning is to study good and reliable text books. Microsoft documentation has greatly improved over the past 10 or so years. Some examples are still questionable, but they have cleaned up their act and most function descriptions are now accurate.

BTW: I always include pragma to remove those warnings about deprecated functions because it is only Microsoft, not c++ standards committee who made them like that. The functions they suggest with "_s" are Microsoft specific and will probably not compile with any other compiler. So I think you are better off not using them.

#pragma warning(disable: 4996)

>the compiler says the functions I am using are deprecated
My copy of the standard says no such thing. This is one case where it's okay to use a pragma to make the compiler shut the hell up.

Your best bet if you are just learning is to study good and reliable text books.

In my experience, you could start out with a C++ For Dummies, they are actually quite helpful.
Another you could try is (my actual textbook for class) Absolute C++ by Walter Savitch (second edition). It might leave out examples and explanations of spaghetti-code, but you shouldn't use that anyway. It has much to go from, and plenty of example codes (that actually work).

In my experience, you could start out with a C++ For Dummies, they are actually quite helpful.
Another you could try is (my actual textbook for class) Absolute C++ by Walter Savitch (second edition). It might leave out examples and explanations of spaghetti-code, but you shouldn't use that anyway. It has much to go from, and plenty of example codes (that actually work).

I personally would also try:

The C++ Programming Language,
by Bjarne Stroustrap,
AT & T Labs
Murray Hill, New Jersey

Published by Addison-Wesley, Pearson Education
Copyright 1997, AT & T

ISBN 0-201-700732-5

Over 1020 pages

There's nothing like getting it from the horses's mouth.
BJ created C++.

I brought this box in 2002, at that time it was slighty over
Hundred dollars Canadain.

Book says it sold for $ 64.99 in USA. I suspect it's
gone up since then.

I think he has retired from AT & T and gone down to
work at some College or University in Texas.

I personally would also try:

The C++ Programming Language,
by Bjarne Stroustrap,
AT & T Labs
Murray Hill, New Jersey

Published by Addison-Wesley, Pearson Education
Copyright 1997, AT & T

ISBN 0-201-700732-5

Over 1020 pages

There's nothing like getting it from the horses's mouth.
BJ created C++.

I brought this box in 2002, at that time it was slighty over
Hundred dollars Canadain.

Book says it sold for $ 64.99 in USA. I suspect it's
gone up since then.

I think he has retired from AT & T and gone down to
work at some College or University in Texas.

His name is Bjarne Stroustrup.

The Fact that Microsoft doesn't know that the C exit function needs
the <stdlib.h> is a cause of great concern. Most C or C++ compilers
need STDLIB.H file for the exit function to work. Even Micosoft C compilers need it.
I suspect this example is one of Millions of examples of bad code
from Microsoft. This is a very simple example they have screwed up
right royally.
It has taken over a hundred days and far as I know has not
been sorted out yet!
I would hate to ask what kind oF OS[Operating System] or name
of Text editor[Edlin,QED,Notepad, or god forbid Word.
I would hate to calculate Lines of code per Month that work.
The answer is always going to be zero.
If I told my manager I produced zero lines of code he would
fire me almost immediatelly, they seem to promote them
at Microsoft for this.

Okay. So microsoft hasn't compiled that code before submitting. Now what about whether it serves as a good example for _localtime64_s ? That is it's main objective isn't it? Once a programmer tries out the example and finds that exit() isn't defined, he will add the stdlib line and get it to compile. That is not much of an issue. It would have been a different matter if the example showed a wrong way of using the _localtime64_s function.

Everybody accepts the fact that the Microsoft examples ( or early compilers for that matter) are not standard compliant. Some examples start using the void main () function. But somehow a large number of programmers have managed to learn through those examples and continue to do so. For you it maybe just one example that needs one line to be added. But what about those all void main lines that need to be changed to int main ? Maybe those examples came from the days before the standard made it mandatory for main to return an int . So while we understand that it is wrong, we are also fedup of hearing how MS suck from time to time.

I think you have succesfully brought the mistake in the example to our attention. You have taken it up to Microsoft and they will hopefully correct it in the future. That is the least you can do. Now it is time you quit overdoing things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.