Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ strptime字符串时间解析为时间时出错\u t_C++_Ctime_Time T - Fatal编程技术网

C++ strptime字符串时间解析为时间时出错\u t

C++ strptime字符串时间解析为时间时出错\u t,c++,ctime,time-t,C++,Ctime,Time T,我正在尝试从mysql中获取%Y-%m-%d%H:%m:%S格式的时间值,并将其存储在time\t中。我用了两个函数 #include <iostream> #include <stdio.h> #include <time.h> #include <stdlib.h> /* convert string time stamp format to time_t and return */ time_t timestamp_to_ctime(cons

我正在尝试从mysql中获取%Y-%m-%d%H:%m:%S格式的时间值,并将其存储在time\t中。我用了两个函数

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* convert string time stamp format to time_t and return */
time_t timestamp_to_ctime(const char* time_stamp )
{
   time_t _return;
   struct tm tm_struct ; 
   strptime(time_stamp ,"%Y-%m-%d %H:%M:%S",&tm_struct);
   _return  = mktime(&tm_struct);   return
   _return;
}

/* convert time_t format into time-stamp format */ 
const char* ctime_to_timestamp(time_t time_t_input) 
{
   char* _return = (char*) malloc(MAX_TIME_STRING_LENGTH);
   struct tm* tm_struct ;   tm_struct = gmtime(&time_t_input);
   strftime( _return, MAX_TIME_STRING_LENGTH,"%Y-%m-%d %H:%M:%S",tm_struct);
   // free (tm_struct);
   return _return;  
}


#define TIME_STRING "2009-08-02 12:3:34"

int main(int argc,char** argv)
{
  time_t time_t_struct ;
  time_t_struct = timestamp_to_ctime(TIME_STRING);

  struct tm* ptr_tm ;
  ptr_tm = gmtime(&time_t_struct);

  printf( "Parsed time is : %d-%d-%d %d:%d:%d" , ptr_tm->tm_year+1900,\
        ptr_tm->tm_mon+1 ,  ptr_tm->tm_mday,ptr_tm->tm_hour,\
        ptr_tm->tm_min,ptr_tm->tm_sec);


  // check the next function //
  const char * timestamp_string = ctime_to_timestamp(time_t_struct);
  printf("Converted time is: %s ", timestamp_string);
  return 0;  
}

我发现使用localtime()而不是gmtime()可以纠正第二个输出。但第一个不是固定的。我相信
strftime
函数期望
%M
格式的分钟值为两位数。我怀疑如果分钟部分有一个前导零(例如,
:03:
而不是
:3:
),您会得到预期的值。@spencer7593:在Mac OS X 10.9.3上,
strtime()
可以处理缺少的前导零。我已经找出了错误。当将时间转换为gm时间并提取到tm结构中时,它将被激活。
Parsed time is : 2009-8-2 6:33:34Converted time is: 2009-08-02 06:33:34