基于规则“SunThu-00:02:00-00:03:00,Mon-16:59:00-20:00:00”时间范围检查(C语言)
立即下载
资源介绍:
规则:
1、Mon、Tue、Wed...表示一周的某一天;
2、00:02:00-00:03:00表示一天之内的某一时间段;
3、SunThu|00:02:00-00:03:00表示某天(和某天)的某一时间段;
4、SunThu|00:02:00-00:03:00,Mon|16:59:00-20:00:00表示某天(和某天)的某一时间段和表示某天(和某天)的某一时间段,一组内可包括一至七天,一组内只能包括一个时间段。
基于以上规则,使用C语言实现检查当前时间点是否在规则规定的时间段内,使用方式:
./time_range_check "SunThu|00:02:00-00:03:00,Mon|16:59:00-20:00:00,FriSun|00:02:00-23:30:59,Sun|15:59:00-20:00:00"
规则格式正确
当前时间点在规则内
#include
#include
#include
#include
typedef struct {
char days[100];
char start_time[10];
char end_time[10];
} TimePeriod;
int check_day(char *days) {
char day[4];
day[3] = '\0';
for(int i = 0;i < strlen(days); i += 3){
memcpy(day, days + i, 3);
if (strcmp(day, "Mon") != 0 && strcmp(day, "Tue") != 0 && strcmp(day, "Wed") != 0 &&
strcmp(day, "Thu") != 0 && strcmp(day, "Fri") != 0 && strcmp(day, "Sat") != 0 &&
strcmp(day, "Sun") != 0) {
return 0;
}
}
return 1;
}
int check_time(char *time) {
int h1, m1, s1, h2, m2, s2;
//if(strlen(time) != 17)
// return 0;
if (sscanf(time, "%02d:%02d:%02d-%02d:%02d:%02d", &h1, &m1, &s1, &h2, &m2, &s2) != 6) {
return 0;
}
if (h1 < 0 || h1 > 23 || m1 < 0 || m1 > 59 || s1 < 0 || s1 > 59 ||
h2 < 0 || h2 > 23 || m2 < 0 || m2 > 59 || s2 < 0 || s2 > 59) {
return 0;
}
return 1;
}
int check_group(char *group) {
char *saveptr1 = NULL;
char *saveptr2 = NULL;
char *token = strtok_r(group, "|", &saveptr1);
if (token == NULL) {
return 0;
}
char *days = token;
token = strtok_r(NULL, "|", &saveptr1);
if (token == NULL) {
return 0;
}
char *time = token;
// 检查 days 部分
char *day = strtok_r(days, "", &saveptr2);
int day_count = 0;
while (day != NULL) {
if (!check_day(day)) {
return 0;
}
day_count++;
day = strtok_r(NULL, "", &saveptr2);
}
if (day_count < 1 || day_count > 7) {
return 0;
}
// 检查 time 部分
if (!check_time(time)) {
return 0;
}
return 1;
}
int check_rule(char *timestr) {
char *saveptr = NULL;
char *rule = strdup(timestr);
char *token = strtok_r(rule, ",", &saveptr);
while (token != NULL) {
if (!check_group(token)) {
return 0;
}
token = strtok_r(NULL, ",", &saveptr);
}
return 1;
}
void getCurrentTime(char *current_time) {
time_t now = time(NULL);
struct tm *t = localtime(&now);
sprintf(current_time, "%02d:%02d:%02d", t->tm_hour, t->tm_min, t->tm_sec);
}
void getCurrentDate(char *current_date) {
time_t now = time(NULL);
struct tm *t = localtime(&now);
sprintf(current_date, "%d-%02d-%02d", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
}
int getCurrentWeekday() {
time_t now = time(NULL);
struct tm *t = localtime(&now);
return t->tm_wday;
}
int compareTime(const char *time1, const char *time2) {
int h1, m1, s1, h2, m2, s2;
sscanf(time1, "%d:%d:%d", &h1, &m1, &s1);
sscanf(time2, "%d:%d:%d", &h2, &m2, &s2);
if (h1 < h2) {
return -1;
} else if (h1 > h2) {
return 1;
} else {
if (m1 < m2) {
return -1;
} else if (m1 > m2) {
return 1;
} else {
if (s1 < s2) {
return -1;
} else if (s1 > s2) {
return 1;
} else {
return 0;
}
}
}
}
int checkTimePeriod(TimePeriod *period, char *current_time) {
int start_comp = compareTime(period->start_time, current_time);
int end_comp = compareTime(period->end_time, current_time);
return start_comp <= 0 && end_comp >= 0;
}
int checkDay(char *days, int current_weekday) {
char weekdays[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
if (current_weekday >= 0 && current_weekday < 7 && (strstr(days, weekdays[current_weekday]) != NULL)) {
return 1;
}
return 0;
}
int time_check(char *timestr) {
char *rules = strdup(timestr);
char current_time[10];
char current_date[20];
getCurrentTime(current_time);
getCurrentDate(current_date);
int current_weekday = getCurrentWeekday();
char *saveptr1, *saveptr2, *saveptr3;
char *token = strtok_r(rules, ",", &saveptr1);
while (token != NULL) {
TimePeriod period;
char *days_time = strtok_r(token, "|", &saveptr2);
strcpy(period.days, days_time);
char *time_range = strtok_r(NULL, "|", &saveptr2);
char *start_time = strtok_r(time_range, "-", &saveptr3);
strcpy(period.start_time, start_time);
char *end_time = strtok_r(NULL, "-", &saveptr3);
strcpy(period.end_time, end_time);
if (checkDay(period.days, current_weekday) && checkTimePeriod(&period, current_time)) {
printf("当前时间点在规则内\n");
return 0;
}
token = strtok_r(NULL, ",", &saveptr1);
}
printf("当前时间点不在规则内\n");
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("请输入时间范围规则,eg:\n %s \"SunThu|00:02:00-00:03:00,Mon|16:59:00-20:00:00,FriSun|00:02:00-23:30:59,Sun|15:59:00-20:00:00\"\n", argv[0]);
return 1;
}
if (check_rule(argv[1])) {
printf("规则格式正确\n");
time_check(argv[1]);
} else {
printf("规则格式错误\n");
}
return 0;
}