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