hackbyte

shellworldclock

Jan 19th, 2018 (edited)
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.25 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ################################################################################
  4. ########## ShellWorldClock - stuff the world does not need
  5. ########## by default shows a list of date outputs according to
  6. ########## different timezones..... .. you can specify the list of zones as
  7. ########## parameters to it ... the first one should probably your local one. ;)
  8. ##########
  9. ##########
  10. ########## 2018-01-12 by hackbyte, do whatever you want with it. ;)
  11. ########## 2018-01-21 by hackbyte, little bit of cleanup and inline documentation ;)
  12. ########## 2018-01-21 by hackbyte, added Pacific/Auckland (it's Rocket Labs launchpad in new zealand)
  13. ########## 2018-07-07 by hackbyte, removed mysteriously disappeared %q, shortened output anyway. You can reverse it as you ##########                         like......
  14. ################################################################################
  15.  
  16.  
  17. # getting the actual time and date to have consistent conversion times later
  18. THISDATE=$(LC_TIME=C date --utc)
  19.  
  20. # Were using these timezones, put your local timezone as first one! ;)
  21. TIMEZONES="Europe/Berlin Universal America/Cayenne US/Central US/Eastern EST US/Pacific Asia/Almaty Asia/Kolkata Asia/Tokyo Pacific/Auckland"
  22.  
  23. # we show this output for each timezone
  24. # default up to 2018-07-07 ## somehow %q disappeared and well, the rest was unnecessary anyways.. 8)
  25. # DATEFORMAT="%_4Z %_3::z %F %T (%B %A Q:%q W:%U D:%j)"
  26. # Shortened variant:
  27. DATEFORMAT="%_4Z %_3::z %F %T (%B %A)"
  28.  
  29. # The first timezone in the list is per definition our local timezone ;)
  30. LOCALTZ=$(echo $TIMEZONES | awk '{print $1}')
  31.  
  32. ################################################################################
  33. ########## Anything you can configure is above, usually no one needs to
  34. ########## do any maintenance below ...
  35. ##########
  36. ########## in other words, no user serviceable parts below
  37. ##########
  38. ########## eniweh, feel free to hack as you like... ;)
  39. ################################################################################
  40.  
  41. ################################################################################
  42. # This just repeatedly calls date with the timezone setting in the list for/with each entry
  43. function displayclocks {
  44.  while [ -n "$*" ] ; do # As long as $1 is not empty/zero
  45.   printf "%17s" $1
  46.   TZ="$1" date --date="$THISDATE" +"$DATEFORMAT" # set TZ to the correct value and then call date
  47.   if [ "$1" = "$LOCALTZ" ] ; then echo ; fi # if we are at the local timezione, just print an empty line
  48.   shift # remove the first parameter from the list, shorting it down by one
  49.  done # and repeat ;)
  50. }
  51.  
  52. ################################################################################
  53. ## here we go with the main functionality
  54. if [ -z "$*" ] ; then # if we have no cmdline parameters
  55.  displayclocks $TIMEZONES # call displayclocks with our internal default list
  56.  exit # and out
  57. else # or
  58.  TIMEZONES="" # empty the timezones variable
  59.  while [ -n "$1" ] ; do # while we have more parameters
  60.   TIMEZONES="${TIMEZONES} ${1}" #> append the actual value to the variable with a trailing space
  61.   shift # shift the parameter list once
  62.  done # and repeat
  63.  displayclocks $TIMEZONES # finally, call the displayclocks with the new list
  64. fi
  65. exit # Houston! Mission accomplished. ;)
Add Comment
Please, Sign In to add comment