Files
2025-06-25 10:47:04 +08:00

31 lines
828 B
C
Raw Permalink 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 <sys/time.h>
#include <rtthread.h>
/**
* @brief 测试 timegm 函数,将指定时间转换为时间戳
*
* 需要注意 tm 结构体的各字段含义:
* tm_year: 年份 = 实际年份 - 1900
* tm_mon : 月份 = 0~111月为0
* tm_mday: 日
* tm_hour: 时
* tm_min : 分
* tm_sec : 秒
* tm_isdst: 夏令时标志0表示不使用夏令时
*/
void test_timegm(void)
{
struct tm tm = {0};
time_t timestamp;
tm.tm_year = 2025 - 1900; // 年份从1900开始
tm.tm_mon = 5 - 1; // 月份0-115月为4
tm.tm_mday = 23;
tm.tm_hour = 9;
tm.tm_min = 56;
tm.tm_sec = 50;
tm.tm_isdst = 0; // 不使用夏令时
timestamp = mktime(&tm);
rt_kprintf("时间戳: %ld\n", (long)timestamp);
}
MSH_CMD_EXPORT(test_timegm, test timegm function);