81 lines
2.7 KiB
Bash
81 lines
2.7 KiB
Bash
#!/bin/bash
|
|
|
|
# 自定义颜色显示
|
|
c_br='\e[1;31m' # bold red
|
|
c_bg='\e[1;32m' # bold green
|
|
c_by='\e[1;33m' # bold yellow
|
|
c_bb='\e[1;34m' # bold blue
|
|
c_bp='\e[1;35m' # bold purple
|
|
c_bc='\e[1;36m' # bold cyan
|
|
c_bir='\e[1;3;31m' # * bold italic red
|
|
c_big='\e[1;3;32m' # bold italic cyan
|
|
c_bib='\e[1;3;34m' # * bold italic cyan
|
|
c_bic='\e[1;3;36m' # bold italic cyan
|
|
c_e='\e[0m' # reset
|
|
|
|
function usage() {
|
|
echo -e "${c_bir}将日期转换成十进制和十六进制时间戳,输入日期格式可参考如下,中间空格可替换成[a-zA-Z@#%^*:]中的任意一个单字符:${c_e}"
|
|
echo -e " 2023/03/09 09:29:02"
|
|
echo -e " 2023-03-09 09:29:02"
|
|
echo -e " 09/03/2023 09:29:02"
|
|
echo -e " 09/Mar/2023 09:29:02"
|
|
exit 4
|
|
}
|
|
|
|
|
|
ts=$@
|
|
|
|
if [[ $ts =~ (([J|j]an)|([F|f]eb)|([M|m]ar)|([A|a]pr)|([M|m]ay)|([J|j]un)|([J|j]ul)|([A|a]ug)|([S|s]ep)|([O|o]ct)|([N|n]ov)|([D|d]ec)) ]]; then
|
|
[[ ${#ts} -ne 20 ]] && usage
|
|
else
|
|
[[ ${#ts} -ne 19 ]] && usage
|
|
fi
|
|
|
|
# 2023/03/09 09:29:02
|
|
# 2023-03-09 09:29:02
|
|
fmt1="^[0-9]{4}[-/][0-9]{2}[-/][0-9]{2}[a-zA-Z@#%^*:]{0,1}[[:space:]]{0,1}[0-9]{2}:[0-9]{2}:[0-9]{2}$"
|
|
# 09/03/2023:09:29:02
|
|
fmt2="^[0-9]{2}/[0-9]{2}/[0-9]{4}[a-zA-Z@#%^*:]{0,1}[[:space:]]{0,1}[0-9]{2}:[0-9]{2}:[0-9]{2}$"
|
|
# 09/Mar/2023:09:29:02
|
|
fmt3="^[0-9]{2}/(([J|j]an)|([F|f]eb)|([M|m]ar)|([A|a]pr)|([M|m]ay)|([J|j]un)|([J|j]ul)|([A|a]ug)|([S|s]ep)|([O|o]ct)|([N|n]ov)|([D|d]ec))/[0-9]{4}[a-zA-Z@#%^*:]{0,1}[[:space:]]{0,1}[0-9]{2}:[0-9]{2}:[0-9]{2}$"
|
|
if [[ $ts =~ $fmt1 || $ts =~ $fmt2 ]]; then
|
|
ts=${ts:0:10}' '${ts:11}
|
|
dec=`date -d "$ts" +%s`
|
|
hex=`echo "obase=16; $dec" | bc`
|
|
echo "十进制的时间戳 - $dec"
|
|
echo "十六进制时间戳 - 0x$hex - $hex"
|
|
echo "十六进制时间戳 - 0x${hex,,} - ${hex,,}"
|
|
|
|
elif [[ $ts =~ $fmt3 ]]; then
|
|
day=${ts:0:2}
|
|
month=${ts:3:3}
|
|
left=${ts:7}
|
|
[[ $month =~ ^[J|j]an$ ]] && month='01'
|
|
[[ $month =~ ^[F|f]eb$ ]] && month='02'
|
|
[[ $month =~ ^[M|m]ar$ ]] && month='03'
|
|
[[ $month =~ ^[A|a]pr$ ]] && month='04'
|
|
[[ $month =~ ^[M|m]ay$ ]] && month='05'
|
|
[[ $month =~ ^[J|j]un$ ]] && month='06'
|
|
[[ $month =~ ^[J|j]ul$ ]] && month='07'
|
|
[[ $month =~ ^[A|a]ug$ ]] && month='08'
|
|
[[ $month =~ ^[S|s]ep$ ]] && month='09'
|
|
[[ $month =~ ^[O|o]ct$ ]] && month='10'
|
|
[[ $month =~ ^[N|n]ov$ ]] && month='11'
|
|
[[ $month =~ ^[D|d]ec$ ]] && month='12'
|
|
ts=$month'/'$day'/'$left
|
|
ts=${ts:0:10}' '${ts:11}
|
|
dec=`date -d "$ts" +%s`
|
|
hex=`echo "obase=16; $dec" | bc`
|
|
echo "十进制的时间 - $dec"
|
|
echo "十六进制时间 - 0x${hex} - $hex"
|
|
echo "十六进制时间 - 0x${hex,,} - ${hex,,}"
|
|
else
|
|
echo -e "${c_br}请检查输入的时间符合正常规则,退出...${c_e}"
|
|
usage
|
|
exit 10
|
|
fi
|
|
|
|
|
|
|
|
|