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:
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
|
||||
Reference in New Issue
Block a user