~kris/dots

srice

ref: e98f3b030dc24445bd55c68d95d2d81933fd68b3 srice/.local/bin/dmenu/dtime -rw-r--r-- 4.3 KiB
e98f3b03 — Kris Yotam chore: sync local state after restore (push updates, no pull) a month ago
                                                                                
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
###############################################################################
# dtime — World clock via dmenu + dunst
#
# Maintainer:   Kris Yotam <krisyotam@pm.me>
# License:      MIT
# Created:      2026-02-15
#
# Select a city/region from presets or type any city name.
# Displays the current time there via dunst notification.
###############################################################################

# Preset cities → IANA timezone
declare -A PRESETS=(
    ["New York"]="America/New_York"
    ["Los Angeles"]="America/Los_Angeles"
    ["Chicago"]="America/Chicago"
    ["London"]="Europe/London"
    ["Paris"]="Europe/Paris"
    ["Berlin"]="Europe/Berlin"
    ["Tokyo"]="Asia/Tokyo"
    ["Seoul"]="Asia/Seoul"
    ["Shanghai"]="Asia/Shanghai"
    ["Hong Kong"]="Asia/Hong_Kong"
    ["Dubai"]="Asia/Dubai"
    ["Mumbai"]="Asia/Kolkata"
    ["Sydney"]="Australia/Sydney"
    ["Auckland"]="Pacific/Auckland"
    ["Moscow"]="Europe/Moscow"
    ["Istanbul"]="Europe/Istanbul"
    ["Cairo"]="Africa/Cairo"
    ["São Paulo"]="America/Sao_Paulo"
    ["Mexico City"]="America/Mexico_City"
    ["Toronto"]="America/Toronto"
    ["Singapore"]="Asia/Singapore"
    ["Bangkok"]="Asia/Bangkok"
    ["Jakarta"]="Asia/Jakarta"
    ["Nairobi"]="Africa/Nairobi"
    ["Lagos"]="Africa/Lagos"
    ["Reykjavik"]="Atlantic/Reykjavik"
    ["Honolulu"]="Pacific/Honolulu"
    ["Anchorage"]="America/Anchorage"
    ["Denver"]="America/Denver"
    ["UTC"]="UTC"
)

# Display order
ORDERED=(
    "New York" "Los Angeles" "Chicago" "Toronto" "Mexico City"
    "São Paulo" "London" "Paris" "Berlin" "Moscow" "Istanbul"
    "Cairo" "Nairobi" "Lagos" "Dubai" "Mumbai" "Bangkok"
    "Jakarta" "Singapore" "Shanghai" "Hong Kong" "Seoul" "Tokyo"
    "Sydney" "Auckland" "Reykjavik" "Honolulu" "Anchorage" "Denver" "UTC"
)

###############################################################################
# resolve a free-form city name to a timezone
###############################################################################

resolve_tz() {
    local query="$1"

    # Check presets first (case-insensitive)
    for city in "${!PRESETS[@]}"; do
        if [[ "${city,,}" == "${query,,}" ]]; then
            echo "${PRESETS[$city]}"
            return 0
        fi
    done

    # Search IANA timezone database for a match
    local match
    match=$(timedatectl list-timezones | grep -i "${query// /_}" | head -1)
    if [ -n "$match" ]; then
        echo "$match"
        return 0
    fi

    # Try partial match on city component (after /)
    match=$(timedatectl list-timezones | grep -i "/${query// /_}" | head -1)
    if [ -n "$match" ]; then
        echo "$match"
        return 0
    fi

    # Try fuzzy: match anywhere in the tz string
    match=$(timedatectl list-timezones | grep -i "${query// /.*}" | head -1)
    if [ -n "$match" ]; then
        echo "$match"
        return 0
    fi

    return 1
}

###############################################################################
# format and notify
###############################################################################

show_time() {
    local city="$1"
    local tz="$2"

    local time_str offset_str
    time_str=$(TZ="$tz" date '+%I:%M %p')
    offset_str=$(TZ="$tz" date '+%Z (UTC%:z)')
    local day_str
    day_str=$(TZ="$tz" date '+%A, %B %d')
    local local_time
    local_time=$(date '+%I:%M %p')

    notify-send -t 8000 "󰥔  $city" "$time_str\n$day_str\n$offset_str\n\nLocal: $local_time"
}

###############################################################################
# main
###############################################################################

# Build menu
MENU=""
for city in "${ORDERED[@]}"; do
    tz="${PRESETS[$city]}"
    t=$(TZ="$tz" date '+%H:%M')
    MENU+="󰥔  $city  ($t)"$'\n'
done

CHOICE=$(printf '%s' "$MENU" | sed '/^$/d' | dmenu -i -l 20 -p "󰥔  World Clock")
[ -z "$CHOICE" ] && exit 0

# Extract city name (strip icon and time)
CITY=$(echo "$CHOICE" | sed 's/^[^ ]* *//; s/ *([0-9:]*) *$//')

# Try preset first
TZ_FOUND="${PRESETS[$CITY]}"

if [ -z "$TZ_FOUND" ]; then
    # User typed a custom city — resolve it
    TZ_FOUND=$(resolve_tz "$CITY")
    if [ -z "$TZ_FOUND" ]; then
        notify-send -t 5000 "󰥔  dtime" "Could not find timezone for: $CITY"
        exit 1
    fi
fi

show_time "$CITY" "$TZ_FOUND"