From: Christian P. <cp...@us...> - 2004-12-26 16:45:57
|
Update of /cvsroot/pclasses/pclasses2/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27931/src Added Files: Time.cpp TimeSpan.cpp Log Message: Added TimeSpan and Time class. --- NEW FILE: Time.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/Time.h" #include <iomanip> namespace P { Time::Time(unsigned int hour, unsigned int minute, unsigned int second, unsigned int usec) throw(InvalidTime) : _hour(hour), _minute(minute), _second(second), _usec(usec) { if(hour > 23 || minute > 59 || second > 59 || usec > ((1000*1000)-1)) throw InvalidTime("Invalid time", P_SOURCEINFO); } Time::~Time() throw() { } void Time::setHour(unsigned int hour) throw(InvalidTime) { if(hour > 23) throw InvalidTime("Invalid hour", P_SOURCEINFO); _hour = hour; } unsigned int Time::hour() const throw() { return _hour; } void Time::setMinute(unsigned int minute) throw(InvalidTime) { if(minute > 59) throw InvalidTime("Invalid minute", P_SOURCEINFO); _minute = minute; } unsigned int Time::minute() const throw() { return _minute; } void Time::setSecond(unsigned int second) throw(InvalidTime) { if(second > 59) throw InvalidTime("Invalid second", P_SOURCEINFO); _second = second; } unsigned int Time::second() const throw() { return _second; } void Time::setUsec(unsigned int usec) throw(InvalidTime) { if(usec > (1000 * 1000) - 1) throw InvalidTime("Invalid hour", P_SOURCEINFO); _usec = usec; } unsigned int Time::usec() const throw() { return _usec; } bool Time::operator>(const Time& t) const throw() { return (_hour > t._hour || (_hour == t._hour && _minute > t._minute) || (_hour == t._hour && _minute == t._minute && _second > t._second) || (_hour == t._hour && _minute == t._minute && _second == t._second && _usec > t._usec)); } bool Time::operator<(const Time& t) const throw() { return (_hour < t._hour || (_hour == t._hour && _minute < t._minute) || (_hour == t._hour && _minute == t._minute && _second < t._second) || (_hour == t._hour && _minute == t._minute && _second == t._second && _usec < t._usec)); } bool Time::operator>=(const Time& t) const throw() { return (operator>(t) || operator==(t)); } bool Time::operator<=(const Time& t) const throw() { return (operator<(t) || operator==(t)); } bool Time::operator==(const Time& t) const throw() { return (_hour == t._hour && _minute == t._minute && _second == t._second && _usec == t._usec); } bool Time::operator!=(const Time& t) const throw() { return (!operator==(t)); } std::ostream& operator << (std::ostream& os, const Time& t) { int oldw = os.width(); char oldf = os.fill(); os << std::setfill('0') << std::setw(2) << t.hour() << ':' << std::setw(2) << t.minute() << ':' << std::setw(2) << t.second() << std::setfill(oldf) << std::setw(oldw); return os; } } // !namespace P --- NEW FILE: TimeSpan.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/TimeSpan.h" namespace P { #define CHECK_OVERFLOW(num, add) \ { \ if(add > (2 ^ (sizeof(num) * 8) - num)) \ throw OverflowError("Overflow", P_SOURCEINFO); \ } TimeSpan::TimeSpan(unsigned int days, unsigned int hours, unsigned int minutes, unsigned int seconds, unsigned int usecs) throw(OverflowError) : _days(days), _hours(hours), _minutes(minutes), _seconds(seconds), _usecs(usecs) { normalize(); } TimeSpan::~TimeSpan() throw() { } void TimeSpan::setDays(unsigned int days) throw(OverflowError) { _days = days; } unsigned int TimeSpan::days() const throw() { return _days; } void TimeSpan::setHours(unsigned int hours) throw(OverflowError) { _hours = hours; if(hours > 23) normalize(); } unsigned int TimeSpan::hours() const throw() { return _hours; } void TimeSpan::setMinutes(unsigned int minutes) throw(OverflowError) { _minutes = minutes; if(minutes > 59) normalize(); } unsigned int TimeSpan::minutes() const throw() { return _minutes; } void TimeSpan::setSeconds(unsigned int seconds) throw(OverflowError) { _seconds = seconds; if(seconds > 59) normalize(); } unsigned int TimeSpan::seconds() const throw() { return _seconds; } void TimeSpan::setUsecs(unsigned int usec) throw(OverflowError) { _usecs = usec; if(usec > ((1000 * 1000) - 1)) normalize(); } unsigned int TimeSpan::usecs() const throw() { return _usecs; } void TimeSpan::normalize() throw(OverflowError) { if(_usecs > (1000 * 1000) - 1) { unsigned int secs = _usecs / (1000 * 1000); CHECK_OVERFLOW(_seconds, secs); _seconds += secs; _usecs -= secs * (1000 * 1000); } if(_seconds > 59) { unsigned int mins = _seconds / 60; CHECK_OVERFLOW(_minutes, mins); _minutes += mins; _seconds -= mins * 60; } if(_minutes > 59) { unsigned int hours = _minutes / 60; CHECK_OVERFLOW(_hours, hours); _hours += hours; _minutes -= hours * 60; } if(_hours > 23) { unsigned int days = _hours / 24; CHECK_OVERFLOW(_days, days); _days += days; _hours -= days * 24; } } bool TimeSpan::operator>(const TimeSpan& sp) const throw() { return (_days > sp._days || (_days == sp._days && _hours > sp._hours) || (_hours == sp._hours && _minutes > sp._minutes) || (_minutes == sp._minutes && _seconds > sp._seconds) || (_seconds == sp._seconds && _usecs > sp._usecs)); } bool TimeSpan::operator<(const TimeSpan& sp) const throw() { return (_days < sp._days || (_days == sp._days && _hours < sp._hours) || (_hours == sp._hours && _minutes < sp._minutes) || (_minutes == sp._minutes && _seconds < sp._seconds) || (_seconds == sp._seconds && _usecs < sp._usecs)); } bool TimeSpan::operator>=(const TimeSpan& sp) const throw() { return (operator>(sp) || operator==(sp)); } bool TimeSpan::operator<=(const TimeSpan& sp) const throw() { return (operator<(sp) || operator==(sp)); } bool TimeSpan::operator==(const TimeSpan& sp) const throw() { return (_days == sp._days && _hours == sp._hours && _minutes == sp._minutes && _seconds == sp._seconds && _usecs == sp._usecs); } bool TimeSpan::operator!=(const TimeSpan& sp) const throw() { return (!operator==(sp)); } TimeSpan& TimeSpan::operator+=(const TimeSpan& sp) throw(OverflowError) { CHECK_OVERFLOW(_days, sp._days); CHECK_OVERFLOW(_hours, sp._hours); CHECK_OVERFLOW(_minutes, sp._minutes); CHECK_OVERFLOW(_seconds, sp._seconds); CHECK_OVERFLOW(_usecs, sp._usecs); _days += sp._days; _hours += sp._hours; _minutes += sp._minutes; _seconds += sp._seconds; _usecs += sp._usecs; normalize(); return *this; } TimeSpan& TimeSpan::operator-=(const TimeSpan& sp) throw() { if(sp._days > _days) _days = 0; else _days -= sp._days; if(sp._hours > _hours) _hours = 0; else _hours -= sp._hours; if(sp._minutes > _minutes) _minutes = 0; else _minutes -= sp._minutes; if(sp._seconds > _seconds) _seconds = 0; else _seconds -= sp._seconds; if(sp._usecs > _usecs) _usecs = 0; else _usecs -= sp._usecs; normalize(); return *this; } TimeSpan operator+(const TimeSpan& sp1, const TimeSpan& sp2) throw(OverflowError) { TimeSpan ret = sp1; ret += sp2; return ret; } TimeSpan operator-(const TimeSpan& sp1, const TimeSpan& sp2) throw() { TimeSpan ret = sp1; ret -= sp2; return ret; } } // !namespace P |