poco/Data/src/Date.cpp

145 lines
2.5 KiB
C++
Raw Normal View History

2012-04-29 18:52:25 +00:00
//
// Date.cpp
//
// $Id: //poco/Main/Data/src/Date.cpp#5 $
//
// Library: Data
// Package: DataCore
// Module: Date
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
2012-04-29 18:52:25 +00:00
//
#include "Poco/Data/Date.h"
#include "Poco/DateTime.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Data/DynamicDateTime.h"
#include "Poco/Dynamic/Var.h"
using Poco::DateTime;
using Poco::Dynamic::Var;
using Poco::NumberFormatter;
namespace Poco {
namespace Data {
Date::Date()
{
DateTime dt;
assign(dt.year(), dt.month(), dt.day());
}
Date::Date(int dateYear, int dateMonth, int dateDay)
2012-04-29 18:52:25 +00:00
{
assign(dateYear, dateMonth, dateDay);
2012-04-29 18:52:25 +00:00
}
Date::Date(const DateTime& dt)
{
assign(dt.year(), dt.month(), dt.day());
}
Date::~Date()
{
}
void Date::assign(int dateYear, int dateMonth, int dateDay)
2012-04-29 18:52:25 +00:00
{
if (dateYear < 0 || dateYear > 9999)
2012-04-29 18:52:25 +00:00
throw InvalidArgumentException("Year must be between 0 and 9999");
if (dateMonth < 1 || dateMonth > 12)
2012-04-29 18:52:25 +00:00
throw InvalidArgumentException("Month must be between 1 and 12");
if (dateDay < 1 || dateDay > DateTime::daysOfMonth(dateYear, dateMonth))
2012-04-29 18:52:25 +00:00
throw InvalidArgumentException("Month must be between 1 and " +
NumberFormatter::format(DateTime::daysOfMonth(dateYear, dateMonth)));
2012-04-29 18:52:25 +00:00
_year = dateYear;
_month = dateMonth;
_day = dateDay;
2012-04-29 18:52:25 +00:00
}
bool Date::operator < (const Date& date) const
2012-04-29 18:52:25 +00:00
{
int dateYear = date.year();
2012-04-29 18:52:25 +00:00
if (_year < dateYear) return true;
else if (_year > dateYear) return false;
2012-04-29 18:52:25 +00:00
else // years equal
{
int dateMonth = date.month();
if (_month < dateMonth) return true;
2012-04-29 18:52:25 +00:00
else
if (_month > dateMonth) return false;
2012-04-29 18:52:25 +00:00
else // months equal
if (_day < date.day()) return true;
}
return false;
}
Date& Date::operator = (const Var& var)
{
2012-09-11 03:32:41 +00:00
#ifndef __GNUC__
// g++ used to choke on this, newer versions seem to digest it fine
// TODO: determine the version able to handle it properly
*this = var.extract<Date>();
#else
*this = var.operator Date();
#endif
2012-04-29 18:52:25 +00:00
return *this;
}
} } // namespace Poco::Data
2012-09-11 03:32:41 +00:00
#ifdef __GNUC__
// only needed for g++ (see comment in Date::operator = above)
2012-04-29 18:52:25 +00:00
namespace Poco {
namespace Dynamic {
using Poco::Data::Date;
using Poco::DateTime;
template <>
Var::operator Date () const
{
2013-02-09 22:50:32 -06:00
VarHolder* pHolder = content();
if (!pHolder)
2012-04-29 18:52:25 +00:00
throw InvalidAccessException("Can not convert empty value.");
2013-02-09 22:50:32 -06:00
if (typeid(Date) == pHolder->type())
2012-04-29 18:52:25 +00:00
return extract<Date>();
else
{
Poco::DateTime result;
2013-02-09 22:51:28 -06:00
pHolder->convert(result);
2012-04-29 18:52:25 +00:00
return Date(result);
}
}
} } // namespace Poco::Dynamic
2012-09-11 03:32:41 +00:00
#endif // __GNUC__