time_point convert_time_point(std::chrono::system_clock::time_point const& value) { std::time_t const time = std::chrono::system_clock::to_time_t(value); std::int64_t day_number = time / 86400L; std::int64_t time_of_day = time % 86400L; time_point result; result.hours = time_of_day / 3600L; result.minutes = time_of_day % 3600L / 60L; result.seconds = time_of_day % 60L; auto const fractional_seconds = value - std::chrono::time_point_cast<std::chrono::seconds>(value); result.microseconds = std::chrono::duration_cast<std::chrono::microseconds>(fractional_seconds).count(); for(result.year = 1970; day_number >= get_days_in_year(result.year); day_number -= get_days_in_year(result.year), ++result.year) { } for(result.month = 1; day_number >= get_days_in_month(result.year, result.month - 1); day_number -= get_days_in_month(result.year, result.month - 1), ++result.month) { } result.month_name = get_month_name(result.month - 1); result.day = day_number + 1; return result; }
/* update 'tm' to the next second */ static void rtc_next_second(struct tm *tm) { int days_in_month; tm->tm_sec++; if ((unsigned)tm->tm_sec >= 60) { tm->tm_sec = 0; tm->tm_min++; if ((unsigned)tm->tm_min >= 60) { tm->tm_min = 0; tm->tm_hour++; if ((unsigned)tm->tm_hour >= 24) { tm->tm_hour = 0; /* next day */ tm->tm_wday++; if ((unsigned)tm->tm_wday >= 7) tm->tm_wday = 0; days_in_month = get_days_in_month(tm->tm_mon, tm->tm_year + 1900); tm->tm_mday++; if (tm->tm_mday < 1) { tm->tm_mday = 1; } else if (tm->tm_mday > days_in_month) { tm->tm_mday = 1; tm->tm_mon++; if (tm->tm_mon >= 12) { tm->tm_mon = 0; tm->tm_year++; } } } } } }