美文网首页
TensorFlow2 常用函数(不断更新)

TensorFlow2 常用函数(不断更新)

作者: 水之心 | 来源:发表于2020-09-09 10:40 被阅读0次

1 tf.timestamp

Unix 时间戳(Unix timestamp),或称 Unix 时间(Unix time)、POSIX 时间(POSIX time),是一种时间表示方式,定义为从格林威治时间 1970 年 01 月 01 日 00 时 00 分 00 秒起至现在的总秒数。Unix 时间戳不仅被使用在 Unix 系统、类 Unix 系统中,也在许多其他操作系统中被采用。

tf.timestamp 返回自 Unix 时代以来以秒为单位的时间戳,以 float64 表示。即返回的是 UTC:

import datetime
 
# 获得当前时间
now = datetime.datetime.now()
# 转换为指定的格式
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")

# TensorFlow
timeStamp = tf.timestamp()
dateArray = datetime.datetime.utcfromtimestamp(timeStamp.numpy())
otherStyleTime1 = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)
print(otherStyleTime1)

输出:

2020-09-09 09:38:27
2020-09-09 01:38:27

可以看出北京时间与 UTC 相差 8 小时。

这样就有:

timestamp = tf.timestamp()
# UTC 时间戳转换为天数(浮点数)
today_ts = tf.timestamp()%(24*60*60)
# UTC 时间转换为北京时间的小时数
hour = tf.cast(today_ts//3600+8, tf.int32)%tf.constant(24)
minute = tf.cast((today_ts%3600)//60, tf.int32)
second = tf.cast(tf.floor(today_ts%60),tf.int32)
tf.print('当前时间是', hour, '时', minute, '分', second, '秒')

输出:

当前时间是 9 时 56 分 59 秒

相关文章

网友评论

      本文标题:TensorFlow2 常用函数(不断更新)

      本文链接:https://www.haomeiwen.com/subject/wkosektx.html