wcsftime输出编码 [英] wcsftime output encoding
问题描述
-----开始PGP签名消息-----
哈希:SHA1
下面列出的程序演示了如何使用wcsftime( )和
std :: time_put< wchar_t>这是一个围绕它的C ++包装器。 (我知道这个
不是C;但是问题存在于
wcsftime())的C库实现中。我不确定这是否是一个依赖于平台的功能或者是B标准的一部分。
我已经用GCC 3.4编译了。 3在GNU / Linux上运行,并以en_GB UTF-8
语言环境运行。输出如下:
$ ./date3
asctime:Fri Nov 26 13:26:48 2004
strftime :2004年11月26日星期五13:26:48 GMT
wcsftime:2004年11月26日星期五13:26:48 GMT
std :: time_put< char>:11月26日星期五2004 13:26:48 GMT
std :: time_put< wchar_t> ;:格林威治标准时间2004年11月26日星期五13:26:48
一切正常。它也适用于我在不同的语言环境中运行(所有
语言环境使用UTF-8作为其代码集):
$ LANG = de_DE LC_ALL = de_DE。 / date3
asctime:Fri Nov 26 13:28:03 2004
strftime:Fr 26 Nov 2004 13:28:03 GMT
wcsftime :2004年11月26日星期三13:28:03 GMT
std :: time_put< char> ;:FG 26 Nov 2004 13:28:03 GMT
std :: time_put< wchar_t>:Fr 26 Nov 2004 13:28:03 GMT
$ LANG = pt_BR LC_ALL = pt_BR ./date3
asctime:11月26日星期五13: 2004年4月29日星期五
strftime:性别2004年11月26日13:29:18 GMT
wcsftime:Sex 26 Nov 2004 13:29:18 GMT
std :: time_put< char>:Sex 26 Nov 2004 13:29:18 GMT
std :: time_put< wchar_t>:Sex 26 Nov 2004 13:29:18 GMT
但是,如果我使用的输出包含非ASCII
字符的语言环境,我会得到:
asctime:星期五11月26日13:30:08
strftime:D ??? D? 26 D?D ??? 2004年13:30:08
wcsftime:^ _B = 26 ^]> O 2004 13:30:08
std :: time_put< char>:D ?? ?d? 26 D?D ??? 2004 13:30:08
std :: time_put< wchar_t>:^ _B = 26 ^]> O 2004 13:30:08
In在这种情况下,狭窄是指和宽的产出不同。 窄
输出是有效的UTF-8,而宽的输出是有效的UTF-8。输出是完全不同的
。 wcsftime()在输出ASCII范围之外的
字符时使用什么编码? UCS-4?
实现定义了什么?我预计两者都会产生可读的
输出;这个假设是不正确的?
我的问题基本上是这样的:wcsftime()实际上是做什么的,
我应该如何从宽字符串中获得可打印的输出它填写了
我?
非常感谢,
Roger
#include< iostream>
#include< locale>
#include< ctime>
#include< cwchar>
int main()
{
//设置语言环境...
std :: locale :: global(std :: locale(");
std :: cout.imbue(std :: locale());
std :: wcout.imbue(std :: locale());
//获取当前时间
time_t simpletime = time(0);
//分解时间。
std :: tm brokentime;
localtime_r(& simpletime,& brokentime);
//标准化。
mktime(& brokentime);
std :: cout<< asctime: << asctime(& brokentime);
//使用strftime打印(3)
char buffer [40];
std :: strftime(& buffer [0],40,"%c"& brokentime);
std :: cout<< strftime: << & buffer [0]<< ''\ n'';
wchar_t wbuffer [40];
std :: wcsftime(& wbuffer [0],40,L" %c",& brokentime);
std :: wcout<< L" wcsftime:" << & wbuffer [0]<< ''\ n'';
//再试一次,但要使用正确的区域设置方面...
const std :: time_put< char> &安培; tp =
std :: use_facet< std :: time_put< char> >(std :: cout.getloc());
std :: string pattern(" std :: time_put< char>:%c\ n");
tp.put(std :: cout,std :: cout,std :: cout.fill(),
& brokentime,& * pattern.begin() ,& * pattern.end());
//再次,但是使用wchar_t ......
const std :: time_put< wchar_t> &安培; wtp =
std :: use_facet< std :: time_put< wchar_t> >(std :: wcout.getloc());
std :: wstring wpattern(L" std :: time_put< wchar_t>:%c\ n");
wtp.put(std :: wcout,std :: wcout,std :: wcout.fill(),
& brokentime,& * wpattern.begin() ,& * wpattern.end());
返回0;
}
- -
Roger Leigh
在GNU / Linux上打印? http://gimp-print.sourceforge.net/
Debian GNU / Linux http://www.debian.org/
GPG公钥:0x25BFB848。请签名并加密您的邮件。
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.2.5(GNU / Linux)>
评论:由Mailcrypt处理3.5.8< http://mailcrypt.sourceforge.net/>
iD8DBQFBpz0qVcFcaSW / uEgRAjGMAKCusoGdSOupZEllYLA5eCh65pL6awCfcnpu
sdoS5qoYLjBiULIarVOD5bE =
= BHQO
----- END PGP SIGNATURE -----
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
The program listed below demonstrates the use of wcsftime() and
std::time_put<wchar_t> which is a C++ wrapper around it. (I know this
isn''t C; but the "problem" lies in the C library implementation of
wcsftime()). I''m not sure if this is a platform-dependent feature or
part of the C standard.
I''ve compiled with GCC 3.4.3 on GNU/Linux, and run in an en_GB UTF-8
locale. The output looks like this:
$ ./date3
asctime: Fri Nov 26 13:26:48 2004
strftime: Fri 26 Nov 2004 13:26:48 GMT
wcsftime: Fri 26 Nov 2004 13:26:48 GMT
std::time_put<char>: Fri 26 Nov 2004 13:26:48 GMT
std::time_put<wchar_t>: Fri 26 Nov 2004 13:26:48 GMT
Everything worked. It also works if I run in a different locale (all
locales use UTF-8 as their codeset):
$ LANG=de_DE LC_ALL=de_DE ./date3
asctime: Fri Nov 26 13:28:03 2004
strftime: Fr 26 Nov 2004 13:28:03 GMT
wcsftime: Fr 26 Nov 2004 13:28:03 GMT
std::time_put<char>: Fr 26 Nov 2004 13:28:03 GMT
std::time_put<wchar_t>: Fr 26 Nov 2004 13:28:03 GMT
$ LANG=pt_BR LC_ALL=pt_BR ./date3
asctime: Fri Nov 26 13:29:18 2004
strftime: Sex 26 Nov 2004 13:29:18 GMT
wcsftime: Sex 26 Nov 2004 13:29:18 GMT
std::time_put<char>: Sex 26 Nov 2004 13:29:18 GMT
std::time_put<wchar_t>: Sex 26 Nov 2004 13:29:18 GMT
However, if I use a locale where the output includes non-ASCII
characters, I get this:
asctime: Fri Nov 26 13:30:08 2004
strftime: D???D? 26 D?D??? 2004 13:30:08
wcsftime: ^_B= 26 ^]>O 2004 13:30:08
std::time_put<char>: D???D? 26 D?D??? 2004 13:30:08
std::time_put<wchar_t>: ^_B= 26 ^]>O 2004 13:30:08
In this case the "narrow" and "wide" outputs differ. The "narrow"
output is valid UTF-8, whereas the "wide" output is something
different entirely. What encoding does wcsftime() use when outputting
characters outside the ASCII range? UCS-4? Something
implementation-defined? I expected that both would result in readable
output; is this assumption incorrect?
My question is basically this: what is wcsftime() actually doing, and
how should I get printable output from the wide string it fills for
me?
Many thanks,
Roger
#include <iostream>
#include <locale>
#include <ctime>
#include <cwchar>
int main()
{
// Set up locale stuff...
std::locale::global(std::locale(""));
std::cout.imbue(std::locale());
std::wcout.imbue(std::locale());
// Get current time
time_t simpletime = time(0);
// Break down time.
std::tm brokentime;
localtime_r(&simpletime, &brokentime);
// Normalise.
mktime(&brokentime);
std::cout << "asctime: " << asctime(&brokentime);
// Print with strftime(3)
char buffer[40];
std::strftime(&buffer[0], 40, "%c", &brokentime);
std::cout << "strftime: " << &buffer[0] << ''\n'';
wchar_t wbuffer[40];
std::wcsftime(&wbuffer[0], 40, L"%c", &brokentime);
std::wcout << L"wcsftime: " << &wbuffer[0] << L''\n'';
// Try again, but use proper locale facets...
const std::time_put<char>& tp =
std::use_facet<std::time_put<char> >(std::cout.getloc());
std::string pattern("std::time_put<char>: %c\n");
tp.put(std::cout, std::cout, std::cout.fill(),
&brokentime, &*pattern.begin(), &*pattern.end());
// And again, but using wchar_t...
const std::time_put<wchar_t>& wtp =
std::use_facet<std::time_put<wchar_t> >(std::wcout.getloc());
std::wstring wpattern(L"std::time_put<wchar_t>: %c\n");
wtp.put(std::wcout, std::wcout, std::wcout.fill(),
&brokentime, &*wpattern.begin(), &*wpattern.end());
return 0;
}
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iD8DBQFBpz0qVcFcaSW/uEgRAjGMAKCusoGdSOupZEllYLA5eCh65pL6awCfcnpu
sdoS5qoYLjBiULIarVOD5bE=
=BHQO
-----END PGP SIGNATURE-----
推荐答案
./ date3
asctime:Fri Nov 26 13:26:48 2004
strftime:2004年11月26日星期五13:26:48 GMT
wcsftime:2004年11月26日星期五13:26:48 GMT
std :: time_put< char> ;:格林威治标准时间2004年11月26日星期五13:26:48
std :: time_put< wchar_t> ;:格林威治标准时间2004年11月26日星期五13:26:48
一切正常。如果我在不同的语言环境中运行它也有效(所有
语言环境使用UTF-8作为它们的代码集):
./date3
asctime: Fri Nov 26 13:26:48 2004
strftime: Fri 26 Nov 2004 13:26:48 GMT
wcsftime: Fri 26 Nov 2004 13:26:48 GMT
std::time_put<char>: Fri 26 Nov 2004 13:26:48 GMT
std::time_put<wchar_t>: Fri 26 Nov 2004 13:26:48 GMT
Everything worked. It also works if I run in a different locale (all
locales use UTF-8 as their codeset):
LANG = de_DE LC_ALL = de_DE ./date3
asctime:Fri Nov 26 13:28:03 2004
strftime:Fr 26 Nov 2004 13:28:03 GMT >
wcsftime:Fr 26 Nov 2004 13:28:03 GMT
std :: time_put< char> ;:Fr 26 Nov 2004 13:28:03 GMT
std :: time_put< wchar_t> ;:格林威治标准时间2004年11月26日13:28:03 GMT
LANG=de_DE LC_ALL=de_DE ./date3
asctime: Fri Nov 26 13:28:03 2004
strftime: Fr 26 Nov 2004 13:28:03 GMT
wcsftime: Fr 26 Nov 2004 13:28:03 GMT
std::time_put<char>: Fr 26 Nov 2004 13:28:03 GMT
std::time_put<wchar_t>: Fr 26 Nov 2004 13:28:03 GMT
LANG = pt_BR LC_ALL = pt_BR ./date3
asctime:星期五11月26日13:29:18 2004
strftime:Sex 26 Nov 2004 13:29:18 GMT
wcsftime:Sex 26 November 2004 13:29:18 GMT
std :: time_put< char>:Sex 26 Nov 2004 13:29:18 GMT
std :: time_put< wchar_t>:Sex 26 2004年11月13:29:18 GMT
但是,如果我使用的语言环境输出包含非ASCII
字符,我明白了:
asctime:星期五11月26日13:30:08 2004
strftime:D ??? D? 26 D?D ??? 2004年13:30:08
wcsftime:^ _B = 26 ^]> O 2004 13:30:08
std :: time_put< char>:D ?? ?d? 26 D?D ??? 2004 13:30:08
std :: time_put< wchar_t>:^ _B = 26 ^]> O 2004 13:30:08
In在这种情况下,狭窄是指和宽的产出不同。 窄
输出是有效的UTF-8,而宽的输出是有效的UTF-8。输出是完全不同的
。 wcsftime()在输出ASCII范围之外的
字符时使用什么编码? UCS-4?
实现定义了什么?我预计两者都会产生可读的
输出;这个假设是不正确的?
我的问题基本上是这样的:wcsftime()实际上是做什么的,
我应该如何从宽字符串中获得可打印的输出它填写了
我?
非常感谢,
Roger
#include< iostream>
#include< locale>
#include< ctime>
#include< cwchar>
int main()
{
//设置语言环境...
std :: locale :: global(std :: locale(");
std :: cout.imbue(std :: locale());
std :: wcout.imbue(std :: locale());
//获取当前时间
time_t simpletime = time(0);
//分解时间。
std :: tm brokentime;
localtime_r(& simpletime,& brokentime);
//标准化。
mktime(& brokentime);
std :: cout<< asctime: << asctime(& brokentime);
//使用strftime打印(3)
char buffer [40];
std :: strftime(& buffer [0],40,"%c"& brokentime);
std :: cout<< strftime: << & buffer [0]<< ''\ n'';
wchar_t wbuffer [40];
std :: wcsftime(& wbuffer [0],40,L" %c",& brokentime);
std :: wcout<< L" wcsftime:" << & wbuffer [0]<< ''\ n'';
//再试一次,但要使用正确的区域设置方面...
const std :: time_put< char> &安培; tp =
std :: use_facet< std :: time_put< char> >(std :: cout.getloc());
std :: string pattern(" std :: time_put< char>:%c\ n");
tp.put(std :: cout,std :: cout,std :: cout.fill(),
& brokentime,& * pattern.begin() ,& * pattern.end());
//再次,但是使用wchar_t ......
const std :: time_put< wchar_t> &安培; wtp =
std :: use_facet< std :: time_put< wchar_t> >(std :: wcout.getloc());
std :: wstring wpattern(L" std :: time_put< wchar_t>:%c\ n");
wtp.put(std :: wcout,std :: wcout,std :: wcout.fill(),
& brokentime,& * wpattern.begin() ,& * wpattern.end());
返回0;
}
- -
Roger Leigh
在GNU / Linux上打印? http://gimp-print.sourceforge.net/
Debian GNU / Linux http://www.debian.org/
GPG公钥:0x25BFB848。请签名并加密您的邮件。
----- BEGIN PGP SIGNATURE -----
版本:GnuPG v1.2.5(GNU / Linux)>
评论:由Mailcrypt处理3.5.8< http://mailcrypt.sourceforge.net/>
iD8DBQFBpz0qVcFcaSW / uEgRAjGMAKCusoGdSOupZEllYLA5eCh65pL6awCfcnpu
sdoS5qoYLjBiULIarVOD5bE =
= BHQO
----- END PGP SIGNATURE -----
LANG=pt_BR LC_ALL=pt_BR ./date3
asctime: Fri Nov 26 13:29:18 2004
strftime: Sex 26 Nov 2004 13:29:18 GMT
wcsftime: Sex 26 Nov 2004 13:29:18 GMT
std::time_put<char>: Sex 26 Nov 2004 13:29:18 GMT
std::time_put<wchar_t>: Sex 26 Nov 2004 13:29:18 GMT
However, if I use a locale where the output includes non-ASCII
characters, I get this:
asctime: Fri Nov 26 13:30:08 2004
strftime: D???D? 26 D?D??? 2004 13:30:08
wcsftime: ^_B= 26 ^]>O 2004 13:30:08
std::time_put<char>: D???D? 26 D?D??? 2004 13:30:08
std::time_put<wchar_t>: ^_B= 26 ^]>O 2004 13:30:08
In this case the "narrow" and "wide" outputs differ. The "narrow"
output is valid UTF-8, whereas the "wide" output is something
different entirely. What encoding does wcsftime() use when outputting
characters outside the ASCII range? UCS-4? Something
implementation-defined? I expected that both would result in readable
output; is this assumption incorrect?
My question is basically this: what is wcsftime() actually doing, and
how should I get printable output from the wide string it fills for
me?
Many thanks,
Roger
#include <iostream>
#include <locale>
#include <ctime>
#include <cwchar>
int main()
{
// Set up locale stuff...
std::locale::global(std::locale(""));
std::cout.imbue(std::locale());
std::wcout.imbue(std::locale());
// Get current time
time_t simpletime = time(0);
// Break down time.
std::tm brokentime;
localtime_r(&simpletime, &brokentime);
// Normalise.
mktime(&brokentime);
std::cout << "asctime: " << asctime(&brokentime);
// Print with strftime(3)
char buffer[40];
std::strftime(&buffer[0], 40, "%c", &brokentime);
std::cout << "strftime: " << &buffer[0] << ''\n'';
wchar_t wbuffer[40];
std::wcsftime(&wbuffer[0], 40, L"%c", &brokentime);
std::wcout << L"wcsftime: " << &wbuffer[0] << L''\n'';
// Try again, but use proper locale facets...
const std::time_put<char>& tp =
std::use_facet<std::time_put<char> >(std::cout.getloc());
std::string pattern("std::time_put<char>: %c\n");
tp.put(std::cout, std::cout, std::cout.fill(),
&brokentime, &*pattern.begin(), &*pattern.end());
// And again, but using wchar_t...
const std::time_put<wchar_t>& wtp =
std::use_facet<std::time_put<wchar_t> >(std::wcout.getloc());
std::wstring wpattern(L"std::time_put<wchar_t>: %c\n");
wtp.put(std::wcout, std::wcout, std::wcout.fill(),
&brokentime, &*wpattern.begin(), &*wpattern.end());
return 0;
}
- --
Roger Leigh
Printing on GNU/Linux? http://gimp-print.sourceforge.net/
Debian GNU/Linux http://www.debian.org/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>
iD8DBQFBpz0qVcFcaSW/uEgRAjGMAKCusoGdSOupZEllYLA5eCh65pL6awCfcnpu
sdoS5qoYLjBiULIarVOD5bE=
=BHQO
-----END PGP SIGNATURE-----
这篇关于wcsftime输出编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!