Linux:GetDateFormat()和GetTimeFormat()是否存在C++
Linux: GetDateFormat() and GetTimeFormat() existed C++?
我导入了下面的库。
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
我想将下面的代码部分转换为Linux C++。怎么可能呢?
我已经将TCHAR
转换为std::string
,它在_countof
中用作GetDateFormat
的参数
TCHAR szDate[16];
TCHAR szTime[16];
GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL,
_T("yyyy-MM-dd"),
szDate, _countof(szDate));
GetTimeFormat ( LOCALE_USER_DEFAULT, 0, NULL,
_T("hh:mm:ss tt"),
szTime, _countof(szTime) );
您可以从<ctime>
中使用strftime()
,这是不一样的,但与您提到的MSDN函数非常匹配。
然而,我强烈建议您检查C++提供的内容:
<chrono>
这是标准C++库的一部分,因此您不必担心特定于平台的实现(Windows、Linux等)
下面是一个使用标准C++、POSIXlocaltime_r
(localtime
的线程安全版本)和strftime
:的等效程序
#include <iostream>
#include <ctime>
int main() {
struct tm t;
time_t tstamp = time(nullptr);
if (!localtime_r(&tstamp, &t)) {
perror("localtime");
return 1;
}
char strdate[16];
if (strftime(strdate, sizeof(strdate), "%Y-%m-%d", &t) == 0) {
perror("strftime");
return 1;
}
std::cout << strdate << "n";
char strtime[16];
if (strftime(strtime, sizeof(strtime), "%H:%M:%S %p", &t) == 0) {
perror("strftime");
return 1;
}
std::cout << strtime << "n";
}
不幸的是,Windows不支持localtime_r
,但有类似的localtime_s
。因此,要使上述代码也能在Windows上运行,您可以添加一些类似的内容
#ifdef _WIN32
# define localtime_r(timet,tm) (!localtime_s(tm,timet))
#endif
相关文章:
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 如何检查QList中是否存在值
- 根据某个函数是否存在启用模板
- 地图计数确实很重要,或者只是检查是否存在
- C++中是否存在 std::conditional 的懒惰等价物?
- 无论如何,我可以确定构造函数是否存在吗?
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- 堆分配的对象是否存在永不为空的唯一所有者?
- 扩展类中的可选 vir 函数,测试它在运行时是否存在
- 模板化检查是否存在带有参数列表的类成员函数?
- 是否存在包含负号的isdigit函数(过载)
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 检查编译时是否存在静态函数
- 向量是否存在行主要形式?
- 检查 TinyXML 中的元素是否存在
- 检测是否存在具有 C++17 的类成员
- 虚拟继承中是否存在多重继承?
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- std::weak_ptr 和相应的 std::shared_ptr 之间是否存在数据竞争?
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?