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?
Related
I have a list of datetime object (let say datetime_obj) Now I want only its time part .
datetime_obj[0] = 22-Jun-23 13:32:00
I'm trying this :
time = [datetime.datetime.strptime(str(i)[11:], '%H:%M:%S') for I in datetime_obj]
But when I am printing time it showing 01-Jan-1900 13:32:00
Basically it is attaching a random date. what should I do?
You can directly call the ".strftime("%H:%M:%S")" method on your datetime object. This way you can avoid the list comprehension method you were using.
For example, the one-liner
print(datetime.datetime.now().strftime("%H:%M:%S"))
gives you the actual time printed in the format that you need from the datetime object datetime_obj=datetime.datetime.now()
If you need the values instead, you could access to the .hour, .minute and .second properties of datetime_obj.
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)
I currently am trying to remove milliseconds from a datetime object in my aggregation of a pymongo query.
I'm grouping by one of their IDs, which they all have in common. I also need the date though.
The current date won't let me group correctly because some objects are 1 millisecond different, so it groups with most of the objects in the grouping, but maybe one of the objects gets left out because of the millisecond time difference.
Edit: I've converted it to a string and it seems to have worked. How do I convert it to a date again within the aggregation. $dateFromString is not working
Is there a way that I can remove the milliseconds so the correct dates all correspond to the same ID?
Question regarding the use of TimestampField in PeeWee using Python 3.6. My model contains the following attribute:
timestamp= peewee.TimestampField(utc=True)
I store a timestamp as follows:
obj = TestObject(timestamp = Decimal(UnixtimestampString))
obj.save()
print(type(TestObject.get().timestamp))
Output:
Type: <class 'datetime.datetime'>
Is this normal behavior? I would like to retrieve the timestamp as Decimal (the way I put it in). Obviously I can convert it, but I was wondering why one would use TimestampField instead of DateTimeField when both return a DateTime object.
Is the only difference, the way the data is entered? (As a DateTime object in case of DateTimeField vs unix timestamp in Integer/Decimal for the TimestampField.)
The TimestampField exists to store datetimes, expressed as timestamps, as an integer...but the Python representation is a datetime object.
If you want to work with integer (or float) timestamps, then there's no reason to use TimestampField, just use an IntegerField or FloatField.
I'm using Peewee to access a SQLite DB. How can I save a timestamp in the following format?
'%Y-%m-%d %H:%M:%S' # year-month-day hour-minute-second
(basically: just crop out the microseconds which are present by default)
EDIT: I was able to make this work, using the following in the class definition:
created = DateTimeField(default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S))
However, I'm still intersted to know if there's a "built in" way of doing this with Peewee, using the 'formats' parameter.
That actually won't work since the timestamp is evaluated at import time (or the time it's declared). Peewee actually doesn't care about the format going into the database, since it'll just take the python datetime object and let pysqlite convert it.
If you wanted, the best way would be to subclass DateTimeField and override the db_value method.