time
1.获得系统时间
time(NULL);
2.时间戳转换成日期
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
char szBeginTime[32] ={0};
time_t tmBeginTime = time(NULL);
struct tm *tmNowBegin = localtime(&tmBeginTime);
strftime(szBeginTime,32,"%Y-%m-%d %H:%M:%S",tmNowBegin);
fprintf(stderr, "%s\n", szBeginTime );
system("pause");
return 0;
}
3.时间戳和日期相互转换
#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
/*
char *pValue = "2011-12-10 11:29:08";
sscanf(pValue, "%d-%d-%d %d:%d:%d", &pRole->Y,&pRole->Online_lastM,&pRole->Online_lastD,&pRole->h,&pRole->m,&pRole->s);
*/
int main(void)
{
time_t timep;
struct tm *p;
time(&timep);
printf("time() : %d \n",timep);
p=localtime(&timep);
timep = mktime(p);
printf("time()->localtime()->mktime():%d\n",timep);
system("pause");
return 0;
}
4.计算一年中的第几天
#include<iostream>
using namespace std;
int main()
{
int year,month,day;
int m[12]={31,28, 31,30,31,30,31,31,30,31,30,31};
cout<<"请输入年月日(空格隔开):";
cin>>year>>month>>day;
if(year%400==0 || year%4==0 && year%100!=0)
m[1]=29;
int sumdays=0;
for(int i=0;i<month-1;i++){
sumdays+=m[i];
}
sumdays+=day;
cout<<year<<"年"<<month<<"月"<<day<<"日是一年中的第"<<sumdays<<"天"<<endl;
system("pause");
return 0;
}
#include<iostream>
#include <stdio.h>
#include <stddef.h>
#include <time.h>
using namespace std;
int main()
{
time_t tmBeginTime = time(NULL);
struct tm *tmNowBegin = localtime(&tmBeginTime);
if (NULL == tmNowBegin)
return false;
int nCreateYear = tmNowBegin->tm_year+1900;
int nCreateMon = tmNowBegin->tm_mon+1;
int nCreateDay = tmNowBegin->tm_mday;
int nCreateYearDay = tmNowBegin->tm_yday;
int nCreateWeek = tmNowBegin->tm_wday;
cout<<nCreateYear<<","<<nCreateMon<<","<<nCreateDay<<","<<nCreateYearDay<<","<<nCreateWeek<<endl;
system("pause");
return 0;
}
5.Linux time
localtime 和 localtime_r
http://blog.csdn.net/maocl1983/article/details/6221810
Linux下面time.h时间函数总结
http://blog.csdn.net/likun_tech/article/details/7317536
判断两个时间是同一天
bool IsToday(time_t nLastTime, time_t nNowTime)
{
time_t LastTime = nLastTime;
struct tm localTime;
localtime_r(&LastTime, &localTime);
time_t NowTime = nNowTime;
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
if (localTime.tm_year == localNowTime.tm_year &&
localTime.tm_mon == localNowTime.tm_mon &&
localTime.tm_mday == localNowTime.tm_mday)
{
return true;
}
else
return false;
}
//扩展
bool CTimer::IsToday() const
{
if (m_Time == 0)
return false;
time_t NowTime = time(NULL);
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
struct tm localTime;
localtime_r(&m_Time, &localTime);
if (localTime.tm_year == localNowTime.tm_year &&
localTime.tm_mon == localNowTime.tm_mon &&
localTime.tm_mday == localNowTime.tm_mday)
{
return true;
}
else
return false;
}
bool CTimer::IsThisWeek() const
{
if (m_Time == 0)
return false;
time_t NowTime = time(NULL);
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
struct tm localTime;
localtime_r(&m_Time, &localTime);
return (localTime.tm_year == localNowTime.tm_year &&
(localTime.tm_yday - localNowTime.tm_yday) == (GetWeek7Day(m_Time) - GetWeek7Day(NowTime)));
}
int CTimer::GetWeek7Day(time_t tTime) const
{
struct tm localNowTime;
localtime_r(&tTime, &localNowTime);
return localNowTime.tm_wday == 0 ? 7 : localNowTime.tm_wday;
}
bool CTimer::IsThisMonth() const
{
if (m_Time == 0)
return false;
time_t NowTime = time(NULL);
struct tm localNowTime;
localtime_r(&NowTime, &localNowTime);
struct tm localTime;
localtime_r(&m_Time, &localTime);
return (localTime.tm_year == localNowTime.tm_year &&
localTime.tm_mon == localNowTime.tm_mon);
}
bool CTimer::IsYesterday() const
{
time_t NowTime = time(NULL);
struct tm local;
localtime_r(&NowTime, &local);
struct tm LoaclTime;
localtime_r(&m_Time, &LoaclTime);
if (LoaclTime.tm_year != local.tm_year)
return false;
return (local.tm_yday - LoaclTime.tm_yday) == 1;
}
time_t CTimer::GetSunday() const
{
time_t NowTime = time(NULL);
struct tm local;
localtime_r(&NowTime, &local);
local.tm_mday = local.tm_mday - local.tm_wday + 1; //加1表示星期一的零时
local.tm_hour = 0;
local.tm_min = 0;
local.tm_sec = 0;
time_t Sunday = mktime(&local);
return Sunday;
}
int CTimer::GetYear() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_year + 1900;
}
int CTimer::GetMonth() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_mon + 1;
}
int CTimer::GetDay() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_mday;
}
int CTimer::GetHour() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_hour;
}
int CTimer::GetMinute() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_min;
}
int CTimer::GetSecond() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_sec;
}
int CTimer::GetYearDay() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_yday;
}
int CTimer::Get12Clock() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
localNowTime.tm_mday = localNowTime.tm_mday + 1;
localNowTime.tm_hour = 0;
localNowTime.tm_min = 0;
localNowTime.tm_sec = 0;
time_t tToNight = mktime(&localNowTime);
return tToNight - m_Time;
}
time_t CTimer::GetDate() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
localNowTime.tm_hour = 0;
localNowTime.tm_min = 0;
localNowTime.tm_sec = 0;
time_t date = mktime(&localNowTime);
return date;
}
int CTimer::GetWeekDay() const
{
struct tm localNowTime;
localtime_r(&m_Time, &localNowTime);
return localNowTime.tm_wday;
}