home *** CD-ROM | disk | FTP | other *** search
- /*
- Module : DATETIME.H
- Purpose: Implementation for a number of Date / Time classes
- Created: PJN / DATE/1 / 05-05-1995
- History: None
-
- Copyright (c) 1995 by PJ Naughter.
- All rights reserved.
-
- */
-
- ///////////////////////////////// Includes //////////////////////////////////
- #include "stdafx.h"
- #include "limits.h"
- #include "time.h"
- #include "stdlib.h"
- #include "math.h"
- #include "float.h"
- #include "datetime.h"
- #include "resource.h"
-
-
-
- ////////////////////////////////// Macros ///////////////////////////////////
- IMPLEMENT_SERIAL(CDate, CObject, VERSIONABLE_SCHEMA|1)
- IMPLEMENT_SERIAL(CLTimeSpan, CObject, VERSIONABLE_SCHEMA|1)
- IMPLEMENT_SERIAL(CLTimeOfDay, CObject, VERSIONABLE_SCHEMA|1)
- IMPLEMENT_SERIAL(CLDate, CObject, VERSIONABLE_SCHEMA|1)
-
-
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #define new DEBUG_NEW
- #endif
-
-
- ////////////////////////////////// Locals /////////////////////////////////////
- LONG lfloor(LONG a, LONG b);
- void SetFebLength(BOOL bIsLeap);
- void ResetFebLength();
- long WeekOfDayModulo(long x, long y);
-
-
-
- ////////////////////////////////// Statics ////////////////////////////////////
-
- BOOL CDate::sm_bDoAsserts = TRUE;
- LONG CDate::sm_lEndJulianYear = 1582;
- WORD CDate::sm_wEndJulianMonth = 10;
- WORD CDate::sm_wEndJulianDay = 4;
- LONG CDate::sm_lBeginGregYear = 1582;
- WORD CDate::sm_wBeginGregMonth = 10;
- WORD CDate::sm_wBeginGregDay = 15;
- CString CDate::sm_sDefaultFormat = _T("%d/%m/%Y");
-
- BOOL CLTimeSpan::sm_bDoAsserts = TRUE;
- CString CLTimeSpan::sm_sDefaultFormat = _T("%D %H:%M:%S");
- CString CLTimeSpan::sm_sDefaultFormatNOS = _T("%H:%M");
-
- BOOL CLTimeOfDay::sm_bDoAsserts = TRUE;
- CString CLTimeOfDay::sm_sDefaultFormat = _T("%H:%M:%S");
- CString CLTimeOfDay::sm_sDefaultFormatNOS = _T("%H:%M");
-
- BOOL CLDate::sm_bDoAsserts = TRUE;
- BOOL CLDate::sm_bIsDst = TRUE;
-
-
-
-
- static const int DeltaTTable[187] =
- {1240, 1150, 1060, 980, 910, 850, 790, 740, 700, 650,
- 620, 580, 550, 530, 500, 480, 460, 440, 420, 400,
- 370, 350, 330, 310, 280, 260, 240, 220, 200, 180,
- 160, 140, 130, 120, 110, 100, 90, 90, 90, 90,
- 90, 90, 90, 90, 100, 100, 100, 100, 100, 110,
- 110, 110, 110, 110, 110, 110, 110, 120, 120, 120,
- 120, 120, 130, 130, 130, 130, 140, 140, 140, 150,
- 150, 150, 150, 160, 160, 160, 160, 160, 170, 170,
- 170, 170, 170, 170, 170, 170, 160, 160, 150, 140,
- 137, 131, 127, 125, 125, 125, 125, 125, 125, 123,
- 120, 114, 106, 96, 86, 75, 66, 60, 57, 56,
- 57, 59, 62, 65, 68, 71, 73, 75, 77, 78,
- 79, 75, 64, 54, 29, 16, -10, -27, -36, -47,
- -54, -52, -55, -56, -58, -59, -62, -64, -61, -47,
- -27, 0, 26, 54, 77, 105, 134, 160, 182, 202,
- 212, 224, 235, 239, 243, 240, 239, 239, 237, 240,
- 243, 253, 262, 273, 282, 291, 300, 307, 314, 322,
- 331, 340, 350, 365, 383, 402, 422, 445, 465, 485,
- 505, 522, 538, 549, 558, 569, 580};
-
- static WORD MonthLength[14] =
- {
- 0, 31, 28, 31, 30, 31, 30,
- 31, 31, 30, 31, 30, 31, 0};
-
-
-
-
- ////////////////////////////////// Implementation /////////////////////////////
-
-
-
- //Free Sub-Programs
-
- long WeekOfDayModulo(long x, long y)
- {
- if (x >= 0)
- return x % y;
- else
- {
- return (y - labs(x % y)) % y;
- }
- }
-
-
- void SetFebLength(BOOL bIsLeap)
- {
- MonthLength[2] = (WORD) (28 + bIsLeap);
- }
-
- void ResetFebLength()
- {
- MonthLength[2] = 28;
- };
-
- LONG lfloor(LONG a, LONG b)
- {
- ASSERT(b > 0);
- return (a >= 0L ? a/b : (a%b == 0L) - 1 - labs(a)/b);
- }
-
- void InitDTime()
- {
- CDate::InitCalendarSettings();
- CDate::GetDefaultFormat();
- CLTimeSpan::GetDefaultFormat();
- CLTimeOfDay::GetDefaultFormat();
- }
-
-
-
- //CDate Implementation
-
- CDate::CDate()
- {
- Set();
- }
-
- CDate::CDate(LONG Year, WORD Month, WORD Day)
- {
- Set(Year, Month, Day);
- }
-
- CDate::CDate(const SYSTEMTIME& st)
- {
- Set(st);
- }
-
- CDate::CDate(LONG Year, WORD Month, WORD WeekOfMonth, WORD DayOfWeek)
- {
- Set(Year, Month, WeekOfMonth, DayOfWeek);
- }
-
- CDate::CDate(LONG Days, DateEpoch e)
- {
- Set(Days, e);
- }
-
- CDate::CDate(const CDate& d)
- {
- m_lDays = d.m_lDays;
- m_bValid = d.m_bValid;
- m_bInGregCalendar = d.m_bInGregCalendar;
- }
-
- CDate::CDate(const CTime& ctime)
- {
- Set(ctime.GetTime()/86400, EPOCH_CTIME);
- }
-
- CDate::CDate(const COleDateTime& oleTime)
- {
- Set(oleTime);
- }
-
- CDate& CDate::Set()
- {
- m_lDays = 0;
- m_bValid = FALSE;
- m_bInGregCalendar = FALSE;
- return *this;
- }
-
- CDate& CDate::Set(LONG Year, WORD Month, WORD Day)
- {
- //The following method has been taken from the magazine "Microsoft Systems Journal"
- //I cannot find the copy again, so if anyone recognises the algorithm please let
- //me know so I can credit the author.
-
- if (Month < JANUARY || Month > DECEMBER ||
- Day > DaysInMonth(Month, IsLeap(Year)))
- {
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return *this;
- }
-
- if (((Year > sm_lEndJulianYear) && (Year < sm_lBeginGregYear)) ||
- ((Year == sm_lEndJulianYear) && ((Month > sm_wEndJulianMonth) && (Month < sm_wBeginGregMonth))) ||
- ((Year == sm_lEndJulianYear) && (Month == sm_wEndJulianMonth) && (Day > sm_wEndJulianDay) && (Day < sm_wBeginGregDay)))
- {
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return *this;
- }
-
- SetFebLength(IsLeap(Year));
- m_lDays = (Year-1)*365 + lfloor(Year-1, 4L);
- m_bInGregCalendar = InGregorianCalendar(Year, Month, Day);
- if (m_bInGregCalendar)
- m_lDays += lfloor(Year-1, 400L) - lfloor(Year-1, 100L);
- while (--Month)
- m_lDays += MonthLength[Month];
- //m_lDays += Day - 577736L + 2000000000L - 2*(!m_bInGregCalendar); //ensure all usable date values are positive
- m_lDays += Day + 1999422264L - 2*(!m_bInGregCalendar);
-
- ResetFebLength();
-
- m_bValid = TRUE;
-
- return *this;
- }
-
- CDate& CDate::Set(LONG Year, WORD Month, WORD WeekOfMonth, WORD DayOfWeek)
- {
- if (WeekOfMonth < 1 || WeekOfMonth > 5 || DayOfWeek < 1 && DayOfWeek > 7)
- {
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return *this;
- }
-
- if (WeekOfMonth < 5)
- {
- CDate FirstMonth(Year, Month, 1);
- if (!FirstMonth.IsValid())
- {
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return *this;
- }
- WORD dow = FirstMonth.GetDayOfWeek();
- WORD Day = (WORD) (((DayOfWeek - dow + 7) % 7) + (7*(WeekOfMonth-1)) + 1);
- Set(Year, Month, Day);
- }
- else //5 means the last week of the month
- {
- WORD NewMonth = (WORD) (Month + 1);
- LONG NewYear = Year;
- if (NewMonth > DECEMBER)
- {
- NewMonth = 1;
- ++NewYear;
- }
-
- Set(NewYear, NewMonth, 1, DayOfWeek);
- AddWeek(-1);
- }
-
- return *this;
- }
-
- CDate& CDate::Set(LONG Days, DateEpoch e)
- {
- switch (e)
- {
- case EPOCH_GREG:
- {
- m_lDays = Days + 2000000000L;
- break;
- }
- case EPOCH_JD:
- {
- //m_lDays = Days - 2299161L + 2000000000L; //-2299161 coresponds to the GDN of JD Epoch namely 1 January -4712
- m_lDays = Days + 1997700839L;
- break;
- }
- case EPOCH_MJD:
- {
- //m_lDays = Days + 100839L + 2000000000L; //100839 coresponds to the GDN of MJD Epoch namely 17 November 1858
- m_lDays = Days + 2000100839L;
- break;
- }
- case EPOCH_1900:
- {
- //m_lDays = Days + 115860L + 2000000000L; //115860 corresponds to the GDN of 1 January 1900
- m_lDays = Days + 2000115860L;
- break;
- }
- case EPOCH_1950:
- {
- //m_lDays = Days + 134122L + 2000000000L; //134122 corresponds to the GDN of 1 January 1950
- m_lDays = Days + 2000134122L;
- break;
- }
- case EPOCH_CTIME:
- {
- //m_lDays = Days + 141427L + 2000000000L; //141427 corresponds to the GDN of 1 January 1970
- m_lDays = Days + 2000141427L;
- break;
- }
- case EPOCH_2000:
- {
- //m_lDays = Days + 152384L + 2000000000L; //152384 corresponds to the GDN of 1 January 2000
- m_lDays = Days + 2000152384L;
- break;
- }
- default: ASSERT(FALSE); //should not occur
- }
-
- m_bValid = TRUE;
-
- //if (m_lDays - 2000000000L > 2146905911L) //Largest valid GDN
- if (m_lDays > 4146905911L) //Largest valid GDN
- {
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- }
-
- m_bInGregCalendar = *this > CDate(BeginGregorianYear(), BeginGregorianMonth(), BeginGregorianDay());
-
- return *this;
- }
-
- CDate& CDate::Set(const SYSTEMTIME& st)
- {
- Set(st.wYear, st.wMonth, st.wDay);
- return *this;
- }
-
- CDate& CDate::Set(const CTime& ctime)
- {
- return Set(ctime.GetTime()/86400, EPOCH_CTIME);
- }
-
- CDate& CDate::Set(const COleDateTime& oleTime)
- {
- COleDateTime::DateTimeStatus dts(oleTime.GetStatus());
- CDate rVal;
- if (dts == COleDateTime::DateTimeStatus::valid)
- Set((LONG) oleTime.GetYear(), (WORD) oleTime.GetMonth(), (WORD) oleTime.GetDay());
- else
- Set();
- return *this;
- }
-
- CDate CDate::CurrentDate()
- {
- SYSTEMTIME st;
- ::GetLocalTime(&st);
- return CDate(st);
- }
-
- WORD CDate::CurrentMonth()
- {
- return CurrentDate().GetMonth();
- }
-
- WORD CDate::CurrentDay()
- {
- return CurrentDate().GetDay();
- }
-
- LONG CDate::CurrentYear()
- {
- return CurrentDate().GetYear();
- }
-
- WORD CDate::CurrentDayOfWeek()
- {
- return CurrentDate().GetDayOfWeek();
- }
-
- WORD CDate::GetBeginingDayOfWeek()
- {
- WORD nDayOfWeek = MONDAY;
- CString sBuf;
- LPTSTR pszBuf = sBuf.GetBuffer(10);
- int nBytes = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IFIRSTDAYOFWEEK, pszBuf, 10);
- ASSERT(nBytes);
- sBuf.ReleaseBuffer();
-
- if (sBuf == _T("0"))
- nDayOfWeek = MONDAY;
- else if (sBuf == _T("1"))
- nDayOfWeek = TUESDAY;
- else if (sBuf == _T("2"))
- nDayOfWeek = WEDNESDAY;
- else if (sBuf == _T("3"))
- nDayOfWeek = THURSDAY;
- else if (sBuf == _T("4"))
- nDayOfWeek = FRIDAY;
- else if (sBuf == _T("5"))
- nDayOfWeek = SATURDAY;
- else if (sBuf == _T("6"))
- nDayOfWeek = SUNDAY;
- else
- ASSERT(FALSE);
-
- return nDayOfWeek;
- }
-
- BOOL CDate::SetEndJulianCalendar(LONG Year, WORD Month, WORD Day)
- {
- //Update the static values in memory first
- sm_wEndJulianDay = Day;
- sm_wEndJulianMonth = Month;
- sm_lEndJulianYear = Year;
-
- //Broadcast a message to all top level windows that we are about to change
- //some internationl settings in the registry
- SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, NULL);
-
- //Settings are stored in the registry under the key HKEY_CURRENT_USER\Control Panel\DTime
- HKEY hcpl;
- BOOL bSuccess = TRUE;
-
- if (RegOpenKeyEx(HKEY_CURRENT_USER,
- _T("Control Panel"),
- 0,
- KEY_WRITE,
- &hcpl) == ERROR_SUCCESS)
- {
- HKEY happ;
- DWORD dwDisp;
- if (RegCreateKeyEx(hcpl, _T("DTime"), 0, _T(""), REG_OPTION_NON_VOLATILE,
- KEY_WRITE, NULL, &happ, &dwDisp) == ERROR_SUCCESS)
- {
- // Set the day part of the end of Julian Calendar
- DWORD dwDay = (DWORD) Day;
- if (RegSetValueEx(happ, _T("JulianEndDay"), 0, REG_DWORD,
- (BYTE*)&dwDay, sizeof(DWORD)) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
- // Set the month part of the end of Julian Calendar
- DWORD dwMonth = (DWORD) Month;
- if (RegSetValueEx(happ, _T("JulianEndMonth"), 0, REG_DWORD,
- (BYTE*)&dwMonth, sizeof(DWORD)) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
- // Set the year part of the end of Julian Calendar
- if (RegSetValueEx(happ, _T("JulianEndYear"), 0, REG_DWORD,
- (BYTE*)&Year, sizeof(LONG)) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
- // Finished with keys
- RegCloseKey(happ);
- }
- else
- bSuccess = FALSE;
-
- RegCloseKey(hcpl);
- }
- else
- bSuccess = FALSE;
-
- return bSuccess;
- }
-
- BOOL CDate::SetBeginGregorianCalendar(LONG Year, WORD Month, WORD Day)
- {
- //Update the static values in memory first
- sm_wBeginGregDay = Day;
- sm_wBeginGregMonth = Month;
- sm_lBeginGregYear = Year;
-
- //Broadcast a message to all top level windows that we are about to change
- //some internationl settings in the registry
- SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, NULL);
-
- //Settings are stored in the registry under the key HKEY_CURRENT_USER\Control Panel\DTime
- HKEY hcpl;
- BOOL bSuccess = TRUE;
-
- if (RegOpenKeyEx(HKEY_CURRENT_USER,
- _T("Control Panel"),
- 0,
- KEY_WRITE,
- &hcpl) == ERROR_SUCCESS)
- {
- HKEY happ;
- DWORD dwDisp;
- if (RegCreateKeyEx(hcpl, _T("DTime"), 0, _T(""), REG_OPTION_NON_VOLATILE,
- KEY_WRITE, NULL, &happ, &dwDisp) == ERROR_SUCCESS)
- {
- // Set the day part of the begin of the Gregorian Calendar
- DWORD dwDay = (DWORD) Day;
- if (RegSetValueEx(happ, _T("GregorianBeginDay"), 0, REG_DWORD,
- (BYTE*)&dwDay, sizeof(DWORD)) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
- // Set the month part of the begin of the Gregorian Calendar
- DWORD dwMonth = (DWORD) Month;
- if (RegSetValueEx(happ, _T("GregorianBeginMonth"), 0, REG_DWORD,
- (BYTE*)&dwMonth, sizeof(DWORD)) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
- // Set the year part of the begin of the Gregorian Calendar
- if (RegSetValueEx(happ, _T("GregorianBeginYear"), 0, REG_DWORD,
- (BYTE*)&Year, sizeof(LONG)) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
- // Finished with keys
- RegCloseKey(happ);
- }
- else
- bSuccess = FALSE;
-
- RegCloseKey(hcpl);
- }
- else
- bSuccess = FALSE;
-
- return bSuccess;
- }
-
- void CDate::GetEndJulianCalendar(LONG& Year, WORD& Month, WORD& Day)
- {
- Year = sm_lEndJulianYear;
- Month = sm_wEndJulianMonth;
- Day = sm_wEndJulianDay;
- }
-
- void CDate::GetBeginGregorianCalendar(LONG& Year, WORD& Month, WORD& Day)
- {
- Year = sm_lBeginGregYear;
- Month = sm_wBeginGregMonth;
- Day = sm_wBeginGregDay;
- }
-
- BOOL CDate::InitCalendarSettings()
- {
- BOOL bSuccess = TRUE;
-
- // Read the current state from the registry
- // Try opening the registry key:
- // HKEY_CURRENT_USER\Control Panel\DTime
- HKEY hCPL;
- if (RegOpenKeyEx(HKEY_CURRENT_USER,
- _T("Control Panel"),
- 0,
- KEY_QUERY_VALUE,
- &hCPL) == ERROR_SUCCESS)
- {
- HKEY hApp;
- if (RegOpenKeyEx(hCPL,
- _T("DTime"),
- 0,
- KEY_QUERY_VALUE,
- &hApp) == ERROR_SUCCESS)
- {
- // Yes we are installed
-
- //the day part of the gregorian begin date
- DWORD dwSize = sizeof(DWORD);
- DWORD dwDay;
- if (RegQueryValueEx(hApp, _T("GregorianBeginDay"), NULL,
- NULL, (BYTE*)&dwDay, &dwSize) != ERROR_SUCCESS)
- bSuccess = FALSE;
- else
- sm_wBeginGregDay = (WORD) dwDay;
-
- //the month part of the gregorian begin date
- dwSize = sizeof(DWORD);
- DWORD dwMonth;
- if (RegQueryValueEx(hApp, _T("GregorianBeginMonth"), NULL,
- NULL, (BYTE*)&dwMonth, &dwSize) != ERROR_SUCCESS)
- bSuccess = FALSE;
- else
- sm_wBeginGregMonth = (WORD) dwMonth;
-
- //the year part of the gregorian begin date
- dwSize = sizeof(LONG);
- if (RegQueryValueEx(hApp, _T("GregorianBeginYear"), NULL,
- NULL, (BYTE*)&sm_lBeginGregYear, &dwSize) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
-
- //the day part of the julian end date
- dwSize = sizeof(DWORD);
- if (RegQueryValueEx(hApp, _T("JulianEndDay"), NULL,
- NULL, (BYTE*)&dwDay, &dwSize) != ERROR_SUCCESS)
- bSuccess = FALSE;
- else
- sm_wEndJulianDay = (WORD) dwDay;
-
- //the month part of the julian end date
- dwSize = sizeof(DWORD);
- if (RegQueryValueEx(hApp, _T("JulianEndMonth"), NULL,
- NULL, (BYTE*)&dwMonth, &dwSize) != ERROR_SUCCESS)
- bSuccess = FALSE;
- else
- sm_wEndJulianMonth = (WORD) dwMonth;
-
- //the year part of the julian end date
- dwSize = sizeof(LONG);
- if (RegQueryValueEx(hApp, _T("JulianEndYear"), NULL,
- NULL, (BYTE*)&sm_lEndJulianYear, &dwSize) != ERROR_SUCCESS)
- bSuccess = FALSE;
-
-
- //Close the DTime Key
- RegCloseKey(hApp);
- }
- else
- bSuccess = FALSE;
-
- //Close the Control Panel Registry key
- RegCloseKey(hCPL);
- }
- else
- bSuccess = FALSE;
-
- return bSuccess;
- }
-
- LONG CDate::EndJulianYear()
- {
- LONG Year;
- WORD Month;
- WORD Day;
- GetEndJulianCalendar(Year, Month, Day);
- return Year;
- }
-
- WORD CDate::EndJulianMonth()
- {
- LONG Year;
- WORD Month;
- WORD Day;
- GetEndJulianCalendar(Year, Month, Day);
- return Month;
- }
-
- WORD CDate::EndJulianDay()
- {
- LONG Year;
- WORD Month;
- WORD Day;
- GetEndJulianCalendar(Year, Month, Day);
- return Day;
- }
-
- LONG CDate::BeginGregorianYear()
- {
- LONG Year;
- WORD Month;
- WORD Day;
- GetBeginGregorianCalendar(Year, Month, Day);
- return Year;
- }
-
- WORD CDate::BeginGregorianMonth()
- {
- LONG Year;
- WORD Month;
- WORD Day;
- GetBeginGregorianCalendar(Year, Month, Day);
- return Month;
- }
-
- WORD CDate::BeginGregorianDay()
- {
- LONG Year;
- WORD Month;
- WORD Day;
- GetBeginGregorianCalendar(Year, Month, Day);
- return Day;
- }
-
- BOOL CDate::InGregorianCalendar(LONG Year, WORD Month, WORD Day)
- {
- if (Year == BeginGregorianYear())
- {
- if (Month == BeginGregorianMonth())
- return (Day >= BeginGregorianDay());
- else
- return (Month > BeginGregorianMonth());
- }
- else
- return (Year > BeginGregorianYear());
- }
-
- BOOL CDate::InJulianCalendar(LONG Year, WORD Month, WORD Day)
- {
- if (Year == EndJulianYear())
- {
- if (Month == EndJulianMonth())
- return (Day < EndJulianDay());
- else
- return (Month < EndJulianMonth());
- }
- else
- return (Year < EndJulianYear());
- }
-
-
- #ifdef _DEBUG
- BOOL CDate::SetDoConstructorAsserts(BOOL bDoAsserts)
- {
- BOOL bOldDoAsserts = sm_bDoAsserts;
- sm_bDoAsserts = bDoAsserts;
- return bOldDoAsserts;
- }
- #endif
-
- CString CDate::GetFullStringDayOfWeek(WORD DayOfWeek)
- {
- CString rVal;
- LPTSTR pszBuf = rVal.GetBuffer(100);
- if (DayOfWeek > 0 && DayOfWeek <= 7)
- {
- int nChars;
- switch (DayOfWeek)
- {
- case MONDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME1, pszBuf, 100); break;
- case TUESDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME2, pszBuf, 100); break;
- case WEDNESDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME3, pszBuf, 100); break;
- case THURSDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME4, pszBuf, 100); break;
- case FRIDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME5, pszBuf, 100); break;
- case SATURDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME6, pszBuf, 100); break;
- case SUNDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDAYNAME7, pszBuf, 100); break;
- default: ASSERT(FALSE); break;
- }
- ASSERT(nChars);
- }
- else
- ASSERT(FALSE);
- rVal.ReleaseBuffer();
- return rVal;
- }
-
- CString CDate::GetAbrStringDayOfWeek(WORD DayOfWeek)
- {
- CString rVal;
- LPTSTR pszBuf = rVal.GetBuffer(100);
- if (DayOfWeek > 0 && DayOfWeek <= 7)
- {
- int nChars;
- switch (DayOfWeek)
- {
- case MONDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME1, pszBuf, 100); break;
- case TUESDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME2, pszBuf, 100); break;
- case WEDNESDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME3, pszBuf, 100); break;
- case THURSDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME4, pszBuf, 100); break;
- case FRIDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME5, pszBuf, 100); break;
- case SATURDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME6, pszBuf, 100); break;
- case SUNDAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVDAYNAME7, pszBuf, 100); break;
- default: ASSERT(FALSE); break;
- }
- ASSERT(nChars);
- }
- else
- ASSERT(FALSE);
- rVal.ReleaseBuffer();
- return rVal;
- }
-
- CString CDate::GetFullStringMonth(WORD Month)
- {
- CString rVal;
- LPTSTR pszBuf = rVal.GetBuffer(100);
- if (Month > 0 && Month <= 12)
- {
- int nChars;
- switch (Month)
- {
- case JANUARY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME1, pszBuf, 100); break;
- case FEBRUARY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME2, pszBuf, 100); break;
- case MARCH: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME3, pszBuf, 100); break;
- case APRIL: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME4, pszBuf, 100); break;
- case MAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME5, pszBuf, 100); break;
- case JUNE: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME6, pszBuf, 100); break;
- case JULY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME7, pszBuf, 100); break;
- case AUGUST: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME8, pszBuf, 100); break;
- case SEPTEMBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME9, pszBuf, 100); break;
- case OCTOBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME10, pszBuf, 100); break;
- case NOVEMBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME11, pszBuf, 100); break;
- case DECEMBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SMONTHNAME12, pszBuf, 100); break;
- default: ASSERT(FALSE); break;
- }
- ASSERT(nChars);
- }
- else
- ASSERT(FALSE);
- rVal.ReleaseBuffer();
- return rVal;
- }
-
- CString CDate::GetAbrStringMonth(WORD Month)
- {
- CString rVal;
- LPTSTR pszBuf = rVal.GetBuffer(100);
- if (Month > 0 && Month <= 12)
- {
- int nChars;
- switch (Month)
- {
- case JANUARY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME1, pszBuf, 100); break;
- case FEBRUARY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME2, pszBuf, 100); break;
- case MARCH: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME3, pszBuf, 100); break;
- case APRIL: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME4, pszBuf, 100); break;
- case MAY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME5, pszBuf, 100); break;
- case JUNE: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME6, pszBuf, 100); break;
- case JULY: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME7, pszBuf, 100); break;
- case AUGUST: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME8, pszBuf, 100); break;
- case SEPTEMBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME9, pszBuf, 100); break;
- case OCTOBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME10, pszBuf, 100); break;
- case NOVEMBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME11, pszBuf, 100); break;
- case DECEMBER: nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SABBREVMONTHNAME12, pszBuf, 100); break;
- default: ASSERT(FALSE); break;
- }
- ASSERT(nChars);
- }
- else
- ASSERT(FALSE);
- rVal.ReleaseBuffer();
- return rVal;
- }
-
- CDate CDate::FirstCurrentMonth()
- {
- CDate c = CDate::CurrentDate();
- return CDate(c.GetYear(), c.GetMonth(), 1);
- }
-
- CDate CDate::LastCurrentMonth()
- {
- CDate c = CDate::CurrentDate();
- return CDate(c.GetYear(), c.GetMonth(), c.DaysInMonth());
- }
-
- CDate CDate::FirstCurrentYear()
- {
- CDate c = CDate::CurrentDate();
- return CDate(c.GetYear(), 1, 1);
- }
-
- CDate CDate::LastCurrentYear()
- {
- CDate c = CDate::CurrentDate();
- return CDate(c.GetYear(), 12, CDate::DaysInMonth(12, c.IsLeap()));
- }
-
- CDate CDate::JDEpoch()
- {
- return CDate(0, EPOCH_JD);
- }
-
- CDate CDate::MJDEpoch()
- {
- return CDate(0, EPOCH_MJD);
- }
-
- CDate CDate::Epoch1900()
- {
- return CDate(0, EPOCH_1900);
- }
-
- CDate CDate::Epoch1950()
- {
- return CDate(0, EPOCH_1950);
- }
-
- CDate CDate::EpochCTime()
- {
- return CDate(0, EPOCH_CTIME);
- }
-
- CDate CDate::Epoch2000()
- {
- return CDate(0, EPOCH_2000);
- }
-
- CDate CDate::GregorianEpoch()
- {
- return CDate(0, EPOCH_GREG);
- }
-
- CDate CDate::NewYearsDay(LONG Year)
- {
- return CDate(Year, JANUARY, 1);
- }
-
- CDate CDate::ValentinesDay(LONG Year)
- {
- return CDate(Year, FEBRUARY, 14);
- }
-
- CDate CDate::AshWednesday(LONG Year)
- {
- return CDate::EasterSunday(Year) - 39L; //Ash Wednesday occurs 39 days before Easter Sunday
- }
-
- CDate CDate::StPatricksDay(LONG Year)
- {
- return CDate(Year, MARCH, 17);
- }
-
- CDate CDate::GoodFriday(LONG Year)
- {
- return CDate::EasterSunday(Year) - 2L; //Good Friday occurs 2 days before Easter Sunday
- }
-
- CDate CDate::EasterSunday(LONG Year)
- {
- //The following method has been taken from the book "Astronimical Algorithms" by Jean Meeus
- //which was taken in turn from a book "General Astronomy" by Spencer Jones. It has been
- //published again in the "Journal of the British Astronomical Association, Vol 88" where
- //it is said that it was devised in 1876 and appeared in Butcher's "Ecclesiastical Calendar"
-
- if (InGregorianCalendar(Year, 4, 1)) //in the Gregorain Calendar, Will possibly give results if Calendar
- { //switches from Julian to Gregorian in March or April because then
- int a = (int) (Year % 19); //the year may be considered Gregorian or Julian
- int b = (int) (Year / 100);
- int c = (int) (Year % 100);
- int d = b / 4;
- int e = b % 4;
- int f = (b+8) / 25;
- int g = (b - f + 1) / 3;
- int h = (19*a + b - d - g + 15) % 30;
- int i = c / 4;
- int k = c % 4;
- int l = (32 + 2*e + 2*i - h -k) % 7;
- int m = (a + 11*h +22*l) / 451;
- int n = (h + l - 7*m + 114) / 31;
- int p = (h + l - 7*m + 114) % 31;
- return CDate(Year, WORD(n), WORD(p+1));
- }
- else //in the Julian Calendar
- {
- int a = (int) (Year % 4);
- int b = (int) (Year % 7);
- int c = (int) (Year % 19);
- int d = (19*c + 15) % 30;
- int e = (2*a + 4*b - d + 34) % 7;
- int f = (d + e + 114) / 31;
- int g = (d + e + 114) % 31;
- return CDate(Year, (WORD) f, (WORD) (g + 1));
- }
- }
-
- CDate CDate::CanadaDay(LONG Year)
- {
- return CDate(Year, JULY, 1);
- }
-
- CDate CDate::IndependenceDay(LONG Year)
- {
- return CDate(Year, JULY, 4);
- }
-
- CDate CDate::BastilleDay(LONG Year)
- {
- return CDate(Year, JULY, 14);
- }
-
- CDate CDate::ChristmasDay(LONG Year)
- {
- return CDate(Year, DECEMBER, 25);
- }
-
- BOOL CDate::IsLeap(LONG Year)
- {
- if (InGregorianCalendar(Year, 1, 1)) //Will give incorrect results if Calendar switches
- { //from Julian to Gregorian in the Year 1600 (if any Country did)
- if ((Year % 100) == 0)
- return ((Year % 400) == 0) ? TRUE : FALSE;
- else
- return ((Year % 4) == 0) ? TRUE : FALSE;
- }
- else
- return ((Year % 4) == 0) ? TRUE : FALSE;
-
- }
-
- WORD CDate::DaysInYear(LONG Year)
- {
- return IsLeap(Year) ? ((WORD) 366) : ((WORD) 365);
- }
-
- WORD CDate::DaysInMonth(WORD Month, BOOL IsLeap)
- {
- SetFebLength(IsLeap);
- ASSERT(Month >= 1 && Month <= 12);
- WORD r = MonthLength[Month];
- ResetFebLength();
- return r;
- }
-
- WORD CDate::DaysSinceJan1(WORD Month, WORD Day, BOOL IsLeap)
- {
- return (WORD) (DaysSinceJan0(Month, Day, IsLeap) - 1);
- }
-
-
- WORD CDate::DaysSinceJan0(WORD Month, WORD Day, BOOL IsLeap)
- {
- ASSERT(Month >= JANUARY && Month <= DECEMBER);
- ASSERT(Day <= DaysInMonth(Month, IsLeap) && Day >= 1);
- int k = IsLeap ? 1 : 2;
- WORD rVal = (WORD) ((int(275*Month/9)) - (k*int((Month+9)/12)) + Day - 30);
- return rVal;
- }
-
- WORD CDate::DaysSinceJan1() const
- {
- AssertValid();
- return (WORD) (*this - CDate(GetYear(), 1, 1));
- }
-
- WORD CDate::DaysSinceJan0() const
- {
- AssertValid();
- return (WORD) (DaysSinceJan1() + 1);
- }
-
- DateS CDate::GetDate() const
- {
- //See the comment in CDate::Set(LONG Year, WORD Month, WORD Day)
- //for references to where the algorithm is taken from
-
- AssertValid();
- BOOL bIsGreg = InGregorianCalendar();
- BOOL bIsJulian = !bIsGreg;
- DateS ds;
- //LONG gdn = m_lDays + 577735L - 2000000000L + 2*bIsJulian;
- LONG gdn = m_lDays - 1999422265L + 2*bIsJulian;
- LONG y4 = 1461L;
- LONG y400 = 146100L - 3*bIsGreg;
- LONG y100 = 36525L - bIsGreg;
- BOOL exception=FALSE;
- ds.lYear = 400*lfloor(gdn, y400);
- gdn -= y400*lfloor(gdn, y400); //400 year periods
- if (gdn > 0L)
- {
- ds.lYear += 100*lfloor(gdn, y100); //100 year periods
- gdn -= y100*lfloor(gdn, y100);
- exception = (gdn == 0L && bIsGreg);
- if (gdn > 0L)
- {
- ds.lYear += 4*lfloor(gdn, y4); //4 year periods
- gdn -= y4*lfloor(gdn, y4);
- if (gdn > 0L)
- {
- int i=0;
- while (gdn > 365 && ++i < 4)
- {
- ds.lYear++;
- gdn -= 365L;
- }
- }
- }
- }
- if (exception)
- gdn = 366L; //occurs once every hundred years with Gregorian calendar
- else
- {
- ds.lYear++;
- gdn++;
- }
- SetFebLength(IsLeap(ds.lYear));
- ds.wMonth = 1;
- while (ds.wMonth < 13 && gdn > ((LONG) MonthLength[ds.wMonth]))
- gdn -= MonthLength[ds.wMonth++];
- if (ds.wMonth == 13)
- {
- ds.wMonth = 1;
- ds.lYear++;
- }
- ResetFebLength();
- ds.wDay = (WORD) gdn;
- ds.wWday = (WORD) (WeekOfDayModulo(m_lDays, 7) + 1);
- ds.wYday = DaysSinceJan0(ds.wMonth, ds.wDay, IsLeap(ds.lYear));
-
- return ds;
- }
-
- WORD CDate::GetDay() const
- {
- AssertValid();
- return GetDate().wDay;
- }
-
- WORD CDate::GetMonth() const
- {
- AssertValid();
- return GetDate().wMonth;
- }
-
- LONG CDate::GetYear() const
- {
- AssertValid();
- return GetDate().lYear;
- }
-
- LONG CDate::GetCEBCEYear(BOOL& IsCE) const
- {
- AssertValid();
- LONG Year = GetDate().lYear;
-
- if (Year > 0)
- {
- IsCE = TRUE;
- return Year;
- }
- else
- {
- IsCE = FALSE;
- return labs(GetYear() - 1);
- }
- }
-
- WORD CDate::Get2DigitYear() const
- {
- AssertValid();
- LONG Year = labs(GetYear());
- return (WORD) (Year - ((Year/100)*100));
- }
-
-
- WORD CDate::GetWeekOfYear() const
- {
- AssertValid();
- CDate FirstOfYear(GetYear(), JANUARY, 1, GetBeginingDayOfWeek());
- LONG diff = *this - FirstOfYear;
- if (diff < 0)
- return 0;
- else
- return (WORD) (diff/7 + 1);
- }
-
-
- WORD CDate::GetWeekOfMonth() const
- {
- AssertValid();
- CDate FirstOfMonth(GetYear(), GetMonth(), 1, GetBeginingDayOfWeek());
- LONG diff = *this - FirstOfMonth;
- if (diff < 0)
- return 0;
- else
- return (WORD) (diff/7 + 1);
- }
-
- LONG CDate::Since1900Epoch() const
- {
- AssertValid();
- return *this - CDate(1900, 1, 1);
- }
-
- LONG CDate::Since1950Epoch() const
- {
- AssertValid();
- return *this - CDate(1950, 1, 1);
- }
-
- LONG CDate::SinceCTimeEpoch() const
- {
- AssertValid();
- return *this - CDate(1970, 1, 1);
- }
-
- LONG CDate::Since2000Epoch() const
- {
- AssertValid();
- return *this - CDate(2000, 1, 1);
- }
-
- LONG CDate::JD() const
- {
- AssertValid();
- return *this - JDEpoch();
- }
-
- LONG CDate::GDN() const
- {
- AssertValid();
- return m_lDays - 2000000000L;
- }
-
- LONG CDate::GetValue() const
- {
- AssertValid();
- return m_lDays;
- }
-
- BOOL CDate::IsLeap() const
- {
- AssertValid();
- return IsLeap(GetYear());
- }
-
- WORD CDate::DaysInYear() const
- {
- AssertValid();
- return DaysInYear(GetYear());
- }
-
- WORD CDate::DaysInMonth() const
- {
- AssertValid();
- return DaysInMonth(GetMonth(), IsLeap());
- }
-
- void CDate::AddYear(int Years)
- {
- AssertValid();
- LONG Months = Years*12;
- if (Months >= INT_MIN && Months <= INT_MAX)
- AddMonth((int) Months);
- else
- {
- ASSERT(FALSE);
- Set();
- }
- }
-
- void CDate::AddMonth(int Months)
- {
- AssertValid();
- LONG NewMonth = (LONG) GetMonth();
- LONG NewYear = GetYear();
- NewMonth += Months;
- if ((NewMonth > DECEMBER) || (NewMonth < JANUARY))
- {
- NewYear += NewMonth / 12;
- NewMonth = NewMonth % 12;
- }
- if (NewMonth < JANUARY)
- {
- --NewYear;
- NewMonth += 12;
- }
-
- WORD Day = GetDay();
-
- WORD MaxDays = DaysInMonth((WORD) NewMonth, IsLeap(NewYear));
- if (Day > MaxDays)
- Day = MaxDays;
- Set(NewYear, (WORD) NewMonth, Day);
- }
-
- void CDate::AddWeek(int Weeks)
- {
- AssertValid();
- *this += 7L*((LONG)Weeks);
- }
-
- WORD CDate::GetDayOfWeek() const
- {
- AssertValid();
- return (WORD) (WeekOfDayModulo(m_lDays, 7) + 1);
- }
-
- CString CDate::GetStringCEBCEYear() const
- {
- AssertValid();
- BOOL bIsCEYear;
- LONG lCEYear = GetCEBCEYear(bIsCEYear);
- CString sYear;
- sYear.Format(_T("%ld"), lCEYear);
- CString rVal;
- if (bIsCEYear)
- AfxFormatString1(rVal, IDS_CEYEAR, sYear);
- else
- AfxFormatString1(rVal, IDS_BCEYEAR, sYear);
- return rVal;
- }
-
- CString CDate::GetFullStringDayOfWeek() const
- {
- AssertValid();
- return GetFullStringDayOfWeek(GetDayOfWeek());
- }
-
- CString CDate::GetAbrStringDayOfWeek() const
- {
- AssertValid();
- return GetAbrStringDayOfWeek(GetDayOfWeek());
- }
-
- CString CDate::GetFullStringMonth() const
- {
- AssertValid();
- return GetFullStringMonth(GetMonth());
- }
-
- CString CDate::GetAbrStringMonth() const
- {
- AssertValid();
- return GetAbrStringMonth(GetMonth());
- }
-
- SYSTEMTIME CDate::GetSYSTEMTIME() const
- {
- AssertValid();
- SYSTEMTIME s;
- s.wYear = 0;
- s.wMonth = 0;
- s.wDayOfWeek = 0;
- s.wDay = 0;
- s.wHour = 0;
- s.wMinute = 0;
- s.wSecond = 0;
- s.wMilliseconds = 0;
-
- DateS ds = GetDate();
-
- //handle range errors
- if ((ds.lYear < 0) || (ds.lYear > USHRT_MAX))
- {
- ASSERT(FALSE);
- s.wYear = 0;
- return s;
- }
-
- s.wYear = (WORD) ds.lYear;
- s.wMonth = ds.wMonth;
- s.wDayOfWeek = (WORD) (ds.wWday - 1); //SYSTEMTIME uses 0 based indices
- s.wDay = ds.wDay;
-
- return s;
- }
-
- tm CDate::GetTM() const
- {
- AssertValid();
- tm rVal;
-
- rVal.tm_sec = 0;
- rVal.tm_min = 0;
- rVal.tm_hour = 0;
- rVal.tm_mday = 0;
- rVal.tm_mon = 0;
- rVal.tm_year = 0;
- rVal.tm_wday = 0;
- rVal.tm_yday = 0;
- rVal.tm_isdst = 0;
-
- DateS ds = GetDate();
-
- long MinYear = 1900L + LONG_MIN;
- long MaxYear = LONG_MAX;
-
- //handle range errors
- if ((ds.lYear < MinYear) || (ds.lYear > MaxYear))
- {
- ASSERT(FALSE);
- return rVal;
- }
-
- rVal.tm_year = (int) (ds.lYear - 1900L);
- rVal.tm_mon = ds.wMonth - 1; //tm struct uses 0 based indices
- rVal.tm_wday = ds.wWday - 1; //tm struct uses 0 based indices
- rVal.tm_mday = ds.wDay;
- rVal.tm_wday = ds.wYday - 1; //Returns days since Jan 1
-
- return rVal;
- }
-
- BOOL CDate::IsValid() const
- {
- return m_bValid;
- }
-
- LONG CDate::Collate() const
- {
- AssertValid();
- DateS s;
- s = GetDate();
- return (s.lYear*10000) + (s.wMonth*100) + s.wDay;
- }
-
- BOOL CDate::InGregorianCalendar() const
- {
- AssertValid();
- return m_bInGregCalendar;
- }
-
- BOOL CDate::InJulianCalendar() const
- {
- AssertValid();
- return !m_bInGregCalendar;
- }
-
- CDate CDate::FirstThisMonth() const
- {
- AssertValid();
- DateS s=GetDate();
- return CDate(s.lYear, s.wMonth, 1);
- }
-
- CDate CDate::LastThisMonth() const
- {
- AssertValid();
- DateS s = GetDate();
- return CDate(s.lYear, s.wMonth, DaysInMonth(s.wMonth, IsLeap(s.lYear)));
- }
-
- CDate CDate::FirstThisYear() const
- {
- AssertValid();
- DateS s = GetDate();
- CDate d(s.lYear, JANUARY, 1);
- return d;
- }
-
- CDate CDate::LastThisYear() const
- {
- AssertValid();
- DateS s = GetDate();
- CDate d(s.lYear, DECEMBER, 1);
- return d;
- }
-
- CDate& CDate::operator=(const CDate& d)
- {
- m_lDays = d.m_lDays;
- m_bInGregCalendar = d.m_bInGregCalendar;
- m_bValid = d.m_bValid;
- return *this;
- }
-
- CDate CDate::operator+(LONG Days) const
- {
- AssertValid();
- return CDate(GDN() + Days, EPOCH_GREG);
- }
-
- LONG CDate::operator-(const CDate& d) const
- {
- AssertValid();
- return m_lDays - d.m_lDays;
- }
-
- CDate CDate::operator-(LONG Days) const
- {
- AssertValid();
- return CDate(GDN() - Days, EPOCH_GREG);
- }
-
- CDate& CDate::operator+=(LONG Days)
- {
- AssertValid();
- m_lDays += Days;
- return *this;
- }
-
- CDate& CDate::operator-=(LONG Days)
- {
- AssertValid();
- m_lDays -= Days;
- return *this;
- }
-
- CDate& CDate::operator++()
- {
- AssertValid();
- m_lDays++;
- return *this;
- }
-
- CDate& CDate::operator--()
- {
- AssertValid();
- m_lDays--;
- return *this;
- }
-
- BOOL CDate::operator==(const CDate& d) const
- {
- AssertValid();
- return m_lDays == d.m_lDays;
- }
-
- BOOL CDate::operator>(const CDate& d) const
- {
- AssertValid();
- return m_lDays > d.m_lDays;
- }
-
- BOOL CDate::operator>=(const CDate& d) const
- {
- AssertValid();
- return m_lDays >= d.m_lDays;
- }
-
- BOOL CDate::operator<(const CDate& d) const
- {
- AssertValid();
- return m_lDays < d.m_lDays;
- }
-
- BOOL CDate::operator<=(const CDate& d) const
- {
- AssertValid();
- return m_lDays <= d.m_lDays;
- }
-
- BOOL CDate::operator!=(const CDate& d) const
- {
- AssertValid();
- return m_lDays != d.m_lDays;
- }
-
- #ifdef _DEBUG
- void CDate::AssertValid() const
- {
- CObject::AssertValid();
- ASSERT(IsValid());
- }
- #endif
-
- #ifdef _DEBUG
- void CDate::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- if (IsValid())
- dc << Format() << _T("\n");
- else
- dc << _T("Invalid state\n");
- }
- #endif
-
- /* //The Following Format parameters are supported
-
- %a Abbreviated weekday name
- %A Full weekday name
- %b Abbreviated month name
- %B Full month name
- %d Day of month as decimal number (01 - 31)
- %j Day of year as decimal number (001 - 366)
- %m Month as decimal number (01 - 12)
- %U Week of year as decimal number
- %w Weekday as decimal number (1 - 7; Sunday is 1)
- %x Short date representation, appropriate to current locale
- %y Year without century, as decimal number (00 - 99)
- %Y Year with century, as decimal number
- %c Year displayed using C.E.(Current Epoch) / B.C.E (Before Current Epoch) convention e.g. -1023 = 1022 BCE
- %#x Long date representation, appropriate to current locale
- %#d, %#j, %#m, %#U, %#y Remove leading zeros (if any)
- */
- CString CDate::Format(const CString& sFormat) const
- {
- CString rVal;
- if (IsValid())
- {
- CString sBuffer;
- int sFmtLength = sFormat.GetLength();
-
- for (int i=0; i<sFmtLength; i++)
- {
- TCHAR c = sFormat.GetAt(i);
- if (c == _T('%'))
- {
- ++i;
- if (i < sFmtLength)
- {
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('a'):
- {
- rVal += GetAbrStringDayOfWeek();
- break;
- }
- case _T('A'):
- {
- rVal += GetFullStringDayOfWeek();
- break;
- }
- case _T('b'):
- {
- rVal += GetAbrStringMonth();
- break;
- }
- case _T('c'):
- {
- rVal += GetStringCEBCEYear();
- break;
- }
- case _T('B'):
- {
- rVal += GetFullStringMonth();
- break;
- }
- case _T('d'):
- {
- sBuffer.Format(_T("%.02d"), GetDay());
- rVal += sBuffer;
- break;
- }
- case _T('j'):
- {
- sBuffer.Format(_T("%.03d"), DaysSinceJan0());
- rVal += sBuffer;
- break;
- }
- case _T('m'):
- {
- sBuffer.Format(_T("%.02d"), GetMonth());
- rVal += sBuffer;
- break;
- }
- case _T('U'):
- {
- sBuffer.Format(_T("%.02d"), GetWeekOfYear());
- rVal += sBuffer;
- break;
- }
- case _T('w'):
- {
- sBuffer.Format(_T("%d"), GetDayOfWeek());
- rVal += sBuffer;
- break;
- }
- case _T('x'):
- {
- sBuffer.Empty();
- SYSTEMTIME st = GetSYSTEMTIME();
- int Res = ::GetDateFormat(LOCALE_USER_DEFAULT,
- DATE_SHORTDATE,
- &st,
- NULL,
- sBuffer.GetBufferSetLength(100),
- 100);
- if (Res == 0)
- TRACE1("GetDateFormat() Failed in CDate::Format(), GetLastError() returned %ul\n", ::GetLastError());
- sBuffer.ReleaseBuffer();
- rVal += sBuffer;
- break;
- }
- case _T('y'):
- {
- LONG Year = GetYear();
- sBuffer.Format(_T("%.02d"), Get2DigitYear());
- rVal += sBuffer;
- break;
- }
- case _T('Y'):
- {
- sBuffer.Format(_T("%ld"), GetYear());
- rVal += sBuffer;
- break;
- }
- case _T('#'):
- {
- if (i < sFmtLength)
- {
- ++i;
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('d'):
- {
- sBuffer.Format(_T("%d"), GetDay());
- rVal += sBuffer;
- break;
- }
- case _T('j'):
- {
- sBuffer.Format(_T("%d"), DaysSinceJan0());
- rVal += sBuffer;
- break;
- }
- case _T('m'):
- {
- sBuffer.Format(_T("%d"), GetMonth());
- rVal += sBuffer;
- break;
- }
- case _T('U'):
- {
- sBuffer.Format(_T("%d"), GetWeekOfYear());
- rVal += sBuffer;
- break;
- }
- case _T('x'):
- {
- sBuffer.Empty();
- SYSTEMTIME st = GetSYSTEMTIME();
- int Res = ::GetDateFormat(LOCALE_USER_DEFAULT,
- DATE_LONGDATE,
- &st,
- NULL,
- sBuffer.GetBufferSetLength(100),
- 100);
- if (Res == 0)
- TRACE1("GetDateFormat() Failed in CDate::Format(), GetLastError() returned %ul\n", ::GetLastError());
-
- sBuffer.ReleaseBuffer();
- rVal += sBuffer;
- break;
- }
- case _T('y'):
- {
- LONG Year = GetYear();
- sBuffer.Format(_T("%d"), Get2DigitYear());
- rVal += sBuffer;
- break;
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- }
- break;
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- }
- }
- else
- {
- rVal += c;
- }
- }
- }
-
- return rVal;
- }
-
- //Format according to the short date format as specified by the control panel
- CString CDate::Format() const
- {
- return Format(sm_sDefaultFormat);
- }
-
- void CDate::GetDefaultFormat()
- {
- CString sBuf;
-
- // Is there to be a leading 0 on the day?
- BOOL bDayLZ = FALSE;
- LPTSTR pszBuf = sBuf.GetBuffer(10);
- int nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDAYLZERO, pszBuf, 10);
- ASSERT(nChars);
- sBuf.ReleaseBuffer();
- if (sBuf == _T("0")) //No leading zeros for days
- bDayLZ = FALSE;
- else if (sBuf == _T("1")) //Leading zeros for days
- bDayLZ = TRUE;
- else
- ASSERT(FALSE);
-
- // Is there to be a leading 0 on the month?
- BOOL bMoLZ = TRUE;
- pszBuf = sBuf.GetBuffer(10);
- nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IMONLZERO, pszBuf, 10);
- ASSERT(nChars);
- sBuf.ReleaseBuffer();
- if (sBuf == _T("0")) //No leading zeros for months
- bMoLZ = FALSE;
- else if (sBuf == _T("1")) //Leading zeros for months
- bMoLZ = TRUE;
- else
- ASSERT(FALSE);
-
-
- // Find if only 2 digits are to be displayed for the year
- BOOL b2DigitYear = FALSE;
-
- //Because DTime can allow years to be much larger than 4 digits in size,
- //DTime always explicitly shows all the year digits, instead of looking
- //at the LOCALE_ICENTURY LCTYPE, but commented out below is the code
- //that would be required for those interested
-
- /*
- pszBuf = sBuf.GetBuffer(10);
- nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICENTURY, pszBuf, 10);
- ASSERT(nChars);
- sBuf.ReleaseBuffer();
- if (sBuf == _T("0")) //Abbreviated 2-digit century
- b2DigitYear = TRUE;
- else if (sBuf == _T("1")) //Full century
- b2DigitYear = FALSE;
- else
- ASSERT(FALSE);
- */
-
-
- // Get the date separator character
- CString sSep(_T("/"));
- pszBuf = sSep.GetBuffer(10);
- nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, pszBuf, 10);
- ASSERT(nChars);
- sSep.ReleaseBuffer();
-
-
- //Retrieve the short date formatting string
- pszBuf = sBuf.GetBuffer(10);
- nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, pszBuf, 10);
- ASSERT(nChars);
- sBuf.ReleaseBuffer();
-
-
- //Format according to the short date formatting string //THESE CODES NEED TO BE CHECKED
- if (sBuf == _T("0")) //MDY format
- {
- if (b2DigitYear)
- sm_sDefaultFormat.Format(_T("%s%s%s%s%%y"), bMoLZ ? _T("%m") : _T("%#m"), sSep, bDayLZ ? _T("%d") : _T("%#d"), sSep);
- else
- sm_sDefaultFormat.Format(_T("%s%s%s%s%%Y"), bMoLZ ? _T("%m") : _T("%#m"), sSep, bDayLZ ? _T("%d") : _T("%#d"), sSep);
- }
- else if (sBuf == _T("1")) //DMY format
- {
- if (b2DigitYear)
- sm_sDefaultFormat.Format(_T("%s%s%s%s%%y"), bDayLZ ? _T("%d") : _T("%#d"), sSep, bMoLZ ? _T("%m") : _T("%#m"), sSep);
- else
- sm_sDefaultFormat.Format(_T("%s%s%s%s%%Y"), bDayLZ ? _T("%d") : _T("%#d"), sSep, bMoLZ ? _T("%m") : _T("%#m"), sSep);
- }
- else
- {
- if (sBuf != _T("2")) //No Recognized format available
- ASSERT(FALSE);
-
- //YMD Format
- if (b2DigitYear)
- sm_sDefaultFormat.Format(_T("%%y%s%s%s%s"), sSep, bMoLZ ? _T("%m") : _T("%#m"), sSep, bDayLZ ? _T("%d") : _T("%#d"));
- else
- sm_sDefaultFormat.Format(_T("%%Y%s%s%s%s"), sSep, bMoLZ ? _T("%m") : _T("%#m"), sSep, bDayLZ ? _T("%d") : _T("%#d"));
- }
- }
-
-
- void CDate::Serialize(CArchive& ar)
- {
- CObject::Serialize(ar);
-
- if (ar.IsStoring())
- {
- ar << m_lDays;
- ar << (WORD) m_bInGregCalendar;
- ar << m_bValid;
- }
- else
- {
- ar >> m_lDays;
- ar >> (WORD&) m_bInGregCalendar;
- ar >> m_bValid;
- }
- }
-
- ostream& operator<<(ostream& os, const CDate& date)
- {
- return os << date.Format();
- }
-
- CArchive& operator<<(CArchive& ar, CDate& date)
- {
- ASSERT(ar.IsStoring());
- date.Serialize(ar);
- return ar;
- }
-
- CArchive& operator>>(CArchive& ar, CDate& date)
- {
- ASSERT(!ar.IsStoring());
- date.Serialize(ar);
- return ar;
- }
-
-
-
-
-
-
-
- //CLTimeSpan Implementation
-
-
- CLTimeSpan::CLTimeSpan()
- {
- Set();
- }
-
- CLTimeSpan::CLTimeSpan(LONG Day, WORD Hour, WORD Minute,
- WORD Second)
- {
- Set(Day, Hour, Minute, Second);
- }
-
- CLTimeSpan::CLTimeSpan(const CLTimeSpan& lts)
- {
- m_nSeconds = lts.m_nSeconds;
- m_bValid = lts.m_bValid;
- }
-
- CLTimeSpan::CLTimeSpan(const CTimeSpan& ts)
- {
- Set(ts);
- }
-
- CLTimeSpan::CLTimeSpan(const CLTimeOfDay& tod)
- {
- Set(tod);
- }
-
- CLTimeSpan::CLTimeSpan(const COleDateTimeSpan& oleTimeSpan)
- {
- Set(oleTimeSpan);
- }
-
- CLTimeSpan::CLTimeSpan(const __int64& Seconds)
- {
- Set(Seconds);
- }
-
- CLTimeSpan& CLTimeSpan::Set()
- {
- m_nSeconds = 0;
- m_bValid = FALSE;
- return *this;
- }
-
- CLTimeSpan& CLTimeSpan::Set(LONG Day, WORD Hour, WORD Minute,
- WORD Second)
- {
- if (Day >= 0)
- m_nSeconds = __int64(Day)*86400L + __int64(Hour)*3600L + __int64(Minute)*60L + Second;
- else
- m_nSeconds = __int64(Day)*86400L - __int64(Hour)*3600L - __int64(Minute)*60L - Second;
- m_bValid = TRUE;
- return *this;
- }
-
- CLTimeSpan& CLTimeSpan::Set(const CTimeSpan& ts)
- {
- m_nSeconds = ts.GetTotalSeconds();
- m_bValid = TRUE;
- return *this;
- }
-
- CLTimeSpan& CLTimeSpan::Set(const CLTimeOfDay& tod)
- {
- Set(0, tod.GetHour(), tod.GetMinute(), tod.GetSecond());
- return *this;
- }
-
- CLTimeSpan& CLTimeSpan::Set(const COleDateTimeSpan& oleTimeSpan)
- {
- COleDateTimeSpan::DateTimeSpanStatus dtss(oleTimeSpan.GetStatus());
- CLTimeSpan rVal;
- if (dtss == COleDateTimeSpan::DateTimeSpanStatus::valid)
- Set(oleTimeSpan.GetTotalSeconds());
- else
- Set();
- return *this;
- }
-
- CLTimeSpan& CLTimeSpan::Set(const __int64& Seconds)
- {
- m_nSeconds = Seconds;
- m_bValid = TRUE;
- return *this;
- }
-
- CLTimeSpan CLTimeSpan::OneCivilYear()
- {
- return CLTimeSpan(365, 6, 0, 0);
- }
-
- CLTimeSpan CLTimeSpan::OneDay()
- {
- return CLTimeSpan(1, 0, 0, 0);
- }
-
- CLTimeSpan CLTimeSpan::OneHour()
- {
- return CLTimeSpan(0, 1, 0, 0);
- }
-
- CLTimeSpan CLTimeSpan::OneMinute()
- {
- return CLTimeSpan(0, 0, 1, 0);
- }
-
- CLTimeSpan CLTimeSpan::OneSecond()
- {
- return CLTimeSpan(0, 0, 0, 1);
- }
-
- #ifdef _DEBUG
- BOOL CLTimeSpan::SetDoConstructorAsserts(BOOL bDoAsserts)
- {
- BOOL bOldDoAsserts = sm_bDoAsserts;
- sm_bDoAsserts = bDoAsserts;
- return bOldDoAsserts;
- }
- #endif
-
- LONG CLTimeSpan::GetTotalDays() const
- {
- AssertValid();
- return (LONG) (m_nSeconds/86400);
- }
-
- WORD CLTimeSpan::GetHours() const
- {
- AssertValid();
-
- if (IsPositiveSpan())
- return (WORD) ((m_nSeconds - 86400*__int64(GetTotalDays()))/3600L);
- else
- {
- CLTimeSpan n(*this);
- n.Negate();
- return n.GetHours();
- }
- }
-
- WORD CLTimeSpan::GetMinutes() const
- {
- AssertValid();
-
- if (IsPositiveSpan())
- return (WORD) ((m_nSeconds - 86400*__int64(GetTotalDays()) - 3600*GetHours())/60);
- else
- {
- CLTimeSpan n(*this);
- n.Negate();
- return n.GetMinutes();
- }
- }
-
- WORD CLTimeSpan::GetSeconds() const
- {
- AssertValid();
-
- if (IsPositiveSpan())
- return (WORD) (m_nSeconds - 86400*__int64(GetTotalDays()) - 3600*GetHours() - 60*GetMinutes());
- else
- {
- CLTimeSpan n(*this);
- n.Negate();
- return n.GetSeconds();
- }
- }
-
- BOOL CLTimeSpan::IsValid() const
- {
- return m_bValid;
- }
-
- CLTimeSpan& CLTimeSpan::Negate()
- {
- AssertValid();
- m_nSeconds = (-m_nSeconds);
- return *this;
- }
-
- BOOL CLTimeSpan::IsPositiveSpan() const
- {
- AssertValid();
- return (m_nSeconds >= 0);
- }
-
- double CLTimeSpan::SecondsAsDouble() const
- {
- AssertValid();
- return (double) m_nSeconds;
- }
-
- BOOL CLTimeSpan::operator==(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- return (m_nSeconds == TimeSpan.m_nSeconds);
- }
-
- BOOL CLTimeSpan::operator>(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- return (m_nSeconds > TimeSpan.m_nSeconds);
- }
-
- BOOL CLTimeSpan::operator>=(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- return (m_nSeconds >= TimeSpan.m_nSeconds);
- }
-
- BOOL CLTimeSpan::operator<(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- return (m_nSeconds < TimeSpan.m_nSeconds);
- }
-
- BOOL CLTimeSpan::operator<=(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- return (m_nSeconds <= TimeSpan.m_nSeconds);
- }
-
- BOOL CLTimeSpan::operator!=(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- return (m_nSeconds != TimeSpan.m_nSeconds);
- }
-
- CLTimeSpan& CLTimeSpan::operator=(const CLTimeSpan& TimeSpan)
- {
- m_nSeconds = TimeSpan.m_nSeconds;
- m_bValid = TimeSpan.m_bValid;
- return *this;
- }
-
- CLTimeSpan CLTimeSpan::operator+(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- TimeSpan.AssertValid();
- __int64 rVal = m_nSeconds + TimeSpan.m_nSeconds;
- return CLTimeSpan(rVal);
- }
-
- CLTimeSpan CLTimeSpan::operator-(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- TimeSpan.AssertValid();
- __int64 rVal = m_nSeconds - TimeSpan.m_nSeconds;
- return rVal;
- }
-
- CLTimeSpan& CLTimeSpan::operator+=(CLTimeSpan& TimeSpan)
- {
- *this = *this + TimeSpan;
- return *this;
- }
-
- CLTimeSpan& CLTimeSpan::operator-=(CLTimeSpan& TimeSpan)
- {
- *this = *this - TimeSpan;
- return *this;
- }
-
- CLTimeSpan operator-(const CLTimeSpan& TimeSpan)
- {
- CLTimeSpan rVal(TimeSpan);
- return rVal.Negate();
- }
-
- CLTimeSpan CLTimeSpan::operator*(WORD Multiplier) const
- {
- return ::operator*(Multiplier, *this);
- }
-
- CLTimeSpan operator*(WORD Multiplier, const CLTimeSpan& TimeSpan)
- {
- TimeSpan.AssertValid();
- __int64 rVal = TimeSpan.m_nSeconds * Multiplier;
- return CLTimeSpan(rVal);
- }
-
- CLTimeSpan CLTimeSpan::operator/(WORD divisor) const
- {
- AssertValid();
- __int64 rVal = m_nSeconds / divisor;
- return CLTimeSpan(rVal);
- }
-
- CLTimeSpan& CLTimeSpan::operator*=(WORD Multiplier)
- {
- *this = operator*(Multiplier);
- return *this;
- }
-
- CLTimeSpan& CLTimeSpan::operator/=(WORD Divisor)
- {
- *this = operator/(Divisor);
- return *this;
- }
-
- #ifdef _DEBUG
- void CLTimeSpan::AssertValid() const
- {
- CObject::AssertValid();
- ASSERT(IsValid());
- }
- #endif
-
- #ifdef _DEBUG
- void CLTimeSpan::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- if (IsValid())
- dc << Format() << _T("\n");
- else
- dc << _T("Invalid state\n");
- }
- #endif
-
- /* //The Following Format parameters are supported
- %D Total days in this CLTimeSpan
- %H Hours in this CLTimeSpan (00 - 23)
- %M Minutes in the current hour in this CLTimeSpan (00 - 59)
- %S Seconds in the current minute in this CLTimeSpan (00 - 59)
- %% Percent sign
- %#H, %#M, %#S Remove leading zeros (if any).
- */
- CString CLTimeSpan::Format(const CString& sFormat) const
- {
- CString rVal;
- if (IsValid())
- {
- CString sBuffer;
- int sFmtLength = sFormat.GetLength();
-
- for (int i=0; i<sFmtLength; i++)
- {
- TCHAR c = sFormat.GetAt(i);
- if (c == _T('%'))
- {
- ++i;
- if (i < sFmtLength)
- {
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('D'):
- {
- LONG Days = GetTotalDays();
- sBuffer.Format(_T("%ld"), Days);
- if (IsPositiveSpan())
- rVal += _T("+");
- else if (Days == 0) //handle neagtive 0
- rVal += _T("-");
- rVal += sBuffer;
- break;
- }
- case _T('H'):
- {
- sBuffer.Format(_T("%.02d"), GetHours());
- rVal += sBuffer;
- break;
- }
- case _T('M'):
- {
- sBuffer.Format(_T("%.02d"), GetMinutes());
- rVal += sBuffer;
- break;
- }
- case _T('S'):
- {
- sBuffer.Format(_T("%.02d"), GetSeconds());
- rVal += sBuffer;
- break;
- }
- case _T('#'):
- {
- if (i < sFmtLength)
- {
- ++i;
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('H'):
- {
- sBuffer.Format(_T("%d"), GetHours());
- rVal += sBuffer;
- break;
- }
- case _T('M'):
- {
- sBuffer.Format(_T("%d"), GetMinutes());
- rVal += sBuffer;
- break;
- }
- case _T('S'):
- {
- sBuffer.Format(_T("%d"), GetSeconds());
- rVal += sBuffer;
- break;
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- break;
- }
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- }
- }
- else
- {
- rVal += c;
- }
- }
- }
-
- return rVal;
- }
-
- //Format according to the time format as specified by the control panel
- CString CLTimeSpan::Format(DWORD dwFlags) const
- {
- if (dwFlags & DTF_NOSECOND)
- return Format(sm_sDefaultFormatNOS);
- else
- return Format(sm_sDefaultFormat);
- }
-
- void CLTimeSpan::GetDefaultFormat()
- {
- CString sBuf;
-
- // Is there to be a leading 0 on the Hour?
- BOOL bHourLZ = FALSE;
- LPTSTR pszBuf = sBuf.GetBuffer(10);
- int nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITLZERO, pszBuf, 10);
- ASSERT(nChars);
- sBuf.ReleaseBuffer();
- if (sBuf == _T("0")) //No leading zeros for hours
- bHourLZ = FALSE;
- else if (sBuf == _T("1")) //Leading zeros for hours
- bHourLZ = TRUE;
- else
- ASSERT(FALSE);
-
- // Get the time separator character
- CString sSep(_T(":"));
- pszBuf = sSep.GetBuffer(10);
- nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, pszBuf, 10);
- ASSERT(nChars);
- sSep.ReleaseBuffer();
-
- //Modify behaviour depending on the various flags
- if (bHourLZ)
- {
- sm_sDefaultFormatNOS.Format(_T("%%D %%H%s%%M"), sSep);
- sm_sDefaultFormat.Format(_T("%%D %%H%s%%M%s%%S"), sSep, sSep);
- }
- else
- {
- sm_sDefaultFormatNOS.Format(_T("%%D %%#H%s%%M"), sSep);
- sm_sDefaultFormat.Format(_T("%%D %%#H%s%%M%s%%S"), sSep, sSep);
- }
- }
-
- void CLTimeSpan::Serialize(CArchive& ar)
- {
- CObject::Serialize(ar);
-
- if (ar.IsStoring())
- {
- ar << m_nSeconds;
- ar << m_bValid;
- }
- else
- {
- ar >> m_nSeconds;
- ar >> m_bValid;
- }
- }
-
- ostream& operator<<(ostream &os, const CLTimeSpan& TimeSpan)
- {
- return os << TimeSpan.Format();
- }
-
- CArchive& operator<<(CArchive& ar, CLTimeSpan& TimeSpan)
- {
- ASSERT(ar.IsStoring());
- TimeSpan.Serialize(ar);
- return ar;
- }
-
- CArchive& operator>>(CArchive& ar, CLTimeSpan& TimeSpan)
- {
- ASSERT(!ar.IsStoring());
- TimeSpan.Serialize(ar);
- return ar;
- }
-
-
-
-
-
-
- //CLTimeOfDay Implementation
-
- CLTimeOfDay::CLTimeOfDay()
- {
- Set();
- }
-
- CLTimeOfDay::CLTimeOfDay(WORD Hour, WORD Minute, WORD Second)
- {
- Set(Hour, Minute, Second);
- }
-
- CLTimeOfDay::CLTimeOfDay(const SYSTEMTIME& st)
- {
- Set(st);
- }
-
- CLTimeOfDay::CLTimeOfDay(const CLTimeOfDay& ltod)
- {
- m_dwTotalSeconds = ltod.m_dwTotalSeconds;
- m_bValid = ltod.m_bValid;
- }
-
- CLTimeOfDay::CLTimeOfDay(DWORD TotalSeconds)
- {
- Set(TotalSeconds);
- }
-
-
- CLTimeOfDay& CLTimeOfDay::Set()
- {
- m_dwTotalSeconds = 0;
- m_bValid = FALSE;
- return *this;
- }
-
- CLTimeOfDay& CLTimeOfDay::Set(WORD Hour, WORD Minute, WORD Second)
- {
- m_dwTotalSeconds = Hour*3600 + Minute*60 + Second;
- if (m_dwTotalSeconds >= 86400L)
- {
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return *this;
- }
-
- m_bValid = TRUE;
- return *this;
- }
-
- CLTimeOfDay& CLTimeOfDay::Set(const SYSTEMTIME& st)
- {
- return Set(st.wHour, st.wMinute, st.wSecond);
- }
-
- CLTimeOfDay& CLTimeOfDay::Set(DWORD TotalSeconds)
- {
- m_dwTotalSeconds = TotalSeconds;
- if (m_dwTotalSeconds >= 86400)
- {
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return *this;
- }
-
- m_bValid = TRUE;
- return *this;
- }
-
- CLTimeOfDay CLTimeOfDay::CurrentTimeOfDay(TimeFrame tf)
- {
- SYSTEMTIME st;
- switch (tf)
- {
- case UCT:
- ::GetSystemTime(&st);
- break;
- case LOCAL:
- ::GetLocalTime(&st);
- break;
- case ET:
- ASSERT(FALSE);
- break;
- default: ASSERT(FALSE);
- }
-
- return CLTimeOfDay(st);
- }
-
- CLTimeOfDay CLTimeOfDay::Midnight()
- {
- return CLTimeOfDay(0, 0, 0);
- }
-
- CLTimeOfDay CLTimeOfDay::Midday()
- {
- return CLTimeOfDay(12, 0, 0);
- }
-
- #ifdef _DEBUG
- BOOL CLTimeOfDay::SetDoConstructorAsserts(BOOL bDoAsserts)
- {
- BOOL bOldDoAsserts = sm_bDoAsserts;
- sm_bDoAsserts = bDoAsserts;
- return bOldDoAsserts;
- }
- #endif
-
- WORD CLTimeOfDay::GetHour() const
- {
- AssertValid();
- return (WORD) (m_dwTotalSeconds/3600L);
- }
-
- WORD CLTimeOfDay::GetMinute() const
- {
- AssertValid();
- return (WORD) ((m_dwTotalSeconds - (GetHour()*3600L))/60);
- }
-
- DWORD CLTimeOfDay::GetTotalSeconds() const
- {
- AssertValid();
- return m_dwTotalSeconds;
- }
-
- WORD CLTimeOfDay::GetSecond() const
- {
- AssertValid();
- return (WORD) (m_dwTotalSeconds - (60L * (GetMinute() + (60L*GetHour()))));
- }
-
- WORD CLTimeOfDay::GetAMPMHour() const
- {
- AssertValid();
- WORD wHour = GetHour();
- if (wHour > 12)
- wHour -= 12;
- return wHour;
- }
-
- CString CLTimeOfDay::GetAMString()
- {
- CString rVal;
- LPTSTR pszBuf = rVal.GetBuffer(20);
- int nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_S1159, pszBuf, 20);
- rVal.ReleaseBuffer();
- return rVal;
- }
-
- CString CLTimeOfDay::GetPMString()
- {
- CString rVal;
- LPTSTR pszBuf = rVal.GetBuffer(20);
- int nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_S2359, pszBuf, 20);
- rVal.ReleaseBuffer();
- return rVal;
- }
-
- CString CLTimeOfDay::GetAMPMString() const
- {
- AssertValid();
- if (GetHour() > 12)
- return GetPMString();
- else
- return GetAMString();
- }
-
- BOOL CLTimeOfDay::IsValid() const
- {
- return m_bValid;
- }
-
- DWORD CLTimeOfDay::Collate() const
- {
- AssertValid();
- DWORD rVal = GetHour()*10000L + GetMinute()*100L + GetSecond();
- return rVal;
- }
-
- SYSTEMTIME CLTimeOfDay::GetSYSTEMTIME() const
- {
- AssertValid();
- SYSTEMTIME st;
-
- st.wYear=0;
- st.wMonth=0;
- st.wDayOfWeek=0;
- st.wDay=0;
- st.wHour = GetHour();
- st.wMinute = GetMinute();
- st.wSecond = GetSecond();
- st.wMilliseconds = 0;
- return st;
- }
-
- tm CLTimeOfDay::GetTM() const
- {
- AssertValid();
- tm rVal;
-
- rVal.tm_hour = GetHour();
- rVal.tm_min = GetMinute();
- rVal.tm_sec = GetSecond();
- rVal.tm_mday = 0;
- rVal.tm_mon = 0;
- rVal.tm_year = 0;
- rVal.tm_wday = 0;
- rVal.tm_yday = 0;
- rVal.tm_isdst = 0;
-
- return rVal;
- }
-
- CLTimeOfDay& CLTimeOfDay::operator=(const CLTimeOfDay& Tod)
- {
- m_dwTotalSeconds = Tod.m_dwTotalSeconds;
- m_bValid = Tod.m_bValid;
- return *this;
- }
-
- CLTimeOfDay CLTimeOfDay::operator+(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- CLTimeSpan t = CLTimeSpan(*this) + CLTimeSpan(0, TimeSpan.GetHours(), TimeSpan.GetMinutes(),
- TimeSpan.GetSeconds());
- return CLTimeOfDay(t.GetHours(), t.GetMinutes(), t.GetSeconds());
- }
-
- CLTimeOfDay CLTimeOfDay::operator-(const CLTimeSpan& TimeSpan) const
- {
- AssertValid();
- //Addition of 1 Day is used to ensure result "t" does not go negative
- CLTimeSpan t = CLTimeSpan::OneDay() + CLTimeSpan(*this) -
- CLTimeSpan(0, TimeSpan.GetHours(), TimeSpan.GetMinutes(),
- TimeSpan.GetSeconds());
- return CLTimeOfDay(t.GetHours(), t.GetMinutes(), t.GetSeconds());
- }
-
- CLTimeOfDay& CLTimeOfDay::operator+=(CLTimeSpan& TimeSpan)
- {
- *this = *this + TimeSpan;
- return *this;
- }
-
- CLTimeOfDay& CLTimeOfDay::operator-=(CLTimeSpan& TimeSpan)
- {
- *this = *this - TimeSpan;
- return *this;
- }
-
- BOOL CLTimeOfDay::operator==(const CLTimeOfDay& Tod) const
- {
- AssertValid();
- return (m_dwTotalSeconds == Tod.m_dwTotalSeconds);
- }
-
- BOOL CLTimeOfDay::operator>(const CLTimeOfDay& Tod) const
- {
- AssertValid();
- return (m_dwTotalSeconds > Tod.m_dwTotalSeconds);
- }
-
- BOOL CLTimeOfDay::operator>=(const CLTimeOfDay& Tod) const
- {
- AssertValid();
- return (m_dwTotalSeconds >= Tod.m_dwTotalSeconds);
- }
-
- BOOL CLTimeOfDay::operator<(const CLTimeOfDay& Tod) const
- {
- AssertValid();
- return (m_dwTotalSeconds < Tod.m_dwTotalSeconds);
- }
-
- BOOL CLTimeOfDay::operator<=(const CLTimeOfDay& Tod) const
- {
- AssertValid();
- return (m_dwTotalSeconds <= Tod.m_dwTotalSeconds);
- }
-
- BOOL CLTimeOfDay::operator!=(const CLTimeOfDay& Tod) const
- {
- AssertValid();
- return !operator==(Tod);
- }
-
- #ifdef _DEBUG
- void CLTimeOfDay::AssertValid() const
- {
- CObject::AssertValid();
- ASSERT(IsValid());
- }
- #endif
-
- #ifdef _DEBUG
- void CLTimeOfDay::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- if (IsValid())
- dc << Format() << _T("\n");
- else
- dc << _T("Invalid state\n");
- }
- #endif
-
- /* //The Following Format parameters are supported
-
- %H Hours in the current day
- %M Minutes in the current hour
- %h 12 Hour format Hours in this CLTimeSpan (00 - 12)
- %P AM / PM indicator
- %S Seconds in the current minute
- %% Percent sign
- %x Time Of Day representation for current locale
- %#H, %#h, %#M, %#S Remove leading zeros (if any).
- */
- CString CLTimeOfDay::Format(const CString& sFormat) const
- {
- CString rVal;
- if (IsValid())
- {
- CString sBuffer;
- int sFmtLength = sFormat.GetLength();
-
- for (int i=0; i<sFmtLength; i++)
- {
- TCHAR c = sFormat.GetAt(i);
- if (c == _T('%'))
- {
- ++i;
- if (i < sFmtLength)
- {
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('H'):
- {
- sBuffer.Format(_T("%.02d"), GetHour());
- rVal += sBuffer;
- break;
- }
- case _T('M'):
- {
- sBuffer.Format(_T("%.02d"), GetMinute());
- rVal += sBuffer;
- break;
- }
- case _T('h'):
- {
- sBuffer.Format(_T("%.02d"), GetAMPMHour());
- rVal += sBuffer;
- break;
- }
- case _T('P'):
- {
- rVal += GetAMPMString();
- break;
- }
- case _T('S'):
- {
- sBuffer.Format(_T("%.02d"), GetSecond());
- rVal += sBuffer;
- break;
- }
- case _T('x'):
- {
- sBuffer.Empty();
- SYSTEMTIME st = GetSYSTEMTIME();
- int Res = ::GetTimeFormat(LOCALE_USER_DEFAULT,
- LOCALE_NOUSEROVERRIDE,
- &st,
- NULL,
- sBuffer.GetBufferSetLength(100),
- 100);
- if (Res == 0)
- TRACE1("GetDateFormat() Failed in CLTimeSpan::Format(), GetLastError() returned %ul\n", ::GetLastError());
- sBuffer.ReleaseBuffer();
- rVal += sBuffer;
- break;
- }
- case _T('#'):
- {
- if (i < sFmtLength)
- {
- ++i;
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('H'):
- {
- sBuffer.Format(_T("%d"), GetHour());
- rVal += sBuffer;
- break;
- }
- case _T('h'):
- {
- sBuffer.Format(_T("%d"), GetAMPMHour());
- rVal += sBuffer;
- break;
- }
- case _T('M'):
- {
- sBuffer.Format(_T("%d"), GetMinute());
- rVal += sBuffer;
- break;
- }
- case _T('S'):
- {
- sBuffer.Format(_T("%d"), GetSecond());
- rVal += sBuffer;
- break;
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- }
- break;
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- }
- }
- else
- {
- rVal += c;
- }
- }
- }
- return rVal;
- }
-
- //Format according to the time format as specified by the control panel
- CString CLTimeOfDay::Format(DWORD dwFlags) const
- {
- if (dwFlags & DTF_NOSECOND)
- return Format(sm_sDefaultFormatNOS);
- else
- return Format(sm_sDefaultFormat);
- }
-
- void CLTimeOfDay::GetDefaultFormat()
- {
- CString sBuf;
-
- // Is there to be a leading 0 on the Hour?
- BOOL bHourLZ = FALSE;
- LPTSTR pszBuf = sBuf.GetBuffer(10);
- int nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITLZERO, pszBuf, 10);
- ASSERT(nChars);
- sBuf.ReleaseBuffer();
- if (sBuf == _T("0")) //No leading zeros for hours
- bHourLZ = FALSE;
- else if (sBuf == _T("1")) //Leading zeros for hours
- bHourLZ = TRUE;
- else
- ASSERT(FALSE);
-
- // Get the time separator character
- CString sSep(_T(":"));
- pszBuf = sSep.GetBuffer(10);
- nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, pszBuf, 10);
- ASSERT(nChars);
- sSep.ReleaseBuffer();
-
- //Get the 24 Hour / 12 Hour indicator
- BOOL b24Hour = FALSE;
- pszBuf = sBuf.GetBuffer(10);
- nChars = ::GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ITIME, pszBuf, 10);
- ASSERT(nChars);
- sBuf.ReleaseBuffer();
- if (sBuf == _T("0")) //AM / PM Style
- b24Hour = FALSE;
- else if (sBuf == _T("1")) //24 Hour Style
- b24Hour = TRUE;
- else
- ASSERT(FALSE);
-
-
- //Modify behaviour depending on the various flags
- if (bHourLZ)
- {
- if (b24Hour)
- {
- sm_sDefaultFormatNOS.Format(_T("%%H%s%%M"), sSep);
- sm_sDefaultFormat.Format(_T("%%H%s%%M%s%%S"), sSep, sSep);
- }
- else
- {
- sm_sDefaultFormatNOS.Format(_T("%%h%s%%M %%P"), sSep);
- sm_sDefaultFormat.Format(_T("%%h%s%%M%s%%S %%P"), sSep, sSep);
- }
- }
- else
- {
- if (b24Hour)
- {
- sm_sDefaultFormatNOS.Format(_T("%%#H%s%%M"), sSep, sSep);
- sm_sDefaultFormat.Format(_T("%%#H%s%%M%s%%S"), sSep, sSep, sSep);
- }
- else
- {
- sm_sDefaultFormatNOS.Format(_T("%%#h%s%%M %%P"), sSep, sSep);
- sm_sDefaultFormat.Format(_T("%%#h%s%%M%s%%S %%P"), sSep, sSep, sSep);
- }
- }
- }
-
- void CLTimeOfDay::Serialize(CArchive& ar)
- {
- CObject::Serialize(ar);
-
- if (ar.IsStoring())
- {
- ar << m_dwTotalSeconds;
- ar << m_bValid;
- }
- else
- {
- ar >> m_dwTotalSeconds;
- ar >> m_bValid;
- }
- }
-
- ostream& operator<<(ostream &os, const CLTimeOfDay &TimeOfDay)
- {
- return os << TimeOfDay.Format();
- }
-
- CArchive& operator<<(CArchive& ar, CLTimeOfDay &TimeOfDay)
- {
- ASSERT(ar.IsStoring());
- TimeOfDay.Serialize(ar);
- return ar;
- }
-
- CArchive& operator>>(CArchive& ar, CLTimeOfDay &TimeOfDay)
- {
- ASSERT(!ar.IsStoring());
- TimeOfDay.Serialize(ar);
- return ar;
- }
-
-
-
-
-
-
- //CLDate implementation
-
-
- CLDate::CLDate()
- {
- Set();
- }
-
- CLDate::CLDate(LONG Year, WORD Month, WORD Day,
- WORD Hour, WORD Minute, WORD Second,
- TimeFrame tf)
- {
- Set(Year, Month, Day, Hour, Minute, Second, tf);
- }
-
- CLDate::CLDate(const SYSTEMTIME& st, TimeFrame tf, BOOL bUseDayOfWeek)
- {
- Set(st, tf, bUseDayOfWeek);
- }
-
- CLDate::CLDate(LONG Year, WORD Month, WORD WeekOfMonth,
- WORD DayOfWeek, WORD Hour, WORD Minute, WORD Second,
- TimeFrame tf)
- {
- Set(Year, Month, WeekOfMonth, DayOfWeek, Hour, Minute, Second, tf);
- }
-
- CLDate::CLDate(LONG Days, CDate::DateEpoch e, WORD Hour, WORD Minute,
- WORD Second, TimeFrame tf)
- {
- Set(Days, e, Hour, Minute, Second, tf);
- }
-
- CLDate::CLDate(const CDate& Date, const CLTimeOfDay& Tod, TimeFrame tf)
- {
- Set(Date, Tod, tf);
- }
-
-
- CLDate::CLDate(const CLDate& ld)
- {
- m_nSeconds = ld.m_nSeconds;
- m_bValid = ld.m_bValid;
- m_TimeFrame = ld.m_TimeFrame;
- }
-
- CLDate::CLDate(const CTime& ct)
- {
- Set(ct);
- }
-
- CLDate::CLDate(const COleDateTime& oleTime, TimeFrame tf)
- {
- Set(oleTime, tf);
- }
-
- CLDate& CLDate::Set()
- {
- m_nSeconds = 0;
- m_bValid = FALSE;
- return *this;
- }
-
- CLDate& CLDate::Set(LONG Year, WORD Month, WORD Day,
- WORD Hour, WORD Minute, WORD Second,
- TimeFrame tf)
- {
- CDate Date(Year, Month, Day);
- CLTimeOfDay Tod(Hour, Minute, Second);
-
- m_TimeFrame = tf;
- m_nSeconds = __int64(Date.GetValue())*86400 + Tod.GetTotalSeconds();
- m_bValid = TRUE;
-
- if (tf == LOCAL)
- CheckForValidLocalDate();
- return *this;
- }
-
- CLDate& CLDate::Set(const SYSTEMTIME& st, TimeFrame tf, BOOL bUseDayOfWeek)
- {
- if (bUseDayOfWeek) //Using Day-in-month format
- {
- if (st.wYear) //an absolute year, wDayOfWeek is 0 based
- Set(st.wYear, st.wMonth, st.wDay, (WORD)(st.wDayOfWeek+1), st.wHour,
- st.wMinute, st.wSecond, tf);
- else //use the current year
- Set(CurrentTime(tf).GetCDate().GetYear(), st.wMonth, st.wDay,
- (WORD)(st.wDayOfWeek+1), st.wHour, st.wMinute, st.wSecond, tf);
- }
- else
- {
- if (st.wYear) //an absolute year
- Set(st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, tf);
- else //use the current year
- Set(CurrentTime(tf).GetCDate().GetYear(), st.wMonth, st.wDay,
- st.wHour, st.wMinute, st.wSecond, tf);
- }
-
- return *this;
- }
-
-
- CLDate& CLDate::Set(LONG Year, WORD Month, WORD WeekOfMonth,
- WORD DayOfWeek, WORD Hour, WORD Minute, WORD Second,
- TimeFrame tf)
- {
- CDate Date(Year, Month, WeekOfMonth, DayOfWeek);
- CLTimeOfDay Tod(Hour, Minute, Second);
-
- m_TimeFrame = tf;
- m_nSeconds = __int64(Date.GetValue())*86400 + Tod.GetTotalSeconds();
- m_bValid = TRUE;
-
- if (tf == LOCAL)
- CheckForValidLocalDate();
- return *this;
- }
-
- CLDate& CLDate::Set(LONG Days, CDate::DateEpoch e, WORD Hour, WORD Minute,
- WORD Second, TimeFrame tf)
- {
- CDate Date(Days, e);
- CLTimeOfDay Tod(Hour, Minute, Second);
-
- m_TimeFrame = tf;
- m_nSeconds = __int64(Date.GetValue())*86400 + Tod.GetTotalSeconds();
- m_bValid = TRUE;
-
- if (tf == LOCAL)
- CheckForValidLocalDate();
- return *this;
- }
-
- CLDate& CLDate::Set(const CTime& ct)
- {
- m_TimeFrame = UCT;
- tm* pTM = ct.GetGmtTm();
- Set(pTM->tm_year + 1900, (WORD) (pTM->tm_mon+1), (WORD) pTM->tm_mday,
- (WORD) pTM->tm_hour, (WORD) pTM->tm_min, (WORD) pTM->tm_sec, UCT);
- return *this;
- }
-
- CLDate& CLDate::Set(const CDate& Date, const CLTimeOfDay& Tod, TimeFrame tf)
- {
- if (!Date.IsValid() || !Tod.IsValid())
- Set();
- else
- {
- m_TimeFrame = tf;
- m_nSeconds = __int64(Date.GetValue())*86400 + Tod.GetTotalSeconds();
- m_bValid = TRUE;
-
- if (tf == LOCAL)
- CheckForValidLocalDate();
- }
-
- return *this;
- }
-
- CLDate& CLDate::Set(const COleDateTime& oleTime, TimeFrame tf)
- {
- COleDateTime::DateTimeStatus dts(oleTime.GetStatus());
- if (dts == COleDateTime::DateTimeStatus::valid)
- Set((LONG) oleTime.GetYear(), (WORD) oleTime.GetMonth(), (WORD) oleTime.GetDay(),
- (WORD) oleTime.GetHour(), (WORD) oleTime.GetMinute(), (WORD) oleTime.GetSecond(), tf);
- else
- Set();
- return *this;
- }
-
- void CLDate::CheckForValidLocalDate()
- {
- //Should only be called when tested a LOCAL CLDate
- ASSERT(m_TimeFrame == LOCAL);
-
- TIME_ZONE_INFORMATION tzi;
- BOOL bSuccess = (::GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
- ASSERT(bSuccess);
-
- //Set the TimeFrame to UCT so that we do not need to perform DST conversions in
- //the following arithmetic
- m_TimeFrame = UCT;
-
- CLDate BeginDaylightTime;
- LONG ThisYear = GetCDate().GetYear();
- if (tzi.DaylightDate.wYear == 0) //Using Day-in-month format
- BeginDaylightTime.Set(ThisYear, tzi.DaylightDate.wMonth, tzi.DaylightDate.wDay, (WORD)(tzi.DaylightDate.wDayOfWeek+1),
- tzi.DaylightDate.wHour, tzi.DaylightDate.wMinute, tzi.DaylightDate.wSecond, UCT);
- else
- BeginDaylightTime.Set(tzi.DaylightDate.wYear, tzi.DaylightDate.wMonth, tzi.DaylightDate.wDay, tzi.DaylightDate.wHour,
- tzi.DaylightDate.wMinute, tzi.DaylightDate.wSecond, UCT);
-
- CLDate BeginStandardTime;
- if (tzi.StandardDate.wYear == 0) //Using Day-in-month format
- BeginStandardTime.Set(ThisYear, tzi.StandardDate.wMonth, tzi.StandardDate.wDay, (WORD)(tzi.StandardDate.wDayOfWeek+1),
- tzi.StandardDate.wHour, tzi.StandardDate.wMinute, tzi.StandardDate.wSecond, UCT);
- else
- BeginStandardTime.Set(tzi.StandardDate.wYear, tzi.StandardDate.wMonth, tzi.StandardDate.wDay, tzi.StandardDate.wHour,
- tzi.StandardDate.wMinute, tzi.StandardDate.wSecond, UCT);
-
-
- #ifdef _DEBUG
- CString s = BeginDaylightTime.Format();
- s = BeginStandardTime.Format();
- #endif
-
- if (tzi.DaylightBias > 0)
- {
- //A CLDate which specifies a date which occurs twice
- CLDate StartNonUnique(BeginDaylightTime - CLTimeSpan(0, 0, (WORD) tzi.DaylightBias, 0));
- if ((*this >= StartNonUnique) && (*this < BeginDaylightTime))
- {
- TRACE0("A CLDate using a LOCAL timeframe tried to be constructed which represents a non unique absolute time\n");
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return;
- }
-
- //A CLDate which specifies a date which does not occur
- CLDate EndSkip(BeginStandardTime + CLTimeSpan(0, 0, (WORD) tzi.DaylightBias, 0));
- if ((*this > BeginStandardTime) && (*this < EndSkip))
- {
- TRACE0("A CLDate using a LOCAL timeframe tried to be constructed which does not occur\n");
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return;
- }
-
- }
- else if (tzi.DaylightBias < 0)
- {
- //A CLDate which specifies a date which does not occur
- CLDate EndSkip(BeginDaylightTime + CLTimeSpan(0, 0, (WORD) -tzi.DaylightBias, 0));
- if ((*this > BeginDaylightTime) && (*this < EndSkip))
- {
- TRACE0("A CLDate using a LOCAL timeframe tried to be constructed which does not occur\n");
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return;
- }
-
- //A CLDate which specifies a date which occurs twice
- CLDate StartNonUnique(BeginStandardTime - CLTimeSpan(0, 0, (WORD) -tzi.DaylightBias, 0));
- if ((*this >= StartNonUnique) &&
- (*this <= BeginStandardTime) )
- {
- TRACE0("A CLDate using a LOCAL timeframe tried to be constructed which represents a non unique absolute time\n");
- if (sm_bDoAsserts)
- ASSERT(FALSE);
- Set();
- return;
- }
- }
-
- //return the timeframe back to local
- m_TimeFrame = LOCAL;
- }
-
- CLDate CLDate::CurrentTime(TimeFrame tf)
- {
- SYSTEMTIME st;
- ::GetSystemTime(&st);
- CLDate rVal(st, UCT);
- rVal.SetTimeFrame(tf);
- return rVal;
- }
-
- CLTimeSpan CLDate::DeltaT(CLDate& d)
- {
- CLTimeSpan Tspan(d - CLDate(CDate::Epoch2000(), CLTimeOfDay(0, 0, 0), d.GetTimeFrame()));
- double T = Tspan.GetTotalDays()/36525.0;
- double S;
-
- double y = 2000 + T * 100;
- if (y > 2000)
- S = 102.3 + T * (123.5 + T * 32.5);
- else if (y < 1620)
- {
- if (y < 948)
- S = 2715.6 + T * (573.36 + T * 46.5);
- else
- S = 50.6 + T * (67.5 + T * 22.5);
- }
- else /* Interpolate from the static data table */
- {
- int Index = (int)((y - 1620) / 2);
- if (Index > 185)
- Index = 185;
- y = y / 2 - Index - 810;
- S = (DeltaTTable[Index] + (DeltaTTable[Index + 1] - DeltaTTable[Index]) * y) / 10;
- }
-
- return CLTimeSpan(S);
- }
-
- BOOL CLDate::CurrentlyInDST()
- {
- TIME_ZONE_INFORMATION tzi;
- return ::GetTimeZoneInformation(&tzi) == TIME_ZONE_ID_DAYLIGHT;
- }
-
- CLTimeSpan CLDate::DaylightBias()
- {
- TIME_ZONE_INFORMATION tzi;
- BOOL bSuccess = (::GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
- ASSERT(bSuccess);
-
- CLTimeSpan rVal;
- if (tzi.DaylightBias > 0)
- rVal = CLTimeSpan(0, 0, (WORD) tzi.DaylightBias, 0);
- else
- {
- rVal = CLTimeSpan(0, 0, (WORD) -tzi.DaylightBias, 0);
- rVal.Negate();
- }
- return rVal;
- }
-
- CLTimeSpan CLDate::TimezoneBias()
- {
- TIME_ZONE_INFORMATION tzi;
- BOOL bSuccess = (::GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
- ASSERT(bSuccess);
-
- CLTimeSpan rVal;
- LONG tzBias = tzi.StandardBias + tzi.Bias;
- if (tzBias > 0)
- rVal = CLTimeSpan(0, 0, (WORD) tzBias, 0);
- else
- {
- rVal = CLTimeSpan(0, 0, (WORD) -tzBias, 0);
- rVal.Negate();
- }
- return rVal;
- }
-
- CString CLDate::DaylightName()
- {
- TIME_ZONE_INFORMATION tzi;
- BOOL bSuccess = (::GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
- ASSERT(bSuccess);
- return tzi.DaylightName;
- }
-
- CString CLDate::StandardName()
- {
- TIME_ZONE_INFORMATION tzi;
- BOOL bSuccess = (::GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
- ASSERT(bSuccess);
- return tzi.StandardName;
- }
-
- #ifdef _DEBUG
- BOOL CLDate::SetDoConstructorAsserts(BOOL bDoAsserts)
- {
- BOOL bOldDoAsserts = sm_bDoAsserts;
- sm_bDoAsserts = bDoAsserts;
- return bOldDoAsserts;
- }
- #endif
-
- CDate CLDate::GetCDate() const
- {
- CDate rVal;
- if (IsValid())
- rVal = CDate(((LONG) (m_nSeconds/86400))-2000000000L, CDate::EPOCH_GREG);
-
- return rVal;
- }
-
- CLTimeOfDay CLDate::GetCLTimeOfDay() const
- {
- CLTimeOfDay rVal;
- if (IsValid())
- {
- __int64 nSeconds = m_nSeconds - __int64(GetCDate().GetValue())*86400;
- rVal = CLTimeOfDay((DWORD) nSeconds);
- }
- return rVal;
- }
-
- DateLS CLDate::GetDate() const
- {
- DateLS rVal;
- DateS ds = GetCDate().GetDate();
-
- rVal.lYear = ds.lYear;
- rVal.wMonth = ds.wMonth;
- rVal.wDay = ds.wDay;
- rVal.wWday = ds.wWday;
- rVal.wYday = ds.wYday;
- rVal.ct = ds.ct;
- rVal.tf = m_TimeFrame;
- CLTimeOfDay tod(GetCLTimeOfDay());
- rVal.wHour = tod.GetHour();
- rVal.wMinute = tod.GetMinute();
- rVal.wSecond = tod.GetSecond();
-
- return rVal;
- };
-
- SYSTEMTIME CLDate::GetSYSTEMTIME() const
- {
- SYSTEMTIME s = GetCDate().GetSYSTEMTIME();
- CLTimeOfDay tod(GetCLTimeOfDay());
-
- s.wHour = tod.GetHour();
- s.wMinute = tod.GetMinute();
- s.wSecond = tod.GetSecond();
- s.wMilliseconds = 0;
-
- return s;
- }
-
- tm CLDate::GetTM()
- {
- AssertValid();
-
- tm rVal;
- rVal.tm_sec = 0;
- rVal.tm_min = 0;
- rVal.tm_hour = 0;
- rVal.tm_mday = 0;
- rVal.tm_mon = 0;
- rVal.tm_year = 0;
- rVal.tm_wday = 0;
- rVal.tm_yday = 0;
- rVal.tm_isdst = 0;
-
- DateLS ds = GetDate();
-
- long MinYear = 1900L + LONG_MIN;
- long MaxYear = LONG_MAX;
-
- //handle range errors
- if ((ds.lYear < MinYear) || (ds.lYear > MaxYear))
- {
- ASSERT(FALSE);
- return rVal;
- }
-
- rVal.tm_hour = ds.wHour;
- rVal.tm_min = ds.wMinute;
- rVal.tm_sec = ds.wSecond;
- rVal.tm_year = (int) (ds.lYear - 1900L);
- rVal.tm_mon = ds.wMonth - 1; //tm struct uses 0 based indices
- rVal.tm_wday = ds.wWday - 1; //tm struct uses 0 based indices
- rVal.tm_mday = ds.wDay;
- rVal.tm_wday = ds.wYday - 1; //Returns days since Jan 1
- rVal.tm_isdst = IsDST();
-
- return rVal;
- }
-
- TimeFrame CLDate::GetTimeFrame() const
- {
- AssertValid();
- return m_TimeFrame;
- }
-
- TimeFrame CLDate::SetTimeFrame(TimeFrame tf)
- {
- if (tf == m_TimeFrame) //quick return
- return tf;
-
- TimeFrame OldTF = m_TimeFrame;
-
- switch (m_TimeFrame)
- {
- case LOCAL:
- {
- switch (tf)
- {
- case UCT:
- {
- //pretend that timeframe is UCT to avoid recursion in the arithmetic
- m_TimeFrame = UCT;
-
- *this += TimezoneBias();
- if (DST())
- *this += DaylightBias();
- break;
- }
- case ET:
- {
- SetTimeFrame(UCT);
- SetTimeFrame(ET);
- break;
- }
- default: ASSERT(FALSE);
- }
- break;
- }
- case ET:
- {
- switch (tf)
- {
- case UCT:
- {
- *this -= DeltaT();
- break;
- }
- case LOCAL:
- {
- SetTimeFrame(UCT);
- SetTimeFrame(LOCAL);
- break;
- }
- default: ASSERT(FALSE);
- }
- break;
- }
- case UCT:
- {
- switch (tf)
- {
- case LOCAL:
- {
- *this -= TimezoneBias();
- if (DST())
- {
- *this -= DaylightBias();
- sm_bIsDst = TRUE;
- }
- else
- sm_bIsDst = FALSE;
- break;
- }
- case ET:
- {
- *this += DeltaT();
- break;
- }
- default: ASSERT(FALSE);
- }
- break;
- }
-
- default: ASSERT(FALSE);
- }
-
- m_TimeFrame = tf;
- return OldTF;
- };
-
- BOOL CLDate::IsDST()
- {
- AssertValid();
- BOOL rVal;
- TimeFrame OldTF = GetTimeFrame();
- if (OldTF != LOCAL)
- {
- SetTimeFrame(LOCAL);
- rVal = sm_bIsDst;
- }
- else
- rVal = DST();
- return rVal;
- }
-
- BOOL CLDate::DST()
- {
- AssertValid();
- BOOL rVal = TRUE;
- TIME_ZONE_INFORMATION tzi;
- DWORD dwTZI = ::GetTimeZoneInformation(&tzi);
- ASSERT(dwTZI != TIME_ZONE_ID_UNKNOWN);
-
- long lYear = GetCDate().GetYear();
-
- //SYSTEMTIME can only accomadate a WORD sized year
- //so we always return FALSE if outside of this range
- if ((lYear > SHRT_MAX) || (lYear < 0))
- rVal = FALSE;
-
- if (rVal)
- {
- TimeFrame OldTF = m_TimeFrame;
- m_TimeFrame = UCT;
-
- BOOL bUseDayOfWeek = FALSE;
- if (tzi.DaylightDate.wYear == 0)
- {
- bUseDayOfWeek = TRUE;
- tzi.DaylightDate.wYear = (WORD) lYear;
- }
-
- CLDate BeginDST(tzi.DaylightDate, UCT, bUseDayOfWeek);
-
- #ifdef _DEBUG
- CString s = BeginDST.Format();
- #endif
-
- bUseDayOfWeek = FALSE;
- if (tzi.StandardDate.wYear == 0)
- {
- bUseDayOfWeek = TRUE;
- tzi.StandardDate.wYear = (WORD) lYear;
- }
-
- CLDate EndDST(tzi.StandardDate, UCT, bUseDayOfWeek);
- #ifdef _DEBUG
- s = EndDST.Format();
- #endif
-
- rVal = rVal && (*this >= BeginDST && *this < EndDST);
-
- m_TimeFrame = OldTF;
- }
-
- return rVal;
- }
-
- CString CLDate::GetStringTimeFrame() const
- {
- CString rVal;
- switch (m_TimeFrame)
- {
- case UCT:
- if (!rVal.LoadString(IDS_UCT))
- ASSERT(FALSE);
- break;
- case ET:
- if (!rVal.LoadString(IDS_ET))
- ASSERT(FALSE);
- break;
- case LOCAL:
- if (!rVal.LoadString(IDS_LOCAL))
- ASSERT(FALSE);
- break;
- default:
- ASSERT(FALSE);
- break;
- }
- return rVal;
- }
-
- void CLDate::AddYear(int Years)
- {
- AssertValid();
- CDate Date(GetCDate());
- CLTimeOfDay Tod(GetCLTimeOfDay());
- Date.AddYear(Years);
- Set(Date, Tod, m_TimeFrame);
- }
-
- void CLDate::AddMonth(int Months)
- {
- AssertValid();
- CDate Date(GetCDate());
- CLTimeOfDay Tod(GetCLTimeOfDay());
- Date.AddMonth(Months);
- Set(Date, Tod, m_TimeFrame);
- }
-
- void CLDate::AddWeek(int Weeks)
- {
- AssertValid();
-
- CDate Date(GetCDate());
- CLTimeOfDay Tod(GetCLTimeOfDay());
- Date.AddWeek(Weeks);
- Set(Date, Tod, m_TimeFrame);
- }
-
- BOOL CLDate::IsValid() const
- {
- return m_bValid;
- }
-
- CLTimeSpan CLDate::DeltaT()
- {
- AssertValid();
- return DeltaT(*this);
- }
-
- CLDate& CLDate::operator=(const CLDate& d)
- {
- m_TimeFrame = d.m_TimeFrame;
- m_nSeconds = d.m_nSeconds;
- m_bValid = d.m_bValid;
- return *this;
- }
-
- CLDate CLDate::operator+(const CLTimeSpan& TimeSpan)
- {
- AssertValid();
- TimeSpan.AssertValid();
-
- //Because LOCAL Time is not a continous timeframe convert if necessary
- TimeFrame OldTF = GetTimeFrame();
- if (GetTimeFrame() == LOCAL)
- SetTimeFrame(UCT);
-
- CLDate rVal;
- rVal.m_nSeconds = m_nSeconds + TimeSpan.m_nSeconds;
- rVal.m_TimeFrame = m_TimeFrame;
- rVal.m_bValid = TRUE;
-
- SetTimeFrame(OldTF);
- rVal.SetTimeFrame(OldTF);
-
- return rVal;
- }
-
- CLTimeSpan CLDate::operator-(CLDate& ld)
- {
- AssertValid();
- ld.AssertValid();
-
- //Because LOCAL Time is not a continous timeframe convert if necessary
- TimeFrame OldTF1 = GetTimeFrame();
- TimeFrame OldTF2 = ld.GetTimeFrame();
-
- BOOL bTimeFrameChangeRequired = ( (OldTF1 == LOCAL) && (OldTF2 != LOCAL) ||
- (OldTF1 != LOCAL) && (OldTF2 == LOCAL) );
- if (bTimeFrameChangeRequired)
- {
- TimeFrame OldTF1 = SetTimeFrame(UCT);
- TimeFrame OldTF2 = ld.SetTimeFrame(UCT);
- }
-
- __int64 s = m_nSeconds - ld.m_nSeconds;
- CLTimeSpan rVal(s);
-
- SetTimeFrame(OldTF1);
- ld.SetTimeFrame(OldTF2);
-
- return rVal;
- }
-
- CLDate CLDate::operator-(const CLTimeSpan& TimeSpan)
- {
- AssertValid();
- TimeSpan.AssertValid();
-
- //Because LOCAL Time is not a continous timeframe convert if necessary
- TimeFrame OldTF = GetTimeFrame();
- if (GetTimeFrame() == LOCAL)
- SetTimeFrame(UCT);
-
- __int64 s = m_nSeconds - TimeSpan.m_nSeconds;
- CLDate rVal;
- rVal.m_nSeconds = s;
- rVal.m_TimeFrame = m_TimeFrame;
- rVal.m_bValid = TRUE;
-
- SetTimeFrame(OldTF);
-
- return rVal;
- }
-
- CLDate& CLDate::operator+=(const CLTimeSpan& TimeSpan)
- {
- *this = *this + TimeSpan;
- return *this;
- }
-
- CLDate& CLDate::operator-=(const CLTimeSpan& TimeSpan)
- {
- *this = *this - TimeSpan;
- return *this;
- }
-
- CLDate& CLDate::operator++()
- {
- AssertValid();
- m_nSeconds += 86400;
- return *this;
- }
-
- CLDate& CLDate::operator--()
- {
- AssertValid();
- m_nSeconds -= 86400;
- return *this;
- }
-
- BOOL CLDate::operator==(CLDate& ld)
- {
- AssertValid();
-
- //Match the two instances timeframes if neccessary
- TimeFrame OldTF1 = GetTimeFrame();
- TimeFrame OldTF2 = ld.GetTimeFrame();
-
- BOOL bTimeFrameChangeRequired = (OldTF1 != OldTF2);
- if (bTimeFrameChangeRequired)
- {
- TimeFrame OldTF1 = SetTimeFrame(UCT);
- TimeFrame OldTF2 = ld.SetTimeFrame(UCT);
- }
-
- BOOL rVal = (m_nSeconds == ld.m_nSeconds);
-
- SetTimeFrame(OldTF1);
- ld.SetTimeFrame(OldTF2);
-
- return rVal;
- }
-
- BOOL CLDate::operator>(CLDate& ld)
- {
- AssertValid();
-
- //Match the two instances timeframes if neccessary
- TimeFrame OldTF1 = GetTimeFrame();
- TimeFrame OldTF2 = ld.GetTimeFrame();
- BOOL bTimeFrameChangeRequired = (OldTF1 != OldTF2);
- if (bTimeFrameChangeRequired)
- {
- TimeFrame OldTF1 = SetTimeFrame(UCT);
- TimeFrame OldTF2 = ld.SetTimeFrame(UCT);
- }
-
- BOOL rVal = (m_nSeconds > ld.m_nSeconds);
-
- SetTimeFrame(OldTF1);
- ld.SetTimeFrame(OldTF2);
-
- return rVal;
- }
-
- BOOL CLDate::operator>=(CLDate& ld)
- {
- AssertValid();
-
- //Match the two instances timeframes if neccessary
- TimeFrame OldTF1 = GetTimeFrame();
- TimeFrame OldTF2 = ld.GetTimeFrame();
- BOOL bTimeFrameChangeRequired = (OldTF1 != OldTF2);
- if (bTimeFrameChangeRequired)
- {
- TimeFrame OldTF1 = SetTimeFrame(UCT);
- TimeFrame OldTF2 = ld.SetTimeFrame(UCT);
- }
-
- BOOL rVal = (m_nSeconds >= ld.m_nSeconds);
-
- SetTimeFrame(OldTF1);
- ld.SetTimeFrame(OldTF2);
-
- return rVal;
- }
-
- BOOL CLDate::operator<(CLDate& ld)
- {
- AssertValid();
-
- //Match the two instances timeframes if neccessary
- TimeFrame OldTF1 = GetTimeFrame();
- TimeFrame OldTF2 = ld.GetTimeFrame();
- BOOL bTimeFrameChangeRequired = (OldTF1 != OldTF2);
- if (bTimeFrameChangeRequired)
- {
- TimeFrame OldTF1 = SetTimeFrame(UCT);
- TimeFrame OldTF2 = ld.SetTimeFrame(UCT);
- }
-
- BOOL rVal = (m_nSeconds < ld.m_nSeconds);
-
- SetTimeFrame(OldTF1);
- ld.SetTimeFrame(OldTF2);
-
- return rVal;
- }
-
- BOOL CLDate::operator<=(CLDate& ld)
- {
- AssertValid();
-
- //Match the two instances timeframes if neccessary
- TimeFrame OldTF1 = GetTimeFrame();
- TimeFrame OldTF2 = ld.GetTimeFrame();
- BOOL bTimeFrameChangeRequired = (OldTF1 != OldTF2);
- if (bTimeFrameChangeRequired)
- {
- TimeFrame OldTF1 = SetTimeFrame(UCT);
- TimeFrame OldTF2 = ld.SetTimeFrame(UCT);
- }
-
- BOOL rVal = (m_nSeconds <= ld.m_nSeconds);
-
- SetTimeFrame(OldTF1);
- ld.SetTimeFrame(OldTF2);
-
- return rVal;
- }
-
- BOOL CLDate::operator!=(CLDate& ld)
- {
- AssertValid();
- return !operator==(ld);
- }
-
- #ifdef _DEBUG
- void CLDate::AssertValid() const
- {
- CObject::AssertValid();
- ASSERT(IsValid());
- }
- #endif
-
- #ifdef _DEBUG
- void CLDate::Dump(CDumpContext& dc) const
- {
- CObject::Dump(dc);
- if (IsValid())
- dc << Format() << _T("\n");
- else
- dc << _T("Invalid state\n");
- }
- #endif
-
- /*
- CString CLDate::Format(const CString& sFormat=CDate::m_sDefaultFormat) const
- %a Abbreviated weekday name
- %A Full weekday name
- %b Abbreviated month name
- %B Full month name
- %d Day of month as decimal number (01 - 31)
- %j Day of year as decimal number (001 - 366)
- %m Month as decimal number (01 - 12)
- %P AM / PM indicator
- %U Week of year as decimal number
- %w Weekday as decimal number (1 - 7; Sunday is 1)
- %x Date representation for current locale, namely
- Date representation + " " + Time Representation for current locale
- %y Year without century, as decimal number (00 - 99)
- %Y Year with century, as decimal number
- %c Year displayed using C.E.(Current Epoch) / B.C.E (Before Current Epoch) convention e.g. -1023 = 1022 BCE
- %t String containing a representation of the TimeFrame ("UCT", "LOCAL" or "ET")
-
- %H Hours in the day
- %h 12 Hour format Hours in (00 - 12)
- %M Minutes in the hour
- %S Seconds in the minute
- %F MilliSeconds in the second
- %% Percent sign
-
-
- //may also need to include full windows escape character
-
- %% Percent sign
-
- As in the printf function, the # flag may prefix any formatting code.
- In that case, the meaning of the format code is changed as follows.
-
- Format Code Meaning
- %#x Long date representation, appropriate to current locale, namely
- Long Date representation + " " + Time Representation for current locale
-
-
- %#d, %#j, %#m, %#U, %#w, %#y, %#H, %#h, %#M, %#S Remove leading zeros (if any).
- */
- CString CLDate::Format(const CString& sFormat) const
- {
- CString rVal;
- if (IsValid())
- {
- CString sBuffer;
- int sFmtLength = sFormat.GetLength();
-
- for (int i=0; i<sFmtLength; i++)
- {
- TCHAR c = sFormat.GetAt(i);
- if (c == '%')
- {
- ++i;
- if (i < sFmtLength)
- {
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('H'):
- {
- sBuffer.Format(_T("%.02d"), GetCLTimeOfDay().GetHour());
- rVal += sBuffer;
- break;
- }
- case _T('h'):
- {
- sBuffer.Format(_T("%.02d"), GetCLTimeOfDay().GetAMPMHour());
- rVal += sBuffer;
- break;
- }
- case _T('M'):
- {
- sBuffer.Format(_T("%.02d"), GetCLTimeOfDay().GetMinute());
- rVal += sBuffer;
- break;
- }
- case _T('P'):
- {
- rVal += GetCLTimeOfDay().GetAMPMString();
- break;
- }
- case _T('S'):
- {
- sBuffer.Format(_T("%.02d"), GetCLTimeOfDay().GetSecond());
- rVal += sBuffer;
- break;
- }
- case _T('a'):
- {
- rVal += GetCDate().GetAbrStringDayOfWeek();
- break;
- }
- case _T('A'):
- {
- rVal += GetCDate().GetFullStringDayOfWeek();
- break;
- }
- case _T('b'):
- {
- rVal += GetCDate().GetAbrStringMonth();
- break;
- }
- case _T('B'):
- {
- rVal += GetCDate().GetFullStringMonth();
- break;
- }
- case _T('c'):
- {
- rVal += GetCDate().GetStringCEBCEYear();
- break;
- }
- case _T('d'):
- {
- sBuffer.Format(_T("%.02d"), GetCDate().GetDay());
- rVal += sBuffer;
- break;
- }
- case _T('j'):
- {
- sBuffer.Format(_T("%.03d"), GetCDate().DaysSinceJan0());
- rVal += sBuffer;
- break;
- }
- case _T('m'):
- {
- sBuffer.Format(_T("%.02d"), GetCDate().GetMonth());
- rVal += sBuffer;
- break;
- }
- case _T('t'):
- {
- rVal += GetStringTimeFrame();
- break;
- }
- case _T('U'):
- {
- sBuffer.Format(_T("%.02d"), GetCDate().GetWeekOfYear());
- rVal += sBuffer;
- break;
- }
- case _T('w'):
- {
- sBuffer.Format(_T("%d"), GetCDate().GetDayOfWeek());
- rVal += sBuffer;
- break;
- }
- case _T('x'):
- {
- rVal += GetCDate().Format(_T("%x")) + _T(" ") + GetCLTimeOfDay().Format(_T("%x"));
- break;
- }
- case _T('y'):
- {
- LONG Year = GetCDate().GetYear();
- sBuffer.Format(_T("%.02d"), GetCDate().Get2DigitYear());
- rVal += sBuffer;
- break;
- }
- case _T('Y'):
- {
- sBuffer.Format(_T("%ld"), GetCDate().GetYear());
- rVal += sBuffer;
- break;
- }
- case _T('#'):
- {
- if (i < sFmtLength)
- {
- ++i;
- c = sFormat.GetAt(i);
- switch (c)
- {
- case _T('d'):
- {
- sBuffer.Format(_T("%d"), GetCDate().GetDay());
- rVal += sBuffer;
- break;
- }
- case _T('j'):
- {
- sBuffer.Format(_T("%d"), GetCDate().DaysSinceJan0());
- rVal += sBuffer;
- break;
- }
- case _T('m'):
- {
- sBuffer.Format(_T("%d"), GetCDate().GetMonth());
- rVal += sBuffer;
- break;
- }
- case _T('U'):
- {
- sBuffer.Format(_T("%d"), GetCDate().GetWeekOfYear());
- rVal += sBuffer;
- break;
- }
- case _T('x'):
- {
- rVal += GetCDate().Format(_T("%#x")) + _T(" ") + GetCLTimeOfDay().Format(_T("%x"));
- break;
- }
- case _T('y'):
- {
- LONG Year = GetCDate().GetYear();
- sBuffer.Format(_T("%d"), GetCDate().Get2DigitYear());
- rVal += sBuffer;
- break;
- }
- case _T('H'):
- {
- sBuffer.Format(_T("%d"), GetCLTimeOfDay().GetHour());
- rVal += sBuffer;
- break;
- }
- case _T('h'):
- {
- sBuffer.Format(_T("%d"), GetCLTimeOfDay().GetAMPMHour());
- rVal += sBuffer;
- break;
- }
- case _T('M'):
- {
- sBuffer.Format(_T("%d"), GetCLTimeOfDay().GetMinute());
- rVal += sBuffer;
- break;
- }
- case _T('S'):
- {
- sBuffer.Format(_T("%d"), GetCLTimeOfDay().GetSecond());
- rVal += sBuffer;
- break;
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- }
- break;
- }
- default:
- {
- rVal += c;
- break;
- }
- }
- }
- }
- else
- {
- rVal += c;
- }
- }
- }
-
- return rVal;
- }
-
- //Format according to the short date and time format as specified by the control panel
- CString CLDate::Format(DWORD dwFlags) const
- {
- //let the component parts of the long date handle their formatting
- CDate Date = GetCDate();
- CLTimeOfDay Tod = GetCLTimeOfDay();
-
- if (dwFlags & DTF_NOTIMEFRAME)
- return Date.Format() + _T(" ") + Tod.Format(dwFlags);
- else
- return Date.Format() + _T(" ") + Tod.Format(dwFlags) + _T(" ") + Format(_T("%t"));
- }
-
- void CLDate::Serialize(CArchive& ar)
- {
- CObject::Serialize(ar);
-
- if (ar.IsStoring())
- {
- ar << m_nSeconds;
- ar << (WORD) m_TimeFrame;
- ar << m_bValid;
- }
- else
- {
- ar >> m_nSeconds;
- ar >> (WORD&) m_TimeFrame;
- ar >> m_bValid;
- }
- }
-
- ostream& operator<<(ostream& os, const CLDate& ld)
- {
- return os << ld.Format();
- }
-
- CArchive& operator<<(CArchive& ar, CLDate& ld)
- {
- ASSERT(ar.IsStoring());
- ld.Serialize(ar);
- return ar;
- }
-
- CArchive& operator>>(CArchive& ar, CLDate& ld)
- {
- ASSERT(!ar.IsStoring());
- ld.Serialize(ar);
- return ar;
- }
-
-
-
-