bashfzf

Bash script for timezones displays all countries time as that of GMT


I am trying to write a bash script for a world clock
This is my script

#!/bin/bash

# Define time zones
declare -A timezones
timezones["Germany"]="/Europe/Berlin"
timezones["United_States_New_York"]="/America/New_York"
timezones["Japan"]="/Asia/Tokyo"

# Use fzf to select multiple countries
selected_countries=$(printf '%s\n' "${!timezones[@]}" | fzf -m)

while true; do
    buffer=$(

    # Iterate over selected countries
    for selected_country in $selected_countries; do
        timezone="${timezones[$selected_country]}"
        echo "The date in ${selected_country} is $(env TZ=$timezone date) $timezone"
    done
)
    clear  
    echo "$buffer"
    sleep 1

done



When I run this script the time for all three countries is always that of GMT The last part of the echo ie $timezone gives me the correct timezone but for some reason its not being set in TZ=$timezone.
Here is an example output.

The date in Germany is Mon May 20 07:43:31 AM 2024 /Europe/Berlin The date in Japan is Mon May 20 07:43:31 AM 2024 /Asia/Tokyo

Any help is much appreciated


Solution

  • Timezone names don't start with a slash. Remove the slash to get the actual timezone name.

    $ TZ=UTC date
    Mon 20 May 2024 08:09:24 AM UTC
    
    $ TZ=/America/New_York date
    Mon 20 May 2024 08:09:34 AM 
    
    $ TZ=America/New_York date
    Mon 20 May 2024 04:09:36 AM EDT