Skip to content

Instantly share code, notes, and snippets.

@btamayo
Last active June 18, 2022 09:48
Show Gist options
  • Save btamayo/8b850c5e86c849e9c871bc4179aeb43b to your computer and use it in GitHub Desktop.
Save btamayo/8b850c5e86c849e9c871bc4179aeb43b to your computer and use it in GitHub Desktop.
bash functions including date formatting
#!/usr/bin/env bash
BLACK=$(tput setaf 0)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
LIME_YELLOW=$(tput setaf 190)
POWDER_BLUE=$(tput setaf 153)
BLUE=$(tput setaf 4)
MAGENTA=$(tput setaf 5)
CYAN=$(tput setaf 6)
WHITE=$(tput setaf 7)
BRIGHT=$(tput bold)
NORMAL=$(tput sgr0)
BLINK=$(tput blink)
REVERSE=$(tput smso)
UNDERLINE=$(tput smul)
RESET=$NORMAL
print_sub_step() {
local spacer=" "
local spacer_per_level=2
((limit = $1 * spacer_per_level))
# In order to use brace expansion with a variable, we must use `eval`.
# printf evaluates arguments as they come in. Here, there are $limit arguments
# because of bash's brace '...' expansion. What ends up happening is this sequence
# of calls in the background:
# printf %.s $spacer 1, then printf %.s $spacer 2, printf %.s $spacer 3, ... to $limit times
# the format string '.s' is passed the number 1, 2, 3, etc. as a param, but is
# not expanded because of the dot ., which makes the field size zero.
local prepad=$(eval "printf '%.s$spacer' {1..$limit} ")
local input_string="[$2]"
local string2=""
local pad=$(printf '%0.1s' " "{1..80})
# <prefix><prepad><input_string><pad><string2>
printf '%s%s' "${CYAN}" "$prefix"
printf '%s' "$prepad"
printf '%s ' "$input_string"
printf '%*.*s%s\n' 0 $((padlength - ${#prefix} - ${#prepad} - ${#input_string} - ${#string2} - ${#CYAN} - ${#RESET} )) "$pad" "$RESET"
}
print_substep_plain() {
local spacer=" "
local spacer_per_level=2
((limit = $1 * spacer_per_level))
# In order to use brace expansion with a variable, we must use `eval`.
# printf evaluates arguments as they come in. Here, there are $limit arguments
# because of bash's brace '...' expansion. What ends up happening is this sequence
# of calls in the background:
# printf %.s $spacer 1, then printf %.s $spacer 2, printf %.s $spacer 3, ... to $limit times
# the format string '.s' is passed the number 1, 2, 3, etc. as a param, but is
# not expanded because of the dot ., which makes the field size zero.
local prepad=$(eval "printf '%.s$spacer' {1..$limit} ")
local input_string="$2"
local string2=""
# <prefix><prepad><input_string><pad><string2>
printf '%s%s' "${prepad}${RESET}"
printf ' %s\n' "$input_string" "$RESET"
}
print_step() {
local pad=$(printf '%0.1s' " "{1..80})
local string2=''
local string1="[$1] "
printf '%s' "$prefix"
printf '%s' "$string1"
printf '%*.*s' 0 $((padlength - ${#prefix} - ${#string1} - ${#string2} )) "$pad"
printf '%s\n' "$string2"
}
dates() {
cat << EOD
Format/result | Command | Output
--------------------------------+----------------------------+------------------------------
YYYY-MM-DD_hh:mm:ss | gdate +%F_%T | $(gdate +%F_%T)
YYYYMMDD_hhmmss | gdate +%Y%m%d_%H%M%S | $(gdate +%Y%m%d_%H%M%S)
YYYYMMDD_hhmmss (UTC version) | gdate --utc +%Y%m%d_%H%M%SZ | $(gdate --utc +%Y%m%d_%H%M%SZ)
YYYYMMDD_hhmmss (with local TZ) | gdate +%Y%m%d_%H%M%S%Z | $(gdate +%Y%m%d_%H%M%S%Z)
YYYYMMSShhmmss | gdate +%Y%m%d%H%M%S | $(gdate +%Y%m%d%H%M%S)
YYYYMMSShhmmssnnnnnnnnn | gdate +%Y%m%d%H%M%S%N | $(gdate +%Y%m%d%H%M%S%N)
YYMMDD_hhmmss | gdate +%y%m%d_%H%M%S | $(gdate +%y%m%d_%H%M%S)
Seconds since UNIX epoch: | gdate +%s | $(gdate +%s)
Nanoseconds only: | gdate +%N | $(gdate +%N)
Nanoseconds since UNIX epoch: | gdate +%s%N | $(gdate +%s%N)
ISO8601 UTC timestamp | gdate --utc +%FT%TZ | $(gdate --utc +%FT%TZ)
ISO8601 UTC timestamp + ms | gdate --utc +%FT%T.%3NZ | $(gdate --utc +%FT%T.%3NZ)
ISO8601 Local TZ timestamp | gdate +%FT%T%Z | $(gdate +%FT%T%Z)
YYYY-MM-DD (Short day) | gdate +%F\(%a\) | $(gdate +%F\(%a\))
YYYY-MM-DD (Long day) | gdate +%F\(%A\) | $(gdate +%F\(%A\))
EOD
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment