📄 vsdatetime.cpp
字号:
//////////////////////////////////////////////////////////////////////////////// Filename : VSDateTime.cpp// Written by : excel96// Description ://////////////////////////////////////////////////////////////////////////////#include "VSDateTime.h"#include <stdio.h>#include <unistd.h>#include <time.h>#include <sys/time.h>#include <string>//////////////////////////////////////////////////////////////////////////////// constants definition//////////////////////////////////////////////////////////////////////////////static const uint FIRST_DAY = 2361222; // Julian day for 1752/09/14static const int FIRST_YEAR = 1752; // ### wrong for many countriesstatic const uint SECS_PER_DAY = 86400;static const uint MSECS_PER_DAY = 86400000;static const uint SECS_PER_HOUR = 3600;static const uint MSECS_PER_HOUR = 3600000;static const uint SECS_PER_MIN = 60;static const uint MSECS_PER_MIN = 60000;static const short monthDays[] ={ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};static const char* whiteSpaces = " \t\n";//////////////////////////////////////////////////////////////////////////////// class VSDate member methods//////////////////////////////////////////////////////////////////////////////VSDate::VSDate(int y, int m, int d){ jd = 0; setYMD(y, m, d);}bool VSDate::isValid() const{ return jd >= FIRST_DAY;}int VSDate::year() const{ int y, m, d; jul2greg(jd, y, m, d); return y;}int VSDate::month() const{ int y, m, d; jul2greg(jd, y, m, d); return m;}int VSDate::day() const{ int y, m, d; jul2greg(jd, y, m, d); return d;}int VSDate::dayOfWeek() const{ return (((jd+1) % 7) + 6)%7 + 1;}int VSDate::dayOfYear() const{ return jd - greg2jul(year(), 1, 1) + 1;}int VSDate::daysInMonth() const{ int y, m, d; jul2greg(jd, y, m, d); if (m == 2 && leapYear(y)) return 29; else return monthDays[m];}int VSDate::daysInYear() const{ int y, m, d; jul2greg(jd, y, m, d); return leapYear(y) ? 366 : 365;}string VSDate::toString() const{ int y, m, d; string result; char buf1[10] = {0, }; char buf2[10] = {0, }; char buf3[10] = {0, }; jul2greg(jd, y, m, d); sprintf(buf1, "%d", y); sprintf(buf2, "%d", m); sprintf(buf3, "%d", d); string year = string(buf1); string month = string(buf2); string day = string(buf3); if (month.size() == 1) month = "0" + month; if (day.size() == 1) day = "0" + day; result += year + "."; result += month + "."; result += day; return result;}string VSDate::toStringforWeb() const{ int y, m, d; string result; char buf1[10] = {0, }; char buf2[10] = {0, }; char buf3[10] = {0, }; jul2greg(jd, y, m, d); sprintf(buf1, "%d", y); sprintf(buf2, "%d", m); sprintf(buf3, "%d", d); string year = string(buf1); string month = string(buf2); string day = string(buf3); if (month.size() == 1) month = "0" + month; if (day.size() == 1) day = "0" + day; result += year.substr(3); result += month; result += day; return result;}bool VSDate::setYMD(int y, int m, int d){ if (!isValid(y,m,d)) return false; jd = greg2jul(y, m, d); //Assert(year() == (y > 99 ? y : 1900+y) && month() == m && day() == d); return true;}VSDate VSDate::addDays(int ndays) const{ VSDate d; d.jd = jd + ndays; return d;}int VSDate::daysTo(const VSDate &d) const{ return d.jd - jd;}VSDate VSDate::currentDate(){ time_t ltime = time(NULL); tm t; localtime_r( <ime, &t ); VSDate d; d.jd = greg2jul(t.tm_year + 1900, t.tm_mon + 1, t.tm_mday); return d;}VSDate VSDate::fromString(string text){ VSDate vsd = VSDate::currentDate(); // 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 ); ///////////////////////////////////////// // 2001.1.1 ///////////////////////////////////////// if (text.size() < 8) return vsd; uint a=0; uint b=0; a = text.find_first_of('.', 0 ); b = text.find_first_of('.', a+1); if (a > b || b > text.size()) return vsd; int year = atoi(text.substr( 0, a).c_str()); int month = atoi(text.substr(a+1, b-a-1).c_str()); int day = atoi(text.substr(b+1, text.size()-b-1).c_str()); vsd.setYMD(year, month, day); return vsd;}bool VSDate::isValid(int y, int m, int d){ if (y >= 0 && y <= 99) y += 1900; else if (y < FIRST_YEAR || (y == FIRST_YEAR && (m < 9 || (m == 9 && d < 14)))) return false; return (d > 0 && m > 0 && m <= 12) && (d <= monthDays[m] || (d == 29 && m == 2 && leapYear(y)));}bool VSDate::leapYear(int y){ return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);}uint VSDate::greg2jul(int y, int m, int d){ uint c, ya; if (y <= 99) y += 1900; if (m > 2) { m -= 3; } else { m += 9; y--; } c = y; // NOTE: Sym C++ 6.0 bug c /= 100; ya = y - 100*c; return 1721119 + d + (146097*c)/4 + (1461*ya)/4 + (153*m+2)/5;}void VSDate::jul2greg(uint jd, int &y, int &m, int &d){ uint x; uint j = jd - 1721119; y = (j*4 - 1)/146097; j = j*4 - 146097*y - 1; x = j/4; j = (x*4 + 3) / 1461; y = 100*y + j; x = (x*4) + 3 - 1461*j; x = (x + 4)/4; m = (5*x - 3)/153; x = 5*x - 3 - 153*m; d = (x + 5)/5; if (m < 10) { m += 3; } else { m -= 9; y++; }}//////////////////////////////////////////////////////////////////////////////// class VSTime member methods//////////////////////////////////////////////////////////////////////////////VSTime::VSTime(int h, int m, int s, int ms){ setHMS(h, m, s, ms);}bool VSTime::isValid() const{ return ds < MSECS_PER_DAY;}int VSTime::hour() const{ return ds / MSECS_PER_HOUR;}int VSTime::minute() const{ return (ds % MSECS_PER_HOUR)/MSECS_PER_MIN;}int VSTime::second() const{ return (ds / 1000)%SECS_PER_MIN;}int VSTime::msec() const{ return ds % 1000;}string VSTime::toString() const{ string result = ""; char buf1[10] = {0,}; char buf2[10] = {0,}; char buf3[10] = {0,}; char buf4[10] = {0,}; sprintf(buf1, "%d", hour()); sprintf(buf2, "%d", minute()); sprintf(buf3, "%d", second()); sprintf(buf4, "%d", msec()); string hour = string(buf1); string minute = string(buf2); string second = string(buf3); string msec = string(buf4); if (hour.size() == 1) hour = "0" + hour; if (minute.size() == 1) minute = "0" + minute; if (second.size() == 1) second = "0" + second; if (msec.size() == 1) msec = "00" + msec; if (msec.size() == 2) msec = "0" + msec; result += hour + ":"; // hour result += minute + ":"; // minute result += second + ":"; // second result += msec; // msec return result;}string VSTime::toStringforWeb() const{ string result = ""; char buf1[10] = {0,}; char buf2[10] = {0,}; sprintf(buf1, "%d", hour()); sprintf(buf2, "%d", minute()); string hour = string(buf1); string minute = string(buf2); if (hour.size() == 1) hour = "0" + hour; if (minute.size() == 1) minute = "0" + minute; result += hour; // hour result += minute; // minute return result;}bool VSTime::setHMS(int h, int m, int s, int ms){ if (!isValid(h,m,s,ms)) { ds = MSECS_PER_DAY;// make this invalid return false; } ds = (h*SECS_PER_HOUR + m*SECS_PER_MIN + s)*1000 + ms; return true;}VSTime VSTime::addSecs(int nsecs) const{ return addMSecs(nsecs*1000);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -