user_time.c 5.9 KB

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