Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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 strtoul将字符串日期(“03/10/2013 14:01:00”)转换为时间_C_Time T_Strtol - Fatal编程技术网

C strtoul将字符串日期(“03/10/2013 14:01:00”)转换为时间

C strtoul将字符串日期(“03/10/2013 14:01:00”)转换为时间,c,time-t,strtol,C,Time T,Strtol,我不明白,为什么这样不行? PS:我在谷歌上找到了这段代码 问题:我不知道为什么它应该工作?这也考虑了时区吗? 1 #include<stdio.h> 2 #include <stdlib.h> 3 #include <string> 4 #include <time.h> 5 int main() 6 { 7 std::string text("10/10/2013 14:01:00"); 8 co

我不明白,为什么这样不行? PS:我在谷歌上找到了这段代码

问题:我不知道为什么它应该工作?这也考虑了时区吗?
  1 #include<stdio.h>
  2 #include <stdlib.h>
  3 #include <string>
  4 #include <time.h>
  5 int main()
  6 {
  7     std::string text("10/10/2013 14:01:00");
  8     const char* nptr = text.c_str();
  9     char* endptr = NULL;
 10     time_t seconds_from_epoch = strtoul(nptr, &endptr, 0);
 11     if (secs != 0)
 12         printf("Secs: %ld\n", secs);
 13     if (*nptr != '\0' && endptr && *endptr  == '\0') {
 14         printf("Secs: %ld\n", secs);
 15     } else {
 16         printf("Unable to convert\n");
 17     }
 18 }
1#包括
2#包括
3#包括
4#包括
5 int main()
6 {
7标准:字符串文本(“10/10/2013 14:01:00”);
8 const char*nptr=text.c_str();
9字符*endptr=NULL;
从epoch开始的10秒=strtoul(nptr和endptr,0);
11如果(秒!=0)
12 printf(“秒:%ld\n”,秒);
13如果(*nptr!='\0'&&endptr&&endptr=='\0'){
14 printf(“秒:%ld\n”,秒);
15}其他{
16 printf(“无法转换\n”);
17     }
18 }

这是错误的假设

我就是这样做的:

 21     printf("************************\n");
 22     int day, month, yr, hr, min, sec, tzone;
 23     char* more = (char *)nptr;
 24     month = strtol(more, &more, 10);
 25     day = strtol(more+1, &more, 10);
 26     yr = strtol(more+1, &more, 10);
 27     hr = strtol(more+1, &more, 10);
 28     min = strtol(more+1, &more, 10);
 29     sec = strtol(more+1, &more, 10);
 30     tzone = strtol(more+1, &more, 10);
 31 
 32     printf("Month: %d, Day: %d, Year: %d, Hour: %d, Min: %d, Sec: %d, Tzone:     %d\n", 
 33             month, day, yr, hr, min, sec, tzone);

然后可以使用struct tm和mktime。

strtime是您的朋友。对,我希望inbuild可以直接转换为time\u t或tm。不管怎样,它起作用了!