Click here to Skip to main content
14,217,673 members

Linq-To-XML Style of Node Creation for C++

Rate this:
4.78 (14 votes)
Please Sign up or sign in to vote.
4.78 (14 votes)
12 Apr 2016Ms-PL
Linq-To-XML Node Creation for Native C++
BuildProcessTemplates
Elmax.vsmdi
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
TryoutNet
Properties
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax-ver089
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
TryoutNet
Properties
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
#include "StdAfx.h"
#include "DateAndTime.h"

using namespace Elmax;

DateAndTime::DateAndTime(void)
: m_nYear(0)
, m_nMonth(1)
, m_nMonthDay(1)
, m_nHour(0)
, m_nMinute(0)
, m_nSecond(0)
{
}

DateAndTime::DateAndTime(short year, short month, short day, short hour, short minute, short second)
{
	try
	{
		SetDateTime(year, month, day, hour, minute, second);
	}
	catch(...)
	{
		m_nYear = 0;
		m_nMonth = 1;
		m_nMonthDay = 1;
		m_nHour = 0;
		m_nMinute = 0;
		m_nSecond = 0;
	}
}

DateAndTime::~DateAndTime(void)
{
}

bool DateAndTime::SetDateTime(short year, short month, short day, short hour, short minute, short second)
{
	if(month<1||month>12)
		throw std::exception("Invalid month [1 - 12]");
	if(hour<0||hour>23)
		throw std::exception("Invalid hour [0 - 23]");
	if(minute<0||minute>59)
		throw std::exception("Invalid minute [0 - 59]");
	if(second<0||second>59)
		throw std::exception("Invalid second [0 - 59]");

	switch(month)
	{
	case 1:
		if(day<1||day>31)
			throw std::exception("Invalid day in January");
		break;
	case 2:
		if(day<1||day>29)
			throw std::exception("Invalid day in February");
		break;
	case 3:
		if(day<1||day>31)
			throw std::exception("Invalid day in March");
		break;
	case 4:
		if(day<1||day>30)
			throw std::exception("Invalid day in April");
		break;
	case 5:
		if(day<1||day>31)
			throw std::exception("Invalid day in May");
		break;
	case 6:
		if(day<1||day>30)
			throw std::exception("Invalid day in June");
		break;
	case 7:
		if(day<1||day>31)
			throw std::exception("Invalid day in July");
		break;
	case 8:
		if(day<1||day>31)
			throw std::exception("Invalid day in August");
		break;
	case 9:
		if(day<1||day>30)
			throw std::exception("Invalid day in September");
		break;
	case 10:
		if(day<1||day>31)
			throw std::exception("Invalid day in October");
		break;
	case 11:
		if(day<1||day>30)
			throw std::exception("Invalid day in November");
		break;
	case 12:
		if(day<1||day>31)
			throw std::exception("Invalid day in December");
		break;
	}

	m_nYear = year;
	m_nMonth = month;
	m_nMonthDay = day;
	m_nHour = hour;
	m_nMinute = minute;
	m_nSecond = second;

	return true;
}

void DateAndTime::SetString(const std::wstring& strDate)
{
	if(strDate.length()<10)
		throw std::exception("Invalid date string");

	std::wstring year = L"";
	for(size_t i=0; i<4; ++i)
	{
		if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			year += strDate.at(i);
		else
			throw std::exception("Invalid character in year, instead of numbers");
	}

	short nYear = (short) (_wtoi(year.c_str()));

	std::wstring month = L"";
	for(size_t i=5; i<7; ++i)
	{
		if(i==5&&strDate.at(i)==L'0')
			continue;
		else if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			month += strDate.at(i);
		else
			throw std::exception("Invalid character in month, instead of numbers");
	}

	short nMonth = (short) (_wtoi(month.c_str()));

	std::wstring monthday = L"";
	for(size_t i=8; i<10; ++i)
	{
		if(i==8&&strDate.at(i)==L'0')
			continue;
		else if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			monthday += strDate.at(i);
		else
			throw std::exception("Invalid character in month, instead of numbers");
	}

	short nMonthDay = (short) (_wtoi(monthday.c_str()));

	std::wstring hour = L"";
	for(size_t i=11; i<13; ++i)
	{
		if(i==11&&strDate.at(i)==L'0')
			continue;
		else if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			hour += strDate.at(i);
		else
			throw std::exception("Invalid character in month, instead of numbers");
	}

	short nHour = (short) (_wtoi(hour.c_str()));

	std::wstring minute = L"";
	for(size_t i=14; i<16; ++i)
	{
		if(i==14&&strDate.at(i)==L'0')
			continue;
		else if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			minute += strDate.at(i);
		else
			throw std::exception("Invalid character in month, instead of numbers");
	}

	short nMinute = (short) (_wtoi(minute.c_str()));

	std::wstring second = L"";
	for(size_t i=17; i<19; ++i)
	{
		if(i==17&&strDate.at(i)==L'0')
			continue;
		else if(strDate.at(i)>=L'0'&&strDate.at(i)<=L'9')
			second += strDate.at(i);
		else
			throw std::exception("Invalid character in month, instead of numbers");
	}

	short nSecond = (short) (_wtoi(second.c_str()));

	SetDateTime(nYear, nMonth, nMonthDay, nHour, nMinute, nSecond);
}

std::wstring DateAndTime::GetString() const
{
	wchar_t buf[40];
	memset(buf, 0, sizeof(buf));
	StringCchPrintfW(buf, sizeof(buf)/sizeof(wchar_t), L"%04d-%02d-%02d %02d:%02d:%02d", m_nYear, m_nMonth, m_nMonthDay, m_nHour, m_nMinute, m_nSecond);

	std::wstring strDate = buf;

	return strDate;
}

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, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Shao Voon Wong
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His CodeProject contributions could range from being used in game changer Large Hadron Collider (LHC) of CERN to smash particles to something as frivolous as Maplestory game to as life changing as electrocardiography(ECG) project to everyday indispensable Irfanview image viewer.

CodeProject bestowed him a MVP award in recognition of his article contributions in 2019. In his spare time, he prefers to writing application based on 3rd party library than writing his own open source library. His interest lies primarily in computer graphics, software optimization, security and Agile methodologies.

One of these days, you can find him working on a DirectX based slideshow app called Mandy Frenzy.

You can reach him by sending a message on CodeProject or at his Coding Tidbit Blog!

Stats

34.4K views
477 downloads
29 bookmarked