Recipe 3.1. Finding Today's Date
Problem
You need to create an object that represents the current date and time, or a time in the future or past.
Solution
The factory method
Time.now creates a Time object containing the current lo...
Recipe 3.2. Parsing Dates, Precisely or Fuzzily
Problem
You want to transform a string describing a date or date/time into a Date object. You might not know the format of the string ahead of time.
Solution
The best solution is to pas...
Recipe 3.3. Printing a Date
Problem
You want to print a date object as a string.
Solution
If you just want to look at a date, you can call Time#to_s or Date#to_s and not bother with fancy formatting:
require 'date'
Time.now.to_s ...
Recipe 3.4. Iterating Over Dates
Problem
Given a point in time, you want to get somewhere else.
Solution
All of Ruby's time objects can be used in ranges as though they were numbers. Date
and
DateTime objects iterate in increme...
Recipe 3.5. Doing Date Arithmetic
Problem
You want to find how much time has elapsed between two
dates, or add a number to a date to get an earlier or later date.
Solution
Adding or subtracting a Time object and a number adds...
Recipe 3.6. Counting the Days Since an Arbitrary Date
Problem
You want to see how many days have elapsed since a particular date, or how many remain until a date in the future.
Solution
Subtract the earlier date from the later one. If you...
Recipe 3.7. Converting Between Time Zones
Problem
You want to change a time object so that it represents the same moment of time in some other time zone.
Solution
The most common time zone conversions are the conversion of syste...
Recipe 3.8. Checking Whether Daylight Saving Time Is in Effect
Problem
You want to see whether the current time in your locale is normal time or Daylight Saving/Summer Time.
Solution
Create a Time object and check its
isdst method...
Recipe 3.9. Converting Between Time and DateTime Objects
Problem
You're working with both DateTime and Time objects, created from Ruby's two standard date/time libraries. You can't mix these objects in comparisons, iterations, or date arit...
Recipe 3.10. Finding the Day of the Week
Problem
You want to find the day of the week for a certain date.
Solution
Use the
wday method (supported by both Time and DateTime) to find the day of the week as a number between 0 and 6. ...
Recipe 3.11. Handling Commercial Dates
Problem
When writing a business or financial application, you need to deal with commercial dates instead of civil or calendar dates.
Solution
DateTime offers some methods for working with commer...
Recipe 3.12. Running a Code Block Periodically
Problem
You want to run some Ruby code (such as a call to a shell command) repeatedly at a certain interval.
Solution
Create a method that runs a code block, then sleeps until it's time ...
Recipe 3.13. Waiting a Certain Amount of Time
Problem
You want to pause your program, or a single thread of it, for a specific amount of time.
Solution
The
Kernel#sleep method takes a floating-point number and puts the current thread t...
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.