我是Python新手。
我正在创建一个新的机器人,使用PRAW和Python,对显示其基本帐户信息的新帖子发表评论(用户名,日期帐户已创建,业力计数)。
我试图让它显示海报帐户活跃的年份,月份和日期,但到目前为止只显示年份。有没有更好的方法可以让它显示这些信息,或者我可以用我已经设置的东西创建它?到目前为止我所拥有的是:
author = post.author
if author:
seconds_year = 60 * 60 * 24 * 365
now = datetime.datetime.now(datetime.timezone.utc).timestamp()
difference = now - author.created_utc
years = difference // seconds_year
author_created = datetime.datetime.utcfromtimestamp(author.created_utc)
author_created = datetime.datetime.strftime(author_created, "%d %B %Y")
print("Commenting user history")
comment = COMMENT_TEMPLATE % (author.name, author_created, years, author.link_karma, author.comment_karma)
post.add_comment(comment)
sql.commit()
答案 0 :(得分:0)
您可以使用divmod()
function查找年,月,日:
DAY = 24 * 60 * 60 # seconds
MONTH = 30 * DAY
YEAR = 365.2524 * DAY # Gregorian year
years, seconds = divmod(difference, YEAR)
months, seconds = divmod(seconds, MONTH)
days, seconds = divmod(seconds, DAY)