您的位置:首页 > 其它

格式化时间 与 时间戳的转换

2015-08-06 19:38 281 查看
2015-08-06 19:37:58

/*!
* <格式化时间与时间戳的转换>
*
* 2015/08/06 by <felove>
*/
#include <stdio.h>
#include <windows.h>
#include <time.h>

void timestampToFormat(time_t& t_1, tm& tm_1);
time_t formatToTimestamp(int _nYear, int _nMonth, int _nDay, int _nHour, int _nMinute, int _nSecond);

int main()
{
//时间戳转格式化时间
time_t timep;
time(&timep);
struct tm tm_1;
timestampToFormat(timep,tm_1);
printf("time():%d\n",timep);
printf("[%04d-%02d-%02d][%02d:%02d:%02d]\n",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);

//格式化时间转时间戳
time_t timep1 = formatToTimestamp(2015,8,6,18,6,0);
printf("time()->localtime()->mktime():%d\n",timep1);

system("pause");
return 0;
}

void timestampToFormat(time_t& t_1, tm& tm_1)
{
localtime_s(&tm_1,&t_1);
}

time_t formatToTimestamp(int _nYear, int _nMonth, int _nDay, int _nHour, int _nMinute, int _nSecond)
{
struct tm tm_1;
tm_1.tm_year = _nYear - 1900;
tm_1.tm_mon = _nMonth - 1;
tm_1.tm_mday = _nDay;
tm_1.tm_hour = _nHour;
tm_1.tm_min  = _nMinute;
tm_1.tm_sec = _nSecond;
tm_1.tm_isdst = -1;
time_t timep1 = mktime(&tm_1);
return timep1;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: