"""하네스 훅 공용 유틸 (표준 라이브러리 전용).""" import json import os import subprocess import sys from datetime import datetime, timedelta, timezone KST = timezone(timedelta(hours=9)) def kst_now() -> datetime: override = os.environ.get("HARNESS_NOW_KST") # 테스트용: 2026-10-02T10:00 if override: try: return datetime.fromisoformat(override).replace(tzinfo=KST) except ValueError: pass return datetime.now(KST) def market_state(now: datetime) -> str: """'휴장-주말' | '장전' | '장중' | '장후'. 공휴일은 판정하지 않는다(market_schedule 과 동일 한계).""" if now.weekday() >= 5: return "휴장-주말" hm = now.hour * 60 + now.minute if hm < 8 * 60 + 30: return "장전" if hm <= 15 * 60 + 40: return "장중" return "장후" def read_stdin_json() -> dict: try: data = json.load(sys.stdin) return data if isinstance(data, dict) else {} except Exception: return {} def project_dir(data: dict | None = None) -> str: return os.environ.get("CLAUDE_PROJECT_DIR") or (data or {}).get("cwd") or os.getcwd() def git(args: list[str], cwd: str, timeout: float = 5.0) -> tuple[int, str]: try: p = subprocess.run( ["git", *args], cwd=cwd, capture_output=True, text=True, timeout=timeout ) return p.returncode, p.stdout except Exception: return 1, ""