In the previous lecture, you learned how to manually set the date and time in the hospital to integrate
real time clock.
Obviously, the problem there is that you have to do that manually every time that you're using the
right to regains power because power is necessary for the ITC to retain time and date.
So what we do in this lecture is to learn how to use an Internet clock service which uses an atomic
clock to provide date and time information programmatically to devices like the HB 32, your computer,
etc..
To do that will be using the Wi-Fi capability that you've learnt about the previous lectures, as well
as the A.P. Time Library.
That makes it very easy to automatically acquire date and time information from a compatible A.P. server.
So I just want to show you a couple of things before we begin.
First, where are those servers?
What other locations, how to find them?
So if you're interested, you can go to this website, NTPC Port Authority.
And depending on where you are in the world, you can find a centerpiece server that is nearby.
You don't actually have to do that because the A.P. Time Library will find the default time server and
use it to sync.
You don't have to tell it which one to use, but in my case, I thought I would use one that is near
by me.
So in Oceania, for example, there are these servers that you can try out and test.
In Australia in particular, you can drill down and find service that are nearby.
So, for example, I can go for this one here, copy it and set it in my script right here, pasted
in here.
Again, this is optional.
You don't have to do that.
But I know that many of you are curious and you want to optimize your scripts, and that's one way to
do it.
Now, no matter which A.P. server you choose, it will report the time to the library in UTC.
So the time will come back to you is always coordinated, universal time and not the time at your local
time zone.
If you want to be able to see the time at your local time zone, you will need to convert.
And I'm also going to show you how to do that.
So I am that Insigne.
The time right now is nine, six in the morning in UTC is 10, 16 in the evening.
And that's the time that the A.P. server will report to the A.P. Time Library.
Speaking of the A.P. time library, here is the documentation.
I'm now looking at the micro python read the Docs website, which contains the documentation for this
particular library, and it shows you how to set it and how to actually use it.
So here's an example.
I built my example from this connected to Wi-Fi, which is necessary for to be time to work.
And then you just call the synchronization Showtime dysfunction right here and you will automatically
go out to a default entity server corrupted time and then use it to do the synchronization.
Let's continue with the script itself and see how it all works.
I've got detailed documentation and links to all of the resources here so you can go and do a bit of
background reading and understand how everything is connected.
But I think this script will be self-explanatory in a moment.
First of all, we need to connect to the Internet so all of this code should be familiar to you if you
have done the Wi-Fi experiment earlier in this course.
And just as a reference, you can have a look at the Wi-Fi, get the profile where I'm actually doing
the exact same thing right here.
So by the time we finish with the connect function, we have a local Wi-Fi network connection and then
we can continue with the time and date clock synchronization.
As I said earlier, in setting the A.P. host to be a local server nearby here in Australia, then I'm
creating the RTC object like we did in the previous lecture, because eventually I want to set the hardware
clock with the actual time and date check to see that we have a connection to Wi-Fi.
And the idea is to print out the local time as it is right now before synchronization.
And of course, this is going to be an incorrect time, especially for the mentally ill.
And then I have never seen it before.
Once I do that just for comparison, I'm going to go and set the time for the set time method on the
time object.
And that is going to go out.
It's going to use this server, in my case, sync the clock to UTC.
So then what is happening is that the time object is going to have a new time set, which is going to
be equal to whatever currently is happening at UTC.
In terms of time, it's going to be this time here.
If something goes wrong and any time fails to sink, then I'm going to have a exception and print out
the error message and the program is going to end.
All right.
Next, I'm going to do a little bit of work because remember, I don't just want to have a date and
time in UTC, but I want it in my local time zone.
So I'm going to take the local time as it is in UTC and stored in the time object.
I'm going to start with this variable and marketed as UTC.
Then I'm going to use the make time method for you time to create a new object which is going to be
local still at this point in time, because I've equated these to current time date logo is going to
be equal to game time that UTC.
But then I'm going to do this operation here and this is what actually shifts the UTC time to be local
time.
So at the moment in Australia, in Sydney in particular, we are 11 hours ahead of UTC.
So what I'm doing is I'm taking 11 times 3600 seconds for each hour.
So I've got this many seconds ahead of UTC and I'm going to add this time to the current local time
and then save the result on the current variable.
So eventually current time that local is going to have the accurate time and date in my local time zone.
So then I'm going to just print this out down here.
I'm going to use new time, local time, pass the current time date local, and that will show me the
time at my location.
So all good so far could actually stop here and have a local time to work with.
But what I haven't done yet is to reset the RTC with my new local time as acquired from the atomic Internet
clock.
So remember that these are two different things right now.
Time is stored in the time object, not in the RTC.
Right.
So I'm going to use the same method to set the RTC as in the previous example like this.
I've got now two options and then you can decide which one is appropriate for you, if you want to set
the ABC with the UTC time, then you will take apart the current time date.
UTC Tuval refer to each of its components individually and use this information to set RTC to the UTC
time.
But if you want the RTC to be sent to your local time zone time, then instead of current time date
UTC, you use current time date local and I'm using local time to convert that to convert the couple
in this variable into a new time object so then I can use it to reset the RTC.
So exactly the same method is just the source of the time.
Differs depending on whether you want UTC or local time zone time ongoing in this example, with local
time, certain time, eventually the time is in DC and I can use the same method as in the previous
example right here.
To extract it and print it out into the shell, it could be a screen in your case, for example, you
may want to create a clock that uses an oily display to show the time to date anyway.
So here I've got a bunch of print statements displaying both UTC time and.
Local time, so she'd probably say nutritiously time here to be more correct because both are coming.
All right, so this is UTC and this is local time, and then finally down here, just every one second
I will print out the date and time directly from the real time clock, which are said to be my local
time.
All right.
So it was a very long explanation.
So there's a lot happening, this script.
So I invite you to take a little bit of time to go through this line by line, make sure that you understand
how it works, but let's run it and make sure that it actually does work.
So run the current script.
You know what's happening?
I think we need to restart the backend.
All right, and Chris Laken.
Something's happening in line 50.
OK, so what's happening is that I have not stored the wi fi settings and the Scott test and Jason file
into onto the data file system, so let's do that right now.
So this file here.
I'm just going to upload it.
And now it should work.
Can connect with these credentials, just double check the credentials.
Correct?
Yes, actually, that is correct.
Try again.
Go this time, it worked.
So the time right now I'm going to bring up the Internet, time from time is.
So we can compare.
So we've got the current time nine twenty seven, as you can see in Sydney right now.
Nine twenty seven.
And.
I'm going to click on Starp because I want to go a little bit earlier and see the Ukichi time.
And there's a UTC time as it came from the A.P. several times, 22, 27, 35, some back then when it
was acquired, it was it's noted right here, I've got an error here with a local time that should be
UTC time instead of local.
And you can see that is twenty to twenty six.
There's a couple of minutes ago, sir, that was the raw date.
UTC Raw Data actually was acquired from the A.P. server.
I want to make a little fix here.
So this is your time before synchronization and this is the time of UTC time after synchronization,
because once 976 runs, then ATP Time synchronises you time with UTC, this time right here.
OK, so this was a bit a bit convoluted, but it has to take a few minutes to walk through this code
to understand how it works.
And this gives you a relatively straightforward way to sink your ISP, reduce real time clock with an
Internet atomic clock and from there to convert that UTC time to your local time zone.