您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将Unix时间戳值转换为人类可读的python

5b51 2022/1/14 8:21:38 python 字数 3841 阅读 516 来源 www.jb51.cc/python

我有一个Unix时间戳,其值为1502878840.这个Unix时间戳值可以转换为人类可读,如2017年8月16日10:20:40.我有2个以下python代码将1502878840转换为2017年8月16日10:20:40.两者都给出相同的结果(2017年8月16日10:20:40)>第一种方法utc = datetime.fromtimestamp

概述

我有一个Unix时间戳,其值为1502878840.这个Unix时间戳值可以转换为人类可读,如2017年8月16日10:20:40.
我有2个以下python代码将1502878840转换为2017年8月16日10:20:40.两者都给出相同的结果(2017年8月16日10:20:40)

>第一种方法
utc = datetime.fromtimestamp(1502878840)
>第二种方法
utc = datetime(1970,1,1)timedelta(秒= 1502878840)

任何人都可以回答我2以下的问题.
1.两种方法的结果相同.但是在Python代码的逻辑观点上,是否有任何可能导致结果差异的情况?
我问这个问题,因为我看到大多数python代码都使用First方法.
2.当我读到here时,Unix时间将在2038年1月19日03:14:08 GMT出现问题.
我运行一个时间戳,其日期是19日,1938年(2148632440- 2月01日,2038年10月20日40:40).结果如下
第一种方法:ValueError:时间戳超出平台time_t的范围
第二种方法:2038-02-01 10:20:40
问题是:我可以使用第二种方法解决“2038年问题”的问题吗?

fromtimestamp() may raise OverflowError,if the timestamp is out of the range of values supported by the platform C localtime() or gmtime() functions,and OSError on localtime() or gmtime() failure. It’s common for this to be restricted to years in 1970 through 2038. Note that on non-POSIX systems that include leap seconds in their notion of a timestamp,leap seconds are ignored by fromtimestamp(),and then it’s possible to have two timestamps differing by a second that yield identical datetime objects. See also utcfromtimestamp().

第二个解决方解决了您的问题:

utc = datetime(1970,1) + timedelta(seconds=1502878840)

总结

以上是编程之家为你收集整理的将Unix时间戳值转换为人类可读的python全部内容,希望文章能够帮你解决将Unix时间戳值转换为人类可读的python所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶