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

📄 timestmp.cpp

📁 一个modbus协议的opc server
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	if (bInitialize)
		ConvertTimeToMS ();

ErrorExit:
	// Return true of time string was valid (nState not -1):
	return (nState != -1);
	}

// **************************************************************************
// SetDate ()
//
// Description:
//	Set the date.
//
// Parameters:
///	LPCTSTR		lpDate		Pointer to string representation of date.  Date
//							  format is obtained from system.
//	bool		bInitialize	Set to true to initialize member variables.
//
// Returns:
//  bool - true if success
// **************************************************************************
bool CTimeStamp::SetDate (LPCTSTR lpDate, bool bInitialize)
	{
	TCHAR szFmt [32];
	int nRetVal;
	unsigned uMM, uDD, uYYYY;

	// Create a CSafeLock to make this object thread safe.  Our critical
	// section gets locked here, and will automatically be unlocked when the
	// CSafeLock goes out of scope.
	CSafeLock cs (&sm_cs);

	// Formatting info must be initialized:
	if (!stDateFmt.nFmtLen)
		SetDateTimeFormat ();

	// Create a format string using correct separator charactor:
	wsprintf (szFmt, _T("%%d%c%%d%c%%d"),
		*stDateFmt.szSeparator,
		*stDateFmt.szSeparator);

	// Extract month, day and year from input string according to system
	// time format type. _stscanf will return number of fields successfully
	// extracted from input string:
	switch (stDateFmt.nFmt)
		{
		case VDATE_MMDDYY:
			nRetVal = _stscanf (lpDate, szFmt, &uMM, &uDD, &uYYYY);
			break;

		case VDATE_DDMMYY:
			nRetVal = _stscanf (lpDate, szFmt, &uDD, &uMM, &uYYYY);
			break;

		case VDATE_YYMMDD:
			nRetVal = _stscanf (lpDate, szFmt, &uYYYY, &uMM, &uDD);
			break;

		default:
			nRetVal = 0;
			break;
		}

	// _stscanf should have extracted 3 fields from input string.  If not
	// return FALSE to indicate error:
	if (nRetVal != 3)
		return (FALSE);

	// Correct year if given as 2 digits:
	// (If between 90 and 99 assume 19xx.  if less than 90 assume 20xx):
	if (uYYYY < 100)
		uYYYY += (uYYYY >= 90) ? 1900 : 2000;

	// Check for valid month:
	if (uMM < 1 || uMM > 12)
		return (FALSE);

	// Make sure day is a not zero:
	if (uDD < 1)
		return (FALSE);

	// Make sure day is not greater than number of days in month:
	// If February, must account for leap year:
	if (uMM == 2)
		{
		// Use table to check  Use ISLEAP macro to adjust for leap year:
		if (uDD > (unsigned)(anDaysPerMonth [1] + ISLEAP (uYYYY)))
			return (FALSE);
		}

	// If any other month, just use table to check day:
	else
		{
		if (uDD > anDaysPerMonth [uMM - 1])
			return (FALSE);
		}

	// If we make it here, date is valid so save it:
	m_st.wYear = uYYYY;
	m_st.wDay = uDD;
	m_st.wMonth = uMM;

	// Convert date to day of year if specified:
	if (bInitialize)
		ConvertDateToDays ();

	// Return TRUE to indicate success:
	return (TRUE);
	}

// **************************************************************************
// FormatDate ()
//
// Description:
//	Place the current date setting in specified output string buffer.  Format
//  will be obtained from the system.  If output buffer is too small, it will
//  be filled with "#" characters.
//
// Parameters:
//  LPTSTR		szDate		Output string buffer.
//	int			cnChars		Size of output string buffer.
//
// Returns:
//  void
// **************************************************************************
void CTimeStamp::FormatDate (LPTSTR szDate, int cnChars)
	{
	int nYear;

	// Can't do anything if we are not given a valid output string
	// pointer, so return:
	if (!szDate)
		return;

	// Create a CSafeLock to make this object thread safe.  Our critical
	// section gets locked here, and will automatically be unlocked when the
	// CSafeLock goes out of scope.
	CSafeLock cs (&sm_cs);

	// Formatting info must be initialized:
	if (!stDateFmt.nFmtLen)
		SetDateTimeFormat ();

	// If the buffer is too small then fill with placeholders ("#") and
	// return:
	if (cnChars <= stDateFmt.nFmtLen)
		{
		for (int i = 0; i < cnChars; i++)
			szDate [i] = _T('#');

		szDate [i] = _T('\0');
		return;
		}

	// Truncate year for non-full year representation:
	nYear = m_st.wYear;
	if (!stDateFmt.bFullYear)
		nYear %= 100;

	// Format the date string according to system date format type:
	switch (stDateFmt.nFmt)
		{
		case VDATE_MMDDYY:
			wsprintf (szDate, stDateFmt.szFmtString, m_st.wMonth, m_st.wDay, nYear);
			break;

		case VDATE_DDMMYY:
			wsprintf (szDate, stDateFmt.szFmtString, m_st.wDay, m_st.wMonth, nYear);
			break;

		case VDATE_YYMMDD:
			wsprintf (szDate, stDateFmt.szFmtString, nYear, m_st.wMonth, m_st.wDay);
			break;

		default:
			wsprintf (szDate, _T("%d/%02d/%02d"), m_st.wMonth, m_st.wDay, m_st.wYear % 100);
			break;
		}
	}

// **************************************************************************
// FormatTime ()
//
// Description:
//	Place the current time setting in specified output string buffer.  Format
//  will be obtained from the system.  If output buffer is too small, it will
//  be filled with "#" characters.
//
// Parameters:
//  LPTSTR		szTime		Output string buffer.
//	int			cnChars		Size of output string buffer.
//	bool		bUseSec		Set to true to include seconds.
//
// Returns:
//  void
// **************************************************************************
void CTimeStamp::FormatTime (LPTSTR szTime, int cnChars, bool bUseSec)
	{
	// Can't do anything if we are not given a valid output string
	// pointer, so return:
	if (!szTime)
		return;

	// Create a CSafeLock to make this object thread safe.  Our critical
	// section gets locked here, and will automatically be unlocked when the
	// CSafeLock goes out of scope.
	CSafeLock cs (&sm_cs);

	// Formatting info must be initialized:
	if (!stTimeFmt.nFmtLen)
		SetDateTimeFormat ();

	// Determine the minimum buffer requirements (add 3 characters for
	// seconds if used):
	int nMinChars = (bUseSec) ? stTimeFmt.nFmtLen + 3 : stTimeFmt.nFmtLen;

	// If the buffer is too small then fill with placeholders ("#") and
	// return:
	if (cnChars <= nMinChars)
		{
		for (int i = 0; i < cnChars; i++)
			szTime [i] = _T('#');

		szTime [i] = 0;
		return;
		}

	// Localize and round up to the nearest second:
	unsigned short wMinute = m_st.wMinute;
	unsigned short wSecond = m_st.wSecond;
	unsigned short wHour = m_st.wHour;

	// Need to round seconds up if milliseconds are >= 500:
	if (m_st.wMilliseconds >= 500)
		{
		// Increment seconds.  If this causes the seconds to equal
		// 60, we need to reset to 0 and increment minutes:
		if (++wSecond == 60)
			{
			wSecond = 0;
			
			// Increment minutes.  If this causes the minutes to equal
			// 60, we need to reset to 0 and increment hours:
			if (++wMinute == 60)
				{
				wMinute = 0;
				
				// Increment hours.  If this causes the hours to equal
				// 24, we need to reset to 0.
				if (++wHour == 24)
					{
					wHour = 0;
					}
				}
			}
		}
	
	// Save the proper separator character:
	TCHAR ch = *stTimeFmt.szSeparator;

	// Set flag if PM:
	bool bNight = (wHour >= 12);

	// Adjust for non-military system time format.  Hour value is currently
	// in 24 hour, military, format.
	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);

⌨️ 快捷键说明

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