用struct tm与time_t来计算日期之间的间隔天数
来源:互联网 发布:mysql设置外键约束 编辑:程序博客网 时间:2024/06/07 18:07
使用标准库提供的struct tm类型的变量与time_t类型的变量,可以比较方便地处理时间。
写了个处理时间的class。
用到了下面的struct tm结构体和time_t类型的变量。
1. struct tm
在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:
#ifndef_TM_DEFINEDstructtm {int tm_sec; /* 秒 – 取值区间为[0,59] */int tm_min; /* 分 - 取值区间为[0,59] */int tm_hour; /* 时 - 取值区间为[0,23] */int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */int tm_year; /* 年份,其值等于实际年份减去1900 */int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/};#define_TM_DEFINED#endif
ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。
2. time_t
在time.h头文件中,我们还可以看到一些函数,它们都是以time_t为参数类型或返回值类型的函数:
doubledifftime(time_t time1, time_t time0);time_tmktime(struct tm * timeptr);time_ttime(time_t * timer);char* asctime(const struct tm * timeptr);char* ctime(const time_t *timer);
用time_t表示的时间(日历时间)是从一个时间点(1970年1月1日0时0分0秒)到此时的秒数。
在time.h中可以看到time_t是一个长整型数:
#ifndef_TIME_T_DEFINEDtypedeflong time_t;#define_TIME_T_DEFINED#endif
3. 自定义的class,用来计算两天之间的间隔天数。
如20120601与20120604这两天之间的间隔天数为3天。
头文件,DateOffset.h#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <stdio.h>#include <time.h>using namespace std;#ifndef DATEOFFSET_H#defineDATEOFFSET_Hclass DateOffset{ public: DateOffset(){} ~DateOffset(){} int time_gap(int date1, int date2); time_t time_maker(int date); int current_time(time_t& current_time); protected: struct tm t; time_t t_of_day1,t_of_day2; };#endif
CPP文件,DateOffset.cpp
#include <iostream>#include "./DateOffset.h"using namespace std;int DateOffset::time_gap(int date1, int date2){ t_of_day1=time_maker(date1); t_of_day2=time_maker(date2); double diff = difftime (t_of_day1,t_of_day2); if(diff<0) diff=0-diff; //60*60*24=86400 int count_gap_day=(int)diff/86400; return count_gap_day;}time_t DateOffset::time_maker(int date){ t.tm_year=date/10000-1900; t.tm_mon=(date-(t.tm_year+1900)*10000)/100-1; t.tm_mday=date-(t.tm_year+1900)*10000-(t.tm_mon+1)*100; t.tm_hour=0; t.tm_min=0; t.tm_sec=1; t.tm_isdst=0; time_t t_of_day=mktime(&t); return t_of_day;}int DateOffset::current_time(time_t& currenttime){ struct tm * timeinfo; timeinfo=localtime(¤ttime); int year=timeinfo->tm_year+1900; int month=timeinfo->tm_mon+1; int day=timeinfo->tm_mday; int date_current=year*10000+month*100+day; return date_current;}
- 用struct tm与time_t来计算日期之间的间隔天数
- 计算两个日期之间的间隔天数
- time_t和struct tm之间的转换
- struct tm 与 time_t
- 计算两个日期之间所间隔天数的快速算法
- 计算连个日期之间间隔的天数
- time_t与struct tm C/C++中的日期和时间
- C/C++中的日期和时间 time_t与struct tm
- C/C++ 日期 时间 time_t与struct tm转换
- C/C++中的日期和时间time_t与struct tm
- C/C++ 日期 时间 time_t与struct tm转换收藏
- C/C++中的日期和时间 time_t与struct tm
- C/C++中的日期和时间 time_t与struct tm
- C/C++中的日期和时间 time_t与struct tm
- C/C++中的日期和时间 time_t与struct tm
- C/C++ 日期 时间 time_t与struct tm转换
- C/C++ 日期 时间 time_t与struct tm转换 收藏
- C/C++中的日期和时间 time_t与struct tm
- WIN7文件夹共享步骤
- systemd、upstart和system V
- 电容基础知识 之 从名称认识电容在电路中的作用
- 利用临时变量对每个分组的数据进行累计
- HDU 1754 线段树裸题
- 用struct tm与time_t来计算日期之间的间隔天数
- 一种注册表沙箱的思路、实现——研究Reactos中注册表函数的实现1
- 利用临时变量对数据进行累计
- Android利用ViewFlipper实现屏幕切换动画效果
- 单元格中有两个字段
- ObjectiveC中的block用法解析
- linux的动态链接库的创建和使用
- launcher初步模型
- workqueue