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

📄 timestmp.cpp

📁 PC客户和opc通信的源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	if (!stTimeFmt.b24Hr)
		{
		// If PM, subtract 12 (value of 12 is OK as is):
		if (bNight && wHour > 12)
			wHour -= 12;

		// Else if 0, reset to 12:
		else if (!wHour)
			wHour = 12;
		}

	// Format the time with appropriate leading zero and seconds if required:
	if (bUseSec)
		{
		if (!stTimeFmt.bLeadingZero)
			wsprintf (szTime,	_T("%2d%c%02d%c%02d"), wHour, ch, wMinute, ch, wSecond);
		else
			wsprintf (szTime,	_T("%02d%c%02d%c%02d"),	wHour, ch, wMinute, ch, wSecond);
		}
	else
		{
		if (!stTimeFmt.bLeadingZero)
			wsprintf (szTime,	_T("%2d%c%02d"), wHour, ch, wMinute);
		else
			wsprintf (szTime,	_T("%02d%c%02d"),	wHour, ch, wMinute);
		}

	// Apply AM/PM indication if not using 24 hour format:
	if (!stTimeFmt.b24Hr)
		{
		// Add a space:
		lstrcat (szTime, _T(" "));

		// Add "PM" if night, or "AM" if not:
		lstrcat (szTime, (bNight) ? stTimeFmt.szPMString : stTimeFmt.szAMString);
		}
	}

// **************************************************************************
// operator <
//
// Description:
//	Custom less than operator.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to be compared with.
//
// Returns:
//  bool - true if we are "less than" cts.
// **************************************************************************
bool CTimeStamp::operator < (CTimeStamp &cts)	const
	{
	// If our year is greater than, then our timestamp can't be less than.
	// Return false.
	if (m_st.wYear > cts.m_st.wYear)
		return (false);

	// If we make it hear, our year must be less than or equal to.  If our 
	// year is less than, timestamp is less than. Return true.
	else if (m_st.wYear < cts.m_st.wYear)
		return (true);

	// If we make it here, we know years must be equal, so we need to
	// look at the day to make comparison.  If our day is less than, 
	// timestamp is less than.  Return true:
	else if (m_nDay < cts.m_nDay)
		return (true);

	// If we make it here, we know our day is greater than or equal.
	// If days are equal, we need to look at time of day to make
	// comparison.
	else if (m_nDay == cts.m_nDay)
		return (m_lMS < cts.m_lMS);

	// If we make it here, we know our day is greater than, so timestamp
	// can't be less than.  Return false.
	return (false);
	}

// **************************************************************************
// operator <=
//
// Description:
//	Custom less than or equal to operator.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to be compared with.
//
// Returns:
//  bool - true if we are "less than or equal to" cts.
// **************************************************************************
bool CTimeStamp::operator <= (CTimeStamp &cts)	const
	{
	// If our year is greater than, then our timestamp can't be less than or 
	// equal to.  Return false.
	if (m_st.wYear > cts.m_st.wYear)
		return (false);

	// If we make it hear, our year must be less than or equal to.  If our 
	// year is less than, timestamp is less than. Return true.
	else if (m_st.wYear < cts.m_st.wYear)
		return (true);

	// If we make it here, we know years must be equal, so we need to
	// look at the day to make comparison.  If our day is less than, 
	// timestamp is less than.  Return true:
	else if (m_nDay < cts.m_nDay)
		return (true);

	// If we make it here, we know our day is greater than or equal.
	// If days are equal, we need to look at time of day to make
	// comparison.
	else if (m_nDay == cts.m_nDay)
		return (m_lMS <= cts.m_lMS);

	// If we make it here, we know our day is greater than, so timestamp
	// can't be less than or equal to.  Return false.
	return (false);
	}

// **************************************************************************
// operator >
//
// Description:
//	Custom greater than operator.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to be compared with.
//
// Returns:
//  bool - true if we are "greater than" cts.
// **************************************************************************
bool CTimeStamp::operator > (CTimeStamp &cts)	const
	{
	// If our year is less than, then our timestamp can't be greater than.
	// Return false.
	if (m_st.wYear < cts.m_st.wYear)
		return (false);

	// If we make it hear, our year must be greater than or equal to.  If our 
	// year is greater than, timestamp is greater than. Return true.
	if (m_st.wYear > cts.m_st.wYear)
		return (true);

	// If we make it here, we know years must be equal, so we need to
	// look at the day to make comparison.  If our day is greater than, 
	// timestamp is greater than.  Return true:
	else if (m_nDay > cts.m_nDay)
		return (true);

	// If we make it here, we know our day is less than or equal.
	// If days are equal, we need to look at time of day to make
	// comparison.
	else if (m_nDay == cts.m_nDay)
		return (m_lMS > cts.m_lMS);

	// If we make it here, we know our day is less than, so timestamp
	// can't be greater than.  Return false.
	return (false);
	}

// **************************************************************************
// operator >=
//
// Description:
//	Custom greater than or equal to operator.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to be compared with.
//
// Returns:
//  bool - true if we are "greater than or equal to" cts.
// **************************************************************************
bool CTimeStamp::operator >= (CTimeStamp &cts)	const
	{
	// If our year is less than, then our timestamp can't be greater than or
	// equal to. Return false.
	if (m_st.wYear < cts.m_st.wYear)
		return (false);

	// If we make it hear, our year must be greater than or equal to.  If our 
	// year is greater than, timestamp is greater than. Return true.
	if (m_st.wYear > cts.m_st.wYear)
		return (true);

	// If we make it here, we know years must be equal, so we need to
	// look at the day to make comparison.  If our day is greater than, 
	// timestamp is greater than.  Return true:
	else if (m_nDay > cts.m_nDay)
		return (true);

	// If we make it here, we know our day is less than or equal.
	// If days are equal, we need to look at time of day to make
	// comparison.
	else if (m_nDay == cts.m_nDay)
		return (m_lMS >= cts.m_lMS);

	// If we make it here, we know our day is less than, so timestamp
	// can't be greater than or equal to.  Return false.
	return (false);
	}

// **************************************************************************
// operator ==
//
// Description:
//	Custom equals operator.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to be compared with.
//
// Returns:
//  bool - true if we are "equal to" cts.
// **************************************************************************
bool CTimeStamp::operator == (CTimeStamp &cts) const
	{
	// If years aren't the same, timestamps can't be the same:
	if (m_st.wYear != cts.m_st.wYear)
		return (false);

	// If days aren't the same, timestamps can't be the same:
	if (m_nDay != cts.m_nDay)
		return (false);

	// If time of days aren't the same, timestamps can't be the same:
	if (m_lMS != cts.m_lMS)
		return (false);

	// If we make it here, we must be the same:
	return (true);
	}

// **************************************************************************
// Compare ()
//
// Description:
//	Compares this object with another instance.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to be compared with.
//
// Returns:
//  int - -1 if we are greater than, 1 if we are less than, 0 if equal to cts.
// **************************************************************************
int CTimeStamp::Compare (CTimeStamp &cts) const
	{
	// If our year is greater than, then our timestamp is greater than:
	if (m_st.wYear > cts.m_st.wYear)
		return (-1);

	// If our year is less than, then our timestamp is less than:
	else if (m_st.wYear < cts.m_st.wYear)
		return (1);

	// If we make it here, we know years are equal.  We need to look
	// at day to make comparison.

	// If our day is greater than, then our timestamp is greater than:
	else if (m_nDay > cts.m_nDay)
		return (-1);

	// If days are the same, we need to look at time of day to make
	// comparison:
	else if (m_nDay == cts.m_nDay)
		{
		// If our time of day is greater than, than our timestamp is 
		// greater than:
		if (m_lMS > cts.m_lMS)
			return (-1);

		// If time of days are equal, then our timestamps are equal:
		else if (m_lMS == cts.m_lMS)
			return (0);

		// If we make it here, we know our time of day is less than,
		// so out timestamp is less than:
		else
			return (1);
		}

	// If we make it here, we know our day is less than, so our timestamp
	// is less than:
	else
		return (1);
	}

// **************************************************************************
// DiffMS ()
//
// Description:
//	Called to compute the difference in milliseconds between this object's
//	current settings and another.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to compute difference with.
//
// Returns:
//  long - difference = (this - cts) MS.
// **************************************************************************
long CTimeStamp::DiffMS (CTimeStamp &cts) const
	{
	long lDiff;
	int cnDayDiff;

	// Compute the difference in days:
	cnDayDiff = m_nDay - cts.m_nDay;

	// Must account for number of days in a year if years are different:

	// If our year is less, subtract 365 days, or 366 if a leap year:
	if (m_st.wYear < cts.m_st.wYear)
		cnDayDiff -= (ISLEAP (m_st.wYear) ? 366 : 365);

	// If our year is greater than, add 365 days, or 366 if a leap year:
	else if (m_st.wYear > cts.m_st.wYear)
		cnDayDiff += (ISLEAP (cts.m_st.wYear) ? 366 : 365);

	// Convert difference in days to milliseconds:
	lDiff = MSPERDAY * (long)cnDayDiff;

	// Add difference in time of day (in milliseconds)
	lDiff += (m_lMS - cts.m_lMS);

	// Return result:
	return (lDiff);
	}

// **************************************************************************
// DiffSec ()
//
// Description:
//	Called to computer the difference in seconds between this object's
//	current settings and another.
//
// Parameters:
//  CTimeStamp	&cts	CTimeStamp object to compute difference with.
//
// Returns:
//  long - difference = (this - cts) Sec.
// **************************************************************************
long CTimeStamp::DiffSec (CTimeStamp &cts) const
	{
	long lDiff;
	int cnDayDiff;

	// Compute the difference in days:
	cnDayDiff = m_nDay - cts.m_nDay;

	// Must account for number of days in a year if years are different:

	// If our year is less, subtract 365 days, or 366 if a leap year:
	if (m_st.wYear < cts.m_st.wYear)
		cnDayDiff -= (ISLEAP (m_st.wYear) ? 366 : 365);

	// If our year is greater than, add 365 days, or 366 if a leap year:
	else if (m_st.wYear > cts.m_st.wYear)
		cnDayDiff += (ISLEAP (cts.m_st.wYear) ? 366 : 365);

	// Convert difference in days to seconds:
	lDiff = SECPERDAY * (long)cnDayDiff;

	// Add difference in time of day (in milliseconds) and convert to seconds:
	lDiff += ((m_lMS - cts.m_lMS) / 1000L);

	// Return result:
	return (lDiff);
	}

// **************************************************************************
// IncDay ()
//
// Description:
//	Increment current settings by specified number of days.
//
// Parameters:
//  int			nDayInc		Number of days to increment by.
//
// Returns:
//  void
// **************************************************************************
void CTimeStamp::IncDay (int nDayInc)
	{
	// Increment the day:
	m_nDay += nDayInc;

	// If result is negative, decrement years until positive:
	if (m_nDay < 0)
		{
		while (m_nDay < 0)
			{
			// Decrement year:
			--m_st.wYear;

			// Add number of day in year (366 if leap year) to
			// account for decremented year:
			m_nDay += ISLEAP (m_st.wYear) ? 366 : 365;
			}
		}

	// Else make sure result is less than the number of days in year:
	else
		{
		// Get number of day in current year (366 if leap year)
		int nDaysPerYear = ISLEAP (m_st.wYear) ? 366 : 365;

		// If day is beyond end of year, increment year until day of year
		// is valid:
		while (m_nDay > nDaysPerYear - 1)
			{
			// Subtract number of days in current year:
			m_nDay -= nDaysPerYear;

⌨️ 快捷键说明

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