Date类实现

要实现一个简单的Date类不是很难,下面简单实现Date的一些基本用处:

要求:

#ifndef DATE_H
#define DATE_H

#include <initializer_list>
#include <string>

class Date {
public:
  /**
  * @brief default constructor
  */
  Date();

  /**
  * @brief constructor with arguments
  */
  Date(int t_year, int t_month, int t_day, int t_hour, int t_minute);

  /**
  * @brief constructor with a string
  */
  Date(std::string dateString);
  /**
  * @brief return the year of a Date
  * @return   a integer indicate the year of a date
  */
  int getYear(void) const;

  /**
  * @brief set the year of a date
  * @param a integer indicate the new year of a date
  */
  void setYear(const int t_year);

  /**
  * @brief return the month of a Date
  * @return   a integer indicate the month of a date
  */
  int getMonth(void) const;

  /**
  * @brief set the month of a date
  * @param a integer indicate the new month of a date
  */
  void setMonth(const int t_month);

  /**
  * @brief return the day of a Date
  * @return   a integer indicate the day of a date
  */
  int getDay(void) const;

  /**
  * @brief set the day of a date
  * @param a integer indicate the new day of a date
  */
  void setDay(const int t_day);

  /**
  * @brief return the hour of a Date
  * @return   a integer indicate the hour of a date
  */
  int getHour(void) const;

  /**
  * @brief set the hour of a date
  * @param a integer indicate the new hour of a date
  */
  void setHour(const int t_hour);

  /**
  * @brief return the minute of a Date
  * @return   a integer indicate the minute of a date
  */
  int getMinute(void) const;

  /**
  * @brief set the minute of a date
  * @param a integer indicate the new minute of a date
  */
  void setMinute(const int t_minute);

  /**
  *   @brief check whether the date is valid or not
  *   @return the bool indicate valid or not
  */
  static bool isValid(const Date t_date);

  /**
  * @brief convert a string to date, if the format is not correct return
  * 0000-00-00/00:00
  * @return a date
  */
  static Date stringToDate(const std::string t_dateString);

  /**
  * @brief convert a date to string, if the format is not correct return
  * 0000-00-00/00:00
  */
  static std::string dateToString(Date t_date);

  /**
  *  @brief overload the assign operator
  */
  Date &operator=(const Date &t_date);

  /**
  * @brief check whether the CurrentDate is equal to the t_date
  */
  bool operator==(const Date &t_date) const;

  /**
  * @brief check whether the CurrentDate is  greater than the t_date
  */
  bool operator>(const Date &t_date) const;

  /**
  * @brief check whether the CurrentDate is  less than the t_date
  */
  bool operator<(const Date &t_date) const;

  /**
  * @brief check whether the CurrentDate is  greater or equal than the t_date
  */
  bool operator>=(const Date &t_date) const;

  /**
  * @brief check whether the CurrentDate is  less than or equal to the t_date
  */
  bool operator<=(const Date &t_date) const;

private:
  int m_year;
  int m_month;
  int m_day;
  int m_hour;
  int m_minute;
};

#endif

实现:

#include "Date.hpp"
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

Date::Date() {
	setYear(0);
	setMonth(0);
	setDay(0);
	setHour(0);
	setMinute(0);
}

Date::Date(int t_year, int t_month, int t_day, int t_hour, int t_minute) {
	setYear(t_year);
	setMonth(t_month);
	setDay(t_day);
	setHour(t_hour);
	setMinute(t_minute);
}

Date::Date(std::string dateString) {
	*this= stringToDate(dateString);
}

int Date::getYear(void) const {
	return m_year;
}

void Date::setYear(const int t_year) {
	m_year= t_year;
}

int Date::getMonth(void) const {
	return m_month;
}

  
void Date::setMonth(const int t_month) {
	m_month= t_month;
}

  
int Date::getDay(void) const {
	return m_day;
}

void Date::setDay(const int t_day) {
	m_day= t_day;
}

int Date::getHour(void) const {
	return m_hour;
}

void Date::setHour(const int t_hour) {
	m_hour= t_hour;
}

int Date::getMinute(void) const {
	return m_minute;
}

void Date::setMinute(const int t_minute) {
	m_minute= t_minute;
}

inline bool dyear(int y) {
	if (y>= 1000&&y<= 9999) {
	return true;
	}
	return false;
}

inline bool dmonth(int m) {
	if (m< 1||m> 12) {
	return false;
	}
	return true;
}

inline bool dhour(int h) {
	if (h< 0||h> 23) {
	return false;
	}
	return true;
}

inline bool dminute(int m) {
	if (m< 0||m> 59) {
	return false;
	}
	return true;
}

bool Date::isValid(const Date t_date) {
	if (!dyear(t_date.getYear())||!dmonth(t_date.getMonth())||!dhour(t_date.getHour())||!dminute(t_date.getMinute()))  // judge year,month,hour,minute
	return false;
	if (t_date.getMonth()== 1||t_date.getMonth()== 3||t_date.getMonth()== 5||t_date.getMonth()== 7|| t_date.getMonth()== 8|| t_date.getMonth()== 10|| t_date.getMonth()== 12) {
	if (t_date.getDay()< 1|| t_date.getDay()> 31) {
	return false;
	}
	}
	if (t_date.getMonth()== 4||t_date.getMonth()== 6||t_date.getMonth()== 9||t_date.getMonth()== 11) {
	if (t_date.getDay()< 1|| t_date.getDay()> 30) {
	return false;
	}
	}
	if (t_date.getYear()% 400== 0|| t_date.getYear()% 4== 0&& t_date.getYear()% 100!= 0) {
		if (t_date.getMonth()== 2&&(t_date.getDay()< 1|| t_date.getDay()> 29)) {
			return false;
		}
	} else {
		if (t_date.getMonth()== 2&&(t_date.getDay()< 1|| t_date.getDay()> 28)) {
			return false;
		}
	}
	return true;
}

Date Date::stringToDate(const std::string t_dateString) {
	Date temp;
	for (int i= 0; i< 16; i++) {  // judge whether the string is vallid.
		if (((i== 4||i== 7)&& t_dateString[i]!= '-')||(i== 10&& t_dateString[i]!= '/')|| (i== 13&& t_dateString[i]!= ':')) return temp;
		if ((i!= 4&& i!= 7&& i!= 10&& i!= 13)&&(t_dateString[i]< '0'||t_dateString[i]> '9')) return temp;
	}
	temp.setYear((t_dateString[0]- 48)* 1000+ (t_dateString[1]- 48)* 100+ (t_dateString[2]- 48)* 10+ t_dateString[3]- 48);
	temp.setMonth((t_dateString[5]- 48)* 10+ t_dateString[6]- 48);
	temp.setDay((t_dateString[8]- 48)* 10+ t_dateString[9]- 48);
	temp.setHour((t_dateString[11]- 48)* 10+ t_dateString[12]- 48);
	temp.setMinute((t_dateString[14]- 48)* 10+ t_dateString[15]- 48);
	return temp;
}

std::string Date::dateToString(Date t_date) {
	if (isValid(t_date)) {
		char s[17];
	    s[0]= t_date.getYear()/ 1000+ 48;
	    s[1]= (t_date.getYear()/ 100)% 10+ 48;
	    s[2]= (t_date.getYear()% 100)/ 10+ 48;
	    s[3]= t_date.getYear()%10 + 48;
	    s[4]= '-';
	    s[5]= t_date.getMonth()/ 10+ 48;
	    s[6]= t_date.getMonth()% 10+ 48;
	    s[7]= '-';
	    s[8]= t_date.getDay()/ 10+ 48;
	    s[9]= t_date.getDay()% 10+ 48;
	    s[10]= '/';
	    s[11]= t_date.getHour()/ 10+ 48;
	    s[12]= t_date.getHour()% 10 +48;
	    s[13]= ':';
	    s[14]= t_date.getMinute()/ 10+ 48;
	    s[15]= t_date.getMinute()% 10+ 48;
	    s[16]= '\0';
	    string str(s);
	    return str;
	} else {
		return "0000-00-00/00:00";
	}
	
}

Date& Date::operator=(const Date &t_date) {
	setYear(t_date.getYear());
	setMonth(t_date.getMonth());
	setDay(t_date.getDay());
	setHour(t_date.getHour());
	setMinute(t_date.getMinute());
	return *this;
}

bool Date::operator==(const Date &t_date) const {
	return (getYear()== t_date.getYear()&& getMonth()== t_date.getMonth()&& getDay()== t_date.getDay()&&
		getHour()== t_date.getHour()&& getMinute()== t_date.getMinute());
}

bool Date::operator>(const Date &t_date) const {
	if (getYear()> t_date.getYear()) return true;
	if (getYear()< t_date.getYear()) return false;
	if (getYear()== t_date.getYear()) {
		if (getMonth()> t_date.getMonth()) return true;
		if (getMonth()< t_date.getMonth()) return false;
		if (getMonth()== t_date.getMonth()) {
			if (getDay()> t_date.getDay()) return true;
			if (getDay()< t_date.getDay()) return false;
			if (getDay()== t_date.getDay()) {
				if (getHour()> t_date.getHour()) return true;
				if (getHour()< t_date.getHour()) return false;
				if (getHour()== t_date.getHour()) {
					if (getMinute()> t_date.getMinute()) return true;
					if (getMinute()< t_date.getMinute()) return false;
					if (getMinute()== t_date.getMinute()) return false;
				}
			}
		}
	}
}

bool Date::operator<(const Date &t_date) const {
	return !(*this== t_date|| *this> t_date);
}

bool Date::operator>=(const Date &t_date) const {
	return (*this> t_date|| *this== t_date);
}

bool Date::operator<=(const Date &t_date) const {
	return !(*this> t_date);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现时钟显示效果的基本思路是: 1. 使用Date获取当前时间; 2. 将时间转换成指定格式的字符串; 3. 将字符串显示在页面上。 具体实现方法如下: 1. 创建一个显示时钟的容器,例如一个div元素。 2. 使用JavaScript的Date获取当前时间,包括时、分、秒。 3. 将时、分、秒转换成指定格式的字符串,格式可以自定义,例如"hh:mm:ss"。 4. 将字符串显示在容器中,可以使用innerHTML或者innerText等方法。 以下是一个简单的实现代码示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>时钟</title> <style> #clock { font-size: 48px; text-align: center; margin-top: 100px; } </style> </head> <body> <div id="clock"></div> <script> function showClock() { var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); var timeStr = formatTime(hour) + ':' + formatTime(minute) + ':' + formatTime(second); document.getElementById('clock').innerHTML = timeStr; } function formatTime(time) { if (time < 10) { return '0' + time; } else { return time; } } setInterval(showClock, 1000); </script> </body> </html> ``` 上述代码中,通过setInterval方法每隔1秒调用一次showClock函数,获取当前时间并显示在时钟容器中。formatTime函数用于将时、分、秒转换成两位数字的格式,例如将2转换成"02"。 该代码可以实现一个简单的时钟效果,但仍有改进的空间,例如可以添加日期、星期等信息,以及美化时钟样式等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值