WEBVTT 1 00:00:01.960 --> 00:00:05.649 so in this video we're going to use the Python Library the module that we've 2 00:00:05.649 --> 00:00:12.429 installed in the previous video so if you haven't installed pytz module you 3 00:00:12.429 --> 00:00:15.599 need to go away and do that before this will work because otherwise you get some 4 00:00:15.599 --> 00:00:19.050 weird errors and you won't be able to use the module so how do we check that 5 00:00:19.050 --> 00:00:21.619 that model has been installed successfully 6 00:00:21.619 --> 00:00:26.239 well lets write up a test program to make sure it's all 7 00:00:26.239 --> 00:00:31.640 working so type....we should be able to type pytz and use it as we 8 00:00:31.640 --> 00:00:37.579 normally used an import.... so you should find 9 00:00:37.579 --> 00:00:45.050 that will work now that we have installed that and lets display the current time in moscow I am going to import date time 10 00:00:45.050 --> 00:00:47.089 as well 11 00:00:47.089 --> 00:01:00.019 and will put... 12 00:01:00.019 --> 00:01:11.670 ....so we are going to display the time zone for 13 00:01:11.670 --> 00:01:21.259 moscow and we will put... 14 00:01:21.259 --> 00:01:36.130 ..... 15 00:01:36.130 --> 00:01:39.530 .....and that's not actually an error that you can see there that's just it 16 00:01:39.530 --> 00:01:44.719 showing in a different color and we will now put.... 17 00:01:44.719 --> 00:02:01.280 .... 18 00:02:01.280 --> 00:02:08.940 ..... 19 00:02:08.940 --> 00:02:17.030 ...... 20 00:02:17.030 --> 00:02:25.160 ...so there is our program so lets right click it and 21 00:02:25.160 --> 00:02:30.040 incidentally I've created a new Python file called tztest.py I'm still 22 00:02:30.040 --> 00:02:36.210 using the same project we've used for date and time so right click that 23 00:02:36.210 --> 00:02:40.940 and run it so we make sure we're running this file and the time in europe moscow 24 00:02:40.940 --> 00:02:49.320 is january 29th 2016 and its 5 am there and if you can 25 00:02:49.320 --> 00:02:53.950 just confirm that we can go to Google and and search for current time 26 00:02:53.950 --> 00:03:04.730 moscow 5:28 a.m. GMT +3 29th of January so you can see that 27 00:03:04.730 --> 00:03:08.150 we've got that working quite nicely so close that down now 28 00:03:08.150 --> 00:03:13.670 obviously from pytz module we used the timezone method on line 6 and that's because 29 00:03:13.670 --> 00:03:19.150 the pytz only deals with time zone information by using pytz to create 30 00:03:19.150 --> 00:03:24.580 a tzinfo object then we then used on line 7 to calculate the 31 00:03:24.580 --> 00:03:28.000 local time in any country so you could change this to any country and do the same 32 00:03:28.000 --> 00:03:32.680 now from pytz we can use the timezone method to get the tzinfo object for any 33 00:03:32.680 --> 00:03:36.680 chosen country but the required string is difficult to sort if you wanted to 34 00:03:36.680 --> 00:03:40.300 provide the entire list to a user so is moscow really in Europe and will most 35 00:03:40.300 --> 00:03:43.670 people really know to look there what we can do is we can print the 36 00:03:43.670 --> 00:03:48.560 entire list of valid strings that the pytz.timezone method accepts 37 00:03:48.560 --> 00:03:54.390 quite easily and the reason we can do that is it quite helpfully provides a Python list 38 00:03:54.390 --> 00:04:00.980 called all_timezones so we could put something.... 39 00:04:00.980 --> 00:04:12.459 ...so run that now you can see we got a list of everything for the various 40 00:04:12.459 --> 00:04:17.330 countries a heap of Australia as you can see but quite a few different ones are showing there 41 00:04:17.330 --> 00:04:22.170 the other thing I actually do also is provide a dictionary of country names 42 00:04:22.170 --> 00:04:27.120 who's keys the iso316 country code in other words GB for britain AU for 43 00:04:27.120 --> 00:04:33.280 Australia etc so we can put... 44 00:04:33.280 --> 00:04:49.910 .... 45 00:04:49.910 --> 00:04:55.290 ....so if we run and have a look at the results you can see we got the identifier to 46 00:04:55.290 --> 00:05:06.990 the left and then the actual name to the right AU for Australia US for United States GB for 47 00:05:06.990 --> 00:05:11.330 Great Britain etcetera now the thing to remember here is that the list of 48 00:05:11.330 --> 00:05:16.470 countries isn't actually good enough for getting the timezone and that's because many countries 49 00:05:16.470 --> 00:05:20.500 have got more than one timezone and so it is possible to retrieve a list of all 50 00:05:20.500 --> 00:05:24.470 the time zones for a country code and display them after the country name so 51 00:05:24.470 --> 00:05:29.710 we might be tempted to just use the same key then pytz.time_zone dictionary 52 00:05:29.710 --> 00:05:37.080 something like this you could do... 53 00:05:37.080 --> 00:05:51.720 ...... 54 00:05:51.720 --> 00:06:03.810 ..... 55 00:06:03.810 --> 00:06:09.970 ....so you could try that so its probably reasonably to expect both dictionaries to 56 00:06:09.970 --> 00:06:16.120 contain the same keys so run that we've actually got an error here so the 57 00:06:16.120 --> 00:06:20.560 program actually ran until it hit the Key BV at which point it crashed 58 00:06:20.560 --> 00:06:25.110 and that's because there is no time zone defined for Ireland so as its 59 00:06:25.110 --> 00:06:28.800 inhabited it's a bit strange that you have keys in the country names in the list at all but we 60 00:06:28.800 --> 00:06:31.169 can't really blame pytz for that 61 00:06:31.169 --> 00:06:36.300 sorry we can't really use the same keys as you can see there now the timezone data 62 00:06:36.300 --> 00:06:40.889 itself it comes from a data base that's maintained by 63 00:06:40.889 --> 00:06:45.349 IANA in which is the internet assigned names authority and I'm just gonna 64 00:06:45.349 --> 00:06:54.319 bring up that link in a browser and it's these guys and that's where the time zone 65 00:06:54.319 --> 00:06:58.599 date is coming from now it's often referred to as the Olsen database and 66 00:06:58.599 --> 00:07:03.409 that's named after its creator Arthur David Olsen the database contains a history of 67 00:07:03.409 --> 00:07:07.479 timezone and Daylight Saving Time changes and is pretty much considered the 68 00:07:07.479 --> 00:07:11.699 definitive source of information on these now it is kept up-to-date and used to 69 00:07:11.699 --> 00:07:15.949 update the time zone information on most computers although that said Windows 70 00:07:15.949 --> 00:07:20.050 does things slightly differently now as long as your computer is receiving those updates 71 00:07:20.050 --> 00:07:24.460 the local timezone database should be kept up to date but if you got a time for 72 00:07:24.460 --> 00:07:31.589 moscow that was four hours behind UTC rather than 3 as you see in our pytz example 73 00:07:31.589 --> 00:07:35.560 that means your probably running Windows XP which is no longer updated and therefore missing 74 00:07:35.560 --> 00:07:39.589 last year's change because its out of date so with that said how to we get 75 00:07:39.589 --> 00:07:44.120 around this well we can do is go back to the code is we can write our code a bit more 76 00:07:44.120 --> 00:07:48.849 defensively by using the Get Command or the get method I should say and if you 77 00:07:48.849 --> 00:07:51.889 remember from the lectures on dictionaries this returns none 78 00:07:51.889 --> 00:07:56.089 rather than an exception if the keys is not in the dictionary so we could take a 79 00:07:56.089 --> 00:07:59.889 copy of this again well I'll just make a change because obviously it's crashing 80 00:07:59.889 --> 00:08:06.490 the problem is is pytz.country_timezones if we change that and instead 81 00:08:06.490 --> 00:08:13.560 of putting and make the a .get with x as the parameter that's going to return 82 00:08:13.560 --> 00:08:18.749 none in the case of there being a time zone nonexistent for a particular 83 00:08:18.749 --> 00:08:24.589 country so you can run that and see what happens now we don't get a crash and you 84 00:08:24.589 --> 00:08:30.060 can see we got time zones now so we come back up Australia and you notice we got multiple time 85 00:08:30.060 --> 00:08:34.029 zones are quite happily working now Australian Lord Howe Macquarie Hobart 86 00:08:34.029 --> 00:08:39.300 etc as we go across there is quite a few there and likewise for some of the other ones 87 00:08:39.300 --> 00:08:43.640 as well Australia America Antarctica etc so its obviously now working 88 00:08:43.640 --> 00:08:44.860 if you go back to B 89 00:08:44.860 --> 00:08:53.540 Bouvet Island is still showing on the list but now is not getting any timezone information and that's 90 00:08:53.540 --> 00:08:58.279 because we got none returned and prevented the program from crashing now if you 91 00:08:58.279 --> 00:09:01.709 needed to perform a more complicated operation than just printing the list 92 00:09:01.709 --> 00:09:06.700 what we could do was split up the access to country_timezones and then check if 93 00:09:06.700 --> 00:09:10.769 that the key is in the dictionary first so 94 00:09:10.769 --> 00:09:22.380 actual what I might do is just comments one of this out now take a copy of that but then 95 00:09:22.380 --> 00:09:29.300 ccomment that out and you can come back and refer to it if you need to so lets go back and paste that command 96 00:09:29.300 --> 00:09:34.529 again what we want to do is to start is removing the time zones from this 97 00:09:34.529 --> 00:09:35.329 part of the 98 00:09:35.329 --> 00:09:42.450 screen here now so I'm going to...so we just got 2 we are gong to print x 99 00:09:42.450 --> 00:09:50.860 as the parameters and the format pytz.country_timezone.get(x) we are going to leave that all together 100 00:09:50.860 --> 00:10:02.110 we gonna put..... 101 00:10:02.110 --> 00:10:08.220 .....if you 102 00:10:08.220 --> 00:10:15.290 get an entry in time zone if that is the case we are going to print... 103 00:10:15.290 --> 00:10:24.990 ...so this time we are actually gonna 104 00:10:24.990 --> 00:10:30.250 show that instead of just using the default which was just to print 105 00:10:30.250 --> 00:10:38.130 anything so we can do that and run this...and you 106 00:10:38.130 --> 00:10:43.310 see we quite happily get the same information we come up here we now got Bouvet Island 107 00:10:43.310 --> 00:10:50.480 no time zone defined and you can see its working nicely and it is a more defensive 108 00:10:50.480 --> 00:10:54.690 way of doing it and more complicated but you can see that by splitting the access 109 00:10:54.690 --> 00:10:57.310 to country_timezone and then checking 110 00:10:57.310 --> 00:11:02.330 the keys in the dictionary first were able to process it that way now one thing 111 00:11:02.330 --> 00:11:06.330 we might want to do is to print the time for each of the timezones in each country 112 00:11:06.330 --> 00:11:11.730 that's fairly easy to do now that we've got pytz to use with date time so what we gonna 113 00:11:11.730 --> 00:11:17.490 do is print each time on a new line indented by 2 tab stops and so 114 00:11:17.490 --> 00:11:21.460 that we can remove the and parameter in the print statement on line 12 so we gonna 115 00:11:21.460 --> 00:11:26.270 do is come up to here after the if instead of just doing a print what 116 00:11:26.270 --> 00:11:36.190 we're going to do is put.... 117 00:11:36.190 --> 00:11:49.820 ...... 118 00:11:49.820 --> 00:12:00.510 ........ 119 00:12:00.510 --> 00:12:04.970 .......and I'm going to print something a bit different 120 00:12:04.970 --> 00:12:11.680 than before and will put some tabs in so..... 121 00:12:11.680 --> 00:12:34.600 ..... 122 00:12:34.600 --> 00:12:39.050 ...so no we've done that we should be able to run this and see 123 00:12:39.050 --> 00:12:43.610 the result and you can see we've now actually got to the actual current time 124 00:12:43.610 --> 00:12:47.500 for that time zone showing on the screen that is pretty cool as you can see if we come 125 00:12:47.500 --> 00:12:53.550 back and check Australia we got Adelaide current times 13:11 if I just open this 126 00:12:53.550 --> 00:13:00.060 up friday 1:11 p.m. 13:11 so that is working nicely ok so I'm gonna end this video 127 00:13:00.060 --> 00:13:02.660 here now because it's getting a little bit long in the next few video we're going to 128 00:13:02.660 --> 00:13:07.830 continue on our discussions and understanding about using pytz 129 00:13:07.830 --> 00:13:12.870 and introduce you to the localize function and to some other conversion 130 00:13:12.870 --> 00:13:16.600 that should be quite interesting for you so we'll see you in the next video