import utime
from machine import Pin, I2C
import ds3231
import tm1637

# Initialize DS3231 RTC (I2C0: GP20=SDA, GP21=SCL)
i2c = I2C(0, scl=Pin(21), sda=Pin(20))
rtc = ds3231.DS3231(i2c)

# Initialize TM1637 7-segment display (CLK=GP2, DIO=GP3)
tm = tm1637.TM1637(clk=Pin(2), dio=Pin(3))

# Retirement date (YYYY, MM, DD)
retirement_date = (2027, 1, 2)

def days_until(target):
    """Calculate remaining days until the target date."""
    year, month, day = rtc.datetime()[:3]  # Get current year, month, day
    current_sec = utime.mktime((year, month, day, 0, 0, 0, 0, 0))  # Convert to seconds
    target_sec = utime.mktime(target + (0, 0, 0, 0, 0))
    return (target_sec - current_sec) // 86400  # Convert seconds to days

def months_until(target):
    """Calculate remaining months until the target date."""
    year, month, _ = rtc.datetime()[:3]
    return (target[0] - year) * 12 + (target[1] - month)

while True:
    days_left = days_until(retirement_date)
    months_left = months_until(retirement_date)

    # Display days left
    tm.number(days_left)
    utime.sleep(3)

    # Display months left
    tm.number(months_left)
    utime.sleep(3)
