C++中的<ctime>库

转自:C++ <ctime>(time.h)库笔记 以及简便计算日期差等

  • struct tm这个结构体有9个成员
Member Type Meaning Range
tm_sec int seconds after the minute 0-60*
tm_min int minutes after the hour 0-59
tm_hour int hours since midnight 0-23
tm_mday int day of the month 1-31
tm_mon int months since January 0-11
tm_year int years since 1900  
tm_wday int days since Sunday 0-6
tm_yday int days since January 1 0-365
tm_isdst int Daylight Saving Time flag  
  • 第一个 tm_sec 一般来说范围是(0~59),但为了适应一些系统有闰秒,范围就扩大了
    最后面那个 tm_isdst是作为夏令时的flag,至于什么夏令时。
    注意 tm_mday是从1开始,如果输入0相当于前一天。
    需要注意 tm_mon 从相对一月开始计算,例如:要设置为11月,那就是 tm_mon = 11 - 1。
    需要特别注意的是 tm_year这个是从1900年作为起始时间的,例如:要设置时间为2016,那就是 tm_year = 2016 - 1900。

  • time_t time (time_t* timer);    //获取当前时间,然后赋值给指针变量 timer

time_t timer;
//下面两个获取当前时间赋值给timer的方法,效果是一样的
time(&timer); 
timer = time(NULL);
  • char* ctime (const time_t * timer);    //将time_t转换成字符串
    字符串的格式为: Www Mmm dd hh:mm:ss yyyy
    星期      月     日  时   分  秒  年

cout << ctime(&timer);//timer = time(NULL);
  • char* asctime (const struct tm * timeptr);//将tm结构体转换成字符串
    返回值字符串格式跟上面ctime一样

  • time_t mktime (struct tm * timeptr);    //将tm结构体转换成time_t
    表示范围从1900年1月1日到3000年1月1日(自己测试结果)
    还会根据结构体的年月日 修改成员 tm_wday 和 tm_yday, (原来的这两个成员变量会被忽视)
struct tm t1 = { 0 };//初始化结构体  
t1.tm_year = 2016 - 1900;//年份设置为2016年  
t1.tm_mon = 10;//月份设置为11月  
t1.tm_mday = 21;//记得tm_mday要设置  否则0就是前一天  
mktime(&t1);//格式化结构体,返回时间 秒为单位  
  • double difftime (time_t end, time_t beginning);   //计算两个时间的时间差 end - beginning(以秒为单位)
  • 输入日期距2008.8.8有多少天计算:
#include<iostream>
#include<ctime>
#include<string>
using namespace std;

//字符串和日期转换
bool Tran_str(string str,int& year,int& mon,int& day) {
	int doc_loc[7];
	int doc_count = 0, i = 0;
	while (i < str.length()) {
		if (str[i] == '.') {
			doc_loc[doc_count] = i;
			doc_count++;
		}
		i++;
	}
	if (doc_count != 2)
		return false;
	//3段日期分开
	string temp[3];
	temp[0] = str.substr(0, doc_loc[0]);
	temp[1] = str.substr((doc_loc[0] + 1), (doc_loc[1] - doc_loc[0] - 1));
	temp[2] = str.substr((doc_loc[1] + 1), (str.length()-doc_loc[1]-1));
	int test;
	//年
	test = atoi(temp[0].c_str());
	if (test > 2500||test<1900)
		return false;
	else
		year = test;
	//月
	test = atoi(temp[1].c_str());
	if (test < 1 || test>12)
		return false;
	else
		mon = test;
	//日
	test = atoi(temp[2].c_str());
	if (test > 31 || test < 1)
		return false;
	else
		day = test;

	return true;
}
//计算日期差
int Day_Diff(string in){
	struct tm t1 = { 0 };
	struct tm t2 = { 0 };
	double seconds;
	int year, mon, day;
	Tran_str(in, year, mon, day);
	t1.tm_year = year - 1900; t1.tm_mon = mon-1; t1.tm_mday = day;	//现在时间
	t2.tm_year = 2008 - 1900; t2.tm_mon = 7; t2.tm_mday = 8;	//2008.8.8
	seconds = difftime(mktime(&t2), mktime(&t1));	//转换结构体为time_t,利用difftime,计算时间差

	int day_diff = seconds / 86400;		//最后输出天数,因为一天有86400秒(60*60*24)

	return day_diff;
}

int main() {
	string in;
	while (cin >> in) {
		cout <<in << " 距2008.8.8还有:" << Day_Diff(in) << endl;
	}
	getchar();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39731083/article/details/82223600