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.
Related
I'm learning Pandas and I have a problem trying to change a format from Object to Date_time.
When I use 'to_datetime' the date I get in return is like in ISO Format, and I just want DD/MM/YYYY (13/10/1960). And I doing something wrong? Thanks a lot!!
enter image description here
At a glance, it doesn't seem like the code uses the right format.
The to_datetime() with the format argument follows strftime standards per the documentation here, and it's a way to tell the method how the original time was represented (so it can properly format it into a datetime object). An example can be seen from this Stack Overflow question.
Simple example:
datetime_object = pd.to_datetime(format='%d/%m/%Y')
The next problem is how you want that datetime object to be printed out (i.e. DD/MM/YYYY). Just throwing thoughts out there (would comment, but I don't have those privileges yet), if you want to print the string, you can cast that datetime object into the string that you want. Many ways to do this, one of which is to use strftime().
Simple example:
date_as_string = datetime_object.strftime('%d/%m/%Y')
But of course, why would you use a datetime object in that case. So the other option I can think of is to override how the datetime object is printed (redefining __str__ in a new class of datetime).
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?
According to this, it's not possible to save datetime.date instances in MongoDB using Python and pymongo. It says in the FAQ that it's an unsupported type and to use datetime.datetime instead.
However, it's listed (as #9) in the BSON data types page here, so is this just out-of-date, or is there a reason I can't use python this data type?
From mongo docs: http://docs.mongodb.org/manual/reference/bson-types/#document-bson-type-date
The official BSON specification refers to the BSON Date type as the UTC datetime.
So, there is while it is written as "Date", it's still a datetime in BSON. If you want just date, you can set the hours/minutes/seconds/ms to 0.
Also, as specified on the docs about timestamp:
*NOTE
The BSON timestamp type is for internal MongoDB use. For most cases, in application development, you will want to use the BSON date type. See Date for more information.*
The difference between datetime and timestamp is: datetime is an abstraction of date (day, month, year) and time (hour, min, second), and timestamp is the number of seconds since epoch.
I have a SQLite DB which has few columns of type datetime.
These column store time (just the time and not the date) in HH:mm:ss format.
When I try to access this field it is returned as null.
In models.py this field have been mapped to equivalent models.DateTimeField.
What is the correct way of accessing such fields?
Is it required that models.DateTimeField HAS TO BE stored in YYYY-MM-DD HH:mm:ss format insqlite?
I am not suprised that DateTimeField expects both a date and a time.
Try TimeField instead.
(Please note that SQLite does not have a native date/time type; it's just a string.)
How to set default datetime format in python because i have multiple tuples to send via template on client side. This is not good approach to set each object's value to specified format. I want to set a datetime format on server side and these converted values will be shown to client. I tried
datetime.strftime("%Y-%m-%d %X")
but it is giving error.
strftime is a method of datetime objects - it doesn't set a default representation, which seems to be what you suggest. For example, you might call it like this:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%Y-%m-%d %X")
'2011-03-17 10:14:12'
If you need to do this a lot, it would be worth creating a method that wraps this conversion of a datetime to a string. The documentation for the datetime module can be found here.
I'm not sure I understand your issue, but this might help
http://docs.djangoproject.com/en/dev/ref/settings/
there is a datetime format section, this sets datetime format globally.