18.221.41.214 |    

Navigation

Google Advertisement

This function converts time designations such as 1d or 12h into seconds.
sec=$( str2seconds "12h" )


str2seconds
  1. # *******************************************************
  2. # This function converts time designations such as
  3. # 1d or 12h into seconds. Supported are Xd, Xh, Xm, Xs.
  4. #
  5. # @param $1 - The time designations
  6. # @return init
  7. #
  8. function str2seconds () {
  9.  
  10. local item=$1
  11. local digit=$(echo $item | perl -n -e 'print $1 if ($_ =~ m/^(\d+)((?:d|h|m|s))$/g);')
  12. local unit=$(echo $item | perl -n -e 'print $2 if ($_ =~ m/^(\d+)((?:d|h|m|s))$/g);')
  13. local sec
  14.  
  15. case $unit in
  16.         d) sec=$((${digit}*86400)) ;;
  17.         h) sec=$((${digit}*3600))  ;;
  18.         m) sec=$((${digit}*60))    ;;
  19.         s) sec=${digit}            ;;
  20. esac
  21.  
  22. echo $sec
  23.  
  24. }
Parsed in 0.002 seconds at 325.32 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 5 users online.

Tags Cloud

Latest snippets
str2seconds
(Bash::Function)
is_integer
(Bash::Function)
file_rotate
(Bash::Function)
confirm
(Bash::Function)
is_workingtime
(Bash::Function)
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)