E
E
Evgeny .2018-10-02 08:22:56
Python
Evgeny ., 2018-10-02 08:22:56

How to calculate the time difference of a post with the current date?

I have this record in my database

indOne({_id: 1})
{
"_id" : 1,
"title" : "hello",
"last_modified" : Timestamp(1538452199, 1),
}

question on the "last_modified" field,
I need to get in python instead of this value either the time difference
(how many seconds have passed since the last modification)
or get only the data for the last 5 seconds.
important point - I should only use the database server time (and not the backend client).
Here is a request in python syntax I could not translate

query = {
timestamp: { // 18 minutes ago (from now)
$gt: new Date(ISODate().getTime() - 1000 * 60 * 18)
}
}

using the Pythonic construction datetime.utc_now() -
does not suit me. time of the client will be taken, in my cases on the client time can lag behind for years.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
yahabrovec, 2018-10-02
@yahabrovec

So, listen. A little explanation. I think you yourself understand that this time is in seconds, reporting from some time immemorial (like since 2000). You take this time and compare it with yours. You get something like this.

import time

lasttime = "тут берешь время которое выплюнул сервер"
nowtime = time.time()
razniza = int(nowtime) - int(lasttime) # получиться разница в секундах
print(razniza)

If you need to see how much time has passed in minutes, divide by 60.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question