Windows平台C语言非指定时段自动关机:时间比较问题求助
Hey there! Let's work through this time-check and auto-shutdown script for your kid's computer—such a smart way to manage screen time. I’ll break down the time-comparison logic you’re stuck on, then share a complete, tested example.
核心问题:时间比较的简化思路
The tricky part with time checks is avoiding messy hour/minute comparisons. The easiest way is to convert all time values (current, start, end) into a single number—total minutes since midnight. This lets you use simple integer comparisons instead of juggling hours and minutes separately.
For example:
- 18:00 becomes
18*60 + 0 = 1080minutes - 22:00 becomes
22*60 + 0 = 1320minutes - Current time 19:30 becomes
19*60 +30=1170minutes
Then you just check if the current total minutes fall outside your allowed range.
完整实现代码
Here’s a complete script that handles both same-day and cross-day time ranges (in case you ever need overnight access windows):
#include <stdio.h> #include <time.h> #include <stdlib.h> int main() { // 自定义允许的时间段:这里设置为18:00到22:00 const int ALLOWED_START_HOUR = 18; const int ALLOWED_START_MIN = 0; const int ALLOWED_END_HOUR = 22; const int ALLOWED_END_MIN = 0; // 获取当前系统时间 time_t now = time(NULL); struct tm *current_time = localtime(&now); if (current_time == NULL) { perror("Failed to retrieve current time"); return EXIT_FAILURE; } // 转换为当日总分钟数 int current_total_min = current_time->tm_hour * 60 + current_time->tm_min; int start_total_min = ALLOWED_START_HOUR * 60 + ALLOWED_START_MIN; int end_total_min = ALLOWED_END_HOUR * 60 + ALLOWED_END_MIN; // 判断是否超出允许时间范围 int is_out_of_range = 0; if (start_total_min < end_total_min) { // 同一天的时间段(比如18:00-22:00) is_out_of_range = (current_total_min < start_total_min) || (current_total_min > end_total_min); } else { // 跨天的时间段(比如22:00-次日6:00) is_out_of_range = !(current_total_min >= start_total_min || current_total_min <= end_total_min); } if (is_out_of_range) { printf("当前时间不在允许范围内,系统将在1分钟后关机...\n"); // 根据你的系统选择对应的关机命令: // Windows系统:60秒后关机(需要管理员权限) // system("shutdown /s /t 60"); // Linux/macOS系统:1分钟后关机(需要root权限) system("shutdown -h +1"); } else { printf("当前时间在允许范围内,可正常使用电脑。\n"); } return EXIT_SUCCESS; }
关键细节说明
- 时间转换逻辑: Converting to total minutes eliminates edge cases like "current minute is 59 vs start minute 0"—no more weird conditional checks for hour rollover.
- 跨天处理: If you want to allow access from, say, 22:00 to 6:00 the next day, the script automatically handles that by checking if the current time is not between the start and end (since start is larger than end in this case).
- 关机权限: Remember, this script needs to run with admin/root privileges to execute the shutdown command. On Windows, you’ll need to run it as Administrator; on Linux, use
sudo.
额外建议
- 开机自启动: To make this work automatically, set the script to run on boot:
- Windows: Add it to Task Scheduler with a trigger for "At log on".
- Linux: Add a cron job or a systemd service that runs on user login.
- 提前警告: You can adjust the shutdown timer (the
/t 60or+1part) to give your kid more time to save work—like 10 minutes instead of 1.
内容的提问来源于stack exchange,提问作者user9447673

