-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtimezone_scrub.sh
More file actions
executable file
·71 lines (57 loc) · 2.03 KB
/
Copy pathtimezone_scrub.sh
File metadata and controls
executable file
·71 lines (57 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env bash
# PostToolUse hook: warn on timezone tokens that don't match the system's
# local timezone family. Most researchers publish status updates in their
# local time; cross-TZ collaboration is rare in personal docs and gets
# mishandled silently.
set -uo pipefail
input=$(cat)
file_path=$(echo "$input" | jq -r '.tool_input.file_path // .tool_input.file // empty' 2> /dev/null)
new_string=$(echo "$input" | jq -r '.tool_input.new_string // .tool_input.content // empty' 2> /dev/null)
if [ -z "$file_path" ] || [ -z "$new_string" ]; then
exit 0
fi
case "$file_path" in
*.md | *.txt | *.tex | *.rst) ;;
*)
exit 0
;;
esac
# Detect system timezone family
sys_tz=$(date +%Z 2> /dev/null || echo "")
sys_tz_long=""
if [ -L /etc/localtime ]; then
sys_tz_long=$(readlink /etc/localtime 2> /dev/null | sed 's|.*zoneinfo/||')
fi
if [ -z "$sys_tz" ] && [ -z "$sys_tz_long" ]; then
exit 0
fi
# Strip fenced code blocks (```...```) and inline code (`...`) before matching
stripped=$(echo "$new_string" | awk '
/^```/ { in_fence = !in_fence; next }
!in_fence { print }
' | sed -E 's/`[^`]+`//g')
# TZ tokens to check
tz_pattern='\b(EST|EDT|CST|CDT|MST|MDT|PST|PDT|UTC|GMT|BST|CET|CEST|JST|IST|AEST|AEDT|NZST|NZDT)\b'
hits=$(echo "$stripped" | grep -oE "$tz_pattern" | sort -u || true)
if [ -z "$hits" ]; then
exit 0
fi
# Filter out hits that match the system TZ
mismatched=""
while read -r tz; do
if [ "$tz" = "$sys_tz" ]; then
continue
fi
if [ -n "$mismatched" ]; then
mismatched="$mismatched, $tz"
else
mismatched="$tz"
fi
done <<< "$hits"
if [ -z "$mismatched" ]; then
exit 0
fi
cat << EOF
{"hookSpecificOutput":{"hookEventName":"PostToolUse","additionalContext":"TIMEZONE SCRUB: ${file_path} contains TZ tokens (${mismatched}) that don't match your system timezone (${sys_tz}). Status updates and ETAs in personal docs should normally use one canonical TZ. If the TZ token is correct (e.g. quoting a remote log), wrap the timestamp in backticks or a fenced code block to mark it as verbatim."}}
EOF
exit 0