【问题标题】:How to get the time since post published on praw如何获取praw上发表后的时间
【发布时间】:2016-10-23 12:04:04
【问题描述】:

我正在使用praw 编写一个机器人,我想知道自从发布帖子以来已经过去了多久。天真的解决方案是使用datetime

import datetime
import praw

... """read a list of submission"""

date = datetime.datetime.fromtimestamp(submission.created)
dif = datetime.datetime.now() - date

但是我得到了负时间戳。我想我需要考虑 reddit 的时区,我该怎么做?

【问题讨论】:

    标签: python bots reddit praw


    【解决方案1】:

    有一个特殊的函数会返回 utc 纪元时间。

    import datetime
    import praw
    
    ... #stuff
    
    date = datetime.datetime.fromtimestamp(submission.created_utc)
    dif = datetime.datetime.utcnow() - date
    

    utcnow() 返回世界时显示的当前时间。与 submit.created_utc 相同

    为了将来参考,您可以动态检查每个 praw 对象中可用的函数/变量(这比文档更可靠)。

    from pprint import pprint
    object = ... #any praw object like praw.Reddit praw.Submission praw.Comment etc
    pprint(dir(object))
    pprint(vars(object))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多