当前位置: 首页 > article >正文

了解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);

}


http://www.kler.cn/a/547680.html

相关文章:

  • Biopython PDB模块的PDBParser和MMCIFParser介绍
  • 如何使用CSS画一个三角形,原理是什么?
  • DeepSeek操作Excel,实现图表自动化生成
  • LLaMA-Factory DeepSeek-R1 模型 微调基础教程
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第十二节】
  • PLC跨平台通信困难?DeepSeek边缘计算网关实现数据无缝连接!
  • 【PSIM】RS触发器的使用
  • 08模拟法 + 技巧 + 数学 + 缓存(D2_技巧)
  • 【自学笔记】深度学习基础知识点总览-持续更新
  • 《Spring实战》(第6版)第2章 开发Web应用
  • 基于STM32的智能鱼塘养殖监控系统
  • 【动态路由】系统Web URL资源整合系列(后端技术实现)【nodejs实现】
  • 免费deepseek的API获取教程及将API接入word或WPS中
  • 安装 Docker Desktop 修改默认安装目录到指定目录
  • DeepSeek 助力 Vue 开发:打造丝滑的卡片(Card)
  • 性能测试流程、主流性能工具
  • Dockerfiles 的 Top 10 常见 DevOps/SRE 面试问题及答案
  • AI赋能电商:创新应用与未来展望
  • Hutool - Cache:简单而强大的缓存实现
  • 华为最新OD机试真题-最长子字符串的长度(一)-Python-OD统一考试(E卷)