Rockchip RK808 calendar
Presented verbatim from Linux rtc-rk808 source code.
Rated "most November per calendar year per SoC".
// SPDX-License-Identifier: GPL-2.0-only
/*
* RTC driver for Rockchip RK808
*
* Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
*
* Author: Chris Zhong <zyw@rock-chips.com>
* Author: Zhang Qing <zhangqing@rock-chips.com>
*/
// ...
/*
* The Rockchip calendar used by the RK808 counts November with 31 days. We use
* these translation functions to convert its dates to/from the Gregorian
* calendar used by the rest of the world. We arbitrarily define Jan 1st, 2016
* as the day when both calendars were in sync, and treat all other dates
* relative to that.
* NOTE: Other system software (e.g. firmware) that reads the same hardware must
* implement this exact same conversion algorithm, with the same anchor date.
*/
static time64_t nov2dec_transitions(struct rtc_time *tm)
{
return (tm->tm_year + 1900) - 2016 + (tm->tm_mon + 1 > 11 ? 1 : 0);
}
static void rockchip_to_gregorian(struct rtc_time *tm)
{
/* If it's Nov 31st, rtc_tm_to_time64() will count that like Dec 1st */
time64_t time = rtc_tm_to_time64(tm);
rtc_time64_to_tm(time + nov2dec_transitions(tm) * 86400, tm);
}
static void gregorian_to_rockchip(struct rtc_time *tm)
{
time64_t extra_days = nov2dec_transitions(tm);
time64_t time = rtc_tm_to_time64(tm);
rtc_time64_to_tm(time - extra_days * 86400, tm);
/* Compensate if we went back over Nov 31st (will work up to 2381) */
if (nov2dec_transitions(tm) < extra_days) {
if (tm->tm_mon + 1 == 11)
tm->tm_mday++; /* This may result in 31! */
else
rtc_time64_to_tm(time - (extra_days - 1) * 86400, tm);
}
}