确定strftime字符数组最大大小的智能方法是什么?
What is an intelligent way to determine max size of a strftime char array?
如何在没有试验和错误的情况下为strftime调整字符数组的大小?使用mktime,示例中的时间戳大小N必须大于86,否则我会得到任意日期。例如
N = 86: 2013-07-13 02:41
N = 82: 1979-05-18 13:23
如何在不事先知道日期的情况下有效地缩放N ?检查>0没有帮助。
#include <iostream>
#include <cstring>
#include <ctime>
#define N 86
using namespace std;
int main(void)
{
time_t t;
struct tm ts;
char timestamp[N] ;
ts.tm_min = 41;
ts.tm_hour = 2;
ts.tm_mday = 13;
ts.tm_mon = 7 - 1;
ts.tm_year = 13 - 1900 + 2000;
t = mktime(&ts);
if (strftime(timestamp, sizeof(timestamp)-1, "%Y-%m-%d %H:%M", &ts) > 0)
cout << timestamp;
else {
cerr << "strftime failed." <<endl;
return 1;
}
return 0;
}
来自strftime的文档:
如果生成的C字符串的长度(包括终止的空字符)不超过maxsize,则该函数返回复制到ptr的字符总数(不包括终止的空字符)。否则,它返回0,并且ptr所指向的数组的内容是不确定的。
这意味着如果你不知道字符串的大小,可以动态地分配字符串,你可以这样做:
int size = N; // Some starting size
char *timestamp = malloc(size);
// Your time stuff
int result = strftime(timestamp, size - 1, "%Y-%m-%d %H:%M", &ts);
// While there isn't enough room to store the result
while (result == 0)
{
free(timestamp); // Free old data
size *= 2; // Double the size (should be more than enough)
timestamp = malloc(size); // Allocate the new size. You can check for failed allocations here as well.
// Retry
result = strftime(timestamp, size - 1, "%Y-%m-%d %H:%M", &ts);
}
std::cout << timestamp;
由于您将其标记为c++,也许您可以考虑以下内容:
-->注意这里没有字符串大小的问题。
// function to create a timestamp style string
std::string yyDmmDdd_hhCmmGet(time_t tt)
{
std::stringstream ss;
// goal - something like: "%Y-%m-%d %H:%M"
{
struct tm mybdtod; // linux api: my broken down time of day
// the following is a relatively slow function
::localtime_r (&tt, &mybdtod);
// linux api - convert time_t to tm as local time
ss << std::setw(4) << (mybdtod.tm_year+1900)
<< "-"
<< std::setfill('0') << std::setw(2) << mybdtod.tm_mon+1
<< "-"
<< std::setfill('0') << std::setw(2) << mybdtod.tm_mday
<< " ";
ss << std::dec << std::setfill('0') << std::setw(2)
<< mybdtod.tm_hour
<< ":"
<< std::setfill('0') << std::setw(2)
<< mybdtod.tm_min;
}
return(ss.str());
}
int t186(void)
{
struct tm ts; // linux api: time struct
::memset(&ts, 0, sizeof(tm));
ts.tm_min = 41;
ts.tm_hour = 3-1;
ts.tm_mday = 13;
ts.tm_mon = 7 - 1;
ts.tm_year = 13 - 1900 + 2000;
time_t tt = mktime(&ts); // linux api: Convert tm struct to time_t
// format time_t to string
std::string s = yyDmmDdd_hhCmmGet(tt); // timestamp style
std::cout << "n" << s
<< "n s.size(): "
<< s.size() << " chars" << std::endl;
// now we know how many chars timestamp needs
// add 1 to size because ?strftime seems to need it?
char timestamp[s.size()+1];
(void)strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M", &ts);
// linux api: format time_t to string
std::cout << "n" << timestamp << std::endl;
std::cout << " sizeof(timestamp): "
<< sizeof(timestamp) << " chars" << std::endl;
return(0);
}
相关文章:
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- 初始化指向类实例的智能指针并访问其方法
- 构造智能点数据类型以及普通数据类型的通用方法
- 类的方法和对象。参考?智能指针?简单的初始化?
- 将派生类作为参数传递给方法,该方法是具有智能指针的基类
- 从ostringstream获取最长行的字符数的智能方法
- 使用push_back方法获取智能指针矢量时应用崩溃
- 有没有更好的方法来添加两个智能指针?
- 从类成员返回智能指针的正确方法?
- 将对象附加到智能指针向量的最佳方法
- 使用删除与智能指针释放内存以及释放内存的正确方法
- 为什么我的COM智能指针从方法返回时会被删除
- 将从方法返回的原始指针存储到智能指针中
- 使用智能指针将右值引用绑定到抽象类的替代方法
- 为性能库制作高效包装器的智能方法
- 如何使用智能指针调用包装对象的方法
- 测试类型是否为(智能)指针的通用方法
- 构造类成员 std::vector<std::unique_ptr > 的智能方法<AClass>
- 将新运算符与函数一起使用C++智能方法是什么?
- 确定strftime字符数组最大大小的智能方法是什么?