Shell 时间与时间戳相换(Mac)

一、时间戳转时间 timestamp2date.sh

#!/bin/sh

function usage(){
    echo "-h --help \n" \
         "  将10/13位时间戳转换为本地时间 \n"\
         "  参数:时间戳,支持10/13位两种 \n"\
         "  默认值:当前时间向后5min \n"\
         "  e.g. 1483430400(10位秒时间戳),1483430400000(13位毫秒时间戳) \n"
    exit 1
}

###
os_platform=`uname -s`
if [[ $# -le 0 ]]; then
    echo "默认按照当前时间向后5min取值"
    if [[ "${os_platform}" = "Darwin" ]];then
        echo `date -v+5M +"%Y-%m-%d %H:%M:%S"`
    elif [[ "${os_platform}" = "Linux" ]];then
        echo `date -d +5min +"%Y-%m-%d %H:%M:%S"`
    fi
else
    case $1 in
      -h|--help)
          usage
      ;;
      *)
          timestampStr=${1}
          length=`echo ${#timestampStr}`
          if [[ ${length} -ne 10 ]] && [[ ${length} -ne 13 ]];then
              echo "请输入10/13位数字时间戳"
              exit 1
          elif [[ ${length} -eq 13 ]];then
              timestampStr=${timestampStr:0:10}
          fi
          echo "时间戳位:${timestampStr}"
      if [[ "${os_platform}" = "Darwin" ]];then
              dateStr=`date -r${timestampStr} +"%Y-%m-%d %H:%M:%S"`
          elif [[ "${os_platform}" = "Linux" ]];then
              dateStr=`date -d @${timestampStr} +"%Y-%m-%d %H:%M:%S"`
          fi
          echo "${1}对应的本地时间为${dateStr}"
      ;;
    esac
fi

命令行执行

sh timestamp2date.sh 1507704300000
时间戳位:1507704300
1507704300000对应的本地时间为2017-10-11 14:45:00

二、时间转时间戳 date2timestamp.sh

#!/bin/sh

function usage(){
    echo "-h --help \n" \
         "  将本地时间转换为13位时间戳(毫秒时间戳) \n"\
         "  只有1个参数:本地时间,参数格式:'%Y-%m-%d %H:%M:%S' \n"\
         "  默认值:当前时间向后5min \n"\
         "  e.g. 2017-01-01 16:00:00 \n"
    exit 1
}


##时间采用正则匹配
time_pattern="^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} [0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}"
os_platform=`uname -s`

if [[ $# -le 0 ]]; then
    echo "默认按照当前时间向后5min取值"
    if [[ "${os_platform}" = "Darwin" ]];then
        echo `date -v+5M +%s`000
    elif [[ "${os_platform}" = "Linux" ]];then
        echo `date -d +5min +%s`000
    fi
else
    case $1 in
      -h|--help)
          usage
      ;;
      *)
          dateStr=${1}
          echo ${dateStr}
          if [[ "${dateStr}" =~ ${time_pattern} ]];then
              if [[ "${os_platform}" = "Darwin" ]];then
                  echo `date -j -f "%Y-%m-%d %H:%M:%S" "${dateStr}" +%s`000
              elif [[ "${os_platform}" = "Linux" ]];then
                  echo `date -d "${dateStr}" +%s`000
              fi
          else
              echo "时间格式不正确,请按照'%Y-%m-%d %H:%M:%S'格式输入,如'2017-01-01 16:00:00' "
          fi
      ;;
    esac
fi

命令行执行

sh date2timestamp.sh '2017-10-13 14:38:30'
2017-10-13 14:38:30
1507876710000
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,203评论 2 33
  • 内容一:Shell脚本语言-管道? ​分析场景:有的时候我们需要一个命令的输出作为另外一个命令输入? ​​ ​语法...
    Jackey_song阅读 454评论 0 1
  • 1.创建文件夹 !/bin/sh mkdir -m 777 "%%1" 2.创建文件 !/bin/sh touch...
    BigJeffWang阅读 10,145评论 3 53
  • 你在远方 春风带来了你的体香 我逆着春风寻找 不小心和寒冬撞了个正着 我 挨过寒冬 忍过秋风 却爱上了夏天的火辣和...
    山姆大叔z阅读 156评论 0 1
  • 是非偶得之你作诗,我配画(一) 初试牡丹,不懂晕墨。 诗自: 东城桂 唐·白居易 遥知天上桂花孤,试问嫦娥更要无。...
    是非成败阅读 229评论 0 2