看代码:
1 #include <stdio.h> 2 #include <time.h> 3 4 char s[100]; 5 6 time_t StringToDatetime (const char *strTime) 7 { 8 if (NULL == strTime) 9 { 10 return 0; 11 } 12 tm tm_; 13 int year, month, day, hour, minute,second; 14 sscanf(strTime,"%d-%d-%dT%d:%d:%d", &year, &month, &day, &hour, &minute, &second); 15 tm_.tm_year = year-1900; 16 tm_.tm_mon = month-1; 17 tm_.tm_mday = day; 18 tm_.tm_hour = hour; 19 tm_.tm_min = minute; 20 tm_.tm_sec = second; 21 tm_.tm_isdst = 0; 22 23 time_t t_ = mktime(&tm_); //已经减了8个时区 24 return t_; //秒时间 25 } 26 27 void DatetimeToString (time_t time) 28 { 29 time_t t = time + 28800;//8h 时区差 30 struct tm *p; 31 p = gmtime(&t); 32 strftime(s, sizeof(s), "%Y-%m-%dT%H:%M:%S", p); 33 } 34 35 int main(int argc, const char * argv[]) 36 { 37 time_t t; 38 t=1479794342;//s 39 DatetimeToString(t); 40 printf("%lld: %s ", t, s); 41 printf("%s", ctime(&t)); 42 time_t t2 = t + 28800; 43 printf("%s", asctime(gmtime(&t2))); 44 printf("%s: %lld ", s, StringToDatetime(s)); 45 46 return 0; 47 }
可以在下面网址进行测试:http://tool.chinaz.com/Tools/unixtime.aspx