📄 timestmp.cpp
字号:
else if (ch == _T('p') || ch == _T('P'))
{
nState = 11;
bPM = TRUE;
}
// Else character is invalid. Set state to -1 to break out
// of loop:
else
nState = -1;
break;
// Looking for first second digit:
case 7:
// If character is a digit, we will assume it is the first
// digit of second. Save as second and set state to 8:
if (_istdigit (ch))
{
tm.wSecond = ch - _T('0'); // Convert from ASCII
++nState;
}
// Else character is invalid. Set state to -1 to break out
// of loop:
else
nState = -1;
break;
// Looking for a digit, A, P or white space
case 8:
// If characer is a digit, we will assume it is the second
// digit of the second. Update second and set state to 9:
if (_istdigit (ch))
{
tm.wSecond *= 10; // First digit parsed becomes second digit
tm.wSecond += (ch - _T('0')); // Convert from ASCII
++nState;
}
// Else if character is an "a" or "A", set state to 11:
else if (ch == _T('a') || ch == _T('A'))
nState = 11;
// Else if character is a "p" or "P", set state to 11:
else if (ch == _T('p') || ch == _T('P'))
{
nState = 11;
bPM = TRUE;
}
// Else character is invalid. Set state to -1 to break out
// of loop:
else
nState = -1;
break;
// Looking for first character of "AM" or "PM":
case 9:
// Else if character is an "a" or "A", set state to 11:
if (ch == _T('a') || ch == _T('A'))
nState = 11;
// Else if character is a "p" or "P", set state to 11:
else if (ch == _T('p') || ch == _T('P'))
{
nState = 11;
bPM = TRUE;
}
// Else character is invalid. Set state to -1 to break out
// of loop:
else
nState = -1;
break;
// Looking for "M":
case 11:
// If character is "m" or "M", set state to 12:
if (ch == _T('m') || ch == _T('M'))
nState = 12;
// Else character is invalid. Set state to -1 to break out
// of loop:
else
nState = -1;
break;
// Only white spaces at this point. If we hit here, then
// character must not be a white space, so set state to -1
// to break out of loop:
case 12:
nState = -1;
break;
}
}
// Check for invalid stop state:
if (nState < 5 || nState == 7)
{
nState = -1;
goto ErrorExit;
}
// Check for valid 24 hour format:
if (nState < 11)
{
if (tm.wHour > 23 || tm.wMinute > 59 || tm.wSecond > 59)
{
nState = -1;
goto ErrorExit;
}
}
// Check for valid 12 hour format:
else
{
if (tm.wHour < 1 || tm.wHour > 12 || tm.wMinute > 59 || tm.wSecond > 59)
{
nState = -1;
goto ErrorExit;
}
// Adjust the hour for PM if less than 12:
if (bPM && tm.wHour < 12)
tm.wHour += 12;
// Adjust for midnight:
else if (!bPM && tm.wHour == 12)
tm.wHour = 0;
}
// If we make it here time string was valid, so save it:
m_st.wHour = tm.wHour;
m_st.wMinute = tm.wMinute;
m_st.wSecond = tm.wSecond;
m_st.wMilliseconds = 0;
// If the init flag is set then convert time to milliseconds from
// midnight:
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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -