From: Christian P. <cp...@us...> - 2004-12-26 17:03:05
|
Update of /cvsroot/pclasses/pclasses2/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31213/src Added Files: Date.cpp Log Message: Added Date class --- NEW FILE: Date.cpp --- /*************************************************************************** * Copyright (C) 2004 by Christian Prochnow * * cp...@se... * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "pclasses/Date.h" #include <iomanip> namespace P { Date::Date(unsigned int year, unsigned int month, unsigned int day) throw(InvalidDate) : _year(year), _month(month), _day(day) { if(month > 12 || day > daysInMonth(month, year)) throw InvalidDate("Invalid date", P_SOURCEINFO); } Date::~Date() throw() { } void Date::setYear(unsigned int year) { _year = year; } unsigned int Date::year() const throw() { return _year; } void Date::setMonth(unsigned int month) throw(InvalidDate) { if(month > 12) throw InvalidDate("Invalid month", P_SOURCEINFO); _month = month; } unsigned int Date::month() const throw() { return _month; } unsigned int Date::daysInMonth() throw() { return daysInMonth(_month, _year); } void Date::setDay(unsigned int day) throw(InvalidDate) { unsigned int dim = daysInMonth(_month, _year); if(day <= dim) { _day = day; return; } throw InvalidDate("Invalid day in month", P_SOURCEINFO); } unsigned int Date::day() const throw() { return _day; } double Date::julianDayNumber() const throw() { int a = (14 - _month) / 12; int y = _year + 4800 - a; int m = _month + 12 * a - 3; return (_day + ((153 * m + 2) / 5) + (365 * y) + (y/4) - (y/100) + (y/400) - 32045); } static int _daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static int _daysOfYear[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; unsigned int Date::dayOfYear() const throw() { unsigned int doy = _daysOfYear[_month - 1] + _day; if(isLeapYear(_year) && _month > 1) return doy + 1; return doy; } unsigned int Date::dayOfWeek() const throw() { return ((_year + dayOfYear() + (_year - 1) / 4 - (_year - 1) / 100 + (_year - 1) / 400) % 7); } bool Date::isInLeapYear() const throw() { return isLeapYear(_year); } bool Date::isLeapYear(unsigned int year) throw() { return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); } unsigned int Date::daysInMonth(unsigned int month, unsigned int year) throw(InvalidDate) { unsigned int dim = _daysInMonth[month - 1]; if(isLeapYear(year) && month > 1) return dim + 1; return dim; } bool Date::operator>(const Date& d) const throw() { return (_year > d._year || (_year == d._year && _month > d._month) || (_month == d._month && _day > d._day)); } bool Date::operator<(const Date& d) const throw() { return (_year < d._year || (_year == d._year && _month < d._month) || (_month == d._month && _day < d._day)); } bool Date::operator>=(const Date& d) const throw() { return (operator>(d) || operator==(d)); } bool Date::operator<=(const Date& d) const throw() { return (operator<(d) || operator==(d)); } bool Date::operator==(const Date& d) const throw() { return (_year == d._year && _month == d._month && _day == d._day); } bool Date::operator!=(const Date& d) const throw() { return (!operator==(d)); } std::ostream& operator << (std::ostream& os, const Date& d) { int oldw = os.width(); char oldf = os.fill(); os << std::setfill('0') << std::setw(4) << d.year() << '-' << std::setw(2) << d.month() << '-' << std::setw(2) << d.day() << std::setfill(oldf) << std::setw(oldw); return os; } } // !namespace P |