将语言环境更改为瑞典语

时间:2018-07-23 12:42:39

标签: c++ locale

我在C ++程序中使用setlocale遇到困难。我有一个ftDateTime类型的字段,我要输入的数据为固定格式。

当我在Windows设置中手动将区域设置更改为适合数据的区域设置时,程序运行正常。当我尝试使用代码更改语言环境时,会引发异常,表明日期和时间格式无效。

void* ptr = setlocale(LC_ALL,"Swedish_Sweden");

数据是一些不相关数据之前的日期和时间。瑞典格式是第一个与传入数据匹配的格式。一项是:

2018-07-23 10:40:30

2 个答案:

答案 0 :(得分:3)

此代码对我有用:

#include <cstdio>
#include <clocale>
#include <ctime>
#include <cwchar>

int main()
{
    std::setlocale(LC_ALL, "sv_SE.UTF-8");

    wchar_t str[100];
    std::time_t t = std::time(NULL);
    std::wcsftime(str, 100, L"%A %c", std::localtime(&t));
    std::wprintf(L"Number: %.2f\nDate: %Ls\n", 3.14, str);
}

输出:

Number: 3,14
Date: måndag mån 23 jul 2018 12:58:16

要了解系统上的语言环境如何,请执行以下操作:

#include <iostream>
#include <clocale>

int main()
{
    std::setlocale(LC_ALL, "");
    std::cout << "LC_ALL: " << std::setlocale(LC_ALL, NULL) << std::endl;
    std::cout << "LC_CTYPE: " << std::setlocale(LC_CTYPE, NULL) << std::endl;
    return 0;
}

答案 1 :(得分:1)

我什至没有使用setlocale解决了我的问题,因为它不起作用。我创建了ftDateTime字段并创建了TFormatSettings字段,并使用数据时间格式填充了该字段。然后我就可以将其解析回本地日期格式

TDateTime D;
TFormatSettings fmt;
fmt.Create();
fmt.DateSeparator = '-';
fmt.ShortDateFormat = "y/m/d";
D = StrToDate(data,fmt);

return D.DateString();

我写道我正在使用RAD Studio,但主持人出于某种原因将其删除。

相关问题