构造一个日期时间类Timedate,实现对日期(年月日)时间(时分秒)的设置及输出
#include <iostream>
#include <iomanip>
using namespace std;
/**********Program**********/
class Timedate
{
public:
Timedate(int syear = 2000, int smonth = 1, int sdate = 1)
{ //构造函数完成所有私有成员的初始化
year = syear;
month = smonth;
date = sdate;
hh = 00;
mm = 00;
ss = 00;
}
void list(); //完成判断
void putdate(int syear, int smonth, int sdate)
{ //完成年月日的设置
year = syear;
month = smonth;
date = sdate;
}
void puttime(int hour, int minute, int second) //内联函数:完成时分秒的设置
{
hh = hour;
mm = minute;
ss = second;
}
private:
int year, month, date, hh, mm, ss;
};
/********** End **********/
void Timedate::list()
{
cout << "year/month/date :";
switch (year)
{
case 2000:
cout << "2000";
break;
case 2001:
cout << "2001";
break;
case 2002:
cout << "2002";
break;
case 2003:
cout << "2003";
break;
case 2004:
cout << "2004";
break;
case 2005:
cout << "2005";
break;
}
switch (month)
{
case 1:
cout << '/' << "Jan";
break;
case 2:
cout << '/' << "Feb";
break;
case 3:
cout << '/' << "Mar";
break;
case 4:
cout << '/' << "Apr";
break;
case 5:
cout << '/' << "May";
break;
case 6:
cout << '/' << "Jun";
break;
case 7:
cout << '/' << "Jul";
break;
case 8:
cout << '/' << "Aug";
break;
case 9:
cout << '/' << "Sep";
break;
case 10:
cout << '/' << "Oct";
break;
case 11:
cout << '/' << "Nov";
break;
case 12:
cout << '/' << "Dec";
break;
}
cout << '/' << date << endl;
cout << "hour:minite:second :";
cout << hh << ':' << mm << ':' << ss << endl;
}
int main()
{
Timedate A(2004, 3, 3), B;
A.list();
B.list();
B.putdate(2005, 10, 18);
B.puttime(17, 30, 10);
B.list();
return 0;
}
#include <iostream>
#include <ctime>
#include <iomanip>
class Timedate
{
public:
Timedate()
{
_time = std::time(nullptr);
}
Timedate(int year, int month, int day, int hour, int minute, int second)
{
std::tm tm{};
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_hour = hour;
tm.tm_min = minute;
tm.tm_sec = second;
_time = std::mktime(&tm);
}
int year() const
{
std::tm *tm = std::localtime(&_time);
return tm->tm_year + 1900;
}
void setYear(int year)
{
std::tm *tm = std::localtime(&_time);
tm->tm_year = year - 1900;
_time = std::mktime(tm);
}
int month() const
{
std::tm *tm = std::localtime(&_time);
return tm->tm_mon + 1;
}
void setMonth(int month)
{
std::tm *tm = std::localtime(&_time);
tm->tm_mon = month - 1;
_time = std::mktime(tm);
}
int day() const
{
std::tm *tm = std::localtime(&_time);
return tm->tm_mday;
}
void setDay(int day)
{
std::tm *tm = std::localtime(&_time);
tm->tm_mday = day;
_time = std::mktime(tm);
}
int hour() const
{
std::tm *tm = std::localtime(&_time);
return tm->tm_hour;
}
void setHour(int hour)
{
std::tm *tm = std::localtime(&_time);
tm->tm_hour = hour;
_time = std::mktime(tm);
}
int minute() const
{
std::tm *tm = std::localtime(&_time);
return tm->tm_min;
}
void setMinute(int minute)
{
std::tm *tm = std::localtime(&_time);
tm->tm_min = minute;
_time = std::mktime(tm);
}
int second() const
{
std::tm *tm = std::localtime(&_time);
return tm->tm_sec;
}
void setSecond(int second)
{
std::tm *tm = std::localtime(&_time);
tm->tm_sec = second;
_time = std::mktime(tm);
}
private:
std::time_t _time;
};
std::ostream &operator<<(std::ostream &os, const Timedate &timedate)
{
std::cout << timedate.year() << '-' << timedate.month() << '-' << timedate.day() << ' '
<< std::setw(2) << std::setfill('0') << timedate.hour() << ':'
<< std::setw(2) << std::setfill('0') << timedate.minute() << ':'
<< std::setw(2) << std::setfill('0') << timedate.second();
return os;
}
int main()
{
Timedate currentDateTime;
std::cout << "当前日期和时间是: " << currentDateTime << std::endl;
int year, month, day, hour, minute, second;
char ch;
std::cout << "输入日期和时间(格式: YYYY-MM-DD HH:MM::SS): ";
std::cin >> year >> ch >> month >> ch >> day >> hour >> ch >> minute >> ch >> second;
Timedate timedate(year, month, day, hour, minute, second);
std::cout << "输出: " << timedate << std::endl;
return 0;
}