strftime not working as expected | | |
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 | | | | re: strftime not working as expected
"Bryan O'Malley" wrote[color=blue]
> 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.[/color]
'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 | | | | re: strftime not working as expected
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
Bryan O'Malley wrote:[color=blue]
> 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.
>[/color]
[snip][color=blue]
>
>
> Any ideas?
>
> Bryan[/color] | | | | re: strftime not working as expected
On Tue, 03 Feb 2004 23:12:45 +0000, Claudio Puviani wrote:
[color=blue]
> "Bryan O'Malley" wrote[color=green]
>> 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.[/color]
>
> '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.[/color]
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 | | | | re: strftime not working as expected
Hello Bryan,
[color=blue]
> It seems that no matter what I change -- local, format, etc., I get
> mm/dd/yy.[/color]
The following code might be useful.
Tilman
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#include <ctime>
#include <locale>
#include <string>
#include <iostream>
void PutDate(std::ostream& 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;
} | | | | re: strftime not working as expected
Martijn Lievaart wrote...[color=blue]
> 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[/color]
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 | | | | re: strftime not working as expected
In article <pan.2004.02.04.07.53.39.960662@remove.this.part.r tij.nl>, m@remove.this.part.rtij.nl says...
[ ... ]
[color=blue]
> IIRC, Windows programs defaults to the "C" locale, but setting the locale
> to what the user specified is trivial.[/color]
The standard requires that programs (on all platforms) start up with the
"C" locale as the global default.
[color=blue]
> 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).[/color]
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.
--
Later,
Jerry.
The universe is a figment of its own imagination. | | | | re: strftime not working as expected
On Thu, 12 Feb 2004 12:18:35 -0700, Jerry Coffin wrote:
[color=blue]
> In article <pan.2004.02.04.07.53.39.960662@remove.this.part.r tij.nl>,
> m@remove.this.part.rtij.nl says...
>
> [ ... ]
>[color=green]
>> IIRC, Windows programs defaults to the "C" locale, but setting the locale
>> to what the user specified is trivial.[/color]
>
> The standard requires that programs (on all platforms) start up with the
> "C" locale as the global default.[/color]
I saw that later. Thanks for the corection.
[color=blue][color=green]
>> 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).[/color]
>
> 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, "");[/color]
That was the trick! Thanks.
M4 | | | | re: strftime not working as expected
In article <pan.2004.02.15.21.22.35.369100@remove.this.part.r tij.nl>, m@remove.this.part.rtij.nl says...
[ ... ]
[color=blue][color=green][color=darkred]
> >> IIRC, Windows programs defaults to the "C" locale, but setting the locale
> >> to what the user specified is trivial.[/color]
> >
> > The standard requires that programs (on all platforms) start up with the
> > "C" locale as the global default.[/color]
>
> I saw that later. Thanks for the corection.[/color]
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.
[ ... ]
[color=blue][color=green]
> > std::setlocale(LC_ALL, "");[/color]
>
> That was the trick! Thanks.[/color]
Glad to help.
--
Later,
Jerry.
The universe is a figment of its own imagination. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|