시간 단위 변환기
초, 분, 시간, 일, 주, 월, 년 사이의 시간 단위를 변환합니다.
자주 묻는 질문
코드 구현
# 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.