Skip to content

Instantly share code, notes, and snippets.

@miy4
Last active April 29, 2021 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miy4/09e757072aabf7452411e64c2ba9c78d to your computer and use it in GitHub Desktop.
Save miy4/09e757072aabf7452411e64c2ba9c78d to your computer and use it in GitHub Desktop.
List examples of GNU Coreutils date
#!/bin/bash
source_user_defined() {
readonly cmds=(
'date +%Y/%m/%d'
'date +"%Y/%m/%d %T"'
'date +"%Y/%-m/%-d (%a)"'
'LC_ALL=C date +"%Y/%-m/%-d (%a)"'
'LC_ALL=ja_JP.utf8 date +"%EY"'
"date --date @$(date +%s)"
'date --iso-8601'
'date --iso-8601=hours'
'date --iso-8601=minutes'
'date --iso-8601=seconds'
'date --iso-8601=ns'
'date --rfc-email'
'date --rfc-822'
'date --rfc-2822'
'date --rfc-3339=date'
'date --rfc-3339=seconds'
'date --rfc-3339=ns'
'date --utc'
)
for s in "${cmds[@]}"; do
printf '%s\t\033[32;1m| %s\033[m\n' "$(eval "$s")" "$s"
done
}
source_date_help() {
date --help |
sed -n '/^ *%%/,/^ *%Z/p' | while read -r line; do
format="${line/% */}"
[[ $format == '%%' ]] && continue
[[ $format == '%t' ]] && continue
[[ $format == '%n' ]] && continue
example="$(date "+$format")"
description=${line#%[a-zA-Z:]* *}
printf '%s\t\033[32;1m| date +%s #%s\033[m\n' "$example" "$format" "$description"
done
}
main() {
{
source_user_defined
source_date_help
} | column -t -s $'\t'
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment