C++ 查看时间戳是否在特定的小时之间

C++ 查看时间戳是否在特定的小时之间,c++,unix-timestamp,C++,Unix Timestamp,我有很多时间戳,以及与之相关的数据。我想检索来自0800-0900的数据。。检查时间戳是否介于两者之间的方法是什么? 我应该如何编写一个函数,输入两个小时,并返回一个时间戳列表,该时间戳在这些小时内,而不管是哪一天 像 std::list getTimestampsBetween(uint16最小小时,uint16最大小时) { if(时间戳列表中的时间戳介于最小小时和最大小时之间) 将其添加到列表中; 退货清单; } 使用localtime将时间戳转换为struct tm,并检查结构的tm\u

我有很多时间戳,以及与之相关的数据。我想检索来自0800-0900的数据。。检查时间戳是否介于两者之间的方法是什么? 我应该如何编写一个函数,输入两个小时,并返回一个时间戳列表,该时间戳在这些小时内,而不管是哪一天

std::list getTimestampsBetween(uint16最小小时,uint16最大小时)
{
if(时间戳列表中的时间戳介于最小小时和最大小时之间)
将其添加到列表中;
退货清单;
}

使用
localtime
将时间戳转换为
struct tm
,并检查结构的tm\u hour属性是否在指定窗口内。
查看time.h了解更多信息。

这取决于时间戳的格式,但如果它们是
time\u t
,则 可以使用
mktime
将给定的
tm
转换为
time
,以及
difftime
比较两个
时间\u t
。大致如下:

bool
timeIsInInterval( time_t toTest, int minHour, int maxHour )
{
    time_t           now = time( NULL );
    tm               scratch = *localtime( &now );
    scratch.tm_sec = scratch.tm_min = 0;
    scratch.tm_hour = minHour;
    time_t           start = mktime( &scratch );
    scratch.tm_hour = maxHour;
    time_t           finish = mktime( &scratch );
    return difftime( toTest, start ) >= 0.0
        && difftime( toTest, finish ) < 0.0;
}
bool
timeIsInInterval(time\u t toTest、int MINHOURE、int maxHour)
{
time\u t now=时间(空);
tm scratch=*本地时间(&now);
scratch.tm_sec=scratch.tm_min=0;
scratch.tm_hour=分钟小时;
开始时间=mktime(&scratch);
scratch.tm_hour=最大小时;
完成时间=mktime(&scratch);
返回时间差(toTest,start)>=0.0
&&差异时间(测试、完成)<0.0;
}
(实际上,
toTest>=开始和&toTest
可能是 足够了。虽然标准允许更多,但我不知道有多少
time\t
不是包含 自某个神奇日期起的秒数。)

当然,这假设你在寻找 今天两个小时。如果你想要一些任意的日期,很容易修改。 如果您想要任何日期,则需要执行相反的操作:转换时间戳
到一个
tm
,比较
tm\u hour
字段。

我找到了一个粗略的方法,将时间戳除以86400,得到从上午12点开始经过的秒数。这样我就可以轻松地掌握一天中的时间。我只要把小时乘以3600,瞧
bool
timeIsInInterval( time_t toTest, int minHour, int maxHour )
{
    time_t           now = time( NULL );
    tm               scratch = *localtime( &now );
    scratch.tm_sec = scratch.tm_min = 0;
    scratch.tm_hour = minHour;
    time_t           start = mktime( &scratch );
    scratch.tm_hour = maxHour;
    time_t           finish = mktime( &scratch );
    return difftime( toTest, start ) >= 0.0
        && difftime( toTest, finish ) < 0.0;
}