#include <QDateTime>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include "commonlib.h"
commonLib*gloCommonLib;
static const int mon_lengths[2][MONS_PER_YEAR] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
static const int year_lengths[2] = {
DAYS_PER_NYEAR, DAYS_PER_LYEAR
};
QString LogContext[] = { _T("其他"),_T("登录信息"),_T("暂时无用"),_T("第一次连接"),_T("非法连接"),
_T("要求登录"),_T("修改密码"),_T("用户离线"),_T("公共信息"),_T("数据查询") };
void commonLib::sleep(unsigned int msec)
{
///* Solution A: OK
//QThread::sleep();//sleep不会释放对象锁,其他线程无法访问对象,因此会阻塞线程
QThread::msleep(msec);
//QThread::usleep();
//QThread::wait();//Wait会释放对象锁
//*/
/* //Solution B: OK
QEventLoop loop;//定义一个新的事件循环
QTimer::singleShot(msec, &loop, SLOT(quit()));//创建单次定时器,槽函数为事件循环的退出函数
loop.exec();//事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出
//*/
/* //Solution C:cpu use high
QTime dieTime = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < dieTime )
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
//*/
/* //Solution D: cpu use high high ,low Solution
QElapsedTimer t;
t.start();
while(t.elapsed()<msec)
;
//*/
}
long commonLib::gmmktime(const struct Sys_Time *tm)//计算从FROM_YEAR年至现在的秒数
{
int yr;
int mn;
long secs;
if (tm->second > 59 ||tm->minute > 59 ||tm->hour > 23 ||tm->day > 31 ||tm->month > 12 ||tm->year < FROM_YEAR)
{
printf("time date's datas format error.\n");
return (long)-1;
}
secs = tm->second;
secs += tm->minute * SECS_PER_MIN;
secs += tm->hour * SECS_PER_HOUR;
secs += (tm->day-1) * SECS_PER_DAY;
for(mn = 0; mn < tm->month-1; mn++)
secs += mon_lengths[isleap(tm->year)][mn] * SECS_PER_DAY;
for(yr=FROM_YEAR; yr < tm->year; yr++)
secs += year_lengths[isleap(yr)]*SECS_PER_DAY;
return secs;
}
int commonLib::mktimetoformattime2(long mktimesec,struct Sys_Time *rettm)//以FROM_YEAR为起点,将秒转换为标准struct Sys_Time时间
{
int totalday=mktimesec/SECS_PER_DAY;
int totalyear=totalday/DAYS_PER_NYEAR;
int yr=FROM_YEAR+totalyear;
struct Sys_Time tmp_tm={yr,1,1,0,0,0,0};
long leftsec=mktimesec-gmmktime(&tmp_tm);
if(leftsec<0)
{
tmp_tm.year-=1;
yr-=1;
leftsec=mktimesec-gmmktime(&tmp_tm);
}
//printf("mktimetoformattime yr:%d leftsec:%ld\n",yr,leftsec);
if(leftsec==0)
{
memcpy(rettm,&tmp_tm,sizeof(tmp_tm));
return 1;
}
else
{
rettm->year=yr;
int leftday=leftsec/SECS_PER_DAY;
int lefthour=leftsec%SECS_PER_DAY/SECS_PER_HOUR;
int leftmin=leftsec%SECS_PER_DAY%SECS_PER_HOUR/SECS_PER_MIN;
int leftsecend=leftsec%SECS_PER_DAY%SECS_PER_HOUR%SECS_PER_MIN;
int i=0;
int tmptotalday=0;
int yorn=isleap(yr);//闰年标记
//printf("yorn:%d\n",yorn);
for(i=0;i<12;i++)
{
tmptotalday+=mon_lengths[yorn][i];
if(tmptotalday>leftday)
break;
}
rettm->month=i+1;
tmptotalday=0;
for(i=0;i<rettm->month-1;i++)
{
tmptotalday+=mon_lengths[yorn][i];
}
leftday=leftday-tmptotalday+1;
rettm->day=leftday;
rettm->hour=lefthour;
rettm->minute=leftmin;
rettm->second=leftsecend;
return 1;
}
return -1;
}
struct Sys_Time commonLib::secToSysTime(long totalSec)//以FROM_YEAR为起点,将秒转换为标准struct Sys_Time时间
{
struct Sys_Time rettm;
int totalday=totalSec/SECS_PER_DAY;
int totalyear=totalday/DAYS_PER_NYEAR;
int yr=FROM_YEAR+totalyear;
struct Sys_Time tmp_tm={yr,1,1,0,0,0,0};
long leftsec=totalSec-gmmktime(&tmp_tm);
if(leftsec<0)
{
tmp_tm.year-=1;
yr-=1;
leftsec=totalSec-gmmktime(&tmp_tm);
}
//printf("mktimetoformattime yr:%d leftsec:%ld\n",yr,leftsec);
if(leftsec==0)
{
memcpy(&rettm,&tmp_tm,sizeof(tmp_tm));
return rettm;
}
else
{
rettm.year=yr;
int leftday=leftsec/SECS_PER_DAY;
int lefthour=leftsec%SECS_PER_DAY/SECS_PER_HOUR;
int leftmin=leftsec%SECS_PER_DAY%SECS_PER_HOUR/SECS_PER_MIN;
int leftsecend=leftsec%SECS_PER_DAY%SECS_PER_HOUR%SECS_PER_MIN;
int i=0;
int tmptotalday=0;
int yorn=isleap(yr);//闰年标记
//printf("yorn:%d\n",yorn);
for(i=0;i<12;i++)
{
tmptotalday+=mon_lengths[yorn][i];
if(tmptotalday>leftday)
break;
}
rettm.month=i+1;
tmptotalday=0;
for(i=0;i<rettm.month-1;i++)
{
tmptotalday+=mon_lengths[yorn][i];
}
leftday=leftday-tmptotalday+1;
rettm.day=leftday;
rettm.hour=lefthour;
rettm.minute=leftmin;
rettm.second=leftsecend;
return rettm;
}
//return -1;
}
struct Sys_Time commonLib::Get_Sys_Time(void)
{
struct Sys_Time time_save;
QDateTime tm = QDateTime::currentDateTime();
QString yearStr=tm.toString("yyyy");
QString monthStr=tm.toString("MM");
QString dayStr=tm.toString("dd");
QString hourStr=tm.toString("hh");
QString minStr=tm.toString("mm");
QString secStr=tm.toString("ss");
time_save.year = yearStr.toInt();
time_save.month = (char)monthStr.toInt();
time_save.day = dayStr.toInt();
time_save.hour = hourStr.toInt();
time_save.minute = minStr.toInt();
time_save.second = secStr.toInt();
time_save.sys_ticks = 0;
return time_save;
}
struct Sys_Time commonLib::get_sys_time(void)
{
QDateTime tm = QDateTime::currentDateTime();
QString yearStr=tm.toString("yyyy");
QString monthStr=tm.toString("MM");
QString dayStr=tm.toString("dd");
QString hourStr=tm.toString("hh");
QString minStr=tm.toString("mm");
QString secStr=tm.toString("ss");
struct Sys_Time time_save;
time_save.year = yearStr.toInt();
time_save.month = (char)monthStr.toInt();
time_save.day = dayStr.toInt();
time_save.hour = hourStr.toInt();
time_save.minute = minStr.toInt();
time_save.second = secStr.toInt();
time_save.sys_ticks = 0;
return time_save;
}
QString commonLib::get_now_sys_time(void)
{
QString qstrNowTime;
struct Sys_Time nowTime=get_sys_time();;
//Get_Sys_Time(&nowTime);
char strNowTime[64];
sprintf(strNowTime,"(%02d/%02d %02d:%02d:%02d)",nowTime.month,nowTime.day,nowTime.hour,nowTime.minute,nowTime.second);
qstrNowTime=QString::fromLatin1(strNowTime);
return qstrNowTime;
}
QString commonLib::get_time_by_date(struct Sys_Time*nowTime)
{
QString qstrNowTime;
char strNowTime[64];
sprintf(strNowTime,"(%04d/%02d/%02d %02d:%02d:%02d)",nowTime->year,nowTime->month,nowTime->day,nowTime->hour,nowTime->minute,nowTime->second);
qstrNowTime=QString::fromLatin1(strNowTime);
return qstrNowTime;
}
QString commonLib::get_now_sys_time_forDb(void)//用于msSql用的时间格式
{
QString qstrNowTime;
struct Sys_Time nowTime=get_sys_time();;
char strNowTime[64];
//nowtimes.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour(), tm.GetMinute(), tm.GetSecond());
sprintf(strNowTime,"%04d-%02d-%02d %02d:%02d
- 1
- 2
前往页