Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Timezonapalooza7:52 with Kenneth Love
Here's my solution to the timezone script homework.
Hey, you came back. 0:00 All right, lets build this appointment scheduler, thing. 0:01 If you need a reminder we're building a script that takes a date and time, and 0:05 then gives it back to us in six other time zones. 0:08 My idea for this is that you'd use it to figure out what time your meetings are in 0:11 other time zones for all of your intercontinental business meetings. 0:14 So we have two hard steps, turning our datetime into UTC. 0:17 And then making our six other datetimes. 0:22 I'll see you in workspaces. 0:24 I'm going to call this file meetings.py. 0:26 And first thing I want to do is I want to import datetime. 0:29 And actually, what I want to do is I just want the datetime 0:32 part of datetime, so I'm just going to do from datetime, import datetime. 0:37 I don't need timedeltas or any of that other stuff, I just need datetimes. 0:41 And then I'm gonna import pytz as well. 0:46 I also need to make the list of time zones that I want to convert my times into. 0:48 Now, I could use random and pick six random ones from pytz.all time zones. 0:53 That's fine, if that's what you wanna do, but I'm gonna go ahead and 0:58 [SOUND] just specify some. 1:03 So we're gonna make a list. 1:05 And in that list we're gonna have 1:08 pytz.timezone, U.S. Eastern, 1:13 Pacific Auckland, Asia Calcutta. 1:19 None of these should be super surprising. 1:27 UTC, which we could've got, again, done pytz.utc. 1:30 Try saying that a few times fast. 1:36 And then let's do Europe/Paris. 1:39 And let's do Africa/Khartoum. 1:42 All right. 1:50 If, in case, you're wondering how I know all these, I looked them up. 1:50 I didn't just memorize all of these time zones. 1:54 But I wanted to spread them out a little bit. 1:58 Kinda move them around the world some. 1:59 So that's why they're spread out like this. 2:02 So then let's, let's build our format string again. 2:05 [BLANK_AUDIO] 2:09 So holding onto seconds there is a little silly because I'm not going to 2:12 actually show seconds or even capture seconds from the user. 2:17 But we're just, we're using the string that we used before. 2:20 So that's why seconds are already there. 2:24 Now, let's make the actual meat of the script. 2:26 We don't have to do this in a continuous loop, if you didn't want to. 2:28 Maybe you wanted to be able to, you wanted the user to restart the script, or 2:31 you wanted to import this and use it somewhere else, whatever. 2:35 Maybe you'd write a function. 2:39 But I'm just gonna have it run forever, in case they give me a bad string and 2:40 they don't have to restart at that point. 2:45 So, we're going to do while true and 2:47 then inside of here, we need to get the date from the user. 2:50 Right? 2:55 So let's do a date input equals input, 2:56 when is your meeting, nope, not metting, meeting. 3:01 Please use MM DD YYYY HH MM format and 3:04 let's try local date equals 3:13 datetime.strptime, date input. 3:19 And we have to specify what our string is. 3:26 So, we're using two months, two days, and four years and then hours and minutes. 3:28 Except if they give me a bad one and I get a value error. 3:37 Then I want to print doesn't seem to be a valid date and time. 3:43 And then we put in there date input. 3:53 Right? 3:57 Okay. 3:57 But if that's all good then we're going to use our else. 3:58 This works much like the else in the for loops or in an if condition. 4:02 The try happens and that runs the code that might explode. 4:07 If it explodes, the accept happens. 4:10 If it doesn't explode. 4:12 The else happens. 4:14 Again, it's a little bit strangely named, but that's the way it works. 4:15 So the else is there for when our try does not blow up. 4:19 So let's override local date to be 4:23 pytz.timezone U.S. Pacific. 4:28 And use that to localize local date. 4:33 So we're just overwriting local date back into it, itself. 4:36 But we're going to turn it into a U.S. 4:40 Pacific timezone, because I'm in the U.S. Pacific timezone. 4:42 If you, for instance, are in U.S. Eastern or wherever,. 4:46 Change that string to match your local timezone. 4:50 And the UTC date is going to be local, 4:53 local date.as timezone pytz.timezone UTC. 4:59 Again, we could do here UTC and that would be perfectly fine. 5:06 What do we wanna do now? 5:11 Well, we want to generate this list of datetimes, right? 5:13 So we need a place to put that, so we'll hold onto a list here. 5:18 And then let's say for timezone in OTHER TIMEZONES, okay? 5:24 So for each of those other time zones. 5:31 Output.append utc date.as timezone, 5:33 whatever timezone we're currently on, yeah? 5:37 Okay, so that's good for that, and now for appointment in output. 5:43 For everything that's inside of output, 5:48 let's print appointment.strf time format. 5:52 And then we can do a break after that. 5:57 So that way our script does end, 6:02 it just doesn't end just because they gave us a bad date. 6:03 It ends once they've given us good dates, and we run through everything. 6:05 Okay, let's give it a test. 6:08 Come down here, python meeting.py. 6:11 Okay, when is my meeting? 6:16 Please use this. 6:18 Okay let's see. 6:19 It's going to be on May 16th, 6:21 2015 at 18:30. 6:26 [SOUND] Oh. 6:30 I misspelled appointment. 6:31 [SOUND] Gotta watch out for those spelling errors kids. 6:34 They will getcha every time. 6:38 [SOUND] All right. 6:40 So let's try this again. 6:43 5, 16, 2015, at 18:30. 6:44 All right so there we go. 6:49 So for Eastern Daylight Time that will be at, what is that, 9:30? 6:50 For Auckland it'll be the next day at 1:30 p.m. 6:54 For India it'll be 7:00 a.m. 6:59 For UTC it'll be at 1:30. 7:01 These are all the next day notice. 7:04 For let's see CEST would be Paris. 7:07 So for Paris it'll be at 3:30. 7:11 And for Khartoum over there in Egypt it'll be at 4:30. 7:13 So that's pretty cool. 7:17 We can, we can change all of our dates and times however we want. 7:18 There's definitely plenty more that can be done with this script. 7:24 We don't have a built in way to end the script early, and these are can't 7:27 pick the timezones they need to convert the submitted date time too. 7:30 Feel free to add both of those or other improvements and post them to the forum. 7:34 Be sure to tag me so I can see them. 7:38 Well, thanks for learning about date times and time zones with me. 7:40 It's not the most glamorous area of programing, or python, but 7:43 they are one of the more useful pieces. 7:46 And you'll definitely find yourself using them repeatedly. 7:48 I'll see ya next time. 7:50
You need to sign up for Treehouse in order to download course files.Sign up