1 00:00:00,620 --> 00:00:05,070 So far, we've seen examples of how to use dates and times in Ruby. 2 00:00:05,070 --> 00:00:09,156 However, there's a lot more that we haven't taken into account, 3 00:00:09,156 --> 00:00:13,180 things like time zones, offsets, daylight savings, and more. 4 00:00:14,670 --> 00:00:19,189 Let's go into a bit more depth with dates and times now using Workspaces. 5 00:00:20,340 --> 00:00:22,490 When we've been working with dates and times so 6 00:00:22,490 --> 00:00:26,310 far, we haven't been worrying too much about time zones. 7 00:00:26,310 --> 00:00:29,050 Different parts of the world use something called an offset 8 00:00:29,050 --> 00:00:30,950 when taking time into account. 9 00:00:30,950 --> 00:00:34,750 You probably have friends or relatives in a different time zone and may need to 10 00:00:34,750 --> 00:00:39,500 mentally subtract or add hours so that you can call them at a reasonable time of day. 11 00:00:39,500 --> 00:00:41,380 Now, the same thing happens in Ruby and 12 00:00:41,380 --> 00:00:44,910 all programming languages when you're working with time zones. 13 00:00:44,910 --> 00:00:47,790 Now, luckily there are only a few different time zones that we 14 00:00:47,790 --> 00:00:49,200 have to deal with. 15 00:00:49,200 --> 00:00:50,180 No, I'm just kidding. 16 00:00:50,180 --> 00:00:53,120 There are a lot of different time zones in the world. 17 00:00:53,120 --> 00:00:59,020 Each different one on this map is denoted by a vertical bar. 18 00:00:59,020 --> 00:01:01,800 Now, we have just a ton of different time zones, and 19 00:01:01,800 --> 00:01:07,170 you'll notice that they're represented using UTC and an offset, 20 00:01:08,240 --> 00:01:14,230 and the offset is different depending on where exactly you are in the world. 21 00:01:14,230 --> 00:01:17,430 So here's a list of time zones, but let's go ahead and 22 00:01:17,430 --> 00:01:20,130 see how that works using Ruby. 23 00:01:20,130 --> 00:01:24,350 Now, when we instantiate a new time object, 24 00:01:24,350 --> 00:01:30,670 if we're not using time.now or time.new, we can specify it as the last argument. 25 00:01:30,670 --> 00:01:34,085 So by default, let me go ahead and launch IRB here. 26 00:01:37,172 --> 00:01:40,040 So by default, if we did a time.new, 27 00:01:40,040 --> 00:01:45,200 it's going to use the time zone wherever the computer is. 28 00:01:45,200 --> 00:01:49,514 Now, since we're using Workspaces, these servers default to UTC time, 29 00:01:49,514 --> 00:01:53,143 which means if we want to create a time in a different time zone, 30 00:01:53,143 --> 00:01:55,350 we need to explicitly tell Ruby that. 31 00:01:56,540 --> 00:01:58,340 So I'm going to create this variable called time1. 32 00:01:59,690 --> 00:02:04,301 And we'll say that's equal to the new time. 33 00:02:04,301 --> 00:02:06,418 It's the year 2014. 34 00:02:06,418 --> 00:02:09,539 It's July 1st at 12 o'clock. 35 00:02:09,539 --> 00:02:15,650 And I'm going to use the offset four hours from UTC time. 36 00:02:16,820 --> 00:02:19,130 So now we have that. 37 00:02:19,130 --> 00:02:22,810 We have that into account, and we can see this time is different. 38 00:02:22,810 --> 00:02:24,260 It says minus 0400. 39 00:02:24,260 --> 00:02:28,900 Well, let's go ahead and see how that compares to another time. 40 00:02:30,140 --> 00:02:32,705 And let's put that time, same day, 41 00:02:38,633 --> 00:02:40,809 but with a five-hour offset. 42 00:02:43,919 --> 00:02:49,594 Now, if we look at these two times, we can see time1 is not equal to time2. 43 00:02:49,594 --> 00:02:51,107 But let's change time2. 44 00:02:54,752 --> 00:02:57,141 Now, we're going to change this to be 11 o'clock. 45 00:02:59,371 --> 00:03:01,397 With that same offset. 46 00:03:03,721 --> 00:03:05,087 Now when we do that. 47 00:03:08,569 --> 00:03:15,270 It's the same time as the other object, just in a different time zone. 48 00:03:15,270 --> 00:03:19,190 Now, to make matters worse when dealing with times and time zones, 49 00:03:19,190 --> 00:03:22,430 as if this wasn't confusing enough, some parts of the world add or 50 00:03:22,430 --> 00:03:27,600 subtract an hour if a certain time takes place in daylight savings time. 51 00:03:27,600 --> 00:03:32,650 Now, Ruby luckily allows us to answer this question if we're talking to a time object 52 00:03:32,650 --> 00:03:35,620 to see if it took place during daylight savings. 53 00:03:35,620 --> 00:03:40,683 We can do that by saying daylight savings time, with a question mark. 54 00:03:42,882 --> 00:03:47,346 And if we look in the documentation, we can see this returns true if 55 00:03:47,346 --> 00:03:51,591 the time occurs during daylight savings time in its time zone. 56 00:03:53,380 --> 00:03:57,680 Now, when we're working with times, we can convert any time to UTC. 57 00:03:58,830 --> 00:04:02,530 And we do that by using the UTC method. 58 00:04:03,840 --> 00:04:06,910 Now, this will modify the time instance, so 59 00:04:06,910 --> 00:04:10,960 if we want to have an unmodified time instance, we'll convert that. 60 00:04:10,960 --> 00:04:16,220 So we'll say time1, and the dup method duplicates an object. 61 00:04:17,610 --> 00:04:20,472 So if we did time1 and UTC, 62 00:04:20,472 --> 00:04:25,451 we can see that this changes from 12 to 16 and 63 00:04:25,451 --> 00:04:30,575 UTC, rather than having an offset of some sort. 64 00:04:37,770 --> 00:04:41,460 Now, if we're looking at this UTC time1 object. 65 00:04:44,415 --> 00:04:47,590 We can call this UTC offset method to see how far it 66 00:04:47,590 --> 00:04:50,110 is from the Universal Time Zone. 67 00:04:50,110 --> 00:04:55,102 And if we took a look at time1 and called the same method, 68 00:04:55,102 --> 00:04:59,280 we can see it return this 14,400 number. 69 00:04:59,280 --> 00:05:01,300 And that's the number of seconds. 70 00:05:03,100 --> 00:05:04,712 We could convert that to hours, 71 00:05:04,712 --> 00:05:08,150 which we would be more familiar with by dividing by 60, twice. 72 00:05:09,660 --> 00:05:11,160 It's important to keep time zones and 73 00:05:11,160 --> 00:05:15,410 offsets in mind when working with dates and times. 74 00:05:15,410 --> 00:05:18,750 There are some gems that make this a little bit easier, but for right now, 75 00:05:18,750 --> 00:05:23,310 just try manipulating and changing times on your own, using a Workspace.