vitorpy's Dotfiles

Add automatic timezone detection with dual-clock display

- Create systemd service/timer for automatic timezone updates based on IP geolocation
- Add smart-clock.sh script that shows Warsaw time when traveling
- Update Waybar config to use custom clock module
- Timer checks timezone every hour and on boot
- Display format: 'Local Time (Warsaw Time)' when outside Warsaw
- Service enabled and working in Singapore

vitorpy d69cba84 cf94cb42

+138 -6
+11
private_dot_config/systemd/user/auto-timezone.service
··· 1 + [Unit] 2 + Description=Automatic Timezone Detection and Update 3 + After=network-online.target 4 + Wants=network-online.target 5 + 6 + [Service] 7 + Type=oneshot 8 + ExecStart=%h/.config/systemd/user/scripts/update-timezone.sh 9 + 10 + [Install] 11 + WantedBy=default.target
+11
private_dot_config/systemd/user/auto-timezone.timer
··· 1 + [Unit] 2 + Description=Automatic Timezone Detection Timer 3 + Requires=auto-timezone.service 4 + 5 + [Timer] 6 + # Check every hour 7 + OnBootSec=1min 8 + OnUnitActiveSec=1h 9 + 10 + [Install] 11 + WantedBy=timers.target
+10
private_dot_config/systemd/user/homebrew.spotifyd.service
··· 1 + [Unit] 2 + Description=Homebrew generated unit for spotifyd 3 + 4 + [Install] 5 + WantedBy=default.target 6 + 7 + [Service] 8 + Type=simple 9 + ExecStart="/home/linuxbrew/.linuxbrew/opt/spotifyd/bin/spotifyd" "--no-daemon" "--backend" "portaudio" 10 + Restart=always
+78
private_dot_config/systemd/user/scripts/executable_update-timezone.sh
··· 1 + #!/bin/bash 2 + 3 + # Automatic timezone detection based on IP geolocation 4 + # Updates system timezone if it differs from detected location 5 + 6 + LOG_FILE="$HOME/.local/share/auto-timezone.log" 7 + CACHE_FILE="$HOME/.cache/detected-timezone" 8 + MAX_AGE=3600 # Cache for 1 hour 9 + 10 + # Function to log messages 11 + log() { 12 + echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" 13 + } 14 + 15 + # Function to get timezone from IP geolocation 16 + get_timezone_from_ip() { 17 + # Using ipapi.co which doesn't require an API key 18 + local tz=$(curl -s --connect-timeout 5 --max-time 10 "https://ipapi.co/timezone/" 2>/dev/null) 19 + 20 + if [ -n "$tz" ] && [ "$tz" != "Undefined" ]; then 21 + echo "$tz" 22 + return 0 23 + fi 24 + 25 + # Fallback to ip-api.com 26 + tz=$(curl -s --connect-timeout 5 --max-time 10 "http://ip-api.com/line/?fields=timezone" 2>/dev/null) 27 + 28 + if [ -n "$tz" ] && [ "$tz" != "Undefined" ]; then 29 + echo "$tz" 30 + return 0 31 + fi 32 + 33 + return 1 34 + } 35 + 36 + # Check if cache exists and is recent 37 + if [ -f "$CACHE_FILE" ]; then 38 + cache_age=$(($(date +%s) - $(stat -c %Y "$CACHE_FILE" 2>/dev/null || echo 0))) 39 + if [ $cache_age -lt $MAX_AGE ]; then 40 + log "Using cached timezone (age: ${cache_age}s)" 41 + exit 0 42 + fi 43 + fi 44 + 45 + # Get current system timezone 46 + current_tz=$(timedatectl show --property=Timezone --value) 47 + 48 + # Detect timezone from IP 49 + detected_tz=$(get_timezone_from_ip) 50 + 51 + if [ -z "$detected_tz" ]; then 52 + log "Failed to detect timezone from IP" 53 + exit 1 54 + fi 55 + 56 + log "Detected timezone: $detected_tz (current: $current_tz)" 57 + 58 + # Update if different 59 + if [ "$detected_tz" != "$current_tz" ]; then 60 + log "Updating timezone from $current_tz to $detected_tz" 61 + 62 + # Update timezone 63 + timedatectl set-timezone "$detected_tz" 2>&1 | tee -a "$LOG_FILE" 64 + 65 + if [ $? -eq 0 ]; then 66 + log "Timezone updated successfully" 67 + echo "$detected_tz" > "$CACHE_FILE" 68 + 69 + # Trigger waybar reload to update clock 70 + pkill -RTMIN+1 waybar 2>/dev/null 71 + else 72 + log "Failed to update timezone" 73 + exit 1 74 + fi 75 + else 76 + log "Timezone unchanged" 77 + echo "$detected_tz" > "$CACHE_FILE" 78 + fi
+4 -6
private_dot_config/waybar/config
··· 6 6 "custom/dnf-updates" 7 7 ], 8 8 "modules-center": [ 9 - "clock" 9 + "custom/clock" 10 10 ], 11 11 "modules-right": [ 12 12 "pulseaudio", ··· 68 68 "cpu": { "format": "{usage}%"}, 69 69 "temperature": { "format": "{temperatureC}°C" }, 70 70 "custom/weather": { "exec": "~/.config/waybar/scripts/weather.sh", "interval": 600 }, 71 - "clock": { 71 + "custom/clock": { 72 + "exec": "~/.config/waybar/scripts/smart-clock.sh", 72 73 "interval": 30, 73 - "format": "{:%A, %d.%m %H:%M}", 74 - "locale": "en_US", 75 - "max-length": 30, 76 - "align": 0.5, 74 + "format": "{}", 77 75 "on-click": "gnome-calendar" 78 76 }, 79 77 "custom/kblayout": {
+21
private_dot_config/waybar/scripts/executable_smart-clock.sh
··· 1 + #!/bin/bash 2 + 3 + # Smart clock that shows local time + Warsaw time when traveling 4 + 5 + WARSAW_TZ="Europe/Warsaw" 6 + 7 + # Get current timezone 8 + current_tz=$(timedatectl show --property=Timezone --value) 9 + 10 + # Get local time 11 + local_time=$(date '+%A, %d.%m %H:%M') 12 + 13 + # Check if we're in Warsaw timezone 14 + if [ "$current_tz" = "$WARSAW_TZ" ]; then 15 + # Just show local time 16 + echo "$local_time" 17 + else 18 + # Show local time + Warsaw time in parentheses 19 + warsaw_time=$(TZ="$WARSAW_TZ" date '+%d.%m %H:%M') 20 + echo "$local_time ($warsaw_time)" 21 + fi