Project 3.05 Section 3 ⚡ Embedded Relevance: Core RTC Epoch Time Leap Year Calendar Math Unix Timestamp

3.05 Gregorian Calendar Rules, RTC Peripheral Math & Unix Epoch Timestamps

Executive Summary: Implementing Gregorian calendar leap year determination. We examine the exact three-tier leap year algorithm (divisible by 4, except centuries unless divisible by 400), analyze hardware Real-Time Clock (RTC) calendar registers, and convert dates to Unix Epoch timestamps (seconds since Jan 1, 1970).

💻 1. Annotated Source Code

#include <iostream>
using namespace std;

int main() {
	int startYear;
	int endYear;

	cout << "Welcome to the Leap Year Checker!" << endl;
	
	cout << "Enter the starting year: ";
	cin >> startYear;

	cout << "Enter the ending year: ";
	cin >> endYear;

	for (int year = startYear; year <= endYear; year++) {
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
			cout << year << " is a leap year." << endl;
		}
		else {
			cout << year << " is not a leap year." << endl;
		}
	}

	return 0;
}

📐 2. Architecture & UML Class Model

📐 Calendar Rule Evaluator & Century Boundary Model
+ Public - Private # Protected
<<compilation-unit>> LeapYearEvaluator Calendar Pipeline
-year : int32_t
+isLeapYear(y: int32_t) : bool[(y%4==0 && y%100!=0) || y%400==0]

📚 3. Core C++ Concepts Deep-Dive

1. The Gregorian Leap Year Algorithm

A year is a leap year if:

  • It is divisible by 4,
  • EXCEPT if it is divisible by 100,
  • UNLESS it is also divisible by 400.

Formula: (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0).

⚡ 4. Embedded Systems & Hardware Reality

1. Hardware Real-Time Clock (RTC) Subsystems

Microcontroller RTC peripherals (backed by a 32.768 kHz quartz crystal and coin cell battery) track calendar time (BCC year/month/day/hour/min/sec) with automatic leap year compensation up to year 2099.

💡 5. Production-Ready Embedded Refactoring

Compile-time constexpr leap year calculation:

💡 Production-Ready Refactor
#include <cstdint>

constexpr bool isLeapYear(uint32_t year) noexcept {
    return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

constexpr uint8_t daysInMonth(uint32_t year, uint8_t month) noexcept {
    constexpr uint8_t DAYS[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && isLeapYear(year)) return 29;
    return month <= 12 ? DAYS[month] : 0;
}

📝 Knowledge Verification Quiz

Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.

Q1. Which of the following years is NOT a leap year under Gregorian calendar rules?
A 1900 (divisible by 100 but not 400)
B 2000 (divisible by 400)
C 2024 (divisible by 4)
D 2004 (divisible by 4)
Detailed Explanation: Century years are only leap years if divisible by 400. 1900 is divisible by 100 but not 400, so it was a standard 365-day year.
Q2. What is the standard quartz crystal frequency used by microcontroller Real-Time Clock (RTC) peripherals?
A 32.768 kHz (2^15 Hz, which a 15-bit prescaler divides to exact 1 Hz ticks)
B 8.000 MHz
C 100.000 kHz
D 1.000 GHz
Detailed Explanation: $32,768\text{ Hz} = 2^{15}\text{ Hz}$. A 15-stage binary counter divides this frequency down to exactly 1 pulse per second ($1\text{ Hz}$) with low power.
Q3. What is the starting reference date (Epoch) for Unix timestamps?
A January 1, 1970 00:00:00 UTC
B January 1, 2000 00:00:00 UTC
C January 1, 1900 00:00:00 UTC
D December 31, 1999 23:59:59 UTC
Detailed Explanation: Unix time measures the continuous elapsed seconds since 00:00:00 UTC on January 1, 1970.
Q4. What is the 'Year 2038 Problem' in 32-bit embedded systems?
A 32-bit signed time_t integers (seconds since 1970) will overflow on January 19, 2038, wrapping around to negative year 1901
B Microcontroller batteries expire
C Flash memory reaches 100% wear
D The RTC crystal stops oscillating
Detailed Explanation: Signed 32-bit integers max out at $2,147,483,647$ seconds, which elapses on Jan 19, 2038. Upgrading to 64-bit int64_t time resolves this.