1
00:00:00,180 --> 00:00:04,170
In the previous lesson, we saw how we could create a pixel.

2
00:00:04,560 --> 00:00:08,220
But one of the things that would be nice is instead of having to type this

3
00:00:08,220 --> 00:00:09,053
manually,

4
00:00:09,240 --> 00:00:14,240
I would really like to be able to create that using the date-time module.

5
00:00:15,090 --> 00:00:19,500
I can import my datetime, so from datetime module,

6
00:00:19,530 --> 00:00:22,860
I'm going to import the datetime class.

7
00:00:23,550 --> 00:00:28,550
And now I can use it to get today's date by tapping into datetime.now.

8
00:00:30,330 --> 00:00:31,950
But if I print this out,

9
00:00:32,040 --> 00:00:37,040
you can see that this is clearly not going to be in the format that I need it to

10
00:00:37,080 --> 00:00:37,770
be.

11
00:00:37,770 --> 00:00:42,770
So I'm gonna comment out my request and I'm going to take a look at what my data

12
00:00:42,900 --> 00:00:46,260
looks like. This contains the date and time,

13
00:00:46,560 --> 00:00:49,530
and it's in a different format.

14
00:00:50,460 --> 00:00:53,910
So whenever we're encounter a new API or a new service,

15
00:00:54,180 --> 00:00:58,980
they often want the date and time in a different format. For example, in Asia,

16
00:00:58,980 --> 00:01:02,430
the year tends to come first, in the US or in the UK,

17
00:01:02,550 --> 00:01:04,440
the year tend to come at the end.

18
00:01:05,250 --> 00:01:09,660
How can we format the Python date to any format we need? Well,

19
00:01:09,720 --> 00:01:14,720
there is a really useful method called the strftime method.

20
00:01:15,630 --> 00:01:20,630
And this allows us to pass in a string that will a format

21
00:01:21,180 --> 00:01:25,680
the date that we get back from datetime into any format we need.

22
00:01:26,250 --> 00:01:27,083
For example,

23
00:01:27,180 --> 00:01:32,180
if we pass in %a then we'll get the short version of the week-

24
00:01:32,400 --> 00:01:35,700
day. If we pass in a %b,

25
00:01:35,760 --> 00:01:40,050
then we'll get the full name of the month and so on and so forth.

26
00:01:40,230 --> 00:01:45,230
So we can use this table to actually create the exact date format we need.

27
00:01:46,380 --> 00:01:51,380
Let's go ahead and take this today and let's go ahead and use the strftime

28
00:01:54,690 --> 00:01:57,300
and we're going to pass in a string. Now,

29
00:01:57,300 --> 00:02:01,260
the first string we need is the year in the full version.

30
00:02:01,290 --> 00:02:04,710
So this is going to be %Y and it's a capital Y.

31
00:02:06,120 --> 00:02:09,449
Now, if you need a dash, then you can add a dash. If you need a space,

32
00:02:09,479 --> 00:02:12,870
you can add a space. But in my case, I actually wanted it all together.

33
00:02:13,230 --> 00:02:16,140
So the next thing I want is the month as a number.

34
00:02:16,170 --> 00:02:18,360
So that's %m

35
00:02:19,980 --> 00:02:23,280
and then it's %d to get the day of the month.

36
00:02:25,980 --> 00:02:28,500
Now, if I actually print this out,

37
00:02:31,770 --> 00:02:35,760
you can see that this is formatted in the exact format I need.

38
00:02:35,850 --> 00:02:39,180
And if I wanna add in a, I dunno,

39
00:02:39,240 --> 00:02:41,940
asterix there then it'll format it like that.

40
00:02:42,240 --> 00:02:47,010
If I want to change the date to a word like Wednesday or December,

41
00:02:47,040 --> 00:02:49,200
then I can simply change that format string.

42
00:02:50,760 --> 00:02:54,900
Now that we've managed to do this, then we can get our date

43
00:02:54,900 --> 00:02:59,320
time programmatically using the datetime.now method.

44
00:02:59,860 --> 00:03:03,280
We can also use it to create any datetime of our choosing.

45
00:03:03,490 --> 00:03:07,720
So lets say I wanted a backtrack to create a pixel for yesterday.

46
00:03:08,080 --> 00:03:12,730
Well then I can simply create my datetime by specifying the year,

47
00:03:13,240 --> 00:03:17,320
the month and the day.

48
00:03:17,350 --> 00:03:18,790
So today's 24th.

49
00:03:18,820 --> 00:03:23,820
So yesterday was 23rd and then it's going to take that date and it's going to

50
00:03:23,890 --> 00:03:28,000
format it into the correct format and then post a quantity.

51
00:03:28,240 --> 00:03:30,700
So let's say that yesterday I cycled,

52
00:03:30,940 --> 00:03:34,630
I did cycle a bit more actually, let's say it was 15 kilometers.

53
00:03:35,320 --> 00:03:39,040
Now if I go ahead and uncomment this and run it again,

54
00:03:39,070 --> 00:03:43,270
it's gonna post the latest piece of data to pixela and

55
00:03:43,750 --> 00:03:45,460
if we now update our graph,

56
00:03:45,580 --> 00:03:50,580
you can see I've now got two pixels and this one is a lot heavier in terms of

57
00:03:51,730 --> 00:03:55,960
color to show that I cycled a lot further than today.

58
00:03:57,070 --> 00:04:01,150
And we've now got a total kilometer, a max, a min,

59
00:04:01,150 --> 00:04:03,610
an average and the total number of pixels.

60
00:04:04,660 --> 00:04:08,530
The last thing I want to show you before we finish off with this project is how

61
00:04:08,530 --> 00:04:13,530
we can update and delete pixels using the put and delete requests.

62
00:04:15,430 --> 00:04:17,290
That's what we're going to do in the next lesson.

