Date format
To generate the variables webtime
and localtime
, you are using respectively time.strftime
and datetime.strftime
, which are 2 methods that return strings
, and not dates.
Dates and times have no format. A date is simply a value (actually a set of values: day, month and year) that represents a specific point in the calendar, and a time is another set of values (time, minute, second, fractions of a second) that represents a specific moment of a day.
These values can be represented in many different ways. For example, the date of "1 February 2018" has 3 values: day 1, month 2 and year 2018. But this same date can be represented in several different ways:
- 01/02/2018 - common format in several countries
- 1 February 2018 - in good Portuguese
- 2018-02-01 - format ISO 8601
- Feb 1st, 2018 - common format in the USA
- 2018 年 2 月 1 日 - in japanese
Although all these formats (all these strings) are different, they all represent the same date.
So, if you want to calculate the difference between two dates, don’t turn them into strings (only do this if you want to show the date in a specific format). To do calculations and other manipulations with the dates, use them directly - and in your case, as you are already using the module datetime
, use the types it offers.
Difference between dates
To calculate the difference between the dates, we first need to get them as a datetime
.
I see you’re wearing time.localtime
. This method receives a timestamp in seconds (the number of seconds since the Unix Epoch) and converts to the "local" date and time (ie the corresponding day and time on the machine Timezone where the code is running).
But then you get the current date on a specific Timezone (America/Sao_Paulo
), then I suggest that both dates are in the same Timezone so that we can compare them.
Using datetime
, you can convert the timestamp to a date and time in a specific Timezone, using datetime.fromtimestamp
(passing the timestamp and Timezone), and can get the current date/time on the same Timezone, using datetime.now
and passing Timezone as parameter. Then you can subtract the datetimes
directly, obtaining a timedelta
:
from datetime import datetime
from pytz import timezone
fuso_horario = timezone('America/Sao_Paulo')
# usando o timestamp e o fuso horário
webdate = datetime.fromtimestamp(response.tx_time, tz=fuso_horario)
# data/hora atual no mesmo timezone usado acima
agora = datetime.now(tz=fuso_horario)
# diferença é um timedelta
diferenca = agora - webdate
# diferença maior ou igual a 2 minutos
if diferenca.total_seconds() >= 120:
print("diferença maior ou igual a 2 minutos")
In the documentation says that if one date has Timezone and another does not, the calculation of the difference between them launches a TypeError
. So I created both with a Timezone.
Note: from Python 3.9 you can use module zoneinfo
, the operation of which is very similar to pytz
:
from datetime import datetime
from zoneinfo import ZoneInfo
fuso_horario = ZoneInfo('America/Sao_Paulo')
# usando o timestamp e o fuso horário
webdate = datetime.fromtimestamp(response.tx_time, tz=fuso_horario)
# data/hora atual no mesmo timezone usado acima
agora = datetime.now(tz=fuso_horario)
# diferença é um timedelta
diferenca = agora - webdate
# diferença maior ou igual a 2 minutos
if diferenca.total_seconds() >= 120:
print("diferença maior ou igual a 2 minutos")
Simplifying
Although, in your case, as you receive from the web the value of timestamp in seconds, just get the value of the current timestamp (also in seconds) using time.time()
and subtract from each other:
import time
# diferença, em segundos, entre o instante atual e o recebido da web
diferenca = time.time() - response.tx_time
if diferenca >= 120:
print("diferença maior ou igual a 2 minutos")
Timestamp is a "universal" value (it is the same all over the world, all computers that run time.time()
at the same time they will have the same result). So I don’t even need to convert it to a date and time in a specific Timezone, since we just want to know the difference between them.
Create datetimes
in a specific Timezone will be useful if you need to manipulate the dates afterwards (or show them on the screen, for example using strftime
). Otherwise, for this specific case, it seems simpler to use only timestamps.
It was unclear if you want to know if the value of the web is in the past or future compared to the current date. If it does, you can get the absolute value of the difference (without the signal) using abs
:
diferenca = abs(time.time() - response.tx_time)
Or, if using the above solution with timedelta
, do if abs(diferenca.total_seconds()) >= 120
.
If you only want to know if the web date is in the future (2 minutes after the current date), just flip and do response.tx_time - time.time()
(and see if it is greater than or equal to 120).
You want it to return true if the difference between a "datetime" and another is less than or equal to 2 minutes, correct?
– Gustavo Sampaio
Yes @Gustavosampaio
– Florida
Is it necessary to compare only with dates of the human way (day, month, year etc)? It seems much simpler to work with only in seconds.
– Giovanni Nunes
It is not necessary to compare the given in the human way, as long as they are compared all right.
– Florida
@Giovanninunes In fact, in this case it seems easier to work with values in seconds than to create dates. Anyway, I left the 2 options below...
– hkotsubo