Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
The Ruby Time Class5:51 with Jason Seifer
The DateTime and Time classes add an additional component compared to the date class -- the concept of a time of day. The Date class allows us to work with individual dates but has no concept of a time of day. If we want to get specific about the time of day, we have to use the DateTime or Time classes. Let's go ahead and see how that works now using Workspaces.
Now let's take a look at the DateTime and Time classes. When we're representing dates and times and need a time of day component, these are the classes we would use.
There is little difference between the date and time classes these days. In older versions of Ruby, there was an internal difference in how they were represented and Time instances could only go up to the year 2038 but these days the differences are not as big. Just pick one class and use use. We're going to work with the Time class in this video.
Time.new Time.new(2011) Time.new(2014, 2, 1) time = Time.new time.hour time.min time.sec time.monday? time.month time.day
Addition and subtraction is seconds:
time + 1 is 1 second time + (60) for 1 minute time + (60 * 60) for 1 hour
We can also see if one time is before or after another
time < Time.now Time.now > time
We can format using
time.strftime("%m %d %Y %I:%M %p")
>> The DateTime and Time classes add an additional component 0:00 compared to the Date class, the concept of a time of day. 0:04 The Date class allows us to work with individual dates, but 0:09 has no concept of a time of day. 0:13 If we wanna get specific about the time of day, 0:16 we have to use the DateTime or Time classes. 0:19 Let's go ahead and see how that works now, using workspaces. 0:24 >> Now we're gonna take a look at the time class in Ruby. 0:28 When we're representing dates and they have a time component, 0:32 these are the classes that we'll need to use. 0:36 Now when we load up the Standard Library Documentation for date DateTime and Time, 0:39 there are two different classes DateTime and Time that represent time. 0:43 Now these days, there is very little difference between the date and 0:50 time classes. 0:53 In older versions of Ruby, there was an internal difference in how they were 0:55 represented and time instances could only up to the year 2038. 0:59 These days, the differences between the classes are not as big. 1:03 We're just gonna pick one class and use it and in this video, 1:07 we're gonna work with the time class. 1:10 Now if we were to click on this in the Standard Library Documentation, 1:13 we only get three methods. 1:17 There's a built-in time class, as well. 1:19 And that is where all of the methods are stored in Ruby Core documentation. 1:22 So you'll find a link to these in the notes right below the video. 1:27 Now the first thing that we're gonna do is take a look at a new time object. 1:31 So, I'm gonna go ahead and load up a workspace here. 1:36 And if I type irb to load in irb and require time, now we're good to go. 1:39 So if we instantiate a new time instance, it's going to default to the current time. 1:47 Now just like with the Date class, we can pass in a year as the first argument. 1:55 So let's say, we were instantiating a new time, 2014. 2:00 This will default to 0 seconds on January 1, 2014. 2:05 Now if we go back to the documentation here, we can see that the default 2:11 arguments, year, no month, no day, no hour, no minute. 2:17 But we could fill all of these in, in this order, if we wanted to. 2:21 I'm gonna go ahead and 2:26 assign a new time instance to this variable called Time here. 2:27 And once you do that, you have access to different attributes of time. 2:33 We could ask time what hour it is, the minute. 2:38 You use the min method for that and we can see that these correspond. 2:42 This is the 19th hour, 17th minute. 2:46 And also, there is a second component as well. 2:49 And as you would also expect, we have month and day and year as well. 2:54 And we could down further even into microseconds, if we wanted too. 3:01 Now just like with dates, times can also be manipulated with math. 3:06 However, the gradation is a little bit different. 3:12 So here's our time instance and 3:14 if we were to add 1 to the time, it adds 1 second. 3:17 This means we have to be a little bit clever, 3:22 if we're trying to be a little bit more specific. 3:24 If we wanted to add 1 minute, we would do time + 60 and 3:27 that would give us 1 minute later than our time instance. 3:30 Now notice that the original time instance is not being manipulated. 3:35 It's not keeping up with the current ticking of time. 3:39 We're representing just one specific instance. 3:43 So if we wanted to add an hour, we would do 60 times 60. 3:46 60 seconds in a minute and 60 minutes in 1 hour and 3:51 we can see that this goes up to the hour 20, just like we expected it to. 3:55 Now since we can perform mathematical functions on time objects, 4:00 that means we can also see if time is before or after another time instance. 4:05 So here's the time instance that I took during the recording of this video and 4:10 we could compare that to see if it is less than the current time, 4:17 which it is or if the current time is greater than that time and it is. 4:23 Now, if we take a look at this, you'll notice that there are four zeros. 4:29 See, +0000 and that is going to be the timezone offset. 4:33 Time can also be converted to an array, 4:39 if we wanted to get back all of these different attributes. 4:41 So we can see it goes in reverse order second, minute, 4:46 hour, day, month, year. 4:50 And then all the way at the end, we can see what zone we're in, 4:56 which is UTC or Universal Time. 4:59 Coordinated Universal Time is the basis of time today and 5:04 that is a good standard that you can work against. 5:09 Now also, if we scroll down in the documentation here. 5:14 We can see the different format strings that we have available for 5:21 our time instances. 5:24 So just like dates, we can format times as well. 5:25 So here we go with the abbreviation for the month, day and year. 5:39 >> Try creating, setting and manipulating times on your own now, using workspaces. 5:45
You need to sign up for Treehouse in order to download course files.Sign up