将Windows SYSTEMTIME转换为字符串或字符buf,C++与用户的"Region and Language"格式?

convert Windows SYSTEMTIME to a string or char buf in C++ with user's "Region and Language" format?

本文关键字:用户 Region and 格式 Language C++ 转换 SYSTEMTIME Windows 字符串 buf      更新时间:2023-10-16

我的C 程序如何在用户首选的"长期格式"中转换一个从其语言设置默认的" long Date Format",或在控制面板 ->区域和语言上作为自定义集对话?

如果我在Visual Studio 2017上很重要,主要是在寻找C/C 03类型解决方案,但是如果有一个新的C 解决方案,我不会介意看到它。

TCHAR buf[200];
GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &mysystime, NULL, buf, 200);

如果您还需要时间部分,请致电GetTimeFormat

TIME_ZONE_INFORMATION tzi;
if ( GetTimeZoneInformation( &tzi ) == TIME_ZONE_ID_INVALID )
MyErrorFunction( "GetTimeZoneInformation() = TIME_ZONE_ID_INVALID : %s",
    GetLastErrorAsString().c_str() );
  :
  :
SYSTEMTIME st, stLocal;
BOOL bRV = FileTimeToSystemTime( ftLastAccessTime, &st );
SystemTimeToTzSpecificLocalTime( &tzi, &st, &stLocal );
GetDateFormat( LOCALE_USER_DEFAULT, DATE_LONGDATE, &stLocal,
               NULL, szBuf, sizeof( szBuf ) );
int iBufUsed = strlen( szBuf );
if ( iBufUsed < sizeof( szBuf ) - 2 )
     szBuf[ iBufUsed++ ] = ' ';
GetTimeFormat( LOCALE_USER_DEFAULT, 0, &stLocal,
               NULL, szBuf + iBufUsed, sizeof( szBuf ) - iBufUsed );
// szBuf holds Date and Time, in the user's timezone, formatted as the user prefers.