Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Fine-tuning your REST service
Lecture: Handling datetime values
0:01 One question that often comes up of Stack Overflow is how do you store datetime values, and how do you query for them.
0:09 Well, the thing is, Json comes with no date types, so usually Rest services fall back to parsing matching strings
0:17 and then store them as datetime values. In order for that to work properly however,
0:23 client server need to agree on a standard string format for datetime fields. By default, Eve accepts datetime fields as rfc1123 strings,
0:34 this format is the de facto standard in Rest and more in general, on the internet. To give you an idea of what an rfc1123 date looks like,
0:46 let's just take a look at the values for the updated and created metafields in any Eve document, we have a string of course,
0:55 the first value within the string is the actual day, 3 letters, comma and then the full date, then the hours, minutes and seconds for the time,
1:06 and finally the timezone, very simple, staightforward, easy to read. So let's try to perform a query on a date field,
1:14 here for example, I am asking for all the people which have been updated at a date greater than or equal to Tue, 20 Feb.
1:25 So let's send this query in and we get a number of documents back, 6 total. If we go back and update and change the date to 21 Feb,
1:37 we should get back a couple of documents right, 2 documents total. So as you can see, the date we used for our queries
1:45 is matching in format the date we have on the documents, and this is the rfc1123 format. But what if you need to have
1:56 some different kind of string format in your documents? Well, we can do that very easily,
2:02 we just need to go back to our code editor and type in something like this, date format is the setting which allows you
2:08 to change the default format for datetime sting values, here we are saying to Eve I don't want to use rfc1123, I want to use this format instead.
2:19 If you are curious about this string in here, you should know that this is the standard Python notation for datetime stings,
2:28 we are using what is called the strf time format so you can go here on the Python website and look at the documentation
2:39 where you get a very nice table with all the meanings, all the values, strings you can use or directive as they are code to the format of your string.
2:50 So back to the editor we go and I am basically saying I only want day month and year in my date format. Now if I go back to Postman,
3:02 let me first save, and relaunch the server as usual, now if we perform the same query here with the old format we should get nothing back exactly,
3:16 because there you have no matches for this string format but if we say that we only want to use the date with this format here
3:27 which should match the one we defined, we should get back on track exactly, so two documents and two fields. Let's try with 20, 7 documents, yes,
3:39 that makes sense because we are losing precision here with the same query we were getting 6 documents before
3:45 but now, we aren't providing any time information, so we're getting all the documents for the 20 Feb and one more document.
3:55 Keep in mind that the date format settings has an epath not only on the queries, but also on the Json payloads,
4:04 so when you change the format, you also need to change your payloads, for example, here I am still trying to write a document
4:11 with the rfc1123 standard format, if I try to send a request to my service, I get an error because born is not of datetime type,
4:21 it actually is, but it is not conforming to our new standard, so what we need to do here, of course, is go and update this field
4:32 and try to post again, and it will work.