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

📄 hightime.cpp

📁 Time to number format convertor
💻 CPP
📖 第 1 页 / 共 3 页
字号:

// ******************************
// HighTimeSpan class
// ******************************
void CHighTimeSpan::SetHighTimeSpan(long lDays, int nHours, int nMinutes, int nSeconds,
                                    int nMillis, int nMicros, int nNanos)   
                                      // Milli, Micro & nano, default = 0
{

    int nHundredsNanos;
    
    if (nNanos >= 0)
        nHundredsNanos = (nNanos+50) / 100;
    else
        nHundredsNanos = (nNanos-50) / 100;

    nMicros += nHundredsNanos / 10;
    nHundredsNanos %= 10;
    nMillis += nMicros / 1000;
    nMicros %= 1000;
    nSeconds +=nMillis / 1000;
    nMillis %= 1000;
    nMinutes += nSeconds / 60;
    nSeconds %= 60;
    nHours += nMinutes / 60;
    nMinutes %= 60;
    lDays += nHours / 24;
    nHours %= 24;

    m_liSpan.QuadPart = lDays;
    m_liSpan.QuadPart *= 86400L;
    m_liSpan.QuadPart += (nHours * 3600L) +
                         (nMinutes * 60)  +
                         nSeconds;
    m_liSpan.QuadPart *= 10000000L;
    m_liSpan.QuadPart += (nMillis * 10000L) +
                         (nMicros * 10L) +
                         nHundredsNanos;

    SetStatus(valid);
} // CHighTimeSpan::SetHighTimeSpan()

LONGLONG CHighTimeSpan::GetTotalDays() const    // span in days (about -3.65e6 to 3.65e6)
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    liTemp /= 86400L;
    return liTemp;
} // CHighTimeSpan::GetTotalDays()

LONGLONG CHighTimeSpan::GetTotalHours() const   // span in hours (about -8.77e7 to 8.77e6)
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    liTemp /= 3600L;
    return liTemp;
} // CHighTimeSpan::GetTotalHours()

LONGLONG CHighTimeSpan::GetTotalMinutes() const // span in minutes (about -5.26e9 to 5.26e9)
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    liTemp /= 60L;
    return liTemp;
} // CHighTimeSpan::GetTotalMinutes()

LONGLONG CHighTimeSpan::GetTotalSeconds() const // span in seconds (about -3.16e11 to 3.16e11)
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    return liTemp;
} // CHighTimeSpan::GetTotalSeconds()

LONGLONG CHighTimeSpan::GetTotalMilliSeconds() const // span in milliseconds
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000L;
    return liTemp;
} // CHighTimeSpan::GetTotalMilliSeconds()

LONGLONG CHighTimeSpan::GetTotalMicroSeconds() const // span in microseconds
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10L;
    return liTemp;
} // CHighTimeSpan::GetTotalMicroSeconds()

LONGLONG CHighTimeSpan::GetTotalNanoSeconds() const // span in nanoseconds
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart * 100L;
    return liTemp;
} // CHighTimeSpan::GetTotalNanoSeconds()

int CHighTimeSpan::GetDays() const       // component days in span
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    liTemp = (liTemp / 86400L);
    return (int)liTemp;
} // CHighTimeSpan::GetDays()

int CHighTimeSpan::GetHours() const      // component hours in span (-23 to 23)
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    liTemp = (liTemp % 86400L) / 3600;
    return (int)liTemp;
} // CHighTimeSpan::GetHours()

int CHighTimeSpan::GetMinutes() const    // component minutes in span (-59 to 59)
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    liTemp = (liTemp % 3600L) / 60;
    return (int)liTemp;
} // CHighTimeSpan::GetMinutes()

int CHighTimeSpan::GetSeconds() const    // component seconds in span (-59 to 59)
{
    LONGLONG liTemp;
    liTemp = m_liSpan.QuadPart / 10000000L;
    liTemp = (liTemp % 60L);
    return (int)liTemp;
} // CHighTimeSpan::GetSeconds()

int CHighTimeSpan::GetMilliSeconds() const // component Milliseconds in span (-999 to 999)
{
    LONGLONG liTemp;
    liTemp = (m_liSpan.QuadPart % 10000000L) / 10000L;
    return (int)liTemp;
} // CHighTimeSpan::GetMilliSeconds()

int CHighTimeSpan::GetMicroSeconds() const // component Microseconds in span (-999 to 999)
{
    LONGLONG liTemp;
    liTemp = (m_liSpan.QuadPart % 10000L) / 10L;
    return (int)liTemp;
} // CHighTimeSpan::GetMicroSeconds()

int CHighTimeSpan::GetNanoSeconds() const  // component Nanoseconds in span (-900 to 900)
{
    LONGLONG liTemp;
    liTemp = (m_liSpan.QuadPart % 10) * 100L;
    return (int)liTemp;
} // CHighTimeSpan::GetNanoSeconds()


// CHighTimeSpan math
CHighTimeSpan CHighTimeSpan::operator+(const CHighTimeSpan& dateSpan) const
{
	CHighTimeSpan dateSpanTemp;

	// If either operand Null, result Null
	if (GetStatus() == null || dateSpan.GetStatus() == null)
	{
		dateSpanTemp.SetStatus(null);
		return dateSpanTemp;
	}

	// If either operand Invalid, result Invalid
	if (GetStatus() == invalid || dateSpan.GetStatus() == invalid)
	{
		dateSpanTemp.SetStatus(invalid);
		return dateSpanTemp;
	}

	// Add spans and validate within legal range
	dateSpanTemp.m_liSpan.QuadPart = m_liSpan.QuadPart + dateSpan.m_liSpan.QuadPart;

	return dateSpanTemp;
} // CHighTimeSpan::operator+()

CHighTimeSpan CHighTimeSpan::operator-(const CHighTimeSpan& dateSpan) const
{
	CHighTimeSpan dateSpanTemp;

	// If either operand Null, result Null
	if (GetStatus() == null || dateSpan.GetStatus() == null)
	{
		dateSpanTemp.SetStatus(null);
		return dateSpanTemp;
	}

	// If either operand Invalid, result Invalid
	if (GetStatus() == invalid || dateSpan.GetStatus() == invalid)
	{
		dateSpanTemp.SetStatus(invalid);
		return dateSpanTemp;
	}

	// Add spans and validate within legal range
	dateSpanTemp.m_liSpan.QuadPart = m_liSpan.QuadPart - dateSpan.m_liSpan.QuadPart;

	return dateSpanTemp;
} // CHighTimeSpan::operator-()

// formatting
LPSTR CHighTimeSpan::Format(LPSTR pBuffer, int iBufferLen, LPCTSTR pFormat) const
{
    CHighTime::_HighTimeFormat tmHighTimeTemp;
    tm tmTemp;
    int iPos;
    LPSTR szTemp;

    // If null, return empty string
	if (pFormat == NULL || GetStatus() == null || iBufferLen <= 0)
		return NULL;

    // If invalid, return DateTimeSpan resource string
    if (GetStatus() == invalid || !CHighTime::ConvertLongLongToTime(m_liSpan, tmHighTimeTemp))
	{
		strncpy(pBuffer, INVALID_DATETIME, iBufferLen);
		return pBuffer;
	}

    // Add milli, micro & nano part!!!!!!
    //%D for the Days part
    //%s for the millisecond part
    //%u for the microsecond part
    //%n for the nanosecond part
    if (iBufferLen <= 10)
        szTemp = new CHAR[10];
    else
        szTemp = new CHAR[iBufferLen+1];

    strncpy(pBuffer, pFormat, iBufferLen);

    iPos = CHighTime::FindStr(pBuffer, "%D", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%0d",LLABS(GetDays()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%D", szTemp);
    }
    iPos = CHighTime::FindStr(pBuffer, "%#D", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%d",LLABS(GetDays()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%#D", szTemp);
    }

    iPos = CHighTime::FindStr(pBuffer, "%s", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%03d",LLABS(GetMilliSeconds()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%s", szTemp);
    }
    iPos = CHighTime::FindStr(pBuffer, "%#s", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%d",LLABS(GetMilliSeconds()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%#s", szTemp);
    }

    iPos = CHighTime::FindStr(pBuffer, "%u", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%03d",LLABS(GetMicroSeconds()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%u", szTemp);
    }
    iPos = CHighTime::FindStr(pBuffer, "%#u", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%d",LLABS(GetMicroSeconds()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%#u", szTemp);
    }

    iPos = CHighTime::FindStr(pBuffer, "%n", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%03d",LLABS(GetNanoSeconds()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%n", szTemp);
    }
    iPos = CHighTime::FindStr(pBuffer, "%#n", 0);
    if (iPos >= 0) {
        wsprintf(szTemp,"%d",LLABS(GetNanoSeconds()));
        CHighTime::ReplaceStr(pBuffer, iBufferLen, "%#n", szTemp);
    }

	// Convert tm from internal format to standard format
	tmTemp.tm_year = tmHighTimeTemp.nYear-1601;   // year is based on 1900
	tmTemp.tm_mon  = tmHighTimeTemp.nMonth-1;     // month of year is 0-based
	tmTemp.tm_wday = tmHighTimeTemp.nDayOfWeek-1; // day of week is 0-based
	tmTemp.tm_yday = tmHighTimeTemp.nDayOfYear-1; // day of year is 0-based
    tmTemp.tm_mday = tmHighTimeTemp.nDay;
    tmTemp.tm_hour = tmHighTimeTemp.nHour;
    tmTemp.tm_min = tmHighTimeTemp.nMinute;
    tmTemp.tm_sec = tmHighTimeTemp.nSecond;
    tmTemp.tm_isdst = 0;

    // Fill in the buffer, disregard return value as it's not necessary
    strcpy(szTemp, pBuffer);
	strftime(pBuffer, iBufferLen, szTemp, &tmTemp);
    delete [] szTemp;

    return pBuffer;
} // CHighTimeSpan::Format()

#if defined(USE_MFC)
CString CHighTimeSpan::Format(LPCTSTR pFormat) const
{
    CString szParse(pFormat);
    CString szTemp, strSpan;
    CHighTime::_HighTimeFormat tmHighTimeTemp;
    tm tmTemp;
    int iPos;

    // If null, return empty string
	if (pFormat == NULL || GetStatus() == null)
		return strSpan;

    // If invalid, return DateTimeSpan resource string
    if (GetStatus() == invalid || !CHighTime::ConvertLongLongToTime(m_liSpan, tmHighTimeTemp))
	{
		VERIFY(strSpan.LoadString(AFX_IDS_INVALID_DATETIMESPAN));
		return strSpan;
	}

    // Add milli, micro & nano part!!!!!!
    //%D for the Days part
    //%s for the millisecond part
    //%u for the microsecond part
    //%n for the nanosecond part
    iPos = szParse.Find("%D", 0);
    if (iPos >= 0) {
        szTemp.Format("%0d",LLABS(GetDays()));
        szParse.Replace("%D",szTemp);
    }
    iPos = szParse.Find("%#D", 0);
    if (iPos >= 0) {
        szTemp.Format("%d",LLABS(GetDays()));
        szParse.Replace("%#D",szTemp);
    }
    iPos = szParse.Find("%s", 0);
    if (iPos >= 0) {
        szTemp.Format("%03d",LLABS(GetMilliSeconds()));
        szParse.Replace("%s",szTemp);
    }
    iPos = szParse.Find("%#s", 0);
    if (iPos >= 0) {
        szTemp.Format("%d",LLABS(GetMilliSeconds()));
        szParse.Replace("%#s",szTemp);
    }
    iPos = szParse.Find("%u", 0);
    if (iPos >= 0) {
        szTemp.Format("%03d",LLABS(GetMicroSeconds()));
        szParse.Replace("%u",szTemp);
    }
    iPos = szParse.Find("%#u", 0);
    if (iPos >= 0) {
        szTemp.Format("%d",LLABS(GetMicroSeconds()));
        szParse.Replace("%#u",szTemp);
    }
    iPos = szParse.Find("%n", 0);
    if (iPos >= 0) {
        szTemp.Format("%03d",LLABS(GetNanoSeconds()));
        szParse.Replace("%n",szTemp);
    }
    iPos = szParse.Find("%#n", 0);
    if (iPos >= 0) {
        szTemp.Format("%d",LLABS(GetNanoSeconds()));
        szParse.Replace("%#n",szTemp);
    }

	// Convert tm from internal format to standard format
	tmTemp.tm_year = tmHighTimeTemp.nYear-1601;   // year is based on 1900
	tmTemp.tm_mon  = tmHighTimeTemp.nMonth-1;     // month of year is 0-based
	tmTemp.tm_wday = tmHighTimeTemp.nDayOfWeek-1; // day of week is 0-based
	tmTemp.tm_yday = tmHighTimeTemp.nDayOfYear-1; // day of year is 0-based
    tmTemp.tm_mday = tmHighTimeTemp.nDay;
    tmTemp.tm_hour = tmHighTimeTemp.nHour;
    tmTemp.tm_min = tmHighTimeTemp.nMinute;
    tmTemp.tm_sec = tmHighTimeTemp.nSecond;
    tmTemp.tm_isdst = 0;
    
    // Fill in the buffer, disregard return value as it's not necessary
	strftime(strSpan.GetBuffer(MAX_TIME_BUFFER_SIZE), MAX_TIME_BUFFER_SIZE, 
              szParse.GetBuffer(szParse.GetLength()), &tmTemp);
    szParse.ReleaseBuffer();
	strSpan.ReleaseBuffer();

    return strSpan;
} // CHighTimeSpan::Format()

CString CHighTimeSpan::Format(UINT nID) const
{
    CString sBuffer;
    sBuffer.LoadString(nID);
    Format(sBuffer);
    return sBuffer;
} // CHighTimeSpan::Format()

// serialization
#ifdef _DEBUG
CDumpContext& AFXAPI operator<<(CDumpContext& dc, CHighTimeSpan dateSpanSrc)
{
	dc << "\nCHighTimeSpan Object:";
	dc << "\n\tm_status = " << (long)dateSpanSrc.m_status;
	dc << "\n\tdateSpan = " << dateSpanSrc.m_liSpan.HighPart;
	return dc << "." << dateSpanSrc.m_liSpan.LowPart;
} //  operator<<(CDumpContext, CHighTimeSpan)
#endif // _DEBUG

CArchive& AFXAPI operator<<(CArchive& ar, CHighTimeSpan dateSpanSrc)
{
	ar << (long)dateSpanSrc.m_status;
	ar << dateSpanSrc.m_liSpan.HighPart;
	return ar << dateSpanSrc.m_liSpan.LowPart;
} // operator<<(CArchive, CHighTimeSpan)

CArchive& AFXAPI operator>>(CArchive& ar, CHighTimeSpan& dateSpanSrc)
{
	ar >> (long&)dateSpanSrc.m_status;
	ar >> dateSpanSrc.m_liSpan.HighPart;
	return ar >> dateSpanSrc.m_liSpan.LowPart;
} // operator>>(CArchive, CHighTimeSpan)

#endif // defined(USE_MFC)

⌨️ 快捷键说明

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