jb.qi | 6f655ca | 2023-08-24 19:17:44 -0700 | [diff] [blame] | 1 | #include"format_change.h" |
xj | 9bbefe6 | 2022-05-26 14:45:57 +0800 | [diff] [blame] | 2 | #include<log/log.h> |
| 3 | |
| 4 | |
| 5 | /***************************************************************************** |
| 6 | * Prototype : format_change |
| 7 | * Description : convert the incoming fixed format string into the corresponding alarm seconds |
| 8 | * Input : char *buffer ; input format : 2022-04-23-15-30-00 ( Mon-Day-Hour-Min-Sec ) Or 1200 ( seconds ) |
| 9 | * Output : None |
| 10 | * Return Value : -1: match error ; >0: set to wake up the devices after seconds |
| 11 | * |
| 12 | *****************************************************************************/ |
| 13 | ssize_t format_change(char *buffer) |
| 14 | { |
| 15 | time_t rawtime; |
| 16 | time_t alarm_tamp; |
| 17 | struct tm *info = NULL; |
| 18 | struct tm *set_alarm = NULL; |
| 19 | char *time_buff = NULL; |
| 20 | |
| 21 | char ebuff[256]; |
| 22 | regex_t reg_alm,reg_sec; |
| 23 | char *pattern_setalm = "^20[0-9][0-9]-[01]?[1-9]-[0-3]?[0-9]-[0-2]?[0-9]-[0-5]?[0-9]-[0-5]?[0-9]$";//regular expression 1 , in order that match alarm time string eg:Mon-Day-Hour-Min-Sec |
| 24 | char *pattern_setsec = "^[1-9][0-9]*$";//regular expression 2 , in order that match seconds |
| 25 | int cflags = REG_EXTENDED | REG_ICASE | REG_NOSUB;//POSIX extend,not care up and low letters,not store result |
| 26 | |
| 27 | int ret; |
| 28 | ssize_t sec; |
| 29 | |
| 30 | ret = regcomp(®_alm,pattern_setalm,cflags); //匹配规则1:\d\d\d\d\d-\d\d-\d\d-\d\d-\d\d-\d\d |
| 31 | if(ret) // judge error code : 0 symbolize success ; other value symbolizes fail |
| 32 | { |
| 33 | regerror(ret,®_alm,ebuff,256); |
| 34 | ALOGI(ebuff); |
| 35 | regfree(®_alm); //free mattch pattern |
| 36 | regfree(®_sec); |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | ret = regcomp(®_sec,pattern_setsec,cflags); //匹配规则2:^\d\d*$ |
| 41 | if(ret)// judge error code : 0 symbolize success ; other value symbolizes fail |
| 42 | { |
| 43 | regerror(ret,®_sec,ebuff,256); |
| 44 | ALOGI(ebuff); |
| 45 | regfree(®_alm); //free mattch pattern |
| 46 | regfree(®_sec); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | time(&rawtime); //获取自1970年到现在一共过去了几秒,赋值给rawtime |
| 51 | |
| 52 | info =localtime(&rawtime); //转换为UTC时间 |
| 53 | |
| 54 | bool leap_flag = (bool)(((info->tm_year%4==0)&&(info->tm_year%100!=0)) || (info->tm_year%400 == 0)); //判断当前是否为闰年 |
| 55 | int day_array[13] = {0,31, leap_flag?29:28,31,30,31,30,31,31,30,31,30,31}; //设置每个月的天数 |
| 56 | |
| 57 | if((ret = regexec(®_alm,buffer,0,NULL,0)) == 0) //Retrieve the incoming buffer string according to matching rule 1 |
| 58 | { |
| 59 | set_alarm = (struct tm *)malloc(sizeof(struct tm)); |
| 60 | memset(set_alarm,0,sizeof(struct tm)); |
| 61 | memcpy(set_alarm,info,sizeof(struct tm)); //拷贝当前时间信息 |
| 62 | |
| 63 | ret = sscanf(buffer,"%d-%d-%d-%d-%d-%d",&(set_alarm->tm_year),&(set_alarm->tm_mon),&(set_alarm->tm_mday),&(set_alarm->tm_hour),&(set_alarm->tm_min),&(set_alarm->tm_sec));//read data form formatted string |
| 64 | |
| 65 | if(ret == -1) //sscanf no mattch |
| 66 | { |
| 67 | ALOGI("sscanf error code -1\n"); |
| 68 | free(set_alarm); |
| 69 | return -1; |
| 70 | } |
| 71 | else if(ret == 6) //Success mattch |
| 72 | { |
| 73 | if((set_alarm->tm_hour > 23) || (set_alarm->tm_hour < 0)) //judge hour |
| 74 | { |
| 75 | ALOGI("hour error\n"); |
| 76 | ret = -1; |
| 77 | } |
| 78 | if((set_alarm->tm_mon > 12) || (set_alarm->tm_mon < 1)) //judge month |
| 79 | { |
| 80 | ALOGI("mon error\n"); |
| 81 | ret = -1; |
| 82 | } |
| 83 | if((set_alarm->tm_mday > day_array[set_alarm->tm_mon]) || (set_alarm->tm_mday < 1)) //judge day |
| 84 | { |
| 85 | ALOGI("day error\n"); |
| 86 | ret = -1; |
| 87 | } |
| 88 | if(ret == -1) //Error setting alarm time |
| 89 | { |
| 90 | free(set_alarm); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | set_alarm->tm_mon -= 1; |
| 95 | sprintf(ebuff,"set alarm is %s\n",asctime(set_alarm)); //print log |
| 96 | ALOGI(ebuff); |
| 97 | alarm_tamp = mktime(set_alarm); //struct tm transform struct time_t |
| 98 | time(&rawtime); |
| 99 | //printf("tamp: %ld\n",alarm_tamp); |
| 100 | sec = alarm_tamp - rawtime; //set second |
| 101 | free(set_alarm); |
| 102 | |
| 103 | if(sec <= 0) //the current alarm time is less than the current system time |
| 104 | { |
| 105 | ALOGI("sec <= 0 setalarm error\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | } |
| 109 | else //sscanf mattch fail |
| 110 | { |
| 111 | ALOGI("sscanf error other\n"); |
| 112 | free(set_alarm); |
| 113 | return -1; |
| 114 | } |
| 115 | } |
| 116 | else if ((ret = regexec(®_sec,buffer,0,NULL,0)) == 0) //Retrieve the incoming buffer string according to matching rule 2 |
| 117 | { |
| 118 | sec = (ssize_t)atoi(buffer); //string convert ssize_t |
| 119 | } |
| 120 | else //matching rule 1 and 2 all fail |
| 121 | { |
| 122 | regerror(ret,®_sec,ebuff,256); //free memony |
| 123 | ALOGI(ebuff); |
| 124 | regfree(®_alm); |
| 125 | regfree(®_sec); |
| 126 | return -1; |
| 127 | } |
| 128 | |
| 129 | regfree(®_alm); |
| 130 | regfree(®_sec); |
| 131 | |
| 132 | return sec; |
jb.qi | 6f655ca | 2023-08-24 19:17:44 -0700 | [diff] [blame] | 133 | } |