CLI World Clock mini script

Forum Forums General Tips and Tricks CLI World Clock mini script

  • This topic has 12 replies, 5 voices, and was last updated Jan 10-10:58 pm by sleekmason.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #74897
    Member
    PPC

      I was looking to adapt a nice little GUI World Clock to include in my FT10. I still have not created such a script, but I managed to create a nice little one that Works in the terminal.
      -Copy the text below to a text document called something like “world-clock.sh” and save it
      -make sure the script is executable
      -run the script from the terminal

      Note: the script automatically creates the needed config file, that can be edited, and includes all needed instructions.

      #!/bin/bash
      # Command-line world clock by PPC 9/1/2022, GPL license
      #Adapted from https://gist.github.com/rangersmyth74/4c7e291b64d48c1beb7029e9b07b6bca
      #and from examples over at stackoverflow like over at https://stackoverflow.com/questions/370075/command-line-world-clock
      
      # create preference file $HOME/.world-clock.zones, if it's missing
      if [ ! -f $HOME/.world-clock.zones ]; then
      echo "Europe/London
      Europe/Paris
      Asia/Tokyo
      America/St_Johns
      Brazil/West
      " > $HOME/.world-clock.zones
      fi
      
      : ${WORLDCLOCK_ZONES:=$HOME/.world-clock.zones}
      : ${WORLDCLOCK_FORMAT:='+%Y-%m-%d %H:%M:%S %Z'}
      
      function world-clock ()
      {
      echo
      echo "		CLI World Clock:"
      echo
      
      while read zone
      do echo $zone '!' $(TZ=$zone date "$WORLDCLOCK_FORMAT")
      done < $WORLDCLOCK_ZONES |
      awk -F '!' '{ printf "%-20s  %s\n", $1, $2;}' |
      sort -b -r -k2,2 -k3,3
      echo
      echo "You can manage the displayed time zones by editing the file $HOME/.world-clock.zones"
      echo "The list of all available time zones, in English, is available at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"
      echo
      echo "Press Ctrl + C to exit"
      }
      
      export -f world-clock
      clear
      
      while true;
      do
      	world-clock;
      	sleep 1;
      	clear
      done
      

      Enjoy
      P.

      • This topic was modified 1 year, 3 months ago by PPC.
      #74900
      Moderator
      christophe
        Helpful
        Up
        0
        ::

        Very cool! 😉

        confirmed antiX frugaler, since 2019

        #74911
        Moderator
        Brian Masinick
          Helpful
          Up
          0
          ::

          I agree, very cool! I changed the London time to America/New-York and kept the others intact, so I have several, including UTC, which I believe is the same as London time anyway. Works great!

          --
          Brian Masinick

          #74962
          Member
          sleekmason
            Helpful
            Up
            0
            ::

            Love this:) Thinking about adding a right-click option in Lilidog for this. Good stuff. My wife and I are always trying to remember time zones of folks we know.

            #74965
            Member
            PPC
              Helpful
              Up
              0
              ::

              Thanks, everyone!

              I have a slightly updated version, that also shows local time (I noticed I was always looking at my toolbar to see what time it was here). Also I shortned the instructions, to make them more practical, and removed the empty final line that crept into the config file:

              #!/bin/bash
              # Command-line world clock by PPC 9/1/2022, GPL license
              #Adapted from https://gist.github.com/rangersmyth74/4c7e291b64d48c1beb7029e9b07b6bca
              #and from examples over at stackoverflow like over at https://stackoverflow.com/questions/370075/command-line-world-clock
              
              # create preference file $HOME/.world-clock.zones, if it's missing
              if [ ! -f $HOME/.world-clock.zones ]; then
              echo "Europe/London
              Europe/Paris
              Asia/Tokyo
              America/St_Johns
              Brazil/West" > $HOME/.world-clock.zones
              fi
              
              : ${WORLDCLOCK_ZONES:=$HOME/.world-clock.zones}
              : ${WORLDCLOCK_FORMAT:='+%Y-%m-%d %H:%M:%S %Z'}
              
              function world-clock ()
              {
              echo
              echo "		CLI World Clock:"
              echo
              local_time=$(date "$WORLDCLOCK_FORMAT")
              echo "Local Time	       $local_time"
              echo
              
              while read zone
              do echo $zone '!' $(TZ=$zone date "$WORLDCLOCK_FORMAT")
              done < $WORLDCLOCK_ZONES |
              awk -F '!' '{ printf "%-20s  %s\n", $1, $2;}' |
              sort -b -r -k2,2 -k3,3
              echo
              echo "To manage the time zones, edit the file $HOME/.world-clock.zones"
              echo "All time zones are listed at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"
              echo
              echo "Press Ctrl + C to exit"
              }
              
              export -f world-clock
              clear
              
              while true;
              do
              	world-clock;
              	sleep 1;
              	clear
              done
              

              @sleekmason – nice plug for your build! Myself, I find antiX full, with my FT10 transformation pack perfect in every single device I tested (from an 32bits/1Gig of RAM laptop to my quad-core, 4Gig desktop)

              P.

              • This reply was modified 1 year, 3 months ago by PPC.
              #74967
              Member
              sleekmason
                Helpful
                Up
                0
                ::

                @PPC
                Not plugging anything, but thanks. If I were, I would just make announcements. In fact, I have not even updated the original announcement I made in the section designed for such purposes even though it has gone through drastic changes.

                It seems I have made an error in how freely I can write/think. I certainly won’t discuss Lilidog or anything related anymore and I will remove access to “My build” in my signature. Sorry if I offended anyone by providing the link.

                I’ve used antiX since it’s inception, and it is one of two distros I regularly depend on for good advice. I’ve no doubt it runs well on everything you have.

                #74968
                Member
                PPC
                  Helpful
                  Up
                  0
                  ::

                  @sleekmason – You missunderstood me! I was happy to see the screenshots from another tinkerer like me!
                  Now and then I mention my FT10 transformation pack in other threads, other than the official one, when I think it makes sense. you simply said you were thinking about adding this script to your distro, I’m even flatered!

                  P.

                  • This reply was modified 1 year, 3 months ago by PPC.
                  #74971
                  Member
                  sleekmason
                    Helpful
                    Up
                    0
                    ::

                    @sleekmason – You missunderstood me! I was happy to see the screenshots from another tinkerer like me!
                    Now and then I mention my FT10 transformation pack in other threads, other than the official one, when I think it makes sense. you simply said you were thinking about adding this script to your distro, I’m even flatered!

                    P.

                    Oh good! I am relieved. Thank you:) I didn’t want to give up coming here:)

                    #74973
                    Member
                    sleekmason
                      Helpful
                      Up
                      0
                      ::

                      Would it be possible to change out Ctrl +C? to exit with something like:

                      echo $'\n\nPress any key to close.'; read -srn1

                      My thinking is to put it as a right-click option on the panel clock, with a simple ‘Press any key to close’.

                      My brilliant scripting ability of replace it until it works, didn’t. 🙂

                      #74985
                      Member
                      seaken64
                        Helpful
                        Up
                        0
                        ::

                        @sleekmason, I don’t think you have to worry about plugging your builds. I’m not an admin here, and Brian or others will correct me if I am wrong, but we did not take any offense. We are all tinkerers and are often interested in what others are doing to modify their systems. A casual mention of a project in context is not a problem.

                        Seaken64

                        #74990
                        Member
                        PPC
                          Helpful
                          Up
                          0
                          ::

                          Would it be possible to change out Ctrl +C?

                          Sure, I poked around some examples on-line and adapted the script- it now runs on even fewer resources and you can press any key (or the middle mouse button) to exit:

                          Here:

                          #!/bin/bash
                          # Command-line world clock by PPC 10/1/2022, GPL license
                          #Adapted from https://gist.github.com/rangersmyth74/4c7e291b64d48c1beb7029e9b07b6bca
                          #and from examples over at stackoverflow like over at https://stackoverflow.com/questions/370075/command-line-world-clock
                          
                          # create preference file $HOME/.world-clock.zones, if it's missing
                          if [ ! -f $HOME/.world-clock.zones ]; then
                          echo "Europe/London
                          Europe/Paris
                          Asia/Tokyo
                          America/St_Johns
                          Brazil/West" > $HOME/.world-clock.zones
                          fi
                          
                          worldtz () {
                          	: ${WORLDCLOCK_ZONES:=$HOME/.world-clock.zones}
                          : ${WORLDCLOCK_FORMAT:='+%Y-%m-%d %H:%M:%S %Z'}
                          echo
                          echo "		CLI World Clock:"
                          echo
                          local_time=$(date "$WORLDCLOCK_FORMAT")
                          echo "Local Time	       $local_time"
                          echo
                          while read zone
                          do echo $zone '!' $(TZ=$zone date "$WORLDCLOCK_FORMAT")
                          
                          done < $WORLDCLOCK_ZONES |
                          awk -F '!' '{ printf "%-20s  %s\n", $1, $2;}' |
                          sort -b -r -k2,2 -k3,3
                          echo
                          echo "To manage the time zones, edit the file $HOME/.world-clock.zones"
                          echo "All time zones are listed at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones"
                          echo
                          echo "Press any key to exit"
                          #sleep 1;
                          }
                          
                          export -f worldtz
                          watch -t -n 1 -x bash -c worldtz  &   # put function in the background
                          read -n 1
                          kill $!               # kill the the job most recently placed into the background
                          read -t 0.1 -n 1000000
                          
                          #74991
                          Moderator
                          Brian Masinick
                            Helpful
                            Up
                            0
                            ::

                            @sleekmason, I don’t think you have to worry about plugging your builds. I’m not an admin here, and Brian or others will correct me if I am wrong, but we did not take any offense. We are all tinkerers and are often interested in what others are doing to modify their systems. A casual mention of a project in context is not a problem.

                            Seaken64

                            You’re right. Simple mention of another distribution is fine; we even have a section to discuss experience with variations or rebuilds and reviews.

                            That would be a good place to discuss other things.

                            --
                            Brian Masinick

                            #74993
                            Member
                            sleekmason
                              Helpful
                              Up
                              0
                              ::

                              Thanks all:)
                              @PPC Cool! Thank you for taking the time to adjust your script! Nicely done:)

                            Viewing 13 posts - 1 through 13 (of 13 total)
                            • You must be logged in to reply to this topic.