支持区域设置的字符串解析到Windows C中的日期
发布时间:2020-12-14 05:38:30 所属栏目:Windows 来源:网络整理
导读:我在使用 Windows C代码: 我正在尝试解析由代表日期的第三方软件返回的字符串,但我想使解析依赖于所使用的语言环境.现在,我回来的日期是以下格式:“mm-dd-YYYY tt:ss A”,但是如果我将语言环境切换到类似加拿大的地方,那么我要回来的字符串是“dd” -mm-Y
我在使用
Windows C代码:
我正在尝试解析由代表日期的第三方软件返回的字符串,但我想使解析依赖于所使用的语言环境.现在,我回来的日期是以下格式:“mm-dd-YYYY tt:ss A”,但是如果我将语言环境切换到类似加拿大的地方,那么我要回来的字符串是“dd” -mm-YYYY tt:ss A“ 如您所见,月份和日期都是交换的.有没有办法检索当前语言环境使用的日期格式?或者甚至更好,有没有办法根据用户的语言环境以不同的方式解析字符串? #include "stdafx.h" #include <iostream> #include <sstream> #include <locale> #include <iomanip> #include <ctime> #include <sstream> int _tmain(int argc,_TCHAR* argv[]) { // Region 1: Go from current time to locale-specific date / time. std::time_t ct = std::time(nullptr); std::tm tm = *std::localtime(&ct); // Save the time in a stringstream to be later used as input std::stringstream time_str; time_str.imbue(std::locale("")); time_str << std::put_time(&tm,"%x %X"); // print the saved stringstream std::cout << std::locale("").name().c_str() << ": " << time_str.str() << "n"; // Region 2: Parse from a local-specific date and time string to time (Parsing is failing) std::tm t = {}; std::istringstream iss(time_str.str().c_str()); iss.imbue(std::locale("")); iss >> std::get_time(&t,"%x,%X"); // I would expect this to parse my string above correctly. if (iss.fail()) { std::cout << "Parse failedn"; } else { std::cout << std::asctime(&t) << 'n'; } return 0; } 解决方法
< iomanip>中有一个
std::get_time .根据您的需要,您可以使用其%x转换来读取区域设置的标准日期格式.
如果这不符合您的格式,您可能需要查看 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows-server-2012 – 打开Windows Server 2012 / Window
- 用于Windows的命令行XML验证器
- windows – 删除孤立的AD域:“从服务器返回引用”
- 通过PowerShell获取见证磁盘当前投票
- 最新win10和office2016,office2019密钥实时更新
- 如何在Windows Server PPTP VPN中自动为客户端分配路由?
- microsoft.build – 以编程方式修改程序集绑定
- win7 windows server 2008R2下 https SSL证书安装的搭配(搭
- Windows2003下php5.4安装配置教程(Apache2.4)
- 在Windows服务器上的相同界面上回复