English 中文(简体)
收到意外结果的灰色杜尔塔
原标题:Python timedelta receiving unexpected results

In a web application I m writing for an existing database I need to calculate the difference between now and a timestamp stored in the database (in a text field, it s stupid, I know). Here s my sqlalchemy Ban class and the relevant method.

class Ban(base):
    __tablename__=  bans 

    id= Column( banID , Integer, primary_key=True)
    unban_timestamp= Column( UNBAN , Text)
    banned_steamid= Column( ID , Text, ForeignKey( rp_users.steamid ))
    banned_name= Column( NAME , Text)
    banner_steamid= Column( BANNER_STEAMID , Text, ForeignKey( rp_users.steamid ))
    banner_name= Column( BANNER , Text)
    reason= Column( REASON , Text)

    def unbanned_in(self, mode):
        if self.unban_timestamp ==  0 :
            return  Never 
        else:
            now= datetime.datetime.utcnow()
            time= datetime.datetime.fromtimestamp(int(self.unban_timestamp))
            if now > time:
                return  Expired 
            else:
                remaining= time - now
                if mode== readable :
                    return remaining
                elif mode== int :
                    return str(remaining.seconds).zfill(10)

我既需要愤怒,又需要粗略的描述,因为我将在一张html桌上提出这种说法,而 j印需要一种简单的方式加以分类。 I m面临的问题是,愤怒和扼杀不一样,正如你在这里看到的那样:

“example

如果任何人能够感觉到产出为何如此rew升,那将受到赞赏! 如果掌握了你回答我问题所需要的更多信息,我很乐意补充。

edit

www.un.org/Depts/DGACM/index_french.htm 如果我通过我的职能来管理,这是我获得的结果。

>>> ban = session.query(db.Ban).filter_by(id=3783).one()
>>> print ban.unbanned_in( int ), ban.unbanned_in( readable )
0000049044 2 days, 13:37:24.179045
最佳回答

如果希望在<代码> 时和<代码>now/code>之间找到第二位数,请使用

remaining.days * 24 * 3600 + remaining.seconds instead of just remaining.seconds

问题回答

产出被重新计算,因为你计算将数秒数改为日数、小时、分钟和秒数是错误的。 既然你没有说我可以说的哪条法典,但你注意到,每天有86 400秒,你产出的所有数字都小于此。

你的计时数、分钟和秒的数值看重,仅仅计算你数天是错误的。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签