#pragma once
#ifdef _WIN32
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <string>
typedef unsigned long long uint64;
namespace Extralib
{
namespace Timer
{
// 时间,精确到s,可以调用GetCurMillis,获得ms时间
class DateTime
{
public:// 静态函数,调用c函数
static time_t GetCurTime(); // 获得当前时间
static tm* GetCurTM(time_t ntime = 0); // 获得当前时间的,tm结构体
static double DiffTime(time_t t1, time_t t2); // 获得两个时间差
static time_t MakeTime(tm* data); // 通过tm转换成time_t结构
static int standard_to_stamp(const char *str_time); //字符串转化成time_t
static uint64 GetCurMillis(); // 获得当前毫秒数,距离1970年开始
static unsigned int GetCurSec(); // 获得当前秒数,距离1970年开始
static void Update(); // 更新一下静态时间
static void FormatNow(char* format, time_t ntime = 0);// 格式化输出当前时间 2011-10-24 11:11:11
static uint64 GetDayTimeOfDay();
static bool IsInSameDay(time_t t1,time_t t2); //两个时间是否落在同一天
static bool IsInSameWeek(time_t dwTime1,time_t dwTime2); //两个时间是否落在同一周
public:
DateTime();
DateTime(time_t timeSec);
void ResetNow(); // 重新设置为当前时间
time_t getTimeS();
tm* GetTM();
int getYear();
int getMonth();
int getDay();
int getHours();
int getMinutes();
int getWDay();
int getSeconds();
int GetAllDays();
//Tests if this date is before the specified date.
bool before(DateTime& when);
//Tests if this date is after the specified date.
bool after(DateTime& when);
//Tests if this date is euqal the specified date.
bool equals(DateTime& when);
public:
static time_t s_NowTime; // 全局的静态数据
static tm* s_TM; // 全局的tm
private:
tm m_tm;
};
}
}
//linux兼容函数(不放在namespace中)
#ifdef WIN32
int gettimeofday(struct timeval *tp, void *tzp);
#endif
#include <cstring>
#include<stdlib.h>
#include "DateTime.h"
#include <sys/timeb.h>
#ifdef WIN32
#define localtime_r(A,B) localtime_s(B,A)
#endif
namespace Extralib
{
namespace Timer
{
time_t DateTime::s_NowTime = 0;
tm* DateTime::s_TM;
time_t DateTime::GetCurTime()
{
return time(NULL);
}
tm* DateTime::GetCurTM(time_t ntime)
{
if (ntime == 0)
{
time_t ntime = time(NULL);
}
return localtime(&ntime);
}
double DateTime::DiffTime(time_t t1, time_t t2)
{
return difftime(t1, t2);
}
time_t DateTime::MakeTime(tm* data)
{
return mktime(data);
}
int DateTime::standard_to_stamp(const char *str_time)
{
tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
iY = atoi(str_time);
iM = atoi(str_time+5);
iD = atoi(str_time+8);
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
/*printf("%d-%0d-%0d %0d:%0d:%0d
", iY, iM, iD, iH, iMin, iS);*/ //标准时间格式例如:2016:08:02 12:12:30
return (int)MakeTime(&stm);
}
uint64 DateTime::GetDayTimeOfDay()
{
#ifdef WIN32
struct _timeb tb;
_ftime_s(&tb);
#else
struct timeb tb;
ftime(&tb);
#endif
return (tb.time * 1000LL + tb.millitm);
}
bool DateTime::IsInSameDay(time_t t1,time_t t2)
{
// 注意: 28800是北京时间与格林威治时间差(秒)
return ((t1 + 28800) / 86400) == ((t2 + 28800) / 86400);
}
bool DateTime::IsInSameWeek(time_t dwTime1,time_t dwTime2)
{
tm tm1 = *localtime(&dwTime1);
tm tm2 = *localtime(&dwTime2);
tm1.tm_wday = (tm1.tm_wday==0)? 7:tm1.tm_wday;
tm2.tm_wday = (tm2.tm_wday==0)? 7:tm2.tm_wday;
time_t dwMoment1 = dwTime1-(tm1.tm_wday-1)*24*60*60-tm1.tm_hour*60*60-tm1.tm_min*60-tm1.tm_sec;
time_t dwMoment2 = dwTime2-(tm2.tm_wday-1)*24*60*60-tm2.tm_hour*60*60-tm2.tm_min*60-tm2.tm_sec;
return dwMoment1 == dwMoment2 ;
}
uint64 DateTime::GetCurMillis()
{
#ifdef WIN32
struct _timeb tb;
_ftime_s(&tb);
#else
struct timeb tb;
ftime(&tb);
#endif
return (tb.time * 1000LL + tb.millitm);
}
unsigned int DateTime::GetCurSec()
{
#ifdef WIN32
struct _timeb tb;
_ftime_s(&tb);
#else
struct timeb tb;
ftime(&tb); //毫秒级的精确度
#endif
return tb.time;
}
void DateTime::Update()
{
s_NowTime = time(NULL);
localtime_r(&s_NowTime, s_TM);
}
void DateTime::FormatNow(char* format, time_t ntime)
{
tm* st = GetCurTM(ntime);
sprintf(format, "%d-%d-%d %d:%d:%d", st->tm_year + 1900, st->tm_mon + 1, st->tm_mday, st->tm_hour, st->tm_min, st->tm_sec);
}
DateTime::DateTime()
{
time_t timeNow = time(NULL);
localtime_r(&timeNow, &m_tm);
}
DateTime::DateTime(time_t timeSec)
{
localtime_r(&timeSec, &m_tm);
}
void DateTime::ResetNow()
{
Update();
m_tm = *s_TM;
}
time_t DateTime::getTimeS()
{
return MakeTime(&m_tm);
}
tm* DateTime::GetTM()
{
return& m_tm;
}
int DateTime::getYear()
{
return (m_tm.tm_year + 1900);
}
int DateTime::getMonth()
{
return m_tm.tm_mon;
}
int DateTime::getDay()
{
return m_tm.tm_mday;
}
int DateTime::getHours()
{
return m_tm.tm_hour;
}
//获得星期
int DateTime::getWDay()
{
return m_tm.tm_wday;
}
int DateTime::getMinutes()
{
return m_tm.tm_min;
}
int DateTime::getSeconds()
{
return m_tm.tm_sec;
}
int DateTime::GetAllDays()
{
return ( GetCurSec() / (3600 * 24) );
}
bool DateTime::before(DateTime& when)
{
return getTimeS() < when.getTimeS();
}
bool DateTime::after(DateTime& when)
{
return getTimeS() > when.getTimeS();
}
bool DateTime::equals(DateTime& when)
{
return getTimeS() == when.getTimeS();
}
}
}
#ifdef WIN32
int gettimeofday(struct timeval *tp, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm. tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif