|
我有一个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年问题”的问题吗?
最佳答案
引用documentation:
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)
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|