Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / MFC

Calculating Business Hours

Rate me:
Please Sign up or sign in to vote.
1.00/5 (13 votes)
11 Jul 20072 min read 65.2K   1.1K   16  
An article on how to calculate business hours between two dates
/////////////////////////////////////////////////////////////////////////////
// WorkTimeDlg.cpp : implementation file
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "WorkTime.h"
#include "WorkTimeDlg.h"
#include "RPCalcBusinessHours.h"

/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

/////////////////////////////////////////////////////////////////////////////

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWorkTimeDlg dialog

CWorkTimeDlg::CWorkTimeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWorkTimeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWorkTimeDlg)
	m_ctEnd = 0;
	m_ctStart = 0;
	m_csBusHours = _T("");
	m_IsUTC = FALSE;
	m_ctEndTime = 0;
	m_ctStartTime = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

/////////////////////////////////////////////////////////////////////////////

void CWorkTimeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWorkTimeDlg)
	DDX_DateTimeCtrl(pDX, IDC_DT_END, m_ctEnd);
	DDX_DateTimeCtrl(pDX, IDC_DT_START, m_ctStart);
	DDX_Text(pDX, IDC_ED_BUSHOURS, m_csBusHours);
	DDX_Check(pDX, IDC_CHECK_UTC, m_IsUTC);
	DDX_DateTimeCtrl(pDX, IDC_DT_TIMEEND, m_ctEndTime);
	DDX_DateTimeCtrl(pDX, IDC_DT_TIMESTART, m_ctStartTime);
	//}}AFX_DATA_MAP
}

/////////////////////////////////////////////////////////////////////////////

BEGIN_MESSAGE_MAP(CWorkTimeDlg, CDialog)
	//{{AFX_MSG_MAP(CWorkTimeDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWorkTimeDlg message handlers

BOOL CWorkTimeDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	SYSTEMTIME systime;
	CTime ct = CTime::GetCurrentTime();
	
	// set datatimepickers to current date
	if (ct.GetAsSystemTime(systime))
	{
		::SendMessage(GetDlgItem(IDC_DT_START)->m_hWnd, DTM_SETSYSTEMTIME, (WPARAM) GDT_VALID, (LPARAM) &systime);
		::SendMessage(GetDlgItem(IDC_DT_END)->m_hWnd, DTM_SETSYSTEMTIME, (WPARAM) GDT_VALID, (LPARAM) &systime);
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

/////////////////////////////////////////////////////////////////////////////

void CWorkTimeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

/////////////////////////////////////////////////////////////////////////////

void CWorkTimeDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

/////////////////////////////////////////////////////////////////////////////

HCURSOR CWorkTimeDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

/////////////////////////////////////////////////////////////////////////////

void CWorkTimeDlg::OnButton1() 
{
	UpdateData(TRUE);

	double	dBusMinutes		= 0.0;
	double	OverAllHours	= 0.0;

	// initialize our temp times with midnight values
	CTime ctTempStart(m_ctStart.GetYear(), m_ctStart.GetMonth(), m_ctStart.GetDay(), 0, 0, 0);
	CTime ctTempEnd(m_ctEnd.GetYear(), m_ctEnd.GetMonth(), m_ctEnd.GetDay(), 0, 0, 0);
	
	ctTempStart += CTimeSpan(0, m_ctStartTime.GetHour(), m_ctStartTime.GetMinute(), 0);
	ctTempEnd	+= CTimeSpan(0, m_ctEndTime.GetHour(), m_ctEndTime.GetMinute(), 0);

	dBusMinutes = CalculateBusinessMinutes(ctTempStart, ctTempEnd, m_IsUTC);
	
	OverAllHours = dBusMinutes / 60;
	m_csBusHours.Format(_T("%1.2f"), OverAllHours);

	UpdateData(FALSE);
}

/////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer
Germany Germany
Ralph started programming in Turbo Pascal, later in Delphi. After this, he began learning C++, which is his favorite language up to now.

He is interested in almost everything that has to do with computing, his special interests are security and networking.

Comments and Discussions