如何使用timesince

0 投票
2 回答
2159 浏览
提问于 2025-04-16 19:08

我找到了这个代码片段:

def timesince(dt, default="just now"):
        now = datetime.utcnow()
        diff = now - dt
        periods = (
                (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    )
    for period, singular, plural in periods:
            if period:
                        return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

我想在查询我的Google App Engine数据库时使用它。我的数据库大概是这样的:

class Service(db.Model):
    name = db.StringProperty(multiline=False)
    urla = db.StringProperty(multiline=False)
    urlb = db.StringProperty(multiline=False)
    urlc = db.StringProperty(multiline=False)
    timestampcreated = db.DateTimeProperty(auto_now_add=True)
    timestamplastupdate = db.DateTimeProperty(auto_now=True)

在我的网页应用的主页面请求处理器中,我想这样做:

            elif self.request.get('type') == 'list':
                    q = db.GqlQuery('SELECT * FROM Service')
                    count = q.count()
                    if count == 0:
                            self.response.out.write('Success: No services registered, your database is empty.')
                    else: 
                            results = q.fetch(1000)
                            for result in results:
                                    resultcreated = timesince(result.timestampcreated)
                                    resultupdated = timesince(result.timestamplastupdate)
                                    self.response.out.write(result.name + '\nCreated:' + resultcreated + '\nLast Updated:' + resultupdated + '\n\n')

我哪里做错了?我在用这个代码片段格式化我的代码时遇到了麻烦。

我应该选择哪个呢?

这个?

def timesince:
class Service
class Mainpage
  def get(self):

还是这个?

class Service
class Mainpage
  def timesince:
  def get(self):

我对Python不太熟悉,任何关于如何解决这个问题的建议都非常感谢!

2 个回答

0

这对我来说运行得很好:

from datetime import datetime 
def timesince(dt, default="now"):
    now = datetime.now()
    diff = now - dt
    periods = (
        (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    )
    for period, singular, plural in periods:
        if period >= 1:
            return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

timesince(datetime(2016,6,7,12,0,0))
timesince(datetime(2016,6,7,13,0,0))
timesince(datetime(2016,6,7,13,30,0))
timesince(datetime(2016,6,7,13,50,0))
timesince(datetime(2016,6,7,13,52,0))
0

我不太明白你遇到的问题是什么,所以请耐心点。能提供一个错误追踪信息会很有帮助。 :)

timesince()这个函数不需要任何类里面的变量,所以我觉得它不应该放在某个类里。如果我是你,我可能会把timesince放在一个单独的文件里,然后在定义Mainpage的文件中导入这个模块。

如果你把它们都放在同一个文件里,确保你的缩进是一致的,并且不要使用制表符。

撰写回答