Introduction to Timezones5:07 with Kenneth Love
How do we deal with timezones in Python?
[MUSIC] 0:00 So what is this terrible developer nightmare? 0:04 This thing that makes experienced programmers cry? 0:07 Time zones. 0:10 If that doesn't [SOUND] immediately send shivers down your spine, 0:11 I've linked to a great video in the teacher's notes that explains why time 0:14 zones are the source of so many headaches [SOUND]. 0:17 Luckily for us, 0:19 Python has some well-tested libraries to help with this subject. 0:20 I can't promise they'll fill every niche, but they should get you well on your way. 0:24 Let's look at datetime.timezone. 0:28 So when we create a date time or time object, we can specify the time zone. 0:31 And now, that object knows where it is in the world and 0:37 how to sort itself out amongst all of our other objects. 0:40 We say that a date time object that knows its time zone, is aware, and 0:42 those that don't are naive. 0:47 Let's make some time zone aware date times. 0:49 So I've imported Date and Time all ready and I'm going to make a new time zone 0:52 here called Pacific, which is going to be a datetime.timezone. 0:55 And then we give it a timedelta, and we say that it is hours minus 8, 1:01 because the Pacific time zone is eight hours off of UTC. 1:07 And we'll say Eastern [SOUND] and it's hours are gonna be 1:12 negative 5 because three hours away, right? 1:17 Okay, so we've made our couple of time zone objects and like I said, 1:22 we have to specify the offset from UTC. 1:26 Pacific time is eight hours before, Eastern time is five hours before. 1:29 So we're gonna set these as, as being negative numbers. 1:32 So, let's make a naive datetime object. 1:35 So naive.datetime.datetime. 1:38 And we're going to fill this in with 2014, 4, 21, 9. 1:43 Alright so this is a date that I think we used earlier, so lets look at that one. 1:48 Naive, there is everything. 1:55 Okay. 1:56 Now, let's make an aware version. 1:58 So, same exact information, but then we say tzinfo time zone info, is Pacific. 2:03 Okay, and we look at where. 2:11 Same thing, but then we get this huge tzinfo thing here, right? 2:13 So, I've got these two, so what, what do we do here? 2:17 Okay, so 2:20 what I want to be able to do is I want to be able to show these in Eastern Time. 2:21 So I would do naive.astimezone. 2:26 And I pass in the time zone I want this to be as. 2:32 And I want it to be as Eastern, and oh! 2:35 I get a value error, because it doesn't, it has no idea! 2:37 It's like what, what are we doing? 2:41 Okay? 2:44 But the aware one shouldn't be that way. 2:45 Let's do it as Eastern, and there we go. 2:48 So where it was, 9 a.m. 2:51 It is now noon, because if it's 9 Eastern, 2:55 10 Mountain, 11 Central, noon Eastern. 2:59 Or, sorry, 9 Pacific. 3:03 That converted the way that we wanted it to. 3:06 All right. 3:08 That's pretty simple. 3:09 Let's try a time zone that would be like way in the future or at some strange time. 3:10 One of my favorite, I'll say weird. 3:16 If you're from there don't find it weird. 3:18 Interesting time zones is the Auckland time zone. 3:21 So we're gonna do a timedelta here for this one and 3:24 it will be oops, not timedelta. 3:28 Timezone and it is a timedelta away. 3:30 But it is 13 hours. 3:36 Oh I have to put an equals sign in here, Auckland equals. 3:39 There we go. 3:45 So lets try changing our aware.astimezone(auckland). 3:46 And there we go, so this one a day ahead and went to 6 in the morning. 3:54 And let's make one more, let's make Mumbai. 3:58 And Mumbai is, is fun. 4:01 All of India is in this one standard time zone, India Standard Time. 4:03 They don't do Daylight Savings Time, if I remember correctly. 4:07 What's weird though, is that they are five and 4:11 a half hours ahead from UGC, five and a half. 4:13 So, we're gonna do datetime.timezone, and 4:17 the time delta, hours is 5, and minutes is 30. 4:22 So, if we do our aware.astimezone(mumbai), 4:29 then it's the same day, but 4:34 it's 10:30 pm instead of 9 in the morning. 4:37 So 14 and a half hours difference, so it converts correctly. 4:42 This is doing exactly what we want it to do. 4:47 So native time zones in Python aren't the worst thing ever. 4:49 But did you notice that we had to know how far ahead or behind each time zone was? 4:53 Do you really wanna have to remember that or look it up every single time? 4:58 I didn't think so. 5:01 In the next video, we'll check out the super useful Pi TZ Library. 5:02
You need to sign up for Treehouse in order to download course files.Sign up