user_time.c 6.3 KB

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