home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / 1991 / 01 / cppkurs / timeclss.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-02  |  2.0 KB  |  94 lines

  1. // Anfang TIMECLSS.CPP    ---------------------------------
  2. // Implementation von Timeclass
  3. //
  4. #include <iostream.h>
  5. #include <iomanip.h>
  6. #include <dos.h>
  7. #include "timeclss.h"
  8.  
  9. TimeFlag Time::flag = OK;
  10. Time::Time (int h, int m, int s) {
  11.     if ( CheckParam (h, m, s) ) {
  12.         hour = h;
  13.         min  = m;
  14.         sec  = s;
  15.     }
  16.     else
  17.         // Initialisation mit Defaultkonstruktor
  18.         *this = Time();
  19. }
  20.  
  21. void Time::SetTo (int h, int m, int s) {
  22.     if ( CheckParam (h, m, s) ) {
  23.         hour = h;
  24.         min  = m;
  25.         sec  = s;
  26.     }
  27. }
  28.  
  29. int Time::CheckParam (int h, int m, int s) {
  30.     if ( (h < 0) || (h > 23) || (m < 0) || (m > 59) ||
  31.          (s < 0) || (s > 59) ) {
  32.         cerr << "Falsche Zeitangabe" << endl;
  33.         return 0;
  34.     }
  35.     else
  36.         return 1;
  37. }
  38.  
  39. void Time::SetToSysTime() {
  40.     struct  time t;
  41.     gettime(&t);
  42.     hour = int (t.ti_hour);
  43.     min  = int (t.ti_min);
  44.     sec  = int (t.ti_sec);
  45. }
  46.  
  47. Time operator + (const Time& t1, const Time& t2) {
  48.     Time t;
  49.     long sec1 = t1.ConvertToSeconds();
  50.     long sec2 = t2.ConvertToSeconds();
  51.     long ssec = sec1 + sec2;
  52.     t.hour = ssec / 3600;
  53.     Time::flag =
  54.       (t.hour > 23) ? (Time::Overflow) : (Time::OK);
  55.     t.hour %= 24;
  56.     ssec %= 3600;
  57.     t.min  = ssec / 60;
  58.     t.sec  = ssec % 60;
  59.     return t;
  60. }
  61.  
  62. Time operator - (const Time& t1, const Time& t2) {
  63.     Time t;
  64.     long sec1 = t1.ConvertToSeconds();
  65.     long sec2 = t2.ConvertToSeconds();
  66.     long dsec = sec1 - sec2;
  67.     Time::flag =
  68.       (dsec < 0) ? (Time::Underflow) : (Time::OK);
  69.     t.hour = dsec / 3600;
  70.     t.hour %= 24;
  71.     dsec %= 3600;
  72.     t.min = dsec / 60;
  73.     t.sec = dsec % 60;
  74.     return t;
  75. }
  76.  
  77. ostream& operator << (ostream& os, const Time& t) {
  78.     char oldfill = os.fill('0');
  79.     os << setw(2) << t.hour << ':'
  80.        << setw(2) << t.min  << ':'
  81.        << setw(2) << t.sec;
  82.     os.fill(oldfill);
  83.     return os;
  84. }
  85.  
  86. istream& operator >> (istream& is, Time& t) {
  87.     int hour, min, sec;
  88.     is >> hour; cin.get();
  89.     is >> min ; cin.get();
  90.     is >> sec ;
  91.     t = Time (hour, min, sec);
  92.     return is;
  93. }
  94. // Ende TIMECLSS.CPP ---------------------------------------