- 論壇徽章:
- 1
|
各位仁兄:
請(qǐng)問(wèn):
C 中有沒(méi)有將“2010-12-10 10:01:25”這個(gè)日期字符串轉(zhuǎn)化成日歷時(shí)間的函數(shù)?
wisage 發(fā)表于 2011-03-22 17:41 ![]() 隨便寫(xiě)了一個(gè)有點(diǎn)笨拙的。- #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);
- }
復(fù)制代碼 驗(yàn)證了一下結(jié)果:- $ ./time_convert
- The result is: 1294624885
復(fù)制代碼 不知道為什么有偏差,和下面的比較:- $ echo | awk '{print mktime("2010 12 10 10 01 25")}'
- 1291946485
復(fù)制代碼 |
|