Actually, Use pytz Instead8:59 with Kenneth Love
Let's use the `pytz` library instead of the timezones in `datetime`.
To make time zones better we're gonna be using library named pytz. 0:00 We've already installed it for you on Workspaces, but 0:03 you'll need to install it yourself locally. 0:06 You can do this with pip install pytz. 0:08 All right, let's see how this tool works. 0:12 All right, we're going to need our date time libraries. 0:14 So we have all that imported. 0:17 And we're also gonna be using pytz as we talked about. 0:18 So we have that imported as well. 0:21 All right, so pytz I really recommend you go look at the docs for 0:24 it, it has a whole lot of stuff that makes all this work so much easier. 0:29 So, in our last video, we did Eastern and Pacific by specifying time deltas. 0:33 Well, let's do those again. 0:38 We'll do Pacific first and [SOUND] we're gonna do the pytz method. 0:39 And so we actually specify the name of the time zone. 0:45 So, for instance, we want the US Pacific time zone, 0:51 we specify US Pacific and if we do Eastern, [SOUND] US Eastern. 0:56 Okay. 1:01 So now we have those, great. 1:02 Now let's use let's give ourselves this format string. 1:04 We're gonna use this a lot so we're just gonna go ahead and put this in here. 1:07 So, this should be familiar. 1:11 [BLANK_AUDIO] 1:13 All right so, year, month, day, hour, minute, second, and 1:16 then we're gonna do cap Z and lowercase z, and so what do these two do? 1:20 Well cap Z gives us the time zone name so Pacific or 1:25 India standard time or whatever. 1:28 And the lowercase z prints out the offset of that timezone from UTC. 1:31 So we'd get like minus 8, or plus 550, or, or whatever. 1:36 Speaking of UTC, let's make a variable that holds onto UTC. 1:41 You can actually just do pytz.utc. 1:46 We're only saving five characters, but, you know, I get lazy as a programmer. 1:49 So let's make a date time in the Pacific time zone, 1:54 with the one that we've been doing, all right? 1:57 Pacific.localize, datetime.datetime, 2:01 2014, 4, 21, 9. 2:07 [BLANK_AUDIO] 2:11 So what's the pacific.localize? 2:12 Well what that does is it takes a naive date time or 2:14 it can work with aware ones too, I do believe. 2:18 But anyway, it takes a date time and gives it to you with a certain time zone. 2:23 So if we do start.strftime with our format string, then there we go. 2:26 We've got Pacific daylight time at 7 hours ahead. 2:32 And we've got our date and our time. 2:35 'Kay, so that's awesome. 2:38 We can see from our usual info here that it's in the Pacific time zone, and let's, 2:39 let's do what we did before and let's try turning this into an Eastern time zone. 2:44 So let's call this start_eastern. 2:48 [SOUND] And we're gonna go back to using that as time zone. 2:51 So now if I look at start_eastern, I get this new one and we can see that it is, 2:57 [SOUND] well let's look at start. 3:02 So we can see these two here, right? 3:04 So we've got PDT and EDT. 3:05 And we've got our correct times. 3:08 So that's great. 3:10 But why did we use this as time zone instead of using localize again? 3:12 Well, localize is for naive date times. 3:15 And since our start variable was already time zone aware, it's no longer naive. 3:18 We can't localize it again. 3:22 Most of the time, though, you're gonna wanna work with times in UTC. 3:24 And you're gonna convert them when they get put out, right, 3:28 when they go out to a local time. 3:31 So, let's see about how we would do that. 3:32 So, let's do a start utc, 3:34 datetime.datetime, [SOUND] 2014, 3:38 4, 21, 1 and tz is gonna be utc. 3:43 So now, if I start_utc and put in my format string, then, we get that, right? 3:48 UTC is plus 0, that's exactly where we wanna be. 3:53 Okay. So now, 3:56 let's convert this back to Pacific. 3:57 So start utc.astimezone, [SOUND] and 3:59 now let's look at start_pacific. 4:03 And there we go, so that's 18 o'clock or rather 6 o'clock. 4:08 And we're in our Pacific time zone. 4:12 If it's always best to work in UTC, how do we do this in the real world? 4:15 Well, we need to know where the data's coming from. 4:20 To illustrate this, let's let's add another couple of time zones to our mix. 4:23 We're gonna use our friends Auckland and Mumbai again. 4:26 So Auckland will be pytz.timezone, Pacific/Auckland. 4:29 So don't get US Pacific and Pacific/Auckland mixed up. 4:36 And Mumbai will be [SOUND] Asia/Calcutta. 4:39 It's like I said, all of India is in the same timezone, so 4:47 we can just use Calcutta instead of trying to find Mumbai or whatever. 4:50 Let's make a new time zone, and we're gonna make it here local in US time. 4:55 So, let's do apollo_13_naive. 5:00 [SOUND] So, this is when, Apollo 13 launched, 5:04 which is 1970, on April 11, at 14, 5:10 13 UTC time, sort of, all right? 5:15 So we're starting this as this local date time. 5:20 So we, we have this time. 5:23 Sorry, and we wanna convert this to Eastern, so 5:26 apollo_13_eastern will be Eastern.localize, apollo_13_naive. 5:30 Okay, so if we look at apollo_13_naive, and 5:39 apollo _13_eastern, we have the same time. 5:44 Because that's, that was when it was launched. 5:49 Right? Two in the afternoon. 5:51 'Kay, so there's our eastern version. 5:52 Now, like I said, 5:54 we should be starting this as UTC for all of our around the world conversions. 5:55 So why do we wanna store it as UTC? 5:59 Well, it's because UTC doesn't deal with daylight saving times. 6:01 So, converting into and out of DST isn't as hard 6:05 as it is if you have to remember whether or not the original time is in DST. 6:08 If you'll going to [UNKNOWN], like fewer calculations have to worry about. 6:12 Not to mention the fact that some countries go in to DST at different times 6:16 from other countries that are even in the same time zone. 6:19 Watch that video, time zones are ridiculous. 6:22 Okay. 6:25 So, let's make apollo_13_utc. 6:26 So we're going to do apollo_13_eastern.astimezone, utc. 6:29 So why do we start with apollo_13_eastern instead of apollo_13_naive? 6:37 Well naive doesn't know where it is, 6:41 so it doesn't know how to convert itself into UTC. 6:42 That's why we wanna start with Eastern. 6:44 That's quite a few steps just to get to having this UTC time. 6:47 But, now, we can find this time all over the world. 6:52 So let's see when Apollo 13 launched in the Pacific. 6:55 [SOUND] So it launched at 11:13. 7:01 Okay, cool. 7:08 When did it do that in Auckland? 7:10 [SOUND] It was the next day at seven in the morning. 7:12 Okay. 7:17 When did it do it in Mumbai? 7:18 [SOUND] That was the next day and it was right after midnight, 7:21 43 minutes after midnight. 7:27 So, so long as you start with UTC dates and times, 7:30 you can always generate new dates and times for 7:33 anywhere in the world without worrying about getting incorrect dates and times. 7:35 Your Tardis wants to work on UTC times. 7:40 Right? 7:43 Your Tardis will be able to get you where you wanna go as long as you 7:44 start with UTC. 7:46 But, wait a minute. 7:47 What if you don't know all of the time zones? 7:48 So, pytz actually has two really handy little bits for finding time zones. 7:51 There is pytz.alltimezones. 7:55 And this lists all the time zones, 8:00 which is this, this huge, huge list of time zones. 8:02 Or you can do pytz.country_timezones. 8:05 And you can pass in a country two letter code. 8:10 And you get back just the time zones for that country. 8:13 So you can see we've got all of our usual ones. 8:17 We've got some crazy ones in like, Indiana and North Dakota, and 8:20 then we've got some in like, Alaska. 8:24 All right. 8:26 That's pretty much it for pytz for right now. 8:27 Hopefully, pytz gives you some more confidence when working with times and 8:32 dates in Python. 8:35 There are some other great libraries out there for 8:36 working with this often confusing data that I'll link to in the teacher's notes. 8:38 Before our last video, use the pytz and date time documentation to build a script 8:42 that takes a date and time in your time zone, and then gives that time back 8:47 in six other time zones using our format string from this video. 8:51 I'll show you my solution in the next video, 8:55 and the format string is in the teacher's notes. 8:56
You need to sign up for Treehouse in order to download course files.Sign up