user_time.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "user_time.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h> // 包含ctype.h头文件以使用isxdigit函数
  6. void user_time_init()
  7. {
  8. }
  9. void user_time_handler()
  10. {
  11. }
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "driver/rtc_io.h"
  17. void setRtcTime(unsigned int year, unsigned int month, unsigned int day,
  18. unsigned int hour, unsigned int minute, unsigned int second)
  19. {
  20. struct tm tm_info = {0};
  21. tm_info.tm_year = year - 1900; // 年份需要减去1900
  22. tm_info.tm_mon = month - 1; // 月份从0开始,需要减去1
  23. tm_info.tm_mday = day;
  24. tm_info.tm_hour = hour;
  25. tm_info.tm_min = minute;
  26. tm_info.tm_sec = second;
  27. time_t timestamp = mktime(&tm_info);
  28. // 设置 RTC 时间戳
  29. struct timeval tv = {.tv_sec = timestamp, .tv_usec = 0};
  30. settimeofday(&tv, NULL);
  31. }
  32. void getRtcTime(Machine_info_t *info)
  33. {
  34. // 获取 RTC 时间
  35. struct timeval tv;
  36. gettimeofday(&tv, NULL);
  37. // 打印 RTC 时间戳
  38. // printf("RTC Timestamp: %lld\n", tv.tv_sec);
  39. // 使用 localtime 函数将时间戳转换为本地时间
  40. struct tm *tm_info = localtime(&tv.tv_sec);
  41. // 打印本地时间
  42. printf("Local Time: %04d-%02d-%02d %02d:%02d:%02d\n",
  43. tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday,
  44. tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
  45. info->year = tm_info->tm_year + 1900;
  46. info->month = tm_info->tm_mon + 1;
  47. info->day = tm_info->tm_mday;
  48. info->hour = tm_info->tm_hour;
  49. info->min = tm_info->tm_min;
  50. info->sec = tm_info->tm_sec;
  51. printf("free_heap_size:%ld \r\n free_internal_heap_size:%ld \r\n minimum_free_heap_size:%ld\r\n",
  52. esp_get_free_heap_size(), esp_get_free_internal_heap_size(), esp_get_minimum_free_heap_size());
  53. }
  54. bool is_sync_time(Machine_info_t *info)
  55. {
  56. getRtcTime(info);
  57. if ((info->year == 1997) && (info->month == 1) && (info->day == 1))
  58. {
  59. return false;
  60. }
  61. return true;
  62. }
  63. // // 设置 RTC 时间戳为 2023-10-30 12:34:56
  64. // setRtcTime(2023, 10, 30, 12, 34, 56);
  65. // // 获取并打印 RTC 时间
  66. // getRtcTime();
  67. // // 将13位时间戳转换为格式化时间字符串,加上时区偏移
  68. // void timestampToStr(const char* timestamp_str, char* output_buffer, int size, int timezone_offset) {
  69. // long long int timestamp = atoll(timestamp_str); // 将字符串转换为 long long int 类型的时间戳
  70. // time_t time_val = (time_t)(timestamp / 1000) + timezone_offset * 3600; // 将13位时间戳转换为秒,并加上时区偏移
  71. // struct tm* tm_info = localtime(&time_val); // 将时间戳转换为本地时间结构体
  72. // //strftime(output_buffer, buffer_size, "%Y-%m-%d %H:%M:%S", tm_info); // 将时间格式化为字符串
  73. // }
  74. // //函数用于从字符串中提取时间戳数字部分
  75. // char* extractTimestamp(const char* input_str) {
  76. // const char* time_str = strstr(input_str, "\"time\":");
  77. // if (time_str != NULL) {
  78. // const char* digit_start = time_str + strlen("\"time\":");
  79. // char* number_str = (char*)malloc(20 * sizeof(char)); // 分配存储时间戳的内存空间
  80. // if (number_str != NULL) {
  81. // sscanf(digit_start, "%[^,}", number_str);
  82. // return number_str;
  83. // } else {
  84. // printf("Memory allocation failed!\n");
  85. // return NULL;
  86. // }
  87. // } else {
  88. // printf("No 'time' field found!\n");
  89. // return NULL;
  90. // }
  91. // }
  92. #if 0
  93. void timestamp_to_local_time(const char *timestamp, int *year, int *month, int *day, int *hour, int *minute, int *second) {
  94. long long_timestamp = atoll(timestamp) / 1000; // Convert 13-digit timestamp to seconds
  95. struct tm *time_info;
  96. time_t timestamp_sec = (time_t)long_timestamp;
  97. time_info = localtime(&timestamp_sec);
  98. *year = time_info->tm_year + 1900;
  99. *month = time_info->tm_mon + 1;
  100. *day = time_info->tm_mday;
  101. *hour = time_info->tm_hour;
  102. *minute = time_info->tm_min;
  103. *second = time_info->tm_sec;
  104. }
  105. #else
  106. 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)
  107. {
  108. long long_timestamp = atoll(timestamp) / 1000; // Convert 13-digit timestamp to seconds
  109. struct tm *time_info;
  110. time_t timestamp_sec = (time_t)long_timestamp;
  111. time_info = gmtime(&timestamp_sec); // Convert to UTC time
  112. // Add the specified offset to the timestamp
  113. timestamp_sec += offset * 3600;
  114. time_info = gmtime(&timestamp_sec);
  115. *year = time_info->tm_year + 1900;
  116. *month = time_info->tm_mon + 1;
  117. *day = time_info->tm_mday;
  118. *hour = time_info->tm_hour;
  119. *minute = time_info->tm_min;
  120. *second = time_info->tm_sec;
  121. }
  122. long long calculate_minutes_difference(int year1, int month1, int day1, int hour1, int minute1, int second1,
  123. int year2, int month2, int day2, int hour2, int minute2, int second2)
  124. {
  125. struct tm timeinfo1 = {0};
  126. struct tm timeinfo2 = {0};
  127. timeinfo1.tm_year = year1 - 1900;
  128. timeinfo1.tm_mon = month1 - 1;
  129. timeinfo1.tm_mday = day1;
  130. timeinfo1.tm_hour = hour1;
  131. timeinfo1.tm_min = minute1;
  132. timeinfo1.tm_sec = second1;
  133. timeinfo2.tm_year = year2 - 1900;
  134. timeinfo2.tm_mon = month2 - 1;
  135. timeinfo2.tm_mday = day2;
  136. timeinfo2.tm_hour = hour2;
  137. timeinfo2.tm_min = minute2;
  138. timeinfo2.tm_sec = second2;
  139. time_t time1 = mktime(&timeinfo1);
  140. time_t time2 = mktime(&timeinfo2);
  141. long long difference = (long long)difftime(time2, time1);
  142. return difference / 60; // Convert seconds difference to minutes
  143. }
  144. #endif