strftime not working as expected

B

Bryan O'Malley

Forgive me if this is an obvious question, but I've done hours of
searching, and I can't figure this out.

VC++ 6 / Win32 platforms

I'm trying to use strftime with the %x option to give me a date in the
format specified in the users Control Panel. Though I live in the US,
for example, I may have told windows that I want to see dates as
dd/mm/yyyy instead of mm/dd/yyyy.

It seems that no matter what I change -- local, format, etc., I get
mm/dd/yy.

This is the code:

#include "stdafx.h"
#include <time.h>
#include <ctime>


int main(int argc, char* argv[])
{
time_t oTime;
tm* oDate;
char sThis[25];

time(&oTime);
oDate = localtime(&oTime);

strftime(sThis, 25, "%x", oDate);

printf(sThis);
return 0;
}


Any ideas?

Bryan
 
C

Claudio Puviani

"Bryan O'Malley" wrote
Forgive me if this is an obvious question, but I've done hours of
searching, and I can't figure this out.

VC++ 6 / Win32 platforms

I'm trying to use strftime with the %x option to give me a date in the
format specified in the users Control Panel. Though I live in the US,
for example, I may have told windows that I want to see dates as
dd/mm/yyyy instead of mm/dd/yyyy.

It seems that no matter what I change -- local, format, etc., I get
mm/dd/yy.

'strftime()' works with the locale set by 'setlocale()'. It knows nothing about
the preferences you set in the control panel. If you're writing Windows-specific
code using Windows-specific functionality (which you are), you should use
Windows API functions (which, unfortunately for you, are not a topic for this
newsgroup) and ideally, wrap them so that you can eventually port your code.

Claudio
 
E

Evan Carew

Bryan,

Well, I'm not sure if this is what you need, but the people writing the
Boost library have nice convienient time & date manipulators for C++.
They are date_time ant posix_time respectively and can be located at
http://www.boost.org/libs/libraries.htm

Hope this helps,
Evan Carew
 
M

Martijn Lievaart

"Bryan O'Malley" wrote

'strftime()' works with the locale set by 'setlocale()'. It knows
nothing about the preferences you set in the control panel. If you're
writing Windows-specific code using Windows-specific functionality
(which you are), you should use Windows API functions (which,
unfortunately for you, are not a topic for this newsgroup) and ideally,
wrap them so that you can eventually port your code.

To the OP:

IIRC, Windows programs defaults to the "C" locale, but setting the locale
to what the user specified is trivial. After that all C/C++ functions
should work with the new locale, the only Windows specific part is to set
the locale at program startup (and iirc, even that can be done with
standard only functions). Ask in a Windows group for more details, the
come back here if you still have problems after you have set the locale
correctly.

You may want to test first by settign a locale explicitly for testing
only. Also, look up the C++ locale support (Josuttis has a nice
explanation), it is different and much better than C locale support.

HTH,
M4
 
T

Tilman Kuepper

Hello Bryan,
It seems that no matter what I change -- local, format, etc., I get
mm/dd/yy.

The following code might be useful.

Tilman

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

#include <ctime>
#include <locale>
#include <string>
#include <iostream>

void PutDate(std::eek:stream& os)
{
using namespace std;

time_t t = time(0);
tm* pTM = localtime(&t);

string fmt = "%x";
const char* pBeg = fmt.c_str();
const char* pEnd = pBeg + fmt.length();

#if defined(_MSC_VER) && (_MSC_VER < 1300) // VC++ 6.0 needs this...
const time_put<char>& tp = _USE(os.getloc(), time_put<char>);
tp.put(os, os, pTM, pBeg, pEnd);
#else
const time_put<char>& tp = use_facet<time_put<char> >(os.getloc());
tp.put(os, os, os.fill(), pTM, pBeg, pEnd);
#endif
}

int main()
{
using namespace std;
PutDate(cout); // 02/04/04
cout << "\n";

locale loc("German");
cout.imbue(loc);
PutDate(cout); // 04.02.2004
cout << "\n";

return 0;
}
 
B

Bryan O'Malley

Martijn Lievaart wrote...
To the OP:

IIRC, Windows programs defaults to the "C" locale, but setting the locale
to what the user specified is trivial. After that all C/C++ functions
should work with the new locale, the only Windows specific part is to set
the locale at program startup (and iirc, even that can be done with
standard only functions). Ask in a Windows group for more details, the
come back here if you still have problems after you have set the locale
correctly.

You may want to test first by settign a locale explicitly for testing
only. Also, look up the C++ locale support (Josuttis has a nice
explanation), it is different and much better than C locale support.

HTH,
M4

Martijn -- thanks for the tips. That helps clear things up a bit. Now I
just need to find out how to get at what Windows says the locale should be.

Bryan
 
J

Jerry Coffin

[ ... ]
IIRC, Windows programs defaults to the "C" locale, but setting the locale
to what the user specified is trivial.

The standard requires that programs (on all platforms) start up with the
"C" locale as the global default.
After that all C/C++ functions
should work with the new locale, the only Windows specific part is to set
the locale at program startup (and iirc, even that can be done with
standard only functions).

As long as you're trying to get the behavior set by the "native
environment", you can do it portably -- this corresponds to a locale
with an empty string as its name. E.g.:

std::setlocale(LC_ALL, "");

sets the global locale to work as the user has set the environment.

It won't affect strftime, but you can use an empty string to construct
an std::locale as well.

I go so far as to say that an empty string should normally be your first
choice of locale.
 
M

Martijn Lievaart

[ ... ]
IIRC, Windows programs defaults to the "C" locale, but setting the locale
to what the user specified is trivial.

The standard requires that programs (on all platforms) start up with the
"C" locale as the global default.

I saw that later. Thanks for the corection.
As long as you're trying to get the behavior set by the "native
environment", you can do it portably -- this corresponds to a locale
with an empty string as its name. E.g.:

std::setlocale(LC_ALL, "");

That was the trick! Thanks.

M4
 
J

Jerry Coffin

[ ... ]
I saw that later. Thanks for the corection.

I'd call it more of an amplification than a correction -- what you said
was perfectly correct; it just didn't tell the whole story.

[ ... ]
That was the trick! Thanks.

Glad to help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top