drivers/rtc/lib.c
Source file repositories/reference/linux-study-clean/drivers/rtc/lib.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/rtc/lib.c- Extension
.c- Size
- 5864 bytes
- Lines
- 212
- Domain
- Driver Families
- Bucket
- drivers/rtc
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/export.hlinux/rtc.h
Detected Declarations
function rtc_month_daysfunction rtc_year_daysfunction rtc_time64_to_tmfunction rtc_valid_tmfunction rtc_tm_to_time64function rtc_tm_to_ktimefunction rtc_ktime_to_tmexport rtc_month_daysexport rtc_year_daysexport rtc_time64_to_tmexport rtc_valid_tmexport rtc_tm_to_time64export rtc_tm_to_ktimeexport rtc_ktime_to_tm
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
/*
* rtc and date/time utility functions
*
* Copyright (C) 2005-06 Tower Technologies
* Author: Alessandro Zummo <a.zummo@towertech.it>
*
* based on arch/arm/common/rtctime.c and other bits
*
* Author: Cassio Neri <cassio.neri@gmail.com> (rtc_time64_to_tm)
*/
#include <linux/export.h>
#include <linux/rtc.h>
static const unsigned char rtc_days_in_month[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
static const unsigned short rtc_ydays[2][13] = {
/* Normal years */
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
/* Leap years */
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
};
/*
* The number of days in the month.
*/
int rtc_month_days(unsigned int month, unsigned int year)
{
return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
}
EXPORT_SYMBOL(rtc_month_days);
/*
* The number of days since January 1. (0 to 365)
*/
int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
{
return rtc_ydays[is_leap_year(year)][month] + day - 1;
}
EXPORT_SYMBOL(rtc_year_days);
/**
* rtc_time64_to_tm - converts time64_t to rtc_time.
*
* @time: The number of seconds since 01-01-1970 00:00:00.
* Works for values since at least 1900
* @tm: Pointer to the struct rtc_time.
*/
void rtc_time64_to_tm(time64_t time, struct rtc_time *tm)
{
int secs;
u64 u64tmp;
u32 u32tmp, udays, century, day_of_century, year_of_century, year,
day_of_year, month, day;
bool is_Jan_or_Feb, is_leap_year;
/*
* The time represented by `time` is given in seconds since 1970-01-01
* (UTC). As the division done below might misbehave for negative
* values, we convert it to seconds since 0000-03-01 and then assume it
* will be non-negative.
* Below we do 4 * udays + 3 which should fit into a 32 bit unsigned
* variable. So the latest date this algorithm works for is 1073741823
* days after 0000-03-01 which is in the year 2939805.
*/
time += (u64)719468 * 86400;
udays = div_s64_rem(time, 86400, &secs);
/*
* day of the week, 0000-03-01 was a Wednesday (in the proleptic
* Gregorian calendar)
*/
tm->tm_wday = (udays + 3) % 7;
/*
* The following algorithm is, basically, Figure 12 of Neri
* and Schneider [1]. In a few words: it works on the computational
* (fictitious) calendar where the year starts in March, month = 2
* (*), and finishes in February, month = 13. This calendar is
* mathematically convenient because the day of the year does not
* depend on whether the year is leap or not. For instance:
*
* March 1st 0-th day of the year;
* ...
* April 1st 31-st day of the year;
Annotation
- Immediate include surface: `linux/export.h`, `linux/rtc.h`.
- Detected declarations: `function rtc_month_days`, `function rtc_year_days`, `function rtc_time64_to_tm`, `function rtc_valid_tm`, `function rtc_tm_to_time64`, `function rtc_tm_to_ktime`, `function rtc_ktime_to_tm`, `export rtc_month_days`, `export rtc_year_days`, `export rtc_time64_to_tm`.
- Atlas domain: Driver Families / drivers/rtc.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.