home *** CD-ROM | disk | FTP | other *** search
- // Anfang TIMECLSS.CPP ---------------------------------
- // Implementation von Timeclass
- //
- #include <iostream.h>
- #include <iomanip.h>
- #include <dos.h>
- #include "timeclss.h"
-
- TimeFlag Time::flag = OK;
- Time::Time (int h, int m, int s) {
- if ( CheckParam (h, m, s) ) {
- hour = h;
- min = m;
- sec = s;
- }
- else
- // Initialisation mit Defaultkonstruktor
- *this = Time();
- }
-
- void Time::SetTo (int h, int m, int s) {
- if ( CheckParam (h, m, s) ) {
- hour = h;
- min = m;
- sec = s;
- }
- }
-
- int Time::CheckParam (int h, int m, int s) {
- if ( (h < 0) || (h > 23) || (m < 0) || (m > 59) ||
- (s < 0) || (s > 59) ) {
- cerr << "Falsche Zeitangabe" << endl;
- return 0;
- }
- else
- return 1;
- }
-
- void Time::SetToSysTime() {
- struct time t;
- gettime(&t);
- hour = int (t.ti_hour);
- min = int (t.ti_min);
- sec = int (t.ti_sec);
- }
-
- Time operator + (const Time& t1, const Time& t2) {
- Time t;
- long sec1 = t1.ConvertToSeconds();
- long sec2 = t2.ConvertToSeconds();
- long ssec = sec1 + sec2;
- t.hour = ssec / 3600;
- Time::flag =
- (t.hour > 23) ? (Time::Overflow) : (Time::OK);
- t.hour %= 24;
- ssec %= 3600;
- t.min = ssec / 60;
- t.sec = ssec % 60;
- return t;
- }
-
- Time operator - (const Time& t1, const Time& t2) {
- Time t;
- long sec1 = t1.ConvertToSeconds();
- long sec2 = t2.ConvertToSeconds();
- long dsec = sec1 - sec2;
- Time::flag =
- (dsec < 0) ? (Time::Underflow) : (Time::OK);
- t.hour = dsec / 3600;
- t.hour %= 24;
- dsec %= 3600;
- t.min = dsec / 60;
- t.sec = dsec % 60;
- return t;
- }
-
- ostream& operator << (ostream& os, const Time& t) {
- char oldfill = os.fill('0');
- os << setw(2) << t.hour << ':'
- << setw(2) << t.min << ':'
- << setw(2) << t.sec;
- os.fill(oldfill);
- return os;
- }
-
- istream& operator >> (istream& is, Time& t) {
- int hour, min, sec;
- is >> hour; cin.get();
- is >> min ; cin.get();
- is >> sec ;
- t = Time (hour, min, sec);
- return is;
- }
- // Ende TIMECLSS.CPP ---------------------------------------