🛠️ToolsShed

Conversor de Unidades de Tiempo

Convierte entre segundos, minutos, horas, días, semanas, meses y años.

Preguntas Frecuentes

Implementación de Código

# Time unit conversion in Python
SECONDS = {
    "ns":  1e-9,       # nanosecond
    "us":  1e-6,       # microsecond
    "ms":  1e-3,       # millisecond
    "s":   1,          # second
    "min": 60,         # minute
    "h":   3600,       # hour
    "d":   86400,      # day
    "wk":  604800,     # week
    "yr":  31536000,   # year (365 days)
}

def convert_time(value: float, from_unit: str, to_unit: str) -> float:
    seconds = value * SECONDS[from_unit]
    return seconds / SECONDS[to_unit]

# divmod approach: seconds → hours, minutes, seconds
def seconds_to_hms(total_seconds: int):
    minutes, secs = divmod(total_seconds, 60)
    hours, mins   = divmod(minutes, 60)
    return hours, mins, secs

# Examples
print(convert_time(1, "h", "min"))    # 60.0
print(convert_time(1, "d", "h"))      # 24.0
print(convert_time(1000, "ms", "s"))  # 1.0
print(seconds_to_hms(3723))           # (1, 2, 3)

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.