Skip to content

Instantly share code, notes, and snippets.

@caryyu
Last active June 17, 2020 12:55
Show Gist options
  • Save caryyu/96c5eb930dc8b23171adff9477cc6f45 to your computer and use it in GitHub Desktop.
Save caryyu/96c5eb930dc8b23171adff9477cc6f45 to your computer and use it in GitHub Desktop.
Shell tips and tricks - 中文

Cron 表达式转换为日期

主要使用 https://github.com/caryyu/cronexpr 命令工具

  • MacOS - cronexpr -n 1 "30 16 * * *" | {read t; date -r "$t"}
  • Linux - cronexpr -n 1 "30 16 * * *" | {read t; date --date=@"$t"}

Date 日期调整(加一天,加一小时,等)

  • MacOS - date -v+1d/date -v-1d/date -v+1H
  • Linux - date -d "+1 days" "+%s"/date -d "-1 days" "+%s"/date -d "-1 hours" "+%s" (Linux 的另外一种变通方案:date "+%s" | {read t; echo $(( $t - 3600 ))})

Date 日期区间计算(获取两个日期的间隔秒)

  • MacOS - echo $(( $(date "+%s") - $(date -v-1d "+%s") ))
  • Linux - echo $(( $(date "+%s") - $(date -d "-1 days" "+%s") ))

Date 三元表达式(判断时间区间,类似 timediff 作用)

  • MacOS - echo $(( $(date "+%s") - $(date -v-20M "+%s") )) | {read t; [[ "$t" < 1800 ]] && echo "true" || echo "false" }
  • Linux - echo $(( $(date "+%s") - $(date -d "-20 minutes" "+%s") )) | {read t; [[ "$t" < 1800 ]] && echo "true" || echo "false" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment