⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vsdatetime.cpp

📁 天之炼狱1服务器端源文件游戏服务端不完整
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}int VSTime::secsTo(const VSTime &t) const{	return ((int)t.ds - (int)ds)/1000;}VSTime VSTime::addMSecs(int ms) const{	VSTime t;	if (ms < 0) 	{		// % not well-defined for -ve, but / is.		int negdays = (MSECS_PER_DAY-ms) / MSECS_PER_DAY;		t.ds = ((int)ds + ms + negdays*MSECS_PER_DAY) % MSECS_PER_DAY;	} 	else 	{		t.ds = ((int)ds + ms) % MSECS_PER_DAY;	}	return t;}int VSTime::msecsTo(const VSTime &t) const{	return (int)t.ds - (int)ds;}VSTime VSTime::currentTime(){	VSTime ct;	currentTime(&ct);	return ct;}VSTime VSTime::fromString(string text){	VSTime vst = VSTime::currentTime();	// trim white space    uint begin = text.find_first_not_of( whiteSpaces );    uint end = text.find_last_not_of( whiteSpaces );    text = text.substr( begin , end - begin + 1 );	/////////////////////////////////////////	// 1:1:1	/////////////////////////////////////////	if (text.size() < 5) return vst;	uint a=0, b=0, c=0;	a = text.find_first_of(':', 0  );	b = text.find_first_of(':', a+1);	c = text.find_first_of(':', b+1);	if (a > b || b > text.size()) return vst;	int hour    = atoi(text.substr(  0, a                  ).c_str());	int minute  = atoi(text.substr(a+1, b           - a - 1).c_str());	int second  = atoi(text.substr(b+1, c           - b - 1).c_str());	int msecond = atoi(text.substr(c+1, text.size() - c - 1).c_str());	vst.setHMS(hour, minute, second, msecond);	return vst;}bool VSTime::currentTime(VSTime *ct){	if (!ct) return false;	struct timeval tv;	gettimeofday(&tv, 0);	time_t ltime = tv.tv_sec;	tm t;	//tm *t = localtime(&ltime);	localtime_r( &ltime, &t );	//ct->ds = (uint)(MSECS_PER_HOUR*t->tm_hour + MSECS_PER_MIN*t->tm_min + 1000*t->tm_sec + tv.tv_usec/1000);	ct->ds = (uint)(MSECS_PER_HOUR*t.tm_hour + MSECS_PER_MIN*t.tm_min + 1000*t.tm_sec + tv.tv_usec/1000);	return (t.tm_hour== 0 && t.tm_min == 0);}bool VSTime::isValid(int h, int m, int s, int ms){	return (uint)h < 24 && (uint)m < 60 && (uint)s < 60 && (uint)ms < 1000;}void VSTime::start(){	*this = currentTime();}int VSTime::restart(){	VSTime t = currentTime();	int n = msecsTo(t);	if (n < 0) n += 86400*1000; // passed midnight	*this = t;	return n;}int VSTime::elapsed(){	int n = msecsTo(currentTime());	if (n < 0) n += 86400*1000; // passed midnight	return n;}//////////////////////////////////////////////////////////////////////////////// class VSDateTime member methods//////////////////////////////////////////////////////////////////////////////VSDateTime::VSDateTime(const VSDate &date)	: d(date){}VSDateTime::VSDateTime(const VSDate &date, const VSTime &time)	: d(date), t(time){}VSDateTime::VSDateTime(const VSDateTime &dt): d(dt.d), t(dt.t){}// 0123456789012345678// 2003-01-25 13:17:06VSDateTime::VSDateTime(const string& DateTime){	if (DateTime.size()==19)	{		int year  = atoi( DateTime.substr(0,4).c_str() );		int month = atoi( DateTime.substr(5,2).c_str() );		int day   = atoi( DateTime.substr(8,2).c_str() );		int hour  = atoi( DateTime.substr(11,2).c_str() );		int min   = atoi( DateTime.substr(14,2).c_str() );		int sec   = atoi( DateTime.substr(17,2).c_str() );		d.setYMD(year, month, day);		t.setHMS(hour, min, sec);	}	else	{		d.setYMD(0, 0, 0);		t.setHMS(0, 0, 0);	}}string VSDateTime::toDateTime() const{	char str[20];	sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", 					d.year(), d.month(), d.day(),					t.hour(), t.minute(), t.second());	return string(str);}void VSDateTime::setTime_t(uint secsSince1Jan1970UTC){	time_t tmp = (time_t) secsSince1Jan1970UTC;	tm *tM = localtime(&tmp);	if (!tM) 	{		tM = gmtime(&tmp);		if (!tM) 		{			d.jd = VSDate::greg2jul(1970, 1, 1);			t.ds = 0;			return;		}	}	d.jd = VSDate::greg2jul(tM->tm_year + 1900, tM->tm_mon + 1, tM->tm_mday);	t.ds = MSECS_PER_HOUR*tM->tm_hour + MSECS_PER_MIN*tM->tm_min + 1000*tM->tm_sec;}string VSDateTime::toString() const{	string rvalue  = d.toString() + "-" + t.toString();	return rvalue;}string VSDateTime::toStringforWeb() const{	string rvalue  = d.toStringforWeb() + t.toStringforWeb();	return rvalue;}VSDateTime VSDateTime::addDays(int ndays) const{	return VSDateTime(d.addDays(ndays), t);}VSDateTime VSDateTime::addSecs(int secs) const{	return addDays(secs/86400).addMSecs((secs%86400)*1000);}VSDateTime VSDateTime::addMSecs(int nsecs) const{	uint dd   = d.jd;	int  tt   = t.ds;	int  sign = 1;		if (nsecs < 0) 	{		nsecs = -nsecs;		sign = -1;	}		if (nsecs >= (int)MSECS_PER_DAY) 	{		dd += sign*(nsecs/MSECS_PER_DAY);		nsecs %= MSECS_PER_DAY;	}		//tt += sign*nsecs*1000;	tt += sign*nsecs;		if (tt < 0) 	{		tt = MSECS_PER_DAY - tt - 1;		dd -= tt / MSECS_PER_DAY;		tt = tt % MSECS_PER_DAY;		tt = MSECS_PER_DAY - tt - 1;	} 	else if (tt >= (int)MSECS_PER_DAY) 	{		dd += (tt / MSECS_PER_DAY);		tt  = tt % MSECS_PER_DAY;	}		VSDateTime ret;	ret.t.ds = tt;	ret.d.jd = dd;	return ret;}int VSDateTime::daysTo(const VSDateTime &dt) const{	return d.daysTo(dt.d);}int VSDateTime::secsTo(const VSDateTime &dt) const{	return t.secsTo(dt.t) + d.daysTo(dt.d)*SECS_PER_DAY;}int VSDateTime::msecsTo(const VSDateTime &dt) const{	return t.msecsTo(dt.t) + d.daysTo(dt.d)*MSECS_PER_DAY;}bool VSDateTime::operator==(const VSDateTime &dt) const{	return t == dt.t && d == dt.d;}bool VSDateTime::operator!=(const VSDateTime &dt) const{	return  t != dt.t || d != dt.d;}bool VSDateTime::operator<(const VSDateTime &dt) const{	if (d < dt.d) return true;	return d == dt.d ? t < dt.t : false;}bool VSDateTime::operator<=(const VSDateTime &dt) const{	if (d < dt.d) return true;	return d == dt.d ? t <= dt.t : false;}bool VSDateTime::operator>(const VSDateTime &dt) const{	if (d > dt.d)return true;	return d == dt.d ? t > dt.t : false;}bool VSDateTime::operator>=(const VSDateTime &dt) const{	if (d > dt.d) return true;	return d == dt.d ? t >= dt.t : false;}VSDateTime VSDateTime::currentDateTime(){	VSDate cd = VSDate::currentDate();	VSTime ct;	if (VSTime::currentTime(&ct))   // too close to midnight?		cd = VSDate::currentDate(); // YES! time for some midnight	// voodoo, fetch date again	return VSDateTime(cd, ct);}VSDateTime VSDateTime::fromString(string text){	VSDateTime vsdt = VSDateTime::currentDateTime();	uint       a    = 0;    uint begin = text.find_first_not_of( whiteSpaces );    uint end = text.find_last_not_of( whiteSpaces );    text = text.substr( begin , end - begin + 1 );	/////////////////////////////////////////	// 2000.1.1-1:1:1:1	/////////////////////////////////////////	if (text.size() < 16) return vsdt;	a = text.find_first_of('-', 0);	if (a > text.size()) return vsdt;	string date_text = text.substr(  0, a);	string time_text = text.substr(a+1, text.size() - a -1);	vsdt.d = VSDate::fromString(date_text);	vsdt.t = VSTime::fromString(time_text);	return vsdt;}////////////////////////////////////////////////////////////////////////////////// return VSDateTime Object from YYYYMMDDHHMMSS style string// 胶飘傅狼 屈侥捞 瘤沥茄单 鳖瘤父 技泼茄促.// ( 窜 YYYYMMDD 鳖瘤绰 乐绢具 茄促. 绝栏搁 泅犁矫埃. 褥. 备府促.)//  YYYYMMDD 屈侥狼 胶飘傅捞扼搁 斥,岿,老 鳖瘤父 技泼// 唱赣瘤 何盒篮 bEndOfDay 啊 true 搁 23矫 59盒 59檬// false 捞搁 0矫 0盒 0檬////////////////////////////////////////////////////////////////////////////////VSDateTime VSDateTime::fromYYYYMMDDHHMMSS( string text, bool bEndOfDay ){	uint szText = text.size();	if ( szText < 8 )		return VSDateTime::currentDateTime();	int year  = atoi( text.substr(0,4).c_str() );	int month = atoi( text.substr(4,2).c_str() );	int day   = atoi( text.substr(6,2).c_str() );	int hour  = 23;	int min   = 59;	int sec   = 59;	if ( !bEndOfDay )	{		hour = 0;		min  = 0;		sec  = 0;	}	if ( szText >= 10 )		hour = atoi( text.substr(8,2).c_str() );		if ( szText >= 12 )		min = atoi( text.substr(10,2).c_str() );	if ( szText >= 14 )		sec = atoi( text.substr(12,2).c_str() );	VSDate vsdate( year, month, day );	VSTime vstime( hour, min, sec );	VSDateTime vsdt( vsdate, vstime );	return vsdt;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -