简体   繁体   English

确定strftime char数组的最大大小的智能方法是什么?

[英]What is an intelligent way to determine max size of a strftime char array?

How can I size the char array for strftime without trial and error? 如何在没有尝试和错误的情况下为strftime确定char数组的大小? Using mktime, the timestamp size N in the example has to be greater 86, otherwise I get arbitrary dates back. 使用mktime,示例中的时间戳大小N必须大于86,否则我会得到任意日期。 eg 例如

N = 86 : 2013-07-13 02:41 N = 862013-07-13 02:41

N = 82 : 1979-05-18 13:23 N = 821979-05-18 13:23

How do I efficiently scale N without prior knowledge of the date? 如何在不事先知道日期的情况下有效地缩放N? The check >0 does not help. 大于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;
}

From the documentation for strftime: 从strftime的文档中:

If the length of the resulting C string, including the terminating null-character, doesn't exceed maxsize, the function returns the total number of characters copied to ptr (not including the terminating null-character). 如果生成的C字符串的长度(包括终止的空字符)不超过maxsize,则该函数返回复制到ptr的字符总数(不包括终止的空字符)。 Otherwise, it returns zero, and the contents of the array pointed by ptr are indeterminate. 否则,它返回零,并且ptr指向的数组的内容是不确定的。

That means if you don't know the size and can dynamically allocate a string you can do something along the lines of: 这意味着,如果您不知道大小,并且可以动态分配字符串,则可以执行以下操作:

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;

Because you tagged this as C++, perhaps you might consider the following. 因为您将其标记为C ++,所以也许您可以考虑以下内容。

--> Note that there is no struggle with the string size here. ->请注意,这里的字符串大小没有任何困难。


// 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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM