Converting local time to Zulu time in order to compare times - python

I have a DB with time entries formatted as follow:
2018-11-05T08:58:00Z
I'm trying to generate SQL queries to compare "now()" with the time in the DB to determine which row(s) to return.
I'm battling to "convert" my local time (now()) to an equivalent time format so that I can use < or > operations against the DB values.
Additionally, I am not sure if the problem has two parts. The example fo the time above is not in a "Datetime" field in MySQL but stored simply as TEXT, leaving me to suspect that I would need to "convert" the DB entries into another format first?

The following code, using the datetime module, works for me (tested in Python 3.6):
import datetime
value = "2018-11-05T08:58:00Z"
dt = datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ")
# Result is: datetime.datetime(2018, 11, 5, 8, 58)
This will convert your string values to datetime instances, which you can then compare to now(). The values that get created should be naive (meaning they have no associated timezone information).
However, if you are sure that now() for you is not UTC (aka Zulu time), you may need to do a conversion. This could be possible if, for example, you are using Django's timezone.now() and your configured timezone is something other than UTC. In this case, I might convert the result of now() to UTC, so you only have to convert one value. The pytz module can easily handle this kind of thing.

Check this :
import time
time = time.strftime('%Y-%m-%dT%H:%M:%SZ')
print(time)

Related

Python: UTC vs local timestamp

Why the followings return different timestamp? Is it because datetime.utcnow() doesn't have a timezone? It looks to me that tzinfo=utc is redudant, so I am probably not getting what is utcnow() and how an UTC number could not have a timezone. I guess there is a reason, so please enlight me :)
from datetime import datetime
from pytz import utc
local_seconds = int(datetime.utcnow().timestamp())
utc_seconds = int(datetime.utcnow().replace(tzinfo=utc).timestamp())
My goal is to get the UTC timestamp. It looks like the first method returns the local timestamp (correct me if I am wrong)
EDIT:
Where I live the timezone is GMT-5. In fact:
(utc_seconds-local_seconds)/3600 # is equal to -5.0
Following two statements would always return different result.
local_seconds = int(datetime.utcnow().timestamp())
utc_seconds = int(datetime.utcnow().replace(tzinfo=utc).timestamp())
Output:
1585584790
1585604590
You ask why? Because, by the time first statement executes, there is some time spent during execution and now the second statement would fetch you different result because datetime.utcnow() for 2nd statement has changed.
What I assume is, you want to see if both operations would give the same result or not? They definitely would have given the same results :
Had you provided them the same input?
Had you performed the similar operation from a common library.
To solve 1. change your code like this.
same_time_input = datetime.utcnow()
local_seconds = int(same_time_input.timestamp())
utc_seconds = int(same_time_input.replace(tzinfo=utc).timestamp())
Still the output would not be same, because you are using an external library, and the replace function is not working as you expected.
If you printout the tzinfo from same_time_input, you would see that it doesn't have any timezone info reason of which can be read here. --> Why does datetime.datetime.utcnow() not contain timezone information?
print(same_time_input.tzinfo)
Now, you are trying to give it a timezone info using a separate library which has different implementation internally resulting in slightly off results.

Is there a way to make datetime objects less precise?

I am trying to filter by time to delete messages from an sqlite3 database. I am taking a string that is accurate to the minute, converting that to a datetime object, and using the filter function on the QuerySet of messages. However, the entry in the database is far more precise than the string, so the message I'm looking for is being filtered out as well.
I'm automatically generating the datetime object using
class Message(models.Model):
....
time = models.DateTimeField(auto_now_add=True)
I'm filtering using
Message.objects.all().filter(time=time)
For example, the string I'm using to create the datetime object to filter is 'Dec. 5, 2015, 5:07 PM' to create a datetime object '2015-12-05 05:07:00'. However, the message I'm looking for is from time '2015-12-05 17:07:19.308321'
Is there an option to make the auto-generated datetime objects less precise, to the minute instead of fraction of a second?

How to get time from PostgreSQL in the same TZ, which was initialy used while uploading to DB, instead of UTC?

There are datetimes that are uploaded to DB by Django in the format “2015-10-31 17:00:00+03” (aware form of time).
PostgreSQL keeps time in UTC
In pgAdmin I see my datetimes as “2015-10-31 17:00:00+03” instead of UTC.
Is it possible to get from DB “2015-10-31 17:00:00+03” instead of “2015-10-31 14:00:00+00”? As I understand, if datetime is aware, there is TZ info in the value in DB, but how to take this TZ?
For example, I want to upload a new datetimes, but before to write to DB, I want to compare these datetimes with the values ​​that already exist in the database. So, a new part of datetimes have format:
“2015-10-31 17:00:00+03”, but the values ​​are taken from the database is UTC, ie
“2015-10-31 14:00:00+00”, when it should be also 17:00:00+03!
How to get value from DB not in UTC, but in TZ +3 (for this example)?
test = Shows.objects.get(name = 'Test')
test.date_time # I get UTC, but I need to get it as TZ +3
PS: hardcoding like this
from pytz import timezone
right_time = utc_from_db.astimezone(timezone('Europe/Berlin'))
is impossible. It is necessary to get exactly the time zone from the database that was originally filled with a value, because all datetimes ​​may have different time zone.
As I was informed, seems it is impossible. PostgreSQL uses TZ from awared type of time to convert time to UTC and can't be converted back natively.
If I want to get back UTC-ed time from DB to the time with TZ it was initially, I can make one more column in DB and put there the name of TZ, such as 'Europe/Berlin' and convert time from DB with UTC to initial time with proper TZ like this (the same code as in the question part):
from pytz import timezone
right_time = utc_from_db.astimezone(timezone('Europe/Berlin'))

Comparing a python date variable with timestamp from select query

I want to take some action based on comparing two dates. Date 1 is stored in a python variable. Date 2 is retrieved from the database in the select statement. For example I want to retrieve some records from the database where the associated date in the record (in form of the timestamp) is later than the date defined by the python variable. Preferably, I would like the comparison to be in readable date format rather than in timestamps.
I am a beginner with python.
----edit -----
Sorry for being ambiguous. Here's what I am trying to do:
import MySQLdb as mdb
from datetime import datetime
from datetime import date
import time
conn = mdb.connect('localhost','root','root','my_db')
cur = conn.cursor()
right_now = date.today()// python date
this is the part which I want to figure out
The database has a table which has timestamp. I want to compare that timestamp with this date and then retrieve records based on that comparison. For example I want to retrieve all records for which timestamp is above this date
cur.execute("SELECT created from node WHERE timestamp > right_now")
results = cur.fetchall()
for row in results:
print row
first of all, I guess Date 1 (python variable) is a datetime object. http://docs.python.org/2/library/datetime.html
As far as I have used it, MySQLdb gives you results in a (python) datetime object if the sql type was datetime.
So actually you have nothing to do, you can use python datetime comparison methods with date 1 and date 2.
I am a little bit confused about "comparison to be in readable date format rather than in timestamps". I mean the timestamps is readable enough, right?
If Date 1 is timestamps data, then you just simply do comparison. If not, then convert it to timestamps or convert the date in database to date type, both way works.
If you are asking how to write the code to do the comparison, you would use either '_mysql' or sqlalchemy to help you. The detailed syntax can be found at any where.
Anyway, the question itself is not clear enough, so the answer is blur, too.

timezone aware vs. timezone naive in python

I am working with datetime objects in python. I have a function that takes a time and finds the different between that time and now.
def function(past_time):
now = datetime.now()
diff = now - past_time
When I initialized past_time before passing it to this function I initialized it as datetime naive. And now is also a datetime naive object. However when I try to call this function I get the error: can't subtract offset-naive and offset-aware datetimes. How come this is the case if they are both theoretically datetime naive objects?
Any help would be appreciated. Thanks!
datetime doesn't do any cross time zone calculations, because it's a complex and involved subject.
I suggest converting dates to UTC universally and performing maths on those.
I recently completed a project using timezones in a large python/Django project and after investigation went with converting everything internally to UTC and converting only on display to the user.
You should look into pytz to do the conversions to/from UTC, and store Olson codes for the timezones you want in your app - perhaps associated with each user, or appropriate to your program.
Use :
now = now.replace(tzinfo=past_time.tzinfo)
before diff = now - past_time.
so that both now and past_time have same tzinfo.
only if now and past_time intended to be in same timezone.

Categories

Resources