1
00:00:00,660 --> 00:00:08,370
In the previous lecture, you learned how to manually set the date and time in the hospital to integrate

2
00:00:08,400 --> 00:00:09,540
real time clock.

3
00:00:10,790 --> 00:00:17,660
Obviously, the problem there is that you have to do that manually every time that you're using the

4
00:00:17,690 --> 00:00:24,230
right to regains power because power is necessary for the ITC to retain time and date.

5
00:00:24,770 --> 00:00:32,300
So what we do in this lecture is to learn how to use an Internet clock service which uses an atomic

6
00:00:32,300 --> 00:00:42,770
clock to provide date and time information programmatically to devices like the HB 32, your computer,

7
00:00:42,770 --> 00:00:43,310
etc..

8
00:00:43,910 --> 00:00:51,380
To do that will be using the Wi-Fi capability that you've learnt about the previous lectures, as well

9
00:00:51,380 --> 00:00:54,280
as the A.P. Time Library.

10
00:00:54,680 --> 00:01:01,010
That makes it very easy to automatically acquire date and time information from a compatible A.P. server.

11
00:01:01,790 --> 00:01:04,700
So I just want to show you a couple of things before we begin.

12
00:01:05,030 --> 00:01:07,840
First, where are those servers?

13
00:01:07,850 --> 00:01:09,640
What other locations, how to find them?

14
00:01:10,180 --> 00:01:15,470
So if you're interested, you can go to this website, NTPC Port Authority.

15
00:01:15,890 --> 00:01:23,070
And depending on where you are in the world, you can find a centerpiece server that is nearby.

16
00:01:23,420 --> 00:01:30,380
You don't actually have to do that because the A.P. Time Library will find the default time server and

17
00:01:30,380 --> 00:01:31,460
use it to sync.

18
00:01:32,000 --> 00:01:38,360
You don't have to tell it which one to use, but in my case, I thought I would use one that is near

19
00:01:38,360 --> 00:01:39,150
by me.

20
00:01:39,590 --> 00:01:45,580
So in Oceania, for example, there are these servers that you can try out and test.

21
00:01:45,590 --> 00:01:51,950
In Australia in particular, you can drill down and find service that are nearby.

22
00:01:51,960 --> 00:02:00,140
So, for example, I can go for this one here, copy it and set it in my script right here, pasted

23
00:02:00,140 --> 00:02:00,660
in here.

24
00:02:00,920 --> 00:02:02,750
Again, this is optional.

25
00:02:02,750 --> 00:02:03,830
You don't have to do that.

26
00:02:04,130 --> 00:02:08,720
But I know that many of you are curious and you want to optimize your scripts, and that's one way to

27
00:02:08,720 --> 00:02:09,050
do it.

28
00:02:09,650 --> 00:02:19,040
Now, no matter which A.P. server you choose, it will report the time to the library in UTC.

29
00:02:19,460 --> 00:02:26,600
So the time will come back to you is always coordinated, universal time and not the time at your local

30
00:02:26,600 --> 00:02:27,260
time zone.

31
00:02:27,800 --> 00:02:32,690
If you want to be able to see the time at your local time zone, you will need to convert.

32
00:02:32,790 --> 00:02:34,540
And I'm also going to show you how to do that.

33
00:02:35,090 --> 00:02:37,640
So I am that Insigne.

34
00:02:37,640 --> 00:02:45,200
The time right now is nine, six in the morning in UTC is 10, 16 in the evening.

35
00:02:45,470 --> 00:02:52,220
And that's the time that the A.P. server will report to the A.P. Time Library.

36
00:02:53,270 --> 00:02:57,650
Speaking of the A.P. time library, here is the documentation.

37
00:02:57,830 --> 00:03:06,590
I'm now looking at the micro python read the Docs website, which contains the documentation for this

38
00:03:06,590 --> 00:03:11,090
particular library, and it shows you how to set it and how to actually use it.

39
00:03:11,280 --> 00:03:12,470
So here's an example.

40
00:03:12,980 --> 00:03:19,880
I built my example from this connected to Wi-Fi, which is necessary for to be time to work.

41
00:03:19,880 --> 00:03:26,990
And then you just call the synchronization Showtime dysfunction right here and you will automatically

42
00:03:26,990 --> 00:03:33,380
go out to a default entity server corrupted time and then use it to do the synchronization.

43
00:03:34,400 --> 00:03:37,070
Let's continue with the script itself and see how it all works.

44
00:03:37,400 --> 00:03:45,020
I've got detailed documentation and links to all of the resources here so you can go and do a bit of

45
00:03:45,650 --> 00:03:49,820
background reading and understand how everything is connected.

46
00:03:50,150 --> 00:03:54,730
But I think this script will be self-explanatory in a moment.

47
00:03:55,960 --> 00:04:02,440
First of all, we need to connect to the Internet so all of this code should be familiar to you if you

48
00:04:02,440 --> 00:04:05,680
have done the Wi-Fi experiment earlier in this course.

49
00:04:05,690 --> 00:04:12,490
And just as a reference, you can have a look at the Wi-Fi, get the profile where I'm actually doing

50
00:04:12,490 --> 00:04:14,300
the exact same thing right here.

51
00:04:15,160 --> 00:04:23,680
So by the time we finish with the connect function, we have a local Wi-Fi network connection and then

52
00:04:23,680 --> 00:04:28,270
we can continue with the time and date clock synchronization.

53
00:04:28,840 --> 00:04:38,440
As I said earlier, in setting the A.P. host to be a local server nearby here in Australia, then I'm

54
00:04:38,440 --> 00:04:45,610
creating the RTC object like we did in the previous lecture, because eventually I want to set the hardware

55
00:04:45,610 --> 00:04:53,710
clock with the actual time and date check to see that we have a connection to Wi-Fi.

56
00:04:53,920 --> 00:04:59,580
And the idea is to print out the local time as it is right now before synchronization.

57
00:04:59,590 --> 00:05:03,690
And of course, this is going to be an incorrect time, especially for the mentally ill.

58
00:05:03,700 --> 00:05:05,350
And then I have never seen it before.

59
00:05:06,040 --> 00:05:12,130
Once I do that just for comparison, I'm going to go and set the time for the set time method on the

60
00:05:12,640 --> 00:05:14,050
time object.

61
00:05:14,050 --> 00:05:15,280
And that is going to go out.

62
00:05:15,520 --> 00:05:19,980
It's going to use this server, in my case, sync the clock to UTC.

63
00:05:20,440 --> 00:05:29,140
So then what is happening is that the time object is going to have a new time set, which is going to

64
00:05:29,140 --> 00:05:34,150
be equal to whatever currently is happening at UTC.

65
00:05:34,150 --> 00:05:36,520
In terms of time, it's going to be this time here.

66
00:05:37,430 --> 00:05:45,770
If something goes wrong and any time fails to sink, then I'm going to have a exception and print out

67
00:05:46,310 --> 00:05:48,620
the error message and the program is going to end.

68
00:05:50,230 --> 00:05:50,710
All right.

69
00:05:51,830 --> 00:05:56,960
Next, I'm going to do a little bit of work because remember, I don't just want to have a date and

70
00:05:56,960 --> 00:06:00,020
time in UTC, but I want it in my local time zone.

71
00:06:00,590 --> 00:06:09,590
So I'm going to take the local time as it is in UTC and stored in the time object.

72
00:06:09,960 --> 00:06:12,670
I'm going to start with this variable and marketed as UTC.

73
00:06:13,250 --> 00:06:22,910
Then I'm going to use the make time method for you time to create a new object which is going to be

74
00:06:22,910 --> 00:06:30,710
local still at this point in time, because I've equated these to current time date logo is going to

75
00:06:30,710 --> 00:06:33,440
be equal to game time that UTC.

76
00:06:33,740 --> 00:06:41,420
But then I'm going to do this operation here and this is what actually shifts the UTC time to be local

77
00:06:41,420 --> 00:06:41,750
time.

78
00:06:41,780 --> 00:06:48,360
So at the moment in Australia, in Sydney in particular, we are 11 hours ahead of UTC.

79
00:06:48,620 --> 00:06:55,180
So what I'm doing is I'm taking 11 times 3600 seconds for each hour.

80
00:06:55,550 --> 00:07:04,460
So I've got this many seconds ahead of UTC and I'm going to add this time to the current local time

81
00:07:04,850 --> 00:07:08,090
and then save the result on the current variable.

82
00:07:08,540 --> 00:07:17,400
So eventually current time that local is going to have the accurate time and date in my local time zone.

83
00:07:17,780 --> 00:07:20,510
So then I'm going to just print this out down here.

84
00:07:20,510 --> 00:07:27,770
I'm going to use new time, local time, pass the current time date local, and that will show me the

85
00:07:27,770 --> 00:07:29,420
time at my location.

86
00:07:30,050 --> 00:07:35,150
So all good so far could actually stop here and have a local time to work with.

87
00:07:35,150 --> 00:07:46,760
But what I haven't done yet is to reset the RTC with my new local time as acquired from the atomic Internet

88
00:07:46,760 --> 00:07:47,250
clock.

89
00:07:48,200 --> 00:07:50,990
So remember that these are two different things right now.

90
00:07:51,890 --> 00:07:55,470
Time is stored in the time object, not in the RTC.

91
00:07:55,640 --> 00:07:55,980
Right.

92
00:07:56,020 --> 00:08:02,510
So I'm going to use the same method to set the RTC as in the previous example like this.

93
00:08:04,070 --> 00:08:11,120
I've got now two options and then you can decide which one is appropriate for you, if you want to set

94
00:08:11,120 --> 00:08:19,310
the ABC with the UTC time, then you will take apart the current time date.

95
00:08:19,310 --> 00:08:27,890
UTC Tuval refer to each of its components individually and use this information to set RTC to the UTC

96
00:08:27,890 --> 00:08:28,280
time.

97
00:08:28,670 --> 00:08:37,460
But if you want the RTC to be sent to your local time zone time, then instead of current time date

98
00:08:37,460 --> 00:08:47,930
UTC, you use current time date local and I'm using local time to convert that to convert the couple

99
00:08:47,930 --> 00:08:55,670
in this variable into a new time object so then I can use it to reset the RTC.

100
00:08:56,120 --> 00:08:59,210
So exactly the same method is just the source of the time.

101
00:08:59,210 --> 00:09:05,930
Differs depending on whether you want UTC or local time zone time ongoing in this example, with local

102
00:09:05,930 --> 00:09:14,150
time, certain time, eventually the time is in DC and I can use the same method as in the previous

103
00:09:14,150 --> 00:09:15,590
example right here.

104
00:09:17,120 --> 00:09:27,130
To extract it and print it out into the shell, it could be a screen in your case, for example, you

105
00:09:27,200 --> 00:09:35,630
may want to create a clock that uses an oily display to show the time to date anyway.

106
00:09:35,640 --> 00:09:43,850
So here I've got a bunch of print statements displaying both UTC time and.

107
00:09:45,410 --> 00:09:53,420
Local time, so she'd probably say nutritiously time here to be more correct because both are coming.

108
00:09:56,610 --> 00:10:03,980
All right, so this is UTC and this is local time, and then finally down here, just every one second

109
00:10:03,990 --> 00:10:10,890
I will print out the date and time directly from the real time clock, which are said to be my local

110
00:10:10,890 --> 00:10:11,250
time.

111
00:10:12,690 --> 00:10:13,160
All right.

112
00:10:13,170 --> 00:10:16,130
So it was a very long explanation.

113
00:10:16,140 --> 00:10:19,020
So there's a lot happening, this script.

114
00:10:19,030 --> 00:10:25,890
So I invite you to take a little bit of time to go through this line by line, make sure that you understand

115
00:10:25,890 --> 00:10:31,410
how it works, but let's run it and make sure that it actually does work.

116
00:10:31,440 --> 00:10:33,330
So run the current script.

117
00:10:34,570 --> 00:10:36,630
You know what's happening?

118
00:10:36,690 --> 00:10:41,920
I think we need to restart the backend.

119
00:10:43,990 --> 00:10:46,150
All right, and Chris Laken.

120
00:10:50,240 --> 00:10:52,010
Something's happening in line 50.

121
00:10:56,520 --> 00:11:03,120
OK, so what's happening is that I have not stored the wi fi settings and the Scott test and Jason file

122
00:11:03,600 --> 00:11:10,290
into onto the data file system, so let's do that right now.

123
00:11:11,030 --> 00:11:12,660
So this file here.

124
00:11:14,730 --> 00:11:16,430
I'm just going to upload it.

125
00:11:18,020 --> 00:11:21,020
And now it should work.

126
00:11:27,780 --> 00:11:31,080
Can connect with these credentials, just double check the credentials.

127
00:11:31,120 --> 00:11:31,560
Correct?

128
00:11:32,160 --> 00:11:33,390
Yes, actually, that is correct.

129
00:11:33,630 --> 00:11:34,110
Try again.

130
00:11:35,580 --> 00:11:36,630
Go this time, it worked.

131
00:11:38,350 --> 00:11:47,980
So the time right now I'm going to bring up the Internet, time from time is.

132
00:11:51,570 --> 00:11:52,630
So we can compare.

133
00:11:53,640 --> 00:11:58,700
So we've got the current time nine twenty seven, as you can see in Sydney right now.

134
00:11:58,820 --> 00:11:59,820
Nine twenty seven.

135
00:12:01,090 --> 00:12:01,840
And.

136
00:12:10,240 --> 00:12:17,200
I'm going to click on Starp because I want to go a little bit earlier and see the Ukichi time.

137
00:12:19,100 --> 00:12:30,320
And there's a UTC time as it came from the A.P. several times, 22, 27, 35, some back then when it

138
00:12:30,320 --> 00:12:35,840
was acquired, it was it's noted right here, I've got an error here with a local time that should be

139
00:12:35,840 --> 00:12:37,240
UTC time instead of local.

140
00:12:37,730 --> 00:12:41,570
And you can see that is twenty to twenty six.

141
00:12:41,910 --> 00:12:47,030
There's a couple of minutes ago, sir, that was the raw date.

142
00:12:47,540 --> 00:12:51,700
UTC Raw Data actually was acquired from the A.P. server.

143
00:12:52,410 --> 00:12:53,510
I want to make a little fix here.

144
00:12:53,510 --> 00:13:04,640
So this is your time before synchronization and this is the time of UTC time after synchronization,

145
00:13:04,640 --> 00:13:14,670
because once 976 runs, then ATP Time synchronises you time with UTC, this time right here.

146
00:13:16,790 --> 00:13:24,320
OK, so this was a bit a bit convoluted, but it has to take a few minutes to walk through this code

147
00:13:24,320 --> 00:13:25,610
to understand how it works.

148
00:13:26,090 --> 00:13:37,490
And this gives you a relatively straightforward way to sink your ISP, reduce real time clock with an

149
00:13:37,490 --> 00:13:44,240
Internet atomic clock and from there to convert that UTC time to your local time zone.
