項目需求:限制一個情景的執行時間段,如:一個情景只能在2點到3點執行,其它時間不執行。
思路:獲取當前的時間戳,把開始和結束時間設爲今天的時間戳,拿當前時間與開始和結束時間三者做對比。當然也要考慮跨天的問題。
涉及的知識點:
1.tm結構體
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
2.mktime();
#include <time.h>
time_t mktime(strcut tm * timeptr);
用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數,返回經過的秒數。
3.strptime()/strftime();
這兩個函數都是時間日期的格式控制函數,在功能上看起來正好相反。strftime將一個tm結構格式化爲一個字符串,strptime則是將一個字符串格式化爲一個tm結構。
size_t strftime(char *s,size_t maxsize,char *format,const struct tm *timeptr);
char *strptime(const char *buf,const char *format,struct tm *timeptr);
4.實例代碼
/*************************************************
* @FileName : timecode.c
* @Description : Determine the execution time of the program
* @Editor : Donkey
* @Data : 2018-12-1 10:02
****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
struct tm time1;
int main(int argc, char const *argv[])
{
/* code */
char time_start[10] = "23:05";//情景開始時間
char time_end[10] = "09:00";//情景結束時間
/* 獲取當前的時間 */
time_t curTime = 0;
curTime = time((time_t *)NULL);
struct tm *localTime;
struct tm tmpLocalTime;
localTime = localtime(&curTime);
memcpy(&tmpLocalTime,localTime,sizeof(struct tm));//拷貝
time_t ntime = mktime(localTime);//用來將參數localTime所指的tm結構數據轉換成從公元1970年1月1日0時0分0秒算起至今的UTC時間所經過的秒數.返回經過的秒數。
/* get start time */
char hour_start[10] = {0};
char minute_start[10] = {0};
/* get end time */
char hour_end[10] = {0};
char minute_end[10] = {0};
/* 把23:05拆分爲:時,分 */
strptime(time_start, "%H:%M", &time1);//將一個字符串格式化爲一個tm結構
strftime(hour_start,sizeof(hour_start),"%H",&time1);//將一個tm結構格式化爲一個字符串
strftime(minute_start,sizeof(minute_start),"%M",&time1);
strptime(time_end, "%H:%M", &time1);
strftime(hour_end,sizeof(hour_end),"%H",&time1);
strftime(minute_end,sizeof(minute_end),"%M",&time1);
/* 轉爲int型 */
int hs = atoi(hour_start);
int ms = atoi(minute_start);
int he = atoi(hour_end);
int me = atoi(minute_end);
/* 把開始時間設置進當天的時間,得到時間戳,如23:05得到今天23:05的時間戳 */
tmpLocalTime.tm_hour = hs;
tmpLocalTime.tm_min = ms;
time_t stime = mktime(&tmpLocalTime);
/* 把結束時間設置進當天的時間,得到時間戳,如23:05得到今天23:05的時間戳 */
tmpLocalTime.tm_hour = he;
tmpLocalTime.tm_min = me;
time_t etime = mktime(&tmpLocalTime);
printf("---------------------------------------------------------------------------------------------------\n\n\n\n");
printf("[%d %s]--- starttime = %lu , endtime = %lu , nowtime = %lu\n",__LINE__,__FUNCTION__,stime,etime,ntime);
printf("---------------------------------------------------------------------------------------------------\n\n\n\n\n");
/****************************************************************
* 存在3種情況:
* 1.順序時間,不跨天:這種情況直接對比當前的時間戳是否在開始和結束時間範圍內即可;
* 2.跨天:比如例子的23點到9點,由於上面是把當前的時間設爲當天的,獲取到的是當天的23點和9點,但實際是要獲取第二天的9點時間戳;
* 3.開始時間和結束時間相等:默認情況,即爲24小時內都可執行。
****************************************************************/
if((stime < etime) || ((stime == etime)))
{
/* 解決第一,第二種情況 */
if(ntime >= stime && ntime <= etime)
{
printf("[%d %s]I will run Scene!\n", __LINE__,__FUNCTION__);
}
else if(stime == etime)
{
printf("[%d %s]I will run Scene!\n", __LINE__,__FUNCTION__);
}
else
{
printf("[%d %s]I don't run Scene!\n", __LINE__,__FUNCTION__);
}
}
else
{
/* 解決第三種情況 */
if((ntime >= stime || ntime <= etime))
{
printf("[%d %s]I will run Scene!\n", __LINE__,__FUNCTION__);
}
else
{
printf("[%d %s]I don't run Scene!\n", __LINE__,__FUNCTION__);
}
}
return 0;
}
5.輸出