All pastes #249830 Raw Edit

Stuff

public text v1 · immutable
#249830 ·published 2006-11-16 05:53 UTC
rendered paste body
#ifndef _DATE_H__
#define _DATE_H__

#include <string>
#include <sstream>

class Date {
  private:
    bool _time;
    std::istringstream _dateStream;
    std::istringstream _timeStream;
    int _intDate;
    int _intTime;
  public:
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;

    bool operator < (const Date&) const;
    bool operator == (const Date&) const;
    Date(int y, int m, int d);
    Date(int y, int m, int d, int h, int min, int s);
    Date(std::string);
};

#endif


//Date.cc

#include "Date.h"
#include <iostream>
#include <sstream>

Date::Date(int y, int m, int d) {
  year = y;
  month = m;
  day = d;
}

Date::Date(int y, int m, int d, int h, int min, int s) {
  year = y;
  month = m;
  day = d;
  hour = h;
  minute = min;
  second = s;
  _time = true;
}

Date::Date(std::string dateString) {
  if (dateString.length() == 15) {
    std::cout << "date _time string detected\n";
    _time = true;
    _timeStream.str(dateString.substr(9));
    dateString = dateString.substr(0,8);
    std::cout << "date: " << dateString << " _time: " << _timeStream.str();
    _timeStream >> _intTime;
    hour = _intTime / 10000;
    minute = _intTime / 100 % 100;
    second = _intTime % 100;
  }
  if (dateString.length() == 8) {
    _dateStream.str(dateString);
    _dateStream >> _intDate;
    year = _intDate / 10000;
    month = _intDate / 100 % 100;
    day = _intDate % 100;
  }
}

bool Date::operator < (const Date& someDate) const {
  if (this->year < someDate.year) return true;
  else if (this->year > someDate.year) return false;
  else if (this->year == someDate.year) {
    if (this->month < someDate.month) return true;
    else if (this->month > someDate.month) return false;
    else if (this->month == someDate.month) {
      if (this->day < someDate.day) return true;
      else if (this->day > someDate.day) return false;
      else if (this->day == someDate.day) {
	if (!_time) return false;
	else {
	  if (this->hour < someDate.hour) return true;
	  else if (this->hour > someDate.hour) return false;
	  else if (this->hour == someDate.hour) {
	    if (this->minute < someDate.minute) return true;
	    else if (this->minute > someDate.minute) return false;
	    else if (this->minute == someDate.minute) {
	      if (this->second < someDate.second) return true;
	      else if (this->second > someDate.second) return false;
	      else if (this->second == someDate.second) return false;
	    }
	  }
	}
      }
    }
  }
}

bool Date::operator == (const Date& someDate) const {
  if (this->year == someDate.year && 
      this->month == someDate.month && 
      this->day == someDate.day) {
    if (!_time)
      return true;
    else
      if (this->hour == someDate.hour && 
	  this->minute == someDate.minute && 
	  this->second == someDate.second)
	return true;
  } else
    return false;
}