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>
This commit is contained in:
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
35
utils/ip_format.py
Normal file
35
utils/ip_format.py
Normal file
@@ -0,0 +1,35 @@
|
||||
"""IP 주소 패딩/정규화 유틸리티"""
|
||||
|
||||
|
||||
def pad_ip(ip: str) -> str:
|
||||
"""IP 주소 각 옥텟을 3자리로 패딩.
|
||||
예: '192.168.0.74' -> '192.168.000.074'
|
||||
"""
|
||||
parts = ip.split(".")
|
||||
padded = []
|
||||
for part in parts:
|
||||
part = part.strip()
|
||||
padded.append(part.zfill(3)[:3])
|
||||
return ".".join(padded)
|
||||
|
||||
|
||||
def normalize_ip(ip: str) -> str:
|
||||
"""패딩된 IP를 일반 형태로 변환.
|
||||
예: '192.168.000.074' -> '192.168.0.74'
|
||||
"""
|
||||
parts = ip.split(".")
|
||||
return ".".join(str(int(p)) for p in parts)
|
||||
|
||||
|
||||
def sized_string(data: str, length: int, encoding: str = "euc-kr") -> str:
|
||||
"""문자열을 지정 바이트 길이로 맞춤 (공백 패딩). MS949/EUC-KR 한글 지원."""
|
||||
result = ""
|
||||
byte_count = 0
|
||||
for ch in data:
|
||||
ch_bytes = len(ch.encode(encoding, errors="replace"))
|
||||
if byte_count + ch_bytes > length:
|
||||
break
|
||||
result += ch
|
||||
byte_count += ch_bytes
|
||||
result += " " * (length - byte_count)
|
||||
return result
|
||||
37
utils/time_format.py
Normal file
37
utils/time_format.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""날짜시간 <-> 프로토콜 포맷 변환 유틸리티"""
|
||||
|
||||
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)
|
||||
Reference in New Issue
Block a user