WEBVTT

0
00:00.180 --> 00:04.170
In the previous lesson, we saw how we could create a pixel.

1
00:04.560 --> 00:08.220
But one of the things that would be nice is instead of having to type this

2
00:08.220 --> 00:09.053
manually,

3
00:09.240 --> 00:14.240
I would really like to be able to create that using the date-time module.

4
00:15.090 --> 00:19.500
I can import my datetime, so from datetime module,

5
00:19.530 --> 00:22.860
I'm going to import the datetime class.

6
00:23.550 --> 00:28.550
And now I can use it to get today's date by tapping into datetime.now.

7
00:30.330 --> 00:31.950
But if I print this out,

8
00:32.040 --> 00:37.040
you can see that this is clearly not going to be in the format that I need it to

9
00:37.080 --> 00:37.770
be.

10
00:37.770 --> 00:42.770
So I'm gonna comment out my request and I'm going to take a look at what my data

11
00:42.900 --> 00:46.260
looks like. This contains the date and time,

12
00:46.560 --> 00:49.530
and it's in a different format.

13
00:50.460 --> 00:53.910
So whenever we're encounter a new API or a new service,

14
00:54.180 --> 00:58.980
they often want the date and time in a different format. For example, in Asia,

15
00:58.980 --> 01:02.430
the year tends to come first, in the US or in the UK,

16
01:02.550 --> 01:04.440
the year tend to come at the end.

17
01:05.250 --> 01:09.660
How can we format the Python date to any format we need? Well,

18
01:09.720 --> 01:14.720
there is a really useful method called the strftime method.

19
01:15.630 --> 01:20.630
And this allows us to pass in a string that will a format

20
01:21.180 --> 01:25.680
the date that we get back from datetime into any format we need.

21
01:26.250 --> 01:27.083
For example,

22
01:27.180 --> 01:32.180
if we pass in %a then we'll get the short version of the week-

23
01:32.400 --> 01:35.700
day. If we pass in a %b,

24
01:35.760 --> 01:40.050
then we'll get the full name of the month and so on and so forth.

25
01:40.230 --> 01:45.230
So we can use this table to actually create the exact date format we need.

26
01:46.380 --> 01:51.380
Let's go ahead and take this today and let's go ahead and use the strftime

27
01:54.690 --> 01:57.300
and we're going to pass in a string. Now,

28
01:57.300 --> 02:01.260
the first string we need is the year in the full version.

29
02:01.290 --> 02:04.710
So this is going to be %Y and it's a capital Y.

30
02:06.120 --> 02:09.450
Now, if you need a dash, then you can add a dash. If you need a space,

31
02:09.480 --> 02:12.870
you can add a space. But in my case, I actually wanted it all together.

32
02:13.230 --> 02:16.140
So the next thing I want is the month as a number.

33
02:16.170 --> 02:18.360
So that's %m

34
02:19.980 --> 02:23.280
and then it's %d to get the day of the month.

35
02:25.980 --> 02:28.500
Now, if I actually print this out,

36
02:31.770 --> 02:35.760
you can see that this is formatted in the exact format I need.

37
02:35.850 --> 02:39.180
And if I wanna add in a, I dunno,

38
02:39.240 --> 02:41.940
asterix there then it'll format it like that.

39
02:42.240 --> 02:47.010
If I want to change the date to a word like Wednesday or December,

40
02:47.040 --> 02:49.200
then I can simply change that format string.

41
02:50.760 --> 02:54.900
Now that we've managed to do this, then we can get our date

42
02:54.900 --> 02:59.320
time programmatically using the datetime.now method.

43
02:59.860 --> 03:03.280
We can also use it to create any datetime of our choosing.

44
03:03.490 --> 03:07.720
So lets say I wanted a backtrack to create a pixel for yesterday.

45
03:08.080 --> 03:12.730
Well then I can simply create my datetime by specifying the year,

46
03:13.240 --> 03:17.320
the month and the day.

47
03:17.350 --> 03:18.790
So today's 24th.

48
03:18.820 --> 03:23.820
So yesterday was 23rd and then it's going to take that date and it's going to

49
03:23.890 --> 03:28.000
format it into the correct format and then post a quantity.

50
03:28.240 --> 03:30.700
So let's say that yesterday I cycled,

51
03:30.940 --> 03:34.630
I did cycle a bit more actually, let's say it was 15 kilometers.

52
03:35.320 --> 03:39.040
Now if I go ahead and uncomment this and run it again,

53
03:39.070 --> 03:43.270
it's gonna post the latest piece of data to pixela and

54
03:43.750 --> 03:45.460
if we now update our graph,

55
03:45.580 --> 03:50.580
you can see I've now got two pixels and this one is a lot heavier in terms of

56
03:51.730 --> 03:55.960
color to show that I cycled a lot further than today.

57
03:57.070 --> 04:01.150
And we've now got a total kilometer, a max, a min,

58
04:01.150 --> 04:03.610
an average and the total number of pixels.

59
04:04.660 --> 04:08.530
The last thing I want to show you before we finish off with this project is how

60
04:08.530 --> 04:13.530
we can update and delete pixels using the put and delete requests.

61
04:15.430 --> 04:17.290
That's what we're going to do in the next lesson.