Timezones and Offsets5:23 with Jason Seifer
When we've been working with dates and times so far, we haven't been worrying about time zones. Different parts of the world use something called an offset when taking time in to account. You probably have friends or relatives in a different timezone and may need to mentally subtract or add hours so you call them at a reasonable time of day. The same thing happens in Ruby when working with time zones.
So far, we've seen examples of how to use dates and times in Ruby. 0:00 However, there's a lot more that we haven't taken into account, 0:05 things like time zones, offsets, daylight savings, and more. 0:09 Let's go into a bit more depth with dates and times now using Workspaces. 0:14 When we've been working with dates and times so 0:20 far, we haven't been worrying too much about time zones. 0:22 Different parts of the world use something called an offset 0:26 when taking time into account. 0:29 You probably have friends or relatives in a different time zone and may need to 0:30 mentally subtract or add hours so that you can call them at a reasonable time of day. 0:34 Now, the same thing happens in Ruby and 0:39 all programming languages when you're working with time zones. 0:41 Now, luckily there are only a few different time zones that we 0:44 have to deal with. 0:47 No, I'm just kidding. 0:49 There are a lot of different time zones in the world. 0:50 Each different one on this map is denoted by a vertical bar. 0:53 Now, we have just a ton of different time zones, and 0:59 you'll notice that they're represented using UTC and an offset, 1:01 and the offset is different depending on where exactly you are in the world. 1:08 So here's a list of time zones, but let's go ahead and 1:14 see how that works using Ruby. 1:17 Now, when we instantiate a new time object, 1:20 if we're not using time.now or time.new, we can specify it as the last argument. 1:24 So by default, let me go ahead and launch IRB here. 1:30 So by default, if we did a time.new, 1:37 it's going to use the time zone wherever the computer is. 1:40 Now, since we're using Workspaces, these servers default to UTC time, 1:45 which means if we want to create a time in a different time zone, 1:49 we need to explicitly tell Ruby that. 1:53 So I'm going to create this variable called time1. 1:56 And we'll say that's equal to the new time. 1:59 It's the year 2014. 2:04 It's July 1st at 12 o'clock. 2:06 And I'm going to use the offset four hours from UTC time. 2:09 So now we have that. 2:16 We have that into account, and we can see this time is different. 2:19 It says minus 0400. 2:22 Well, let's go ahead and see how that compares to another time. 2:24 And let's put that time, same day, 2:30 but with a five-hour offset. 2:38 Now, if we look at these two times, we can see time1 is not equal to time2. 2:43 But let's change time2. 2:49 Now, we're going to change this to be 11 o'clock. 2:54 With that same offset. 2:59 Now when we do that. 3:03 It's the same time as the other object, just in a different time zone. 3:08 Now, to make matters worse when dealing with times and time zones, 3:15 as if this wasn't confusing enough, some parts of the world add or 3:19 subtract an hour if a certain time takes place in daylight savings time. 3:22 Now, Ruby luckily allows us to answer this question if we're talking to a time object 3:27 to see if it took place during daylight savings. 3:32 We can do that by saying daylight savings time, with a question mark. 3:35 And if we look in the documentation, we can see this returns true if 3:42 the time occurs during daylight savings time in its time zone. 3:47 Now, when we're working with times, we can convert any time to UTC. 3:53 And we do that by using the UTC method. 3:58 Now, this will modify the time instance, so 4:03 if we want to have an unmodified time instance, we'll convert that. 4:06 So we'll say time1, and the dup method duplicates an object. 4:10 So if we did time1 and UTC, 4:17 we can see that this changes from 12 to 16 and 4:20 UTC, rather than having an offset of some sort. 4:25 Now, if we're looking at this UTC time1 object. 4:37 We can call this UTC offset method to see how far it 4:44 is from the Universal Time Zone. 4:47 And if we took a look at time1 and called the same method, 4:50 we can see it return this 14,400 number. 4:55 And that's the number of seconds. 4:59 We could convert that to hours, 5:03 which we would be more familiar with by dividing by 60, twice. 5:04 It's important to keep time zones and 5:09 offsets in mind when working with dates and times. 5:11 There are some gems that make this a little bit easier, but for right now, 5:15 just try manipulating and changing times on your own, using a Workspace. 5:18
You need to sign up for Treehouse in order to download course files.Sign up