#include "user_time.h" #include #include #include #include // 包含ctype.h头文件以使用isxdigit函数 void user_time_init() { } void user_time_handler() { } #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "driver/rtc_io.h" void setRtcTime(unsigned int year, unsigned int month, unsigned int day, unsigned int hour, unsigned int minute, unsigned int second) { struct tm tm_info = {0}; tm_info.tm_year = year - 1900; // 年份需要减去1900 tm_info.tm_mon = month - 1; // 月份从0开始,需要减去1 tm_info.tm_mday = day; tm_info.tm_hour = hour; tm_info.tm_min = minute; tm_info.tm_sec = second; time_t timestamp = mktime(&tm_info); // 设置 RTC 时间戳 struct timeval tv = {.tv_sec = timestamp, .tv_usec = 0}; settimeofday(&tv, NULL); } void printRtcTime(char * data) { struct timeval tv; gettimeofday(&tv, NULL); // 使用 localtime 函数将时间戳转换为本地时间 struct tm *tm_info = localtime(&tv.tv_sec); // 打印本地时间 printf("%s print Time: %04d-%02d-%02d %02d:%02d:%02d\n",data, tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday, tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec); } void getRtcTime(Machine_info_t *info) { // 获取 RTC 时间 struct timeval tv; gettimeofday(&tv, NULL); // 打印 RTC 时间戳 // printf("RTC Timestamp: %lld\n", tv.tv_sec); // 使用 localtime 函数将时间戳转换为本地时间 struct tm *tm_info = localtime(&tv.tv_sec); // // 打印本地时间 // printf("Local Time: %04d-%02d-%02d %02d:%02d:%02d\n", // tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday, // tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec); info->year = tm_info->tm_year + 1900; info->month = tm_info->tm_mon + 1; info->day = tm_info->tm_mday; info->hour = tm_info->tm_hour; info->min = tm_info->tm_min; info->sec = tm_info->tm_sec; // printf("free_heap_size:%ld \r\n free_internal_heap_size:%ld \r\n minimum_free_heap_size:%ld\r\n", // esp_get_free_heap_size(), esp_get_free_internal_heap_size(), esp_get_minimum_free_heap_size()); } bool is_sync_time(Machine_info_t *info) { getRtcTime(info); if ((info->year == 1997) && (info->month == 1) && (info->day == 1)) { return false; } return true; } // // 设置 RTC 时间戳为 2023-10-30 12:34:56 // setRtcTime(2023, 10, 30, 12, 34, 56); // // 获取并打印 RTC 时间 // getRtcTime(); // // 将13位时间戳转换为格式化时间字符串,加上时区偏移 // void timestampToStr(const char* timestamp_str, char* output_buffer, int size, int timezone_offset) { // long long int timestamp = atoll(timestamp_str); // 将字符串转换为 long long int 类型的时间戳 // time_t time_val = (time_t)(timestamp / 1000) + timezone_offset * 3600; // 将13位时间戳转换为秒,并加上时区偏移 // struct tm* tm_info = localtime(&time_val); // 将时间戳转换为本地时间结构体 // //strftime(output_buffer, buffer_size, "%Y-%m-%d %H:%M:%S", tm_info); // 将时间格式化为字符串 // } // //函数用于从字符串中提取时间戳数字部分 // char* extractTimestamp(const char* input_str) { // const char* time_str = strstr(input_str, "\"time\":"); // if (time_str != NULL) { // const char* digit_start = time_str + strlen("\"time\":"); // char* number_str = (char*)malloc(20 * sizeof(char)); // 分配存储时间戳的内存空间 // if (number_str != NULL) { // sscanf(digit_start, "%[^,}", number_str); // return number_str; // } else { // printf("Memory allocation failed!\n"); // return NULL; // } // } else { // printf("No 'time' field found!\n"); // return NULL; // } // } #if 0 void timestamp_to_local_time(const char *timestamp, int *year, int *month, int *day, int *hour, int *minute, int *second) { long long_timestamp = atoll(timestamp) / 1000; // Convert 13-digit timestamp to seconds struct tm *time_info; time_t timestamp_sec = (time_t)long_timestamp; time_info = localtime(×tamp_sec); *year = time_info->tm_year + 1900; *month = time_info->tm_mon + 1; *day = time_info->tm_mday; *hour = time_info->tm_hour; *minute = time_info->tm_min; *second = time_info->tm_sec; } #else void timestamp_to_local_time(const char *timestamp, int offset, int *year, uint8_t *month, uint8_t *day, uint8_t *hour, uint8_t *minute, uint8_t *second) { long long_timestamp = atoll(timestamp) / 1000; // Convert 13-digit timestamp to seconds struct tm *time_info; time_t timestamp_sec = (time_t)long_timestamp; time_info = gmtime(×tamp_sec); // Convert to UTC time // Add the specified offset to the timestamp timestamp_sec += offset * 3600; time_info = gmtime(×tamp_sec); *year = time_info->tm_year + 1900; *month = time_info->tm_mon + 1; *day = time_info->tm_mday; *hour = time_info->tm_hour; *minute = time_info->tm_min; *second = time_info->tm_sec; } long long calculate_minutes_difference(int year1, int month1, int day1, int hour1, int minute1, int second1, int year2, int month2, int day2, int hour2, int minute2, int second2) { struct tm timeinfo1 = {0}; struct tm timeinfo2 = {0}; timeinfo1.tm_year = year1 - 1900; timeinfo1.tm_mon = month1 - 1; timeinfo1.tm_mday = day1; timeinfo1.tm_hour = hour1; timeinfo1.tm_min = minute1; timeinfo1.tm_sec = second1; timeinfo2.tm_year = year2 - 1900; timeinfo2.tm_mon = month2 - 1; timeinfo2.tm_mday = day2; timeinfo2.tm_hour = hour2; timeinfo2.tm_min = minute2; timeinfo2.tm_sec = second2; time_t time1 = mktime(&timeinfo1); time_t time2 = mktime(&timeinfo2); long long difference = (long long)difftime(time2, time1); return difference / 60; // Convert seconds difference to minutes } #endif