Ruby's Time class represents moments that can be created, queried, converted, calculated, and formatted. Choose a constructor according to whether you need the current time, epoch-based seconds, local calendar fields, or a parsed string. section 8.4.1 section 8.5
Create Time objects with several practical constructors:
- Time.new or Time.now returns the current time. section 8.4.1
- Time.at(seconds) represents seconds since the Unix epoch. section 8.4.1
- Time.mktime, also called Time.local, accepts calendar and clock fields such as year, month, day, hour, minute, and second. section 8.4.1
Inspecting a moment
A Time object exposes its year, month, day, hour, minute, and second through query methods. For example, t.month and t.sec retrieve components from Time.now; dst? reports daylight-saving-time status, while weekday predicates such as sunday? answer calendar questions. section 8.4.2
Time arithmetic adds or subtracts seconds and returns a new Time object, leaving the original moment available. Time can also convert to Date or DateTime; when the target carries more information, missing fields become zero. Date/time values are commonly formatted with methods such as strftime rather than relying on default string output. section 8.4.4 section 8.4.2 section 8.5
