了解rtc_time64_to_tm()和rtc_tm_to_time64()

rtc_time64_to_tm()和rtc_tm_to_time64()主要用于RTC的驱动程序,在Linux外部RTC驱动中较常见。

打开"drivers/rtc/lib.c"

/*

* rtc_time64_to_tm - Converts time64_t to rtc_time.

* Convert seconds since 01-01-1970 00:00:00 to Gregorian date.

*/

//将time转换为年月日时分秒和星期几

void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)

{

unsigned int month, year, secs;

int days;

/* time must be positive */

days = div_s64_rem(time, 86400, &secs);/*计算总共有多少天*/

/* day of the week, 1970-01-01 was a Thursday */

tm->tm_wday = (days + 4) % 7;/*计算是星期几*/

year = 1970 + days / 365;/*计算公元年数值*/

days -= (year - 1970) * 365

  • LEAPS_THRU_END_OF(year - 1)
  • LEAPS_THRU_END_OF(1970 - 1);

while (days < 0) {

year -= 1;

days += 365 + is_leap_year(year);

}

tm->tm_year = year - 1900;/*计算年*/

tm->tm_yday = days + 1;

for (month = 0; month < 11; month++) {

int newdays;

newdays = days - rtc_month_days(month, year);

if (newdays < 0)

break;

days = newdays;

}

tm->tm_mon = month;

tm->tm_mday = days + 1;

tm->tm_hour = secs / 3600;/*计算小时*/

secs -= tm->tm_hour * 3600;

tm->tm_min = secs / 60;/*计算分钟*/

tm->tm_sec = secs - tm->tm_min * 60;/*计算秒*/

tm->tm_isdst = 0;

}

/*

* rtc_tm_to_time64 - Converts rtc_time to time64_t.

* Convert Gregorian date to seconds since 01-01-1970 00:00:00.

*/

//将年月日时分秒和星期几转换为64位的time

time64_t rtc_tm_to_time64(struct rtc_time *tm)

{

return mktime64(((unsigned int)tm->tm_year + 1900), tm->tm_mon + 1,

tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);

}

相关推荐
2401_8534482312 小时前
STM32F103C8T6---OLED显示屏
stm32·单片机·oled
沐欣工作室_lvyiyi12 小时前
基于单片机的居家智能音箱系统(论文+源码)
stm32·单片机·嵌入式硬件·毕业设计·智能音箱
沐欣工作室_lvyiyi12 小时前
基于单片机的飞机客舱窗帘控制系统(论文+源码)
单片机·嵌入式硬件·毕业设计·窗户
biter008812 小时前
Ubuntu 22.04 有线网络时好时坏?最终解决方案
linux·网络·ubuntu
zzzsde13 小时前
【Linux】基础开发工具(3):编译器
linux·运维·服务器
CHY_12813 小时前
JESD204B 协议解析(4)Subclass2 时序分析
嵌入式硬件·fpga开发·jesd204
Bona Sun13 小时前
单片机手搓掌上游戏机(十六)—pico运行fc模拟器之程序修改烧录
c语言·c++·单片机·游戏机
愚戏师14 小时前
Python3 多线程
linux·运维·服务器·python
小邓   ༽14 小时前
50道C++编程练习题及解答-C编程例题
c语言·汇编·c++·编程练习·c语言练习题
n***333514 小时前
linux redis简单操作
linux·运维·redis