Files
BLE-USB_Dongle/apps/usr_utc/usr_utc_de_en.c
T

187 lines
5.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "system/includes.h"
#include "app_config.h"
#include "asm/pwm_led.h"
#include "tone_player.h"
#include "ui_manage.h"
#include "app_main.h"
#include "app_task.h"
#include "asm/charge.h"
#include "app_power_manage.h"
#include "app_charge.h"
#include "user_cfg.h"
#include "audio.h"
#include "vm.h"
#include "sys_time.h"
// 定义时间结构体
typedef struct {
uint16_t year; // 年份 (1970-2106)
uint8_t month; // 月份 (1-12)
uint8_t day; // 日 (1-31)
uint8_t hour; // 时 (0-23)
uint8_t minute; // 分 (0-59)
uint8_t second; // 秒 (0-59)
} DateTime;
DateTime usr_dt;
void parseMillisecondTimestamp(int64_t timestampMs, int64_t timezoneOffset, DateTime *dt);
void usr_set_data_time(int64_t timestampMs, int64_t timezoneOffset)
{
parseMillisecondTimestamp(timestampMs,timezoneOffset,&usr_dt);
}
extern struct sys_time usr_set_time;
extern void usr_set_sys_time();//设置时间
// 判断是否为闰年
static uint8_t isLeapYear(uint16_t year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
}
return 0;
}
// 计算从1970年到指定年份之前的总天数
static uint32_t getDaysSinceEpoch(uint16_t year) {
uint32_t days = 0;
for (uint16_t y = 1970; y < year; y++) {
days += isLeapYear(y) ? 366 : 365;
}
return days;
}
// 获取指定年份和月份的天数
static uint8_t getDaysInMonth(uint16_t year, uint8_t month) {
const uint8_t daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && isLeapYear(year)) {
return 29;
}
if (month >= 1 && month <= 12) {
return daysInMonth[month - 1];
}
return 0;
}
// 解析毫秒级UTC时间戳并应用时区偏移
void parseMillisecondTimestamp(int64_t timestampMs, int64_t timezoneOffset, DateTime *dt)
{
// 转换为秒(丢弃毫秒部分)
int64_t timestamp = timestampMs ;/// 1000;
// 应用时区偏移(转换为秒)
timestamp += timezoneOffset;// * 3600;
usr_var.usr_timezoneOffset = timezoneOffset;
// 处理可能的负值
if (timestamp < 0) {
timestamp = 0;
}
uint64_t days, seconds, minutes, hours;
uint16_t year = 1970;
uint8_t month = 1;
// 计算秒、分、时
seconds = timestamp % 60;
timestamp /= 60;
minutes = timestamp % 60;
timestamp /= 60;
hours = timestamp % 24;
timestamp /= 24;
// 计算年月日
while (1) {
uint16_t daysInYear = isLeapYear(year) ? 366 : 365;
if (timestamp >= daysInYear) {
timestamp -= daysInYear;
year++;
} else {
break;
}
}
while (1) {
uint8_t daysInMonth = getDaysInMonth(year, month);
if (timestamp >= daysInMonth) {
timestamp -= daysInMonth;
month++;
} else {
break;
}
}
// 填充结构体
dt->year = year;
dt->month = month;
dt->day = (uint8_t)(timestamp + 1);
dt->hour = (uint8_t)hours;
dt->minute = (uint8_t)minutes;
dt->second = (uint8_t)seconds;
usr_set_time.year=dt->year;
usr_set_time.month=dt->month;
usr_set_time.day=dt->day;
usr_set_time.hour=dt->hour;
usr_set_time.min=dt->minute;
usr_set_time.sec=dt->second;
usr_set_sys_time();
}
/*
> 喂食计划设置请求
* 命令ID 205
* 参数说明
| 索引 | 类型 | 描述 | 取值 |
| ---- | ------ | ------------------------------------------------------------ | ---------------------------- |
| 0 | int32 | 喂食计划个数(最多10个计划) | 0-10 |
| 0-N | string | 周计划 + 时间点 + 份数 +开关<br />周计划:ddddddd, 从左到右表示7个,分别表示周一到周日,1表示有效,0表示无效
<br />时间点:hhmm, hh表示小时,mm表示分钟<br />份数 n 表示份数<br />开关:x 1 表示开,0表示关<br />
例如<br />0000001+1828+5+1 表示每周日重复 +18:28+5份粮+开<br />
1000001+0928+3+1 表示每周一和周日重复+09:28+3份粮+开<br />
1100000+1328+2+0 表示每周一和周二重复+13:28+2份粮+关<br />
0000000+1032+1+1 表示单次 +10:32+1份粮+开 | 字符串<br />ddddddd+hhmm+n+x |
*/
// 示例用法
int utc_test_main() {
/* DateTime dt;
uint64_t utcTimestamp = 1747568305845; // 2024-05-18 00:00:00 UTC
int32_t timezoneOffset = 8*60*60; // 东八区(北京时间)
parseMillisecondTimestamp(utcTimestamp, timezoneOffset, &dt);
printf("utc_read_sys_time: %d-%d-%d %d:%d:%d\n",
dt.year,
dt.month,
dt.day,
dt.hour,
dt.minute,
dt.second);
// 现在dt结构体中包含解析后的本地时间信息
// dt.year = 2024
// dt.month = 5
// dt.day = 18
// dt.hour = 8 (UTC+8)
// dt.minute = 0
// dt.second = 0
*/
return 0;
}