In the previous lesson, we saw how we could create a pixel.
But one of the things that would be nice is instead of having to type this
manually,
I would really like to be able to create that using the date-time module.
I can import my datetime, so from datetime module,
I'm going to import the datetime class.
And now I can use it to get today's date by tapping into datetime.now.
But if I print this out,
you can see that this is clearly not going to be in the format that I need it to
be.
So I'm gonna comment out my request and I'm going to take a look at what my data
looks like. This contains the date and time,
and it's in a different format.
So whenever we're encounter a new API or a new service,
they often want the date and time in a different format. For example, in Asia,
the year tends to come first, in the US or in the UK,
the year tend to come at the end.
How can we format the Python date to any format we need? Well,
there is a really useful method called the strftime method.
And this allows us to pass in a string that will a format
the date that we get back from datetime into any format we need.
For example,
if we pass in %a then we'll get the short version of the week-
day. If we pass in a %b,
then we'll get the full name of the month and so on and so forth.
So we can use this table to actually create the exact date format we need.
Let's go ahead and take this today and let's go ahead and use the strftime
and we're going to pass in a string. Now,
the first string we need is the year in the full version.
So this is going to be %Y and it's a capital Y.
Now, if you need a dash, then you can add a dash. If you need a space,
you can add a space. But in my case, I actually wanted it all together.
So the next thing I want is the month as a number.
So that's %m
and then it's %d to get the day of the month.
Now, if I actually print this out,
you can see that this is formatted in the exact format I need.
And if I wanna add in a, I dunno,
asterix there then it'll format it like that.
If I want to change the date to a word like Wednesday or December,
then I can simply change that format string.
Now that we've managed to do this, then we can get our date
time programmatically using the datetime.now method.
We can also use it to create any datetime of our choosing.
So lets say I wanted a backtrack to create a pixel for yesterday.
Well then I can simply create my datetime by specifying the year,
the month and the day.
So today's 24th.
So yesterday was 23rd and then it's going to take that date and it's going to
format it into the correct format and then post a quantity.
So let's say that yesterday I cycled,
I did cycle a bit more actually, let's say it was 15 kilometers.
Now if I go ahead and uncomment this and run it again,
it's gonna post the latest piece of data to pixela and
if we now update our graph,
you can see I've now got two pixels and this one is a lot heavier in terms of
color to show that I cycled a lot further than today.
And we've now got a total kilometer, a max, a min,
an average and the total number of pixels.
The last thing I want to show you before we finish off with this project is how
we can update and delete pixels using the put and delete requests.
That's what we're going to do in the next lesson.