📄 timestmp.cpp
字号:
// Increment year to account for incremented year:
++m_st.wYear;
// Get number of days in new year for next test:
nDaysPerYear = ISLEAP (m_st.wYear) ? 366 : 365;
}
}
ConvertDateFromDays ();
}
// **************************************************************************
// IncMS ()
//
// Description:
// Increment current settings by specified number of milliseconds.
//
// Parameters:
// int lIntervalMS Number of milliseconds to increment by.
//
// Returns:
// void
// **************************************************************************
void CTimeStamp::IncMS (long lIntervalMS)
{
int nDayInc;
// No need to do anything if interval is zero:
if (!lIntervalMS)
return;
// Calculate whole number of days to increment by:
nDayInc = (int)(lIntervalMS / MSPERDAY);
// Calculate fractional number of days to increment by:
// If interval is positive:
if (lIntervalMS > 0)
{
// Increment milliseconds:
m_lMS += (lIntervalMS % MSPERDAY);
// If result is greater than the number of milliseconds per day,
// decrement by number of milliseconds per day and increment day:
if (m_lMS >= MSPERDAY)
{
m_lMS -= MSPERDAY;
++nDayInc;
}
}
// If interval is negative:
else
{
// Decrement milliseconds:
m_lMS -= (-lIntervalMS % MSPERDAY);
// If result is negative, increment by number of milliseconds per
// day and decrement the day:
if (m_lMS < 0)
{
m_lMS += MSPERDAY;
--nDayInc;
}
}
// Increment day if needed:
if (nDayInc)
IncDay (nDayInc);
// Update the time members of the time stamp struct:
ConvertTimeFromMS ();
}
// **************************************************************************
// IncSec ()
//
// Description:
// Increment current settings by specified number of seconds.
//
// Parameters:
// int lIntervalSec Number of seconds to increment by.
//
// Returns:
// void
// **************************************************************************
void CTimeStamp::IncSec (long lIntervalSec)
{
int nDayInc;
// No need to do anything if interval is zero:
if (!lIntervalSec)
return;
// Calculate whole number of days to increment by:
nDayInc = (int)(lIntervalSec / SECPERDAY);
// Calculate fractional number of days to increment by:
// If interval is positive:
if (lIntervalSec > 0)
{
// Increment milliseconds:
m_lMS += (lIntervalSec % SECPERDAY) * 1000;
// If result is greater than the number of milliseconds per day,
// decrement by number of milliseconds per day and increment day:
if (m_lMS >= MSPERDAY)
{
m_lMS -= MSPERDAY;
++nDayInc;
}
}
// If interval is negative:
else
{
// Decrement milliseconds:
m_lMS -= (-lIntervalSec % SECPERDAY) * 1000;
// If result is negative, increment by number of milliseconds per
// day and decrement the day:
if (m_lMS < 0)
{
m_lMS += MSPERDAY;
--nDayInc;
}
}
// Increment day if needed:
if (nDayInc)
IncDay (nDayInc);
// Update the time members of the time stamp struct:
ConvertTimeFromMS ();
}
// **************************************************************************
// ConvertDateToDays ()
//
// Description:
// Convert date to day of the year.
//
// Parameters:
// none
//
// Returns:
// void
// **************************************************************************
void CTimeStamp::ConvertDateToDays ()
{
int nMonth = 0;
// Assign number of days in February:
anDaysPerMonth [1] = ISLEAP (m_st.wYear) ? 29 : 28;
// Save of day of month (zero based):
m_nDay = m_st.wDay - 1;
// Add number of days in each previous month:
while (nMonth < m_st.wMonth - 1)
m_nDay += anDaysPerMonth [nMonth++];
}
// **************************************************************************
// ConvertDateFromDays ()
//
// Description:
// Convert day of year to date.
//
// Parameters:
// none
//
// Returns:
// void
// **************************************************************************
void CTimeStamp::ConvertDateFromDays ()
{
// Day of year as one based value:
int cnDays = m_nDay + 1;
// Assign number of days in February
anDaysPerMonth [1] = ISLEAP (m_st.wYear) ? 29 : 28;
m_st.wMonth = 0;
// Subtract number of days in each month until result is less than
// the number of days in month. At that point we know what month and
// what day of that month date is for:
while (cnDays > anDaysPerMonth [m_st.wMonth])
cnDays -= anDaysPerMonth [m_st.wMonth++];
++m_st.wMonth; // Month is one based
m_st.wDay = cnDays; // Date is one based
}
// **************************************************************************
// ConvertTimeToMS ()
//
// Description:
// Convert time to milliseconds from midnight.
//
// Parameters:
// none
//
// Returns:
// void
// **************************************************************************
void CTimeStamp::ConvertTimeToMS ()
{
// Take value in m_st and convert it to milliseconds. Store result in
// m_lMS:
m_lMS = (long)m_st.wHour * 3600;
m_lMS += (long)m_st.wMinute * 60;
m_lMS += m_st.wSecond;
m_lMS *= 1000;
m_lMS += m_st.wMilliseconds;
}
// **************************************************************************
// ConvertTimeFromMS ()
//
// Description:
// Convert milliseconds from midnight to time.
//
// Parameters:
// none
//
// Returns:
// void
// **************************************************************************
void CTimeStamp::ConvertTimeFromMS ()
{
// Save time of day in milliseconds:
long lTime = m_lMS;
// Compute whole number of hours in this number of milliseconds.
m_st.wHour = (unsigned short)(lTime / (3600L * 1000L));
// Subtract this number of hours. Remainder is number of minutes:
lTime -= (long)m_st.wHour * (3600L * 1000L);
// Compute whole number of minutes in remainder:
m_st.wMinute = (unsigned short)(lTime / (60L * 1000L));
// Subtract this number of minutes. Remainder is number of seconds:
lTime -= (long)m_st.wMinute * (60L * 1000L);
// Compute whole number of seconds in remainder:
m_st.wSecond = (unsigned short)(lTime / 1000L);
// Subtract this number of seconds. Remainder is number of milliseconds:
lTime -= (long)m_st.wSecond * 1000L;
// Remander is number of milliseconds:
m_st.wMilliseconds = (unsigned short)lTime;
}
// **************************************************************************
// SetDateTimeFormat ()
//
// Description:
// Obtain date and time format from system. Called when a program starts up
// and when win.ini changes
// Parameters:
// none
//
// Returns:
// void
// **************************************************************************
void CTimeStamp::SetDateTimeFormat ()
{
// 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);
TRACE (_T("Reading system date/time settings ...\n"));
// Create registry section string. This is where time and date formats
// are stored:
LPCTSTR lpintl = _T("intl");
// Get numerical format properties:
stDateFmt.nFmt = GetProfileInt (lpintl, _T("iDate"), VDATE_MMDDYY);
stTimeFmt.b24Hr = GetProfileInt (lpintl, _T("iTime"), FALSE);
stTimeFmt.bLeadingZero = GetProfileInt (lpintl, _T("iTLZero"), FALSE);
// Get string format properties:
GetProfileString (lpintl, _T("sDate"), _T("/"), stDateFmt.szSeparator, 2);
GetProfileString (lpintl, _T("sTime"), _T(":"), stTimeFmt.szSeparator, 2);
GetProfileString (lpintl, _T("sShortDate"), _T("M/d/yy"), stDateFmt.szShortDate,
_countof (stDateFmt.szShortDate));
GetProfileString (lpintl, _T("s1159"), _T("am"), stTimeFmt.szAMString, 8);
GetProfileString (lpintl, _T("s2359"), _T("pm"), stTimeFmt.szPMString, 8);
// Determine where to place leading zeroes:
int nLen = lstrlen (stDateFmt.szShortDate);
// Initialize number of leading zero members so that when we increment
// for each corresponding field in format string, result will give
// us the correct number of leading zeros:
stDateFmt.bMonthLeadingZero = -1; // Expect at least one digit
stDateFmt.bDayLeadingZero = -1; // Expect at least one digit
stDateFmt.bFullYear = -2; // Expect at least two digits
// Process each character in format string. Add increment leading
// zero members each time we encounter one of their digit place holders
// in string:
for (int i = 0; i < nLen; i++)
{
switch (stDateFmt.szShortDate [i])
{
// Month place holders:
case _T('m'):
case _T('M'):
++stDateFmt.bMonthLeadingZero;
break;
// Day place holders:
case _T('d'):
case _T('D'):
++stDateFmt.bDayLeadingZero;
break;
// Year place holders:
case _T('y'):
case _T('Y'):
++stDateFmt.bFullYear;
break;
// Skip over all other characters:
default:
continue;
}
}
// Construct a format string for date display:
switch (stDateFmt.nFmt)
{
case VDATE_MMDDYY:
wsprintf (
stDateFmt.szFmtString,
_T("%%.%dd%c%%.%dd%c%%.%dd"),
(stDateFmt.bMonthLeadingZero) ? 2 : 1,
*stDateFmt.szSeparator,
(stDateFmt.bDayLeadingZero) ? 2 : 1,
*stDateFmt.szSeparator,
(stDateFmt.bFullYear) ? 4 : 2
);
break;
case VDATE_DDMMYY:
wsprintf (
stDateFmt.szFmtString,
_T("%%.%dd%c%%.%dd%c%%.%dd"),
(stDateFmt.bDayLeadingZero) ? 2 : 1,
*stDateFmt.szSeparator,
(stDateFmt.bMonthLeadingZero) ? 2 : 1,
*stDateFmt.szSeparator,
(stDateFmt.bFullYear) ? 4 : 2
);
break;
case VDATE_YYMMDD:
wsprintf (
stDateFmt.szFmtString,
_T("%%.%dd%c%%.%dd%c%%.%dd"),
(stDateFmt.bFullYear) ? 4 : 2,
*stDateFmt.szSeparator,
(stDateFmt.bMonthLeadingZero) ? 2 : 1,
*stDateFmt.szSeparator,
(stDateFmt.bDayLeadingZero) ? 2 : 1
);
break;
}
// Initialize the time format length:
stTimeFmt.nFmtLen = 5; // Minimum (hh:mm)
// Adjust format length if 12 hour format:
if (!stTimeFmt.b24Hr)
{
int nMaxLen;
// Save length of "PM" string:
nMaxLen = lstrlen (stTimeFmt.szPMString);
// If length of "AM" string is longer than "PM" string, save it
// instead:
if (lstrlen (stTimeFmt.szAMString) > nMaxLen)
nMaxLen = lstrlen (stTimeFmt.szAMString);
// Add length of "AM" or "PM" string plus 1 for space:
stTimeFmt.nFmtLen += (nMaxLen + 1);
}
// Assign the date format length:
stDateFmt.nFmtLen = 8; // Minimum (mm/dd/yy)
// Add 2 if using 4 digit years:
if (stDateFmt.bFullYear)
stDateFmt.nFmtLen += 2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -