articlewritingcafe.com –
#include <iostream>#include <cstring>#include <ctime>#include “Date.h”namespace sdds { int Date::currentYear() { time_t current_time; current_time = time(NULL); int year = 1970 + current_time / 31537970; return year; } int Date::currentMonth() { std::time_t current_time; current_time = std::time(NULL); std::tm* now = std::localtime(¤t_time); int month = now->tm_mon + 1; return month; } int Date::currentDay() { std::time_t current_time; current_time = std::time(NULL); std::tm* now = std::localtime(¤t_time); int day = now->tm_mday; return day; } int Date::numOfDays(int mon, int year) const { int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, -1 }; int month = mon >= 1 && mon <= 12 ? mon : 13; month–; return days[month] + int((month == 1) * ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); } bool Date::validate() { bool isValid; int min_year = currentYear(); if (m_year < min_year || m_year > max_year) { State = “Invalid year in date”; State = 1; isValid = false; } else if (m_month < 1 || m_month > 12) { State = “Invalid month in date”; State = 2; isValid = false; } else if (m_day < 1 || m_day > numOfDays(m_month, m_year)) { State = “Invalid day in date”; State = 3; isValid = false; } else { State.clear(); isValid = true; } return isValid; } Date::Date() { m_year = currentYear(); m_month = currentMonth(); m_day = currentDay(); m_formatted = true; } Date::Date(int year, int month, int day) { m_year = year; m_month = month; m_day = day; m_formatted = true; } int Date::uniqueDateValue()const { return m_year * 372 + m_month * 31 + m_day; } bool Date::operator==(const Date& data) const { return (this->uniqueDateValue() == data.uniqueDateValue()); } bool Date::operator!=(const Date& data) const { return (this->uniqueDateValue() != data.uniqueDateValue()); } bool Date::operator<(const Date& data) const { return (this->uniqueDateValue() < data.uniqueDateValue()); } bool Date::operator>(const Date& data)const { return (this->uniqueDateValue() > data.uniqueDateValue()); } bool Date::operator<=(const Date& data)const { return (this->uniqueDateValue() <= data.uniqueDateValue()); } bool Date::operator>=(const Date& data)const { return (this->uniqueDateValue() >= data.uniqueDateValue()); } const Status& Date::state() { return State; } Date& Date::formatted(bool formatted) { m_formatted = formatted; return *this; } std::ostream& Date::write(std::ostream& ostr) const { if (m_formatted == true) { ostr << m_year << “/”; if (m_month < 10) { ostr << “0” << m_month << “/”; } else { ostr << m_month << “/”; } if (m_day < 10) { ostr << “0” << m_day; } else { ostr << m_day; } } else if (m_formatted == false) { ostr << m_year % 100; if (m_month < 10) { ostr << “0” << m_month; } else { ostr << m_month; } if (m_day < 10) { ostr << “0” << m_day; } else { ostr << m_day; } } return ostr; } std::istream& Date::read(std::istream& istr) { int date, size; int year, month, day; string tmp; istr >> date; tmp = to_string(date); size = tmp.size(); if (size == 2) { m_year = currentYear(); m_month = 0; m_day = date; } else if (size == 4) { m_year = currentYear(); m_month = date / 100; m_day = date % 100; } else if (size == 6) { m_year = 2000 + date / 10000; m_month = (date / 100) % 100; m_day = date % 100; } else { cout << “Invalid date value”; } if (validate() == false) istr.std::istream::setstate(ios::badbit); return istr; } std::ostream& operator<<(std::ostream& ostr, Date const& data) { return data.write(ostr); } std::istream& operator>>(std::istream& istr, Date& data) { return data.read(istr); }}
The post articlewritingcafe.com – Date.cpp appeared first on Articlewritingcafe.