使用 Strftime 格式化 DateTime
Using Strftime to format DateTime
我正在研究将输入时间戳格式化为输入格式的函数。
std::string 1stformat = "dd - MM - yyyy HH 'Hours' mm 'Minutes' ss 'Seconds' SSS 'Miliseconds –' a '– Time Zone: ' Z '-' zzzz";//will not print anything
std::string 2ndformat = "'This took about' h 'minutes and' s 'seconds.'";//will print out
格式化后
char date_string[100];
strftime(date_string, 50, format.c_str(), curr_tm);
我的问题是有时输入格式太长导致缓冲区 date_string
内容不足。在过去的 3 周里,我才开始接触 C++,所以我对此没有太多了解。
strftime()
的包装器,它根据需要增加缓冲区,直到它足够大以适应所需的时间字符串:
#include <ctime>
#include <iostream>
#include <memory>
#include <string>
std::string safe_strftime(const char *fmt, const std::tm *t) {
std::size_t len = 10; // Adjust initial length as desired. Maybe based on the length of fmt?
auto buff = std::make_unique<char[]>(len);
while (std::strftime(buff.get(), len, fmt, t) == 0) {
len *= 2;
buff = std::make_unique<char[]>(len);
}
return std::string{buff.get()};
}
int main() {
std::time_t now;
std::time(&now);
std::cout << safe_strftime("The date is %Y-%m-%d", std::localtime(&now))
<< '\n';
return 0;
}
不幸的是,std::strftime()
的接口不如 std::snprintf()
的接口有用,因为它 returns 0 如果缓冲区太小,而不是字符数被写。我们需要试探性地增加缓冲区大小并重试,可能像这样:
#include <ctime>
#include <string>
#include <vector>
std::string time_to_string(const char *format, const std::tm* time)
{
// first try with an on-stack buffer (fast path)
char buf[200];
auto written = std::strftime(buf, sizeof buf, format, time);
if (written > 0) {
return buf;
}
// now, iterate with an allocated buffer
auto len = sizeof buf;
std::vector<char> v;
do {
v.resize(len *= 2);
written = std::strftime(v.data(), v.size(), format, time);
} while (written == 0);
return {v.data(), written};
}
我正在研究将输入时间戳格式化为输入格式的函数。
std::string 1stformat = "dd - MM - yyyy HH 'Hours' mm 'Minutes' ss 'Seconds' SSS 'Miliseconds –' a '– Time Zone: ' Z '-' zzzz";//will not print anything
std::string 2ndformat = "'This took about' h 'minutes and' s 'seconds.'";//will print out
格式化后
char date_string[100];
strftime(date_string, 50, format.c_str(), curr_tm);
我的问题是有时输入格式太长导致缓冲区 date_string
内容不足。在过去的 3 周里,我才开始接触 C++,所以我对此没有太多了解。
strftime()
的包装器,它根据需要增加缓冲区,直到它足够大以适应所需的时间字符串:
#include <ctime>
#include <iostream>
#include <memory>
#include <string>
std::string safe_strftime(const char *fmt, const std::tm *t) {
std::size_t len = 10; // Adjust initial length as desired. Maybe based on the length of fmt?
auto buff = std::make_unique<char[]>(len);
while (std::strftime(buff.get(), len, fmt, t) == 0) {
len *= 2;
buff = std::make_unique<char[]>(len);
}
return std::string{buff.get()};
}
int main() {
std::time_t now;
std::time(&now);
std::cout << safe_strftime("The date is %Y-%m-%d", std::localtime(&now))
<< '\n';
return 0;
}
不幸的是,std::strftime()
的接口不如 std::snprintf()
的接口有用,因为它 returns 0 如果缓冲区太小,而不是字符数被写。我们需要试探性地增加缓冲区大小并重试,可能像这样:
#include <ctime>
#include <string>
#include <vector>
std::string time_to_string(const char *format, const std::tm* time)
{
// first try with an on-stack buffer (fast path)
char buf[200];
auto written = std::strftime(buf, sizeof buf, format, time);
if (written > 0) {
return buf;
}
// now, iterate with an allocated buffer
auto len = sizeof buf;
std::vector<char> v;
do {
v.resize(len *= 2);
written = std::strftime(v.data(), v.size(), format, time);
} while (written == 0);
return {v.data(), written};
}