gpt4 book ai didi

C++ strftime %b 未按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:22 25 4
gpt4 key购买 nike

我想在不同的语言环境中使用 C++ 打印时间戳,我在一个特定的主机上得到 fr_FR 的奇怪输出。

这是代码,

#include <time.h>       /* time_t, struct tm, time, localtime, strftime */
#include <stdlib.h>
#include <locale.h>
#include <clocale>

#include <iostream>
using namespace std;

int main()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [32];

setlocale(LC_ALL, "");
cout << "LC_ALL: " << setlocale(LC_ALL, NULL) << endl;
cout << "LC_CTYPE: " << setlocale(LC_CTYPE, NULL) << endl;


time (&rawtime);
timeinfo = localtime (&rawtime);

strftime (buffer,sizeof(buffer),"%a %b %d %H:%M:%S %Y %Z",timeinfo);
puts (buffer);

return 0;
}

在运行上面的程序之前我做了

export LANG=fr_FR.ISO8859-1

它给出输出,

LC_ALL: fr_FR.ISO8859-1
LC_CTYPE: fr_FR.ISO8859-1
ven. avril 12 08:49:55 2013 UTC

它为 %b 和 %B 提供相同的输出,当我检查另一台机器时,它按预期工作。输出,

LC_ALL: fr_FR.ISO8859-1
LC_CTYPE: fr_FR.ISO8859-1
jeu avr 11 23:26:24 2013 PDT

在一台机器上,

$date +%b
avr
$date +%B
avril

在概率论机器中,

$date +%b
avril
$date +%B
avril

请帮我解决这个问题。

最佳答案

输出看起来是正确的。

“avril”是“avril”的合法缩写 Reference .

“Avr”也是“avril”的缩写。法语月份“juin”和“juillet”都以相同的 3 个字母开头,这表明使用前 3 个字母并不总是有效。

C++ 没有指定如何缩写默认语言环境以外的月份名称。因此,假设缩写很短,在一个语言环境中使用相同的长度或在世界范围内的各种操作系统中实现相同的长度是有问题的。

建议检查strftime()的返回值。

关于C++ strftime %b 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15967039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com