【问题标题】:Python: Issue with datetime.timestamp | Date March 26, 2017, 01:55, 02:55, 03:55Python:datetime.timestamp 问题 |日期 2017 年 3 月 26 日 01:55、02:55、03:55
【发布时间】:2017-03-23 13:08:45
【问题描述】:

我被一个特殊的情况难住了,我正在为我的输入文件创建纪元时间戳以检查重复项。运行代码后,我得到重复的错误,虽然实际上没有重复。

INPUT 1(带围栏分隔符的 CSV 文件):

1:55|The Chris Ramsey Show|||3/26/2017
2:25|South Park|The Biggest Douche in the Universe|615|3/26/2017
2:55|South Park|My Future Self 'n' Me|616|3/26/2017

在 INPUT 1 中发现重复项(来自我的代码):

(i.e., programs with the same start time and date):
(1490489700, '01:55', 'The Chris Ramsey Show', '', '03/26/2017')
(1490489700, '02:55', 'South Park', "My Future Self 'n' Me", '03/26/2017')

INPUT 2(带围栏分隔符的 CSV 文件):

3/26/2017|2:55|The Chris Ramsey Show|30|||103|Episode 3
3/26/2017|3:25|South Park|30|||615|The Biggest Douche in the Universe
3/26/2017|3:55|South Park|20|||616|My Future Self n' Me

在 INPUT 2 中发现重复项(来自我的代码):

(i.e., programs with the same start time and date):
(1490493300, '02:55', 'The Chris Ramsey Show', 'Episode 3', '03/26/2017')
(1490493300, '03:55', 'South Park', "My Future Self n' Me", '03/26/2017')

在调试时,我发现 3 个不同的时间段导致了这个问题。

03/26/2017 01:55
03/26/2017 02:55
03/26/2017 03:55

我尝试直接在 Python 中复制此问题,因此创建了以下代码:

import datetime
t1 = datetime.datetime(2017, 3, 26, 1, 55)
t2 = datetime.datetime(2017, 3, 26, 2, 55)
t3 = datetime.datetime(2017, 3, 26, 3, 55)
print(t1, t1.timestamp())
print(t2, t2.timestamp())
print(t3, t3.timestamp())

更令人困惑的是,Sublime Text 2 Python IDE 和 IDLE 给出了 2 个不同的重复项。

Sublime Text 2 IDE 的输出:

2017-03-26 01:55:00 1490489700.0
2017-03-26 02:55:00 1490489700.0
2017-03-26 03:55:00 1490493300.0
[Finished in 0.1s]

IDLE 的输出:

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================== RESTART: C:\Users\<>\Desktop\test.py ==================
2017-03-26 01:55:00 1490489700.0
2017-03-26 02:55:00 1490493300.0
2017-03-26 03:55:00 1490493300.0
>>> 

当然,我可以尝试使用不同的代码来获取纪元时间,但我想知道为什么会出现这个问题。

【问题讨论】:

    标签: python-3.x sublimetext2 python-idle


    【解决方案1】:

    当我按原样运行您的代码时,我会在 3.5.3 和 3.6.1 中获得 3 个不同的时间戳。

    2017-03-26 01:55:00 1490507700.0
    2017-03-26 02:55:00 1490511300.0
    2017-03-26 03:55:00 1490514900.0
    

    当我将日期更改为 3-12 时,即美国更改时间的“提前”日期,我分别得到了 3.5 和 3.6 的不同副本。

    # 3.5
    2017-03-12 01:55:00 1489301700.0
    2017-03-12 02:55:00 1489301700.0
    2017-03-12 03:55:00 1489305300.0
    
    # 3.6
    2017-03-12 01:55:00 1489301700.0
    2017-03-12 02:55:00 1489305300.0
    2017-03-12 03:55:00 1489305300.0
    

    我的结论是 a) 下周日是您所在国家/地区的“春季前进”日期,并且 b) Sublime Text 运行 3.5。消除歧义(重复)时间的方法在 3.6 中进行了更改。请参阅 datetime.timestamp 的文档。

    【讨论】:

    • 这个信息对我来说是新闻。感谢您分享此反馈。
    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多