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


一、时间戳转时间 timestamp2date.sh

 1 #!/bin/sh
 2 
 3 function usage(){  4     echo "-h --help \n" \  5          " 将10/13位时间戳转换为本地时间 \n"\  6          " 参数:时间戳,支持10/13位两种 \n"\  7          " 默认值:当前时间向后5min \n"\  8          " e.g. 1483430400(10位秒时间戳),1483430400000(13位毫秒时间戳) \n"
 9     exit 1
10 } 11 
12 ### 13 os_platform=`uname -s` 14 if [[ $# -le 0 ]]; then
15     echo "默认按照当前时间向后5min取值"
16     if [[ "${os_platform}" = "Darwin" ]];then
17         echo `date -v+5M +"%Y-%m-%d %H:%M:%S"` 18     elif [[ "${os_platform}" = "Linux" ]];then
19         echo `date -d +5min +"%Y-%m-%d %H:%M:%S"` 20     fi
21 else
22     case $1 in
23       -h|--help) 24  usage 25  ;; 26       *) 27           timestampStr=${1} 28           length=`echo ${#timestampStr}` 29           if [[ ${length} -ne 10 ]] && [[ ${length} -ne 13 ]];then
30               echo "请输入10/13位数字时间戳"
31               exit 1
32           elif [[ ${length} -eq 13 ]];then
33               timestampStr=${timestampStr:0:10} 34           fi
35           echo "时间戳位:${timestampStr}"
36       if [[ "${os_platform}" = "Darwin" ]];then
37               dateStr=`date -r${timestampStr} +"%Y-%m-%d %H:%M:%S"` 38           elif [[ "${os_platform}" = "Linux" ]];then
39               dateStr=`date -d @${timestampStr} +"%Y-%m-%d %H:%M:%S"` 40           fi
41           echo "${1}对应的本地时间为${dateStr}"
42  ;; 43     esac
44 fi

 命令行执行

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

二、时间转时间戳 date2timestamp.sh

 1 #!/bin/sh
 2 
 3 function usage(){  4     echo "-h --help \n" \  5          " 将本地时间转换为13位时间戳(毫秒时间戳) \n"\  6          " 只有1个参数:本地时间,参数格式:'%Y-%m-%d %H:%M:%S' \n"\  7          " 默认值:当前时间向后5min \n"\  8          " e.g. 2017-01-01 16:00:00 \n"
 9     exit 1
10 } 11 
12 
13 ##时间采用正则匹配 14 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}"
15 os_platform=`uname -s` 16 
17 if [[ $# -le 0 ]]; then
18     echo "默认按照当前时间向后5min取值"
19     if [[ "${os_platform}" = "Darwin" ]];then
20         echo `date -v+5M +%s`000
21     elif [[ "${os_platform}" = "Linux" ]];then
22         echo `date -d +5min +%s`000
23     fi
24 else
25     case $1 in
26       -h|--help) 27  usage 28  ;; 29       *) 30           dateStr=${1} 31           echo ${dateStr} 32           if [[ "${dateStr}" =~ ${time_pattern} ]];then
33               if [[ "${os_platform}" = "Darwin" ]];then
34                   echo `date -j -f "%Y-%m-%d %H:%M:%S" "${dateStr}" +%s`000
35               elif [[ "${os_platform}" = "Linux" ]];then
36                   echo `date -d "${dateStr}" +%s`000
37               fi
38           else
39               echo "时间格式不正确,请按照'%Y-%m-%d %H:%M:%S'格式输入,如'2017-01-01 16:00:00' "
40           fi
41  ;; 42     esac
43 fi

 

命令行执行

1 sh date2timestamp.sh '2017-10-13 14:38:30'
2 2017-10-13 14:38:30
3 1507876710000

 

作者:BestFei
链接:https://www.jianshu.com/p/063e81713de3
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM