[download]

local/bin/daylight

   1 #!/bin/bash
   2 
   3 f=~/.cache/daylight
   4 p='--socks5 localhost:9050'
   5 
   6 [[ -s $f ]] && [[ $(date +%F) == $(date -r $f +%F) ]] ||
   7 curl -s $p 'https://ru.wttr.in/spb?format=%D+%S+%z+%s+%d' > $f
   8 
   9 declare -A map=(
  10        [dawn]=Рассвет
  11     [sunrise]=Восход
  12      [zenith]=Зенит
  13      [sunset]=Заход
  14        [dusk]=Сумерки
  15 )
  16 read dawn sunrise zenith sunset dusk < $f
  17 
  18 now=$(date +%s)
  19 order=$(
  20     for i in dawn sunrise zenith sunset dusk; do
  21         time=$(date +%s -d ${!i})
  22         diff=$(( time - now ))
  23 
  24         (( diff < 0 )) && continue
  25 
  26         echo "$diff $i ${map[$i]}"
  27     done | sort -n | cut -d ' ' -f 2
  28 
  29     for i in dawn sunrise zenith sunset dusk; do
  30         time=$(date +%s -d ${!i})
  31         diff=$(( time - now ))
  32 
  33         (( diff < 0 )) || continue
  34 
  35         echo "$diff $i ${map[$i]}"
  36     done | sort -n | cut -d ' ' -f 2
  37 )
  38 
  39 {
  40     for i in $order; do       echo "${map[$i]}"; done | paste -s
  41     for i in $order; do i=${!i}; echo "${i%:*}"; done | paste -s
  42 } | column -t
  43 
  44 a=$(date -d $sunrise +%s)
  45 b=$(date -d $sunset  +%s)
  46 
  47 echo
  48 echo "Светлое время суток: $(date -ud @$(( b - a )) +'%_H ч. %M мин.')"
  49 echo "Тёмное  время суток: $(date -ud @$(( a - b )) +'%_H ч. %M мин.')"