[智能家居]限制程序可执行时间范围

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/localhostcom/article/details/84670870

项目需求:限制一个情景的执行时间段,如:一个情景只能在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.输出

猜你喜欢

转载自blog.csdn.net/localhostcom/article/details/84670870
今日推荐