1
00:00:05,420 --> 00:00:09,100
so in this video we're going to use the
Python Library the module that we've

2
00:00:09,100 --> 00:00:15,880
installed in the previous video so
if you haven't installed pytz module you

3
00:00:15,880 --> 00:00:19,050
need to go away and do that before this
will work because otherwise you get some

4
00:00:19,050 --> 00:00:22,510
weird errors and you won't be able to
use the module so how do we check that

5
00:00:22,510 --> 00:00:25,070
that model has been installed
successfully

6
00:00:25,070 --> 00:00:29,690
well lets write up a test program to make sure it's all

7
00:00:29,690 --> 00:00:35,100
working so type....we should be able to type pytz and use it as we

8
00:00:35,100 --> 00:00:41,030
normally used an import.... so you should find

9
00:00:41,030 --> 00:00:48,510
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:48,510 --> 00:00:50,540
as well

11
00:00:50,540 --> 00:01:03,470
and will put...

12
00:01:03,470 --> 00:01:15,130
....so we are going to display the time zone for

13
00:01:15,130 --> 00:01:24,710
moscow and we will put...

14
00:01:24,710 --> 00:01:39,590
.....

15
00:01:39,590 --> 00:01:42,990
.....and that's not actually an error
that you can see there that's just it

16
00:01:42,990 --> 00:01:48,170
showing in a different color and we will now put....

17
00:01:48,170 --> 00:02:04,740
....

18
00:02:04,740 --> 00:02:12,400
.....

19
00:02:12,400 --> 00:02:20,490
......

20
00:02:20,490 --> 00:02:28,620
...so there is our program so lets right click it and

21
00:02:28,620 --> 00:02:33,500
incidentally I've created a new Python file called tztest.py I'm still

22
00:02:33,500 --> 00:02:39,670
using the same project we've used for
date and time so right click that

23
00:02:39,670 --> 00:02:44,400
and run it so we make sure we're running
this file and the time in europe moscow

24
00:02:44,400 --> 00:02:52,780
is january 29th 2016 and its
5 am there and if you can

25
00:02:52,780 --> 00:02:57,410
just confirm that we can go to Google
and and search for current time

26
00:02:57,410 --> 00:03:08,190
moscow 5:28 a.m. GMT +3
29th of January so you can see that

27
00:03:08,190 --> 00:03:11,610
we've got that working quite nicely so
close that down now

28
00:03:11,610 --> 00:03:17,130
obviously from pytz module we used the timezone
method on line 6 and that's because

29
00:03:17,130 --> 00:03:22,610
the pytz only deals with time zone
information by using pytz to create

30
00:03:22,610 --> 00:03:28,040
a tzinfo object then we then used on line 7 to calculate the

31
00:03:28,040 --> 00:03:31,460
local time in any country so you could
change this to any country and do the same

32
00:03:31,460 --> 00:03:36,140
now from pytz we can use the timezone
method to get the tzinfo object for any

33
00:03:36,140 --> 00:03:40,140
chosen country but the required string
is difficult to sort if you wanted to

34
00:03:40,140 --> 00:03:43,760
provide the entire list to a user so is moscow
really in Europe and will most

35
00:03:43,760 --> 00:03:47,130
people really know to look there
what we can do is we can print the

36
00:03:47,130 --> 00:03:52,020
entire list of valid strings that the
pytz.timezone method accepts

37
00:03:52,020 --> 00:03:57,850
quite easily and the reason we can do that is it
quite helpfully provides a Python list

38
00:03:57,850 --> 00:04:04,440
called all_timezones so we could put
something....

39
00:04:04,440 --> 00:04:15,910
...so run that now you can see we got a
list of everything for the various

40
00:04:15,910 --> 00:04:20,790
countries a heap of Australia as you can see but
quite a few different ones are showing there

41
00:04:20,790 --> 00:04:25,630
the other thing I actually do also is
provide a dictionary of country names

42
00:04:25,630 --> 00:04:30,580
who's keys the iso316 country code
in other words GB for britain AU for

43
00:04:30,580 --> 00:04:36,740
Australia etc so we can put...

44
00:04:36,740 --> 00:04:53,370
....

45
00:04:53,370 --> 00:04:58,750
....so if we run and have a look at the
results you can see we got the identifier to

46
00:04:58,750 --> 00:05:10,450
the left and then the actual name to the
right AU for Australia US  for United States GB for

47
00:05:10,450 --> 00:05:14,790
Great Britain etcetera now the thing to
remember here is that the list of

48
00:05:14,790 --> 00:05:19,930
countries isn't actually good enough for getting the timezone and that's because many countries

49
00:05:19,930 --> 00:05:23,960
have got more than one timezone and so it
is possible to retrieve a list of all

50
00:05:23,960 --> 00:05:27,930
the time zones for a country code and
display them after the country name so

51
00:05:27,930 --> 00:05:33,170
we might be tempted to just use the same
key then pytz.time_zone dictionary

52
00:05:33,170 --> 00:05:40,540
something like this you could do...

53
00:05:40,540 --> 00:05:55,180
......

54
00:05:55,180 --> 00:06:07,270
.....

55
00:06:07,270 --> 00:06:13,430
....so you could try that so its probably
reasonably to expect both dictionaries to

56
00:06:13,430 --> 00:06:19,580
contain the same keys so run that
we've actually got an error here so the

57
00:06:19,580 --> 00:06:24,020
program actually ran until it hit the
Key BV at which point it crashed

58
00:06:24,020 --> 00:06:28,570
and that's because there is no time zone defined for Ireland
so as its

59
00:06:28,570 --> 00:06:32,260
inhabited it's a bit strange that you have keys
in the country names in the list at all but we

60
00:06:32,260 --> 00:06:34,620
can't really blame pytz for that

61
00:06:34,620 --> 00:06:39,760
sorry we can't really use the same keys as you
can see there now the timezone data

62
00:06:39,760 --> 00:06:44,340
itself it comes
from a data base that's maintained by

63
00:06:44,340 --> 00:06:48,800
IANA in which is the internet assigned
names authority and I'm just gonna

64
00:06:48,800 --> 00:06:57,770
bring up that link in a browser and it's
these guys and that's where the time zone

65
00:06:57,770 --> 00:07:02,050
date is coming from now it's often
referred to as the Olsen database and

66
00:07:02,050 --> 00:07:06,860
that's named after its creator Arthur David Olsen the database contains a history of

67
00:07:06,860 --> 00:07:10,930
timezone and Daylight Saving Time changes
and is pretty much considered the

68
00:07:10,930 --> 00:07:15,150
definitive source of information on
these now it is kept up-to-date and used to

69
00:07:15,150 --> 00:07:19,400
update the time zone information on most
computers although that said Windows

70
00:07:19,400 --> 00:07:23,510
does things slightly differently now
as long as your computer is receiving those updates

71
00:07:23,510 --> 00:07:27,920
the local timezone database should be
kept up to date but if you got a time for

72
00:07:27,920 --> 00:07:35,040
moscow that was four hours behind UTC
rather than 3 as you see in our pytz example

73
00:07:35,040 --> 00:07:39,020
that means your probably running Windows XP which is
no longer updated and therefore missing

74
00:07:39,020 --> 00:07:43,040
last year's change because its out of date
so with that said how to we get

75
00:07:43,040 --> 00:07:47,580
around this well we can do is go back to the
code is we can write our code a bit more

76
00:07:47,580 --> 00:07:52,300
defensively by using the Get Command
or the get method I should say and if you

77
00:07:52,300 --> 00:07:55,340
remember from the lectures on
dictionaries this returns none

78
00:07:55,340 --> 00:07:59,540
rather than an exception if the keys is not
in the dictionary so we could take a

79
00:07:59,540 --> 00:08:03,340
copy of this again well I'll just make
a change because obviously it's crashing

80
00:08:03,340 --> 00:08:09,950
the problem is is pytz.country_timezones if we change that and instead

81
00:08:09,950 --> 00:08:17,020
of putting and make the a .get
with x as the parameter that's going to return

82
00:08:17,020 --> 00:08:22,200
none in the case of there being a time
zone nonexistent for a particular

83
00:08:22,200 --> 00:08:28,040
country so you can run that and see what
happens now we don't get a crash and you

84
00:08:28,040 --> 00:08:33,520
can see we got time zones now so we come back
up Australia and you notice we got multiple time

85
00:08:33,520 --> 00:08:37,480
zones are quite happily working now
Australian Lord Howe Macquarie Hobart

86
00:08:37,480 --> 00:08:42,760
etc as we go across there is quite a
few there and likewise for some of the other ones

87
00:08:42,760 --> 00:08:47,100
as well Australia America Antarctica
etc so its obviously now working

88
00:08:47,100 --> 00:08:48,320
if you go back to B

89
00:08:48,320 --> 00:08:57,000
Bouvet Island is still showing on the list but now is not getting any
timezone information and that's

90
00:08:57,000 --> 00:09:01,730
because we got none returned and
prevented the program from crashing now if you

91
00:09:01,730 --> 00:09:05,160
needed to perform a more complicated
operation than just printing the list

92
00:09:05,160 --> 00:09:10,160
what we could do was split up the access
to country_timezones and then check if

93
00:09:10,160 --> 00:09:14,220
that the key is in the dictionary
first so

94
00:09:14,220 --> 00:09:25,840
actual what I might do is just comments one of
this out now take a copy of that but then

95
00:09:25,840 --> 00:09:32,760
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:32,760 --> 00:09:37,980
again what we want to do is to start
is removing the time zones from this

97
00:09:37,980 --> 00:09:38,780
part of the

98
00:09:38,780 --> 00:09:45,910
screen here now so I'm going to...so we just got 2 we are gong to print x

99
00:09:45,910 --> 00:09:54,320
as the parameters and the format pytz.country_timezone.get(x) we are going to leave that all together

100
00:09:54,320 --> 00:10:05,570
we gonna put.....

101
00:10:05,570 --> 00:10:11,680
.....if you

102
00:10:11,680 --> 00:10:18,750
get an entry in time zone if that is the case we are going to print...

103
00:10:18,750 --> 00:10:28,450
...so this time we are actually gonna

104
00:10:28,450 --> 00:10:33,710
show that instead of just using
the default which was just to print

105
00:10:33,710 --> 00:10:41,590
anything so we can do that and run this...and you

106
00:10:41,590 --> 00:10:46,770
see we quite happily get the same
information we come up here we now got Bouvet Island

107
00:10:46,770 --> 00:10:53,940
no time zone defined and you can see its working nicely and it is a more defensive

108
00:10:53,940 --> 00:10:58,150
way of doing it and more complicated but
you can see that by splitting the access

109
00:10:58,150 --> 00:11:00,770
to country_timezone and
then checking

110
00:11:00,770 --> 00:11:05,790
the keys in the dictionary first were able to process it that way now one thing

111
00:11:05,790 --> 00:11:09,790
we might want to do is to print the
time for each of the timezones in each country

112
00:11:09,790 --> 00:11:15,190
that's fairly easy to do now that we've
got pytz to use with date time so what we gonna

113
00:11:15,190 --> 00:11:20,950
do is print each time on a new line
indented by 2 tab stops and so

114
00:11:20,950 --> 00:11:24,920
that we can remove the and parameter in the
print statement on line 12 so we gonna

115
00:11:24,920 --> 00:11:29,730
do is come up to here after the if instead of just doing a print what

116
00:11:29,730 --> 00:11:39,650
we're going to do is put....

117
00:11:39,650 --> 00:11:53,280
......

118
00:11:53,280 --> 00:12:03,970
........

119
00:12:03,970 --> 00:12:08,430
.......and I'm
going to print something a bit different

120
00:12:08,430 --> 00:12:15,140
than before and will put some tabs in
so.....

121
00:12:15,140 --> 00:12:38,060
.....

122
00:12:38,060 --> 00:12:42,510
...so no we've done
that we should be able to run this and see

123
00:12:42,510 --> 00:12:47,070
the result and you can see we've now
actually got to the actual current time

124
00:12:47,070 --> 00:12:50,960
for that time zone showing on the screen
that is pretty cool as you can see if we come

125
00:12:50,960 --> 00:12:57,010
back and check Australia we got Adelaide
current times 13:11 if I just open this

126
00:12:57,010 --> 00:13:03,520
up friday 1:11 p.m. 13:11 so that is
working nicely ok so I'm gonna end this video

127
00:13:03,520 --> 00:13:06,120
here now because it's getting a little bit long in
the next few video we're going to

128
00:13:06,120 --> 00:13:11,290
continue on our discussions and
understanding about using pytz

129
00:13:11,290 --> 00:13:16,330
and introduce you to the localize
function and to some other conversion

130
00:13:16,330 --> 00:13:20,060
that should be quite interesting for you
so we'll see you in the next video

