【问题标题】:MSVCs Implementation of std::put_timestd::put_time 的 MSVC 实现
【发布时间】:2013-06-22 05:40:43
【问题描述】:

我正在使用 Microsoft Visual Studio 2012,并且正在考虑使用 std::put_time,因此我创建了以下示例:

int main()
{
    std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());

    std::locale::global( std::locale("en-GB") );

    std::cout << std::put_time( std::localtime( &t ), "%x" ) << std::endl;
}

这会产生以下输出:

06/25/2013

这不是我期望的 en-GB 语言环境的日期格式。我也试过了:

std::cout.imbue( std::locale("en-GB") );

但是,同样的输出。这是我应该为这个语言环境获得的输出,还是我在某个地方犯了错误?

【问题讨论】:

  • 那肯定是错的。
  • @R.MartinhoFernandes 你知道这是否已被归档在微软的错误数据库中吗?

标签: c++ visual-studio-2012 c++11 locale standard-library


【解决方案1】:

按预期工作。 std::put_time 在流的语言环境下工作,而不是全局语言环境。在输入main 之前,会创建cout,并使用当前的全局语言环境。对全局语言环境的后续更改不会影响它。你需要明确地imbue()它。

【讨论】:

  • 那么为什么std::cout.imbue( std::locale("en-GB") ) 在使用std::put_time 之前会产生相同的结果?
  • 可能是因为“en-GB”不是 MSVC 的 std::locale 实现识别的区域设置标识符(请注意,标准未指定此字符串的形式)。 “English_Britain”对我有用,因为结果输出采用日/月/年格式。
猜你喜欢
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
相关资源
最近更新 更多