【问题标题】:How to get the short date format string from the current locale in C/C++?如何从 C/C++ 中的当前语言环境获取短日期格式字符串?
【发布时间】:2019-02-04 14:41:57
【问题描述】:

以下代码(Windows、C++)设置当前区域设置并检索格式化的短日期字符串,根据区域设置的短日期格式“%c”格式化。

  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

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

  strftime (buffer,80,"%c",timeinfo);

假设这给出了给定日期和区域设置的“31/01/2012”。 这对应于“%d/%m/%Y”的日期格式,尽管已指定“%c”。

有没有办法我可以获取格式字符串本身,即给定语言环境的“%d/%m/%Y”?

【问题讨论】:

  • Nitpick: "31/01/2012" 不能对应日期格式 "%m/%d/%Y" 但可以对应日期格式 "%d/%m/ %Y"。
  • @Ian Abbott:当然。已编辑。

标签: c windows date


【解决方案1】:

在 langinfo 中有一个调用来执行此操作

作为一个简单的演示

#include <stdio.h>
#include <langinfo.h>

int main()
{
    printf("%s\n", nl_langinfo( D_FMT ) );
}

如果您的 C 编译器不兼容 POSIX,而我认为 Windows 应该是,那么这个问题的答案可能更像是一个指南

What is the correct way to get a LOCALE_SSHORTDATE that is guaranteed to have a full (4-digit) year number?

【讨论】:

  • 那是一个外部库。没有标准的 C 或 C++ 解决方案?还是 WinApi 调用?
  • POSIX 非常标准。我认为 Windows 应该是 POSIX 兼容的。查看添加的链接以回答您感兴趣的领域内的原生 Windows 问题
猜你喜欢
  • 2011-02-16
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
相关资源
最近更新 更多