- 论坛徽章:
- 1
|
各位仁兄:
请问:
C 中有没有将“2010-12-10 10:01:25”这个日期字符串转化成日历时间的函数?
wisage 发表于 2011-03-22 17:41 ![]() 随便写了一个有点笨拙的。- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- int main(void)
- {
- struct tm t_struct;
- char buf[256];
- char t_str[] = "2010-12-10 10:01:25";
- time_t cal_time;
- strncpy(buf, t_str, 4);
- t_struct.tm_year = atoi(buf) - 1900;
- memset(buf, 0, 4);
- strncpy(buf, t_str + 5, 2);
- t_struct.tm_mon = atoi(buf);
- strncpy(buf, t_str + 8, 2);
- t_struct.tm_mday = atoi(buf);
- strncpy(buf, t_str + 11, 2);
- t_struct.tm_hour = atoi(buf);
- strncpy(buf, t_str + 14, 2);
- t_struct.tm_min = atoi(buf);
- strncpy(buf, t_str + 17, 2);
- t_struct.tm_sec = atoi(buf);
- cal_time = mktime(&t_struct);
- printf("The result is: %ld\n", (long int)cal_time);
- exit(0);
- }
复制代码 验证了一下结果:- $ ./time_convert
- The result is: 1294624885
复制代码 不知道为什么有偏差,和下面的比较:- $ echo | awk '{print mktime("2010 12 10 10 01 25")}'
- 1291946485
复制代码 |
|