/////////////////////////////////////////////////////////// // Softwarebauelemente II, C2.1 // // author: Stephan Brumme // last changes: August 2, 2001 #include "stdafx.h" #include "Moment.h" // we are using OS-specific date/time operations #include #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif // serializable IMPLEMENT_SERIAL(CMoment, CObject, 1) // constructor, default value is current date and time CMoment::CMoment() { GetCurrent(m_nDay, m_nMonth, m_nYear, m_nHour, m_nMinute, m_nSecond); } // copy constructor CMoment::CMoment(const CMoment& moment) { operator=(moment); } // set user defined date/time at construction time CMoment::CMoment(unsigned int nDay, unsigned int nMonth, unsigned int nYear, unsigned int nHour, unsigned int nMinute, unsigned int nSecond) { // store date m_nDay = nDay; m_nMonth = nMonth; m_nYear = nYear; // store time m_nHour = nHour; m_nMinute = nMinute; m_nSecond = nSecond; } // destructor, nothing left to do ... CMoment::~CMoment() { } // serialization void CMoment::Serialize(CArchive &ar) { // do not forget to serialize CObject's data members CObject::Serialize(ar); if (ar.IsStoring()) ar << m_nYear << m_nMonth << m_nDay << m_nHour << m_nMinute << m_nSecond; else ar >> m_nYear >> m_nMonth >> m_nDay >> m_nHour >> m_nMinute >> m_nSecond; } // validate a moment void CMoment::AssertValid() const { // validate month ASSERT(m_nMonth >= JANUARY && m_nMonth <= DECEMBER); // days per month, february may vary, note that array starts with 0 (JANUARY=1 !) unsigned int nDaysPerMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // adjust days of february if (IsLeapYear(m_nYear)) nDaysPerMonth[FEBRUARY]++; // validate day ASSERT(m_nDay > 0 && m_nDay <= nDaysPerMonth[m_nMonth]); // day and month are valid // now check the time ASSERT(m_nHour >= 0 && m_nHour <= 23); ASSERT(m_nMinute >= 0 && m_nMinute <= 59); ASSERT(m_nSecond >= 0 && m_nSecond <= 59); } // dump void CMoment::Dump(CDumpContext& dc) const { CObject::Dump(dc); CString strOutput; strOutput.Format("date: %02d.%02d.%04d\ntime: %02d:%02d:%02d", m_nDay, m_nMonth, m_nYear, m_nHour, m_nMinute, m_nSecond); dc << strOutput; } // return current moment void CMoment::GetCurrent(unsigned int& nDay, unsigned int& nMonth, unsigned int& nYear, unsigned int& nHour, unsigned int& nMinute, unsigned int& nSecond) { // the following code is basically taken from MSDN // use system functions to get the current date as UTC time_t secondsSince1970; time(&secondsSince1970); // convert UTC to local time zone struct tm *localTime; localTime = localtime(&secondsSince1970); // store retrieved date nDay = localTime->tm_mday; nMonth = localTime->tm_mon + 1; nYear = localTime->tm_year + 1900; // store time nHour = localTime->tm_hour; nMinute = localTime->tm_min; nSecond = localTime->tm_sec; } // determine whether it is a leap year bool CMoment::IsLeapYear(unsigned int nYear) { // used to speed up code, may be deleted if (nYear % 4 != 0) return false; // algorithm taken from MSDN, just converted from VBA to C++ if (nYear % 400 == 0) return true; if (nYear % 100 == 0) return false; if (nYear % 4 == 0) return true; // this line won't be executed because of optimization (see above) return false; } // copy CMoment& CMoment::operator =(const CMoment& moment) { // date m_nDay = moment.m_nDay; m_nMonth = moment.m_nMonth; m_nYear = moment.m_nYear; // time m_nHour = moment.m_nHour; m_nMinute = moment.m_nMinute; m_nSecond = moment.m_nSecond; return *this; } // see operator= CMoment& CMoment::Copy(const CMoment& moment) { return operator=(moment); } // accept CTime, too CMoment& CMoment::operator =(const CTime& time) { // date m_nDay = time.GetDay(); m_nMonth = time.GetMonth(); m_nYear = time.GetYear(); // time m_nHour = time.GetHour(); m_nMinute = time.GetMinute(); m_nSecond = time.GetSecond(); return *this; } // compare two moments bool CMoment::operator ==(const CMoment &moment) const { // compare moment and time attributes return (m_nDay == moment.m_nDay && m_nMonth == moment.m_nMonth && m_nYear == moment.m_nYear && m_nHour == moment.m_nHour && m_nMinute == moment.m_nMinute && m_nSecond == moment.m_nSecond); } // see operator== bool CMoment::EqualValue(const CMoment &moment) const { return operator==(moment); } // convert to MFC's CTime CMoment::operator CTime() const { return CTime(m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond); } // set attributes, returns validility of moment void CMoment::SetDay(unsigned int nDay) { m_nDay = nDay; } void CMoment::SetMonth(unsigned int nMonth) { m_nMonth = nMonth; } void CMoment::SetYear(unsigned int nYear) { m_nYear = nYear; } void CMoment::SetHour(unsigned int nHour) { m_nHour = nHour; } void CMoment::SetMinute(unsigned int nMinute) { m_nMinute = nMinute; } void CMoment::SetSecond(unsigned int nSecond) { m_nSecond = nSecond; } // return attributes unsigned int CMoment::GetDay() const { return m_nDay; } unsigned int CMoment::GetMonth() const { return m_nMonth; } unsigned int CMoment::GetYear() const { return m_nYear; } unsigned int CMoment::GetHour() const { return m_nHour; } unsigned int CMoment::GetMinute() const { return m_nMinute; } unsigned int CMoment::GetSecond() const { return m_nSecond; }