date_tim.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 1,709 行 · 第 1/5 页

C
1,709
字号
}


// operator+= -- Add a time interval to a CoolDate_Time object, assign
//               the result, and update the tm structure
// Input:        "n" seconds representing time interval
// Output:       CoolDate_Time reference

CoolDate_Time& CoolDate_Time::operator+= (long n) {
  this->time_seconds += n;
  this->resolve ();
  return (*this);
}


// operator-= -- Subtract a time interval from a CoolDate_Time object,
//               assign the result, and update the tm structure
// Input:        "n" seconds representing time interval
// Output:       CoolDate_Time reference

CoolDate_Time& CoolDate_Time::operator-= (long n) {
  this->time_seconds -= n;
  this->resolve ();
  return (*this);
}


// operator>> -- Input CoolDate_Time object from input stream. If a time zone is
//               also entered, it will be set too.
// Input:        Input stream reference, CoolDate_Time reference
// Output:       CoolDate_Time reference

istream& operator>> (istream& is, CoolDate_Time& d) {
  char* s = "";                                 // Temporary pointer
  is.get(s,256);                                // Read upto 256 characters
  d.parse(s,1);                                 // Parse input string
  return is;                                    // Return stream object
}


// incr_sec -- Advance Date/Time by "n" second(s)
// Input:      Number of seconds to advance (default 1)
// Output:     None -- CoolDate_Time object updated 

void CoolDate_Time::incr_sec (int n) {
  this->time_seconds += n;
  this->resolve ();
}


// incr_min -- Advance Date/Time by "n" minute(s)
// Input:      Number of minutes to advance (default 1)
// Output:     None -- CoolDate_Time object updated 

void CoolDate_Time::incr_min (int n) {
  this->time_seconds += (MINUTE * n);
  this->resolve ();
}


// incr_hour -- Advance Date/Time by "n" hour(s)
// Input:       Number of hours to advance (default 1)
// Output:      None -- CoolDate_Time object updated 

void CoolDate_Time::incr_hour (int n) {
  this->time_seconds += (HOUR * n);
  this->resolve ();
}


// incr_day -- Advance Date/Time by "n" day(s)
// Input:      Number of days to advance (default 1)
// Output:     None -- CoolDate_Time object updated 

void CoolDate_Time::incr_day (int n) {
  this->time_seconds += (DAY * n);
  this->resolve ();
}


// incr_week -- Advance Date/Time by "n" week(s)
// Input:       Number of weeks to advance (default 1)
// Output:      None -- CoolDate_Time object updated 

void CoolDate_Time::incr_week (int n) {
  this->time_seconds += (WEEK * n);
  this->resolve ();
}


// incr_month -- Advance Date/Time by "n" month(s)
// Input:        Number of months to advance (default 1)
// Output:       None -- CoolDate_Time object updated

void CoolDate_Time::incr_month (int n) {
  int temp_year = this->dt.tm_year;
  int temp_month = this->dt.tm_mon;
  for (int i = 0; i < n; i++, temp_month++) {
    if (temp_month > DECEMBER) {
      temp_month = JANUARY;
      temp_year++;
    }
    time_seconds += (DAY * days_in_month[temp_month]);
    if (temp_month == FEBRUARY && IS_LEAP_YEAR (temp_year))
      time_seconds += DAY;
  }
  this->resolve ();
}


// incr_year -- Advance Date/Time by "n" year(s)
// Input:       Number of years to advance (default 1)
// Output:      None -- CoolDate_Time object updated 

void CoolDate_Time::incr_year (int n) {
  int temp_year = this->dt.tm_year;
  for (int i = 0; i < n; i++, temp_year++) {
    time_seconds += (YEAR+DAY);
    if ((IS_LEAP_YEAR (temp_year) && (this->dt.tm_mon < FEBRUARY || 
        (this->dt.tm_mon == FEBRUARY && this->dt.tm_mday != 29))) ||
        (IS_LEAP_YEAR (temp_year+1) && this->dt.tm_mon > FEBRUARY))
      time_seconds += DAY;
  }
  this->resolve ();
}


// decr_month -- Retreat Date/Time by "n" month(s)
// Input:        Number of months to retreat (default 1)
// Output:       None -- CoolDate_Time object updated

void CoolDate_Time::decr_month (int n) {
  int temp_year = this->dt.tm_year;
  int temp_month = this->dt.tm_mon;
  for (int i = 0; i < n; i++, temp_month--) {
    if (temp_month < JANUARY) {
      temp_month = DECEMBER;
      temp_year--;
    }
    time_seconds -= (DAY * days_in_month[temp_month]);
    if (temp_month == FEBRUARY && IS_LEAP_YEAR (temp_year))
      time_seconds -= DAY;
  }
  this->resolve ();
}


// decr_year -- Retreat Date/Time by "n" year(s)
// Input:       Number of years to retreat (default 1)
// Output:      None -- CoolDate_Time object updated 

void CoolDate_Time::decr_year (int n) {
  int temp_year = this->dt.tm_year;
  for (int i = 0; i < n; i++, temp_year--) {
    time_seconds -= (YEAR+DAY);
    if ((IS_LEAP_YEAR (temp_year) && (this->dt.tm_mon > FEBRUARY ||
        (this->dt.tm_mon == FEBRUARY && this->dt.tm_mday == 29))) ||
        (IS_LEAP_YEAR (temp_year-1) && this->dt.tm_mon <= FEBRUARY))
      time_seconds -= DAY;
  }
  this->resolve ();
}


// start_min -- Increment CoolDate_Time object to start of "n" minutes
// Input:       Number of minutes to advance to start (default 1)
//              where time is set to HH:MM:00
// Output:      None -- CoolDate_Time object updated

void CoolDate_Time::start_min (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec);
  incr_min (n-1);
}


// end_min -- Increment CoolDate_Time object to end of "n" minutes
//            time is set to HH:MM:59
// Input:     Number of minutes to advance to end (default 1)
// Output:    None -- CoolDate_Time object updated

void CoolDate_Time::end_min (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec -1);
  incr_min (n-1);
}


// start_hour -- Increment CoolDate_Time object to start of "n" hours
// Input:        Number of hours to advance to start (default 1)
//               where time is set to HH:00:00
// Output:       None -- CoolDate_Time object updated

void CoolDate_Time::start_hour (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  incr_hour (n-1);
}


// end_hour --   Increment CoolDate_Time object to end of "n" hours
//               time is set to HH:59:59
// Input:        Number of hours to advance to end (default 1)
// Output:       None -- CoolDate_Time object updated

void CoolDate_Time::end_hour (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec - 1);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  incr_hour (n-1);
}


// start_day -- Increment CoolDate_Time object to start of "n" days
//              time set to 00:00:00
// Input:       Number of days to advance to start (default 1)
// Output:      None -- CoolDate_Time object updated

void CoolDate_Time::start_day (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);
  incr_day (n-1);
}


// end_day -- Increment CoolDate_Time object to end of "n" days
//            time set to 23:59:59
// Input:     Number of days to advance to end (default 1)
// Output:    None -- CoolDate_Time object updated

void CoolDate_Time::end_day (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec - 1);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);
  incr_day (n-1);
}


// start_week -- Increment CoolDate_Time object to start of "n" weeks
//               Date/time set to Monday 00:00:00
// Input:        Number of weeks to advance to start (default 1)
// Output:       None -- CoolDate_Time object updated

void CoolDate_Time::start_week (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);
  this->time_seconds += (WEEK - (this->dt.tm_wday * DAY));
  this->time_seconds += DAY;
  incr_week (n-1);
}


// end_week -- Increment CoolDate_Time object to end of "n" weeks
//             Date/Time set to Sunday 23:59:59
// Input:      Number of weeks to advance to end (default 1)
// Output:     None -- CoolDate_Time object updated

void CoolDate_Time::end_week (int n) {
  this->time_seconds += (MINUTE - this->dt.tm_sec - 1);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);
  this->time_seconds += (WEEK - (this->dt.tm_wday * DAY) - DAY);
  this->time_seconds += DAY;
  incr_week (n-1);
}


// start_month -- Increment CoolDate_Time object to start of "n" months
//                Date/time set to 01/MM/YYYY 00:00:00
// Input:         Number of months to advance to start (default 1)
// Output:        None -- CoolDate_Time object updated

void CoolDate_Time::start_month (int n) {
  int temp_day = this->dt.tm_mday;              // Date in month
  int temp_month = this->dt.tm_mon;             // Current month
  this->time_seconds += (MINUTE - this->dt.tm_sec);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);
  for ( ; temp_day < days_in_month[temp_month]; temp_day++)
    this->time_seconds += DAY;
  if (temp_month == FEBRUARY && IS_LEAP_YEAR (this->dt.tm_year))
    this->time_seconds += DAY;
  incr_month (n-1);
}


// end_month -- Increment CoolDate_Time object to end of "n" months
//              Date/time set to 31/MM/YYYY 23:59:59
// Input:       Number of months to advance to end (default 1)
// Output:      None -- CoolDate_Time obejct updated

void CoolDate_Time::end_month (int n) {
  int temp_day = this->dt.tm_mday;              // Date in month
  int temp_month = this->dt.tm_mon;             // Current month
  this->time_seconds += (MINUTE - this->dt.tm_sec - 1);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);
  for ( ; temp_day < days_in_month[temp_month]; temp_day++)
    this->time_seconds += DAY;
  if (temp_month == FEBRUARY && IS_LEAP_YEAR (this->dt.tm_year))
    this->time_seconds += DAY;
  incr_month (n-1);
}


// start_year -- Increment CoolDate_Time object to start of "n" years
//               Date/Time set to 01/01/YYYY 00:00:00
// Input:        Number of years to advance to start (default 1)
// Output:       None -- CoolDate_Time object updated

void CoolDate_Time::start_year (int n) {
  int temp_year = this->dt.tm_year;
  int temp_day = this->dt.tm_mday;              // Date in month
  int temp_month = this->dt.tm_mon;             // Current month
  this->time_seconds += (MINUTE - this->dt.tm_sec);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);
  this->time_seconds += HOUR;
  for ( ; temp_day < days_in_month[temp_month]; temp_day++)
    this->time_seconds += DAY;
  if (temp_month == FEBRUARY && IS_LEAP_YEAR (temp_year))
    this->time_seconds += DAY;
  for (++temp_month; temp_month <= DECEMBER; temp_month++) {
    this->time_seconds += (DAY * days_in_month[temp_month]);
    if (temp_month == FEBRUARY && IS_LEAP_YEAR (temp_year))
      time_seconds += DAY;
  }
  incr_year (n-1);
}
  

// end_year -- Increment CoolDate_Time object to end of "n" years
//             Date/time set to 31/12/YYYY 23:59:59
// Input:      Number of years to advance to end (default 1)
// Output:     None -- CoolDate_Time object updated

void CoolDate_Time::end_year (int n) {
  int temp_year = this->dt.tm_year;
  int temp_day = this->dt.tm_mday;              // Date in month
  int temp_month = this->dt.tm_mon;             // Current month
  this->time_seconds += (MINUTE - this->dt.tm_sec - 1);
  this->time_seconds += (HOUR - (this->dt.tm_min * MINUTE) - MINUTE);
  this->time_seconds += (DAY - (this->dt.tm_hour * HOUR) - HOUR);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?