学校的课设作业,要弄一个酒店管理系统,今年刚学的c++,想请教一下如何获取当前的系统时间(最好是可以用来计算的),并且可以计算两个时间段的函数,大概思路可以是怎样,请教各位大神
可以使用std::tm 结构体。
这是结构体说明。
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
下面是一个范例(输出今天的年月日):
#include <ctime>//注意要使用tm结构体,需要包含此头文件
#include <iostream>
using namespace std;
int main() {
time_t t = time(0); // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< endl;
}
结构体各分量都是int型,因此计算也是很方便的。