python / shell / mysql 获取转换毫秒时间戳 13位

最近,写监控脚本需要 和 mysql 的13位时间戳 做对比:

python 2 & 3 通用

import datetime
import time

# 本机时间
hlocal = datetime.datetime.now()
# utc时间
hutc = datetime.datetime.utcnow()
# utc - 12 时间
hutc12 = datetime.datetime.utcnow()-datetime.timedelta(hours=12)
# utc 30 天
hutcday30 = datetime.timedelta(days=30)
# utc - 12h - 30d 格式化位时
hutc12h30d = (hutc12-hutcday30).strftime("%Y-%m-%d %H:00:00")
# 10位 时间戳
hutc12h30dtuple10w = int(time.mktime((time.strptime(hutc12h30d, "%Y-%m-%d %H:%M:%S"))))
# 13位时间戳
hutc12h30dtuple13w = int(time.mktime((time.strptime(hutc12h30d, "%Y-%m-%d %H:%M:%S"))) * 1000)

# 当前时间 - 15d
hlocal15 = datetime.datetime.now()-datetime.timedelta(days=15)
# 当前时间 - 15d 转 毫秒级时间戳
hlocal15inttuple = (time.mktime(hlocal15.timetuple()) * 1000 + hlocal15.microsecond / 1000)

shell

# 获取当前时间 / 毫秒精度
current=`date "+%Y-%m-%d %H:%M:%S"`
# 获取当前时间 - 30d / 毫秒精度
current15d=`date -d -30days "+%Y-%m-%d %H:%M:%S"`
# 转换毫秒时间戳
timeStamp=`date -d "$current" +%s`
currentTimeStamp=$((timeStamp*1000+`date "+%N"`/1000000))
echo $currentTimeStamp

mysql

mysql -u root -p'password' -e "SELECT REPLACE(unix_timestamp(current_timestamp(3)),'.','');"
posted @ 2020-08-27 12:32  运维之爪  阅读(1455)  评论(0编辑  收藏  举报