Skip to main content
added 66 characters in body
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11
 # "location=label"; ordered by label locations=( "America/Santiago=Chile" "Atlantic/Stanley=Falkland Islands" "Pacific/Auckland=New Zealand" "Asia/Dubai=Port Alfred, Iles Crozet" "Africa/Johannesburg=South Africa" "Australia/Adelaide=South Australia, Australia" "Atlantic/South_Georgia=South Georgia and the South Sandwich Islands" "America/Argentina/Ushuaia=Tierra Del Fuego" "Etc/UTC=UTC" "Australia/Melbourne=VIC, NSW, ACT, and QLD, Australia" "Australia/Perth=Western Australia, Australia" ) printf -v now "%(%s)T" for ((i=0; i<"$i<${#locations[@]}";; i++)) do IFS="=" read -r location label <<< "${locations[i]}" # split on "=" delimiter into variables "location" and "label" TZ="$location" printf '%s: %(%H:%M%p, %A, %d %B, %Y)T\n' "$label" "$now" done 
 # "location=label"; ordered by label locations=( "America/Santiago=Chile" "Atlantic/Stanley=Falkland Islands" "Pacific/Auckland=New Zealand" "Asia/Dubai=Port Alfred, Iles Crozet" "Africa/Johannesburg=South Africa" "Australia/Adelaide=South Australia, Australia" "Atlantic/South_Georgia=South Georgia and the South Sandwich Islands" "America/Argentina/Ushuaia=Tierra Del Fuego" "Etc/UTC=UTC" "Australia/Melbourne=VIC, NSW, ACT, and QLD, Australia" "Australia/Perth=Western Australia, Australia" ) printf -v now "%(%s)T" for ((i=0; i<"${#locations[@]}"; i++)) do IFS="=" read -r location label <<< "${locations[i]}" TZ="$location" printf '%s: %(%H:%M%p, %A, %d %B, %Y)T\n' "$label" "$now" done 
 # "location=label"; ordered by label locations=( "America/Santiago=Chile" "Atlantic/Stanley=Falkland Islands" "Pacific/Auckland=New Zealand" "Asia/Dubai=Port Alfred, Iles Crozet" "Africa/Johannesburg=South Africa" "Australia/Adelaide=South Australia, Australia" "Atlantic/South_Georgia=South Georgia and the South Sandwich Islands" "America/Argentina/Ushuaia=Tierra Del Fuego" "Etc/UTC=UTC" "Australia/Melbourne=VIC, NSW, ACT, and QLD, Australia" "Australia/Perth=Western Australia, Australia" ) printf -v now "%(%s)T" for ((i=0; i<${#locations[@]}; i++)) do IFS="=" read -r location label <<< "${locations[i]}" # split on "=" delimiter into variables "location" and "label" TZ="$location" printf '%s: %(%H:%M%p, %A, %d %B, %Y)T\n' "$label" "$now" done 
Source Link
markp-fuso
  • 1.7k
  • 1
  • 9
  • 11

Modifying OP's current code to use OP's associative array:

for location in "${!locationsArray[@]}" do td=$(TZ="$location" date "+%H:%M%p, %A, %d %B, %Y") echo "${locationsArray[$location]}: ${td}" done 

This generates:

Port Alfred, Iles Crozet: 17:27PM, Friday, 27 September, 2024 New Zealand: 01:27AM, Saturday, 28 September, 2024 Western Australia, Australia: 21:27PM, Friday, 27 September, 2024 UTC: 13:27PM, Friday, 27 September, 2024 South Australia, Australia: 22:57PM, Friday, 27 September, 2024 Chile: 10:27AM, Friday, 27 September, 2024 South Georgia and the South Sandwich Islands: 11:27AM, Friday, 27 September, 2024 VIC, NSW, ACT, and QLD, Australia: 23:27PM, Friday, 27 September, 2024 Falkland Islands: 10:27AM, Friday, 27 September, 2024 Tierra Del Fuego: 10:27AM, Friday, 27 September, 2024 South Africa: 15:27PM, Friday, 27 September, 2024 

NOTES:

  • as alluded to in Stéphane's answer you can't (easily) control the order in which associative array entries are processed
  • this is not an exact match to OP's expected output due to inconsistencies in the inputs/outputs, eg, for input #1 South Australia, Australia the expected output #2 South Australia strips the country, while for input #2 VIC, NSW, ACT, and QLD, Australia the expected output #2 VIC, NSW, ACT, and QLD, Australia does not strip the country; OP will need to provide the rules for if/when to strip strings from the input, or update the question to insure inputs and outputs match

Modifying the above code to use a variation of Stéphane's bash / printf call:

printf -v now "%(%s)T" # obtain current epoch (seconds), store in variable "now" for location in "${!locationsArray[@]}" do TZ="$location" printf '%s: %(%H:%M%p, %A, %d %B, %Y)T\n' "${locationsArray[$location]}" "$now" done 

This generates:

Port Alfred, Iles Crozet: 17:45PM, Friday, 27 September, 2024 New Zealand: 01:45AM, Saturday, 28 September, 2024 Western Australia, Australia: 21:45PM, Friday, 27 September, 2024 UTC: 13:45PM, Friday, 27 September, 2024 South Australia, Australia: 23:15PM, Friday, 27 September, 2024 Chile: 10:45AM, Friday, 27 September, 2024 South Georgia and the South Sandwich Islands: 11:45AM, Friday, 27 September, 2024 VIC, NSW, ACT, and QLD, Australia: 23:45PM, Friday, 27 September, 2024 Falkland Islands: 10:45AM, Friday, 27 September, 2024 Tierra Del Fuego: 10:45AM, Friday, 27 September, 2024 South Africa: 15:45PM, Friday, 27 September, 2024 

To print the results in a particular order OP could switch to a normal array while making sure to populate the array in the same order as the desired output, eg:

 # "location=label"; ordered by label locations=( "America/Santiago=Chile" "Atlantic/Stanley=Falkland Islands" "Pacific/Auckland=New Zealand" "Asia/Dubai=Port Alfred, Iles Crozet" "Africa/Johannesburg=South Africa" "Australia/Adelaide=South Australia, Australia" "Atlantic/South_Georgia=South Georgia and the South Sandwich Islands" "America/Argentina/Ushuaia=Tierra Del Fuego" "Etc/UTC=UTC" "Australia/Melbourne=VIC, NSW, ACT, and QLD, Australia" "Australia/Perth=Western Australia, Australia" ) printf -v now "%(%s)T" for ((i=0; i<"${#locations[@]}"; i++)) do IFS="=" read -r location label <<< "${locations[i]}" TZ="$location" printf '%s: %(%H:%M%p, %A, %d %B, %Y)T\n' "$label" "$now" done 

This generates:

Chile: 10:57AM, Friday, 27 September, 2024 Falkland Islands: 10:57AM, Friday, 27 September, 2024 New Zealand: 01:57AM, Saturday, 28 September, 2024 Port Alfred, Iles Crozet: 17:57PM, Friday, 27 September, 2024 South Africa: 15:57PM, Friday, 27 September, 2024 South Australia, Australia: 23:27PM, Friday, 27 September, 2024 South Georgia and the South Sandwich Islands: 11:57AM, Friday, 27 September, 2024 Tierra Del Fuego: 10:57AM, Friday, 27 September, 2024 UTC: 13:57PM, Friday, 27 September, 2024 VIC, NSW, ACT, and QLD, Australia: 23:57PM, Friday, 27 September, 2024 Western Australia, Australia: 21:57PM, Friday, 27 September, 2024