📄 sptime.cpp.svn-base
字号:
// 周六周日
if( 7 == t.GetDayOfWeek() )
return FALSE;
else if( 1 == t.GetDayOfWeek() )
return FALSE;
time_t day = (t.GetTime()+8*3600) % 86400;
if( day >= 9*3600+25*60-nInflateSeconds && day <= 11*3600+30*60+nInflateSeconds )
return TRUE;
else if( day >= 13*3600-nInflateSeconds && day <= 15*3600+nInflateSeconds )
return TRUE;
return FALSE;
}
BOOL CSPTime::FromStockTimeDay( DWORD date )
{
int nHour = 0;
int nMinute = 0;
int nYear = date / 10000;
int nMonth = (date - nYear*10000)/100;
int nDay = (date - nYear*10000 - nMonth*100);
struct tm atm;
atm.tm_sec = 0;
atm.tm_min = nMinute;
atm.tm_hour = nHour;
if( nDay < 1 || nDay > 31) return FALSE;
atm.tm_mday = nDay;
if( nMonth < 1 && nMonth > 12) return FALSE;
atm.tm_mon = nMonth - 1; // tm_mon is 0 based
if( nYear < 1900 ) return FALSE;
atm.tm_year = nYear - 1900; // tm_year is 1900 based
atm.tm_isdst = -1;
time_t tmt = mktime(&atm);
if( tmt == -1 ) // indicates an illegal input time
return FALSE;
*this = CSPTime( nYear, nMonth, nDay, nHour, nMinute, 0 );
return TRUE;
}
BOOL CSPTime::FromStockTimeMin( DWORD date, DWORD year )
{
int nYear = year;
if( -1 == nYear )
nYear = 1990 + date/100000000;
date = date % 100000000;
int nMonth = date/1000000;
int nDay = (date - nMonth*1000000)/10000;
int nHour = (date - nMonth*1000000 - nDay*10000)/100;
int nMinute = (date - nMonth*1000000 - nDay*10000 - nHour*100);
struct tm atm;
atm.tm_sec = 0;
atm.tm_min = nMinute;
atm.tm_hour = nHour;
if( nDay < 1 || nDay > 31) return FALSE;
atm.tm_mday = nDay;
if( nMonth < 1 || nMonth > 12) return FALSE;
atm.tm_mon = nMonth - 1; // tm_mon is 0 based
if( nYear < 1900 ) return FALSE;
atm.tm_year = nYear - 1900; // tm_year is 1900 based
atm.tm_isdst = -1;
time_t tmt = mktime(&atm);
if( tmt == -1 ) // indicates an illegal input time
return FALSE;
*this = CSPTime( nYear, nMonth, nDay, nHour, nMinute, 0 );
return TRUE;
}
BOOL CSPTime::FromStockTime( DWORD dwDate, BOOL bDayOrMin, DWORD dwYear )
{
if( bDayOrMin )
return FromStockTimeDay( dwDate );
else
return FromStockTimeMin( dwDate, dwYear );
}
DWORD CSPTime::ToStockTimeDay( )
{
if( -1 == GetTime() || GetTime() < 0 )
return -1;
if( 0 == GetTime() )
return 0;
return ( GetYear() * 10000 + GetMonth() * 100 + GetDay() );
}
DWORD CSPTime::ToStockTimeMin( )
{
if( -1 == GetTime() || GetTime() < 0 )
return -1;
if( 0 == GetTime() )
return 0;
return ( (GetYear() - 1990) * 100000000 + GetMonth() * 1000000 + GetDay() * 10000
+ GetHour() * 100 + GetMinute() );
}
DWORD CSPTime::ToStockTimeSecOrder( DWORD dwStockExchange )
{
if( -1 == GetTime() || GetTime() < 0 || 0 == GetTime() )
return 0;
if( GetHour() < 9 || (GetHour() == 9 && GetMinute() < 30) )
return 0;
CSPTime tmStart = CSPTime(GetYear(),GetMonth(),GetDay(),9,30,0);
CSPTime tmEnd = CSPTime(GetYear(),GetMonth(),GetDay(),15,0,0);
if( *this < tmStart )
return 0;
if( *this > tmEnd )
return 14400;
CSPTimeSpan tmSpan = *this - tmStart;
int nSec = tmSpan.GetTotalSeconds();
if( nSec >= 0 && nSec <= 7200 )
return nSec;
if( nSec > 7200 && nSec < 12600 )
return 7200;
if( nSec >= 12600 && nSec <= 19800 )
return nSec-5400;
SP_ASSERT( FALSE );
return 0;
}
DWORD CSPTime::ToStockTime( BOOL bDayOrMin )
{
if( bDayOrMin )
return ToStockTimeDay( );
else
return ToStockTimeMin( );
}
struct tm* CSPTime::GetGmtTm(struct tm* ptm) const
{
if (ptm != NULL)
{
*ptm = *gmtime(&m_time);
return ptm;
}
else
return gmtime(&m_time);
}
struct tm* CSPTime::GetLocalTm(struct tm* ptm) const
{
time_t time_temp = m_time;
if( m_time > 0 )
time_temp = m_time+28800; // 北京时间
if (ptm != NULL)
{
struct tm* ptmTemp = gmtime(&time_temp);
if (ptmTemp == NULL)
return NULL; // indicates the m_time was not initialized!
*ptm = *ptmTemp;
return ptm;
}
else
return gmtime(&time_temp);
/*
if (ptm != NULL)
{
struct tm* ptmTemp = localtime(&m_time);
if (ptmTemp == NULL)
return NULL; // indicates the m_time was not initialized!
*ptm = *ptmTemp;
return ptm;
}
else
return localtime(&m_time);
*/
}
BOOL CSPTime::GetAsSystemTime(SYSTEMTIME& timeDest) const
{
struct tm* ptm = GetLocalTm(NULL);
if (ptm == NULL)
return FALSE;
timeDest.wYear = (WORD) (1900 + ptm->tm_year);
timeDest.wMonth = (WORD) (1 + ptm->tm_mon);
timeDest.wDayOfWeek = (WORD) ptm->tm_wday;
timeDest.wDay = (WORD) ptm->tm_mday;
timeDest.wHour = (WORD) ptm->tm_hour;
timeDest.wMinute = (WORD) ptm->tm_min;
timeDest.wSecond = (WORD) ptm->tm_sec;
timeDest.wMilliseconds = 0;
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// String formatting
#define maxTimeBufferSize 128
// Verifies will fail if the needed buffer size is too large
#ifdef _UNICODE
#endif
CSPString CSPTimeSpan::Format(LPCTSTR pFormat) const
// formatting timespans is a little trickier than formatting CSPTimes
// * we are only interested in relative time formats, ie. it is illegal
// to format anything dealing with absolute time (i.e. years, months,
// day of week, day of year, timezones, ...)
// * the only valid formats:
// %D - # of days -- NEW !!!
// %H - hour in 24 hour format
// %M - minute (0-59)
// %S - seconds (0-59)
// %% - percent sign
{
TCHAR szBuffer[maxTimeBufferSize];
TCHAR ch;
LPTSTR pch = szBuffer;
while ((ch = *pFormat++) != '\0')
{
SP_ASSERT(pch < &szBuffer[maxTimeBufferSize]);
if (ch == '%')
{
switch (ch = *pFormat++)
{
default:
SP_ASSERT(FALSE); // probably a bad format character
case '%':
*pch++ = ch;
break;
case 'D':
pch += wsprintf(pch, _T("%ld"), GetDays());
break;
case 'H':
pch += wsprintf(pch, _T("%02d"), GetHours());
break;
case 'M':
pch += wsprintf(pch, _T("%02d"), GetMinutes());
break;
case 'S':
pch += wsprintf(pch, _T("%02d"), GetSeconds());
break;
}
}
else
{
*pch++ = ch;
if (_istlead(ch))
{
SP_ASSERT(pch < &szBuffer[maxTimeBufferSize]);
*pch++ = *pFormat++;
}
}
}
*pch = '\0';
return szBuffer;
}
CSPString CSPTime::Format(LPCTSTR pFormat) const
{
TCHAR szBuffer[maxTimeBufferSize];
struct tm* ptmTemp = localtime(&m_time);
if (ptmTemp == NULL ||
!_tcsftime(szBuffer, sizeof(szBuffer), pFormat, ptmTemp))
szBuffer[0] = '\0';
return szBuffer;
}
CSPString CSPTime::FormatGmt(LPCTSTR pFormat) const
{
TCHAR szBuffer[maxTimeBufferSize];
struct tm* ptmTemp = gmtime(&m_time);
if (ptmTemp == NULL ||
!_tcsftime(szBuffer, sizeof(szBuffer), pFormat, ptmTemp))
szBuffer[0] = '\0';
return szBuffer;
}
#ifdef _UNICODE
// These functions are provided for compatibility with MFC 3.x
CSPString CSPTime::Format(LPCSTR pFormat) const
{
CSPString strFormat(pFormat);
return Format((LPCTSTR)strFormat);
}
CSPString CSPTime::FormatGmt(LPCSTR pFormat) const
{
CSPString strFormat(pFormat);
return FormatGmt((LPCTSTR)strFormat);
}
CSPString CSPTimeSpan::Format(LPCSTR pFormat) const
{
CSPString strFormat = pFormat;
return Format((LPCTSTR)strFormat);
}
#endif // _UNICODE
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -