Files
time_manager/utils/time_format.py
insulee 3c14e1e401 Initial commit: Dabit Time Manager project
Python-based time management application with UDP discovery,
TCP protocol communication, time sync, and drift monitoring.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-10 11:10:55 +09:00

38 lines
1.1 KiB
Python

"""날짜시간 <-> 프로토콜 포맷 변환 유틸리티"""
from datetime import datetime
def datetime_to_protocol(dt: datetime) -> str:
"""datetime을 프로토콜 포맷으로 변환.
포맷: YYMMDDdHHMMSS (13자)
d: 요일 (0=일, 1=월, 2=화, 3=수, 4=목, 5=금, 6=토)
예: 2026-02-09 23:02:29 (월) → '2602091230229'
"""
yy = dt.strftime("%y")
mm = dt.strftime("%m")
dd = dt.strftime("%d")
dow = dt.isoweekday() % 7 # 7->0(일), 1->1(월), ..., 6->6(토)
hh = dt.strftime("%H")
mi = dt.strftime("%M")
ss = dt.strftime("%S")
return f"{yy}{mm}{dd}{dow}{hh}{mi}{ss}"
def protocol_to_datetime(data: str) -> datetime:
"""프로토콜 포맷 문자열을 datetime으로 변환.
입력: YYMMDDdHHMMSS (13자)
예: '2602091230229' → 2026-02-09 23:02:29
"""
yy = int(data[0:2])
mm = int(data[2:4])
dd = int(data[4:6])
# data[6] = 요일 (무시)
hh = int(data[7:9])
mi = int(data[9:11])
ss = int(data[11:13])
year = 2000 + yy
return datetime(year, mm, dd, hh, mi, ss)