2015-08-06 19:37:58
1 /*! 2 * <格式化时间与时间戳的转换> 3 * 4 * 2015/08/06 by <felove> 5 */ 6 #include <stdio.h> 7 #include <windows.h> 8 #include <time.h> 9 10 void timestampToFormat(time_t& t_1, tm& tm_1); 11 time_t formatToTimestamp(int _nYear, int _nMonth, int _nDay, int _nHour, int _nMinute, int _nSecond); 12 13 int main() 14 { 15 //时间戳转格式化时间 16 time_t timep; 17 time(&timep); 18 struct tm tm_1; 19 timestampToFormat(timep,tm_1); 20 printf("time():%d ",timep); 21 printf("[%04d-%02d-%02d][%02d:%02d:%02d] ",tm_1.tm_year + 1900,tm_1.tm_mon + 1,tm_1.tm_mday,tm_1.tm_hour,tm_1.tm_min,tm_1.tm_sec); 22 23 //格式化时间转时间戳 24 time_t timep1 = formatToTimestamp(2015,8,6,18,6,0); 25 printf("time()->localtime()->mktime():%d ",timep1); 26 27 system("pause"); 28 return 0; 29 } 30 31 void timestampToFormat(time_t& t_1, tm& tm_1) 32 { 33 localtime_s(&tm_1,&t_1); 34 } 35 36 time_t formatToTimestamp(int _nYear, int _nMonth, int _nDay, int _nHour, int _nMinute, int _nSecond) 37 { 38 struct tm tm_1; 39 tm_1.tm_year = _nYear - 1900; 40 tm_1.tm_mon = _nMonth - 1; 41 tm_1.tm_mday = _nDay; 42 tm_1.tm_hour = _nHour; 43 tm_1.tm_min = _nMinute; 44 tm_1.tm_sec = _nSecond; 45 tm_1.tm_isdst = -1; 46 time_t timep1 = mktime(&tm_1); 47 return timep1; 48 }