Time zones and durations

Time with an offset

A Time stores an instant and an offset. The offset is part of the value, not a property of the machine, so a time written with +03:00 prints with that offset until it is converted:

ruby -e 'require "time"; puts Time.new(2026, 11, 5, 12, 0, 0, "+03:00").utc.iso8601'

2026-11-05T09:00:00Z

ruby -e 't = Time.new(2026, 11, 5, 12, 0, 0, "+03:00"); p t.utc_offset; p t.utc; p t.getlocal("-05:00")'

10800 2026-11-05 09:00:00 UTC 2026-11-05 04:00:00 -0500

utc_offset is in seconds, utc returns a copy in UTC, and getlocal returns a copy in the offset given. The instant never changes; only the clock face does.

utc and localtime

Store and exchange UTC; convert for a reader. Time.now is in the machine's zone, and .utc is the same instant in the zone a log wants. The example fixes the instant, so the line is the same on every run:

ruby -e 'require "time"; puts Time.new(2026, 11, 5, 0, 27, 39, "+00:00").utc.iso8601'

2026-11-05T00:27:39Z

iso8601 from the time standard library is the format to write: it sorts lexically, it carries the offset, and every language parses it. A timestamp without an offset is ambiguous, and an ambiguous timestamp in a shared log is a bug waiting for the first traveller.

Durations with the monotonic clock

A duration is a different question from a date. For "how long did this take" the right clock is the monotonic one, which never jumps when the system time is adjusted:

ruby -e 'start = Process.clock_gettime(Process::CLOCK_MONOTONIC); sleep 0.2; p (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).round(3)'

0.2

Process.clock_gettime returns seconds as a float. Subtract two readings for a duration, and use Time.now only when the answer is a date or a timestamp someone reads.

Adding and subtracting time

Adding a number to a Time adds seconds, and subtracting two times gives seconds:

ruby -e 'require "time"; t = Time.new(2026, 11, 5, 12, 0, 0, "+00:00"); p (t + 90).strftime("%H:%M:%S"); p ((t + 90) - t)'

"12:01:30"
90.0

Seconds are not days. A day is 86,400 seconds except twice a year in a zone with daylight saving, when it is 82,800 or 90,000. For calendar arithmetic use Date and its >> operator, which the next lesson covers.

What to watch for

Time.now in a test makes the result depend on the clock. Pass the time in, or freeze it with a helper, so a failure is reproducible.

A duration measured with Time.now can be negative when the clock is corrected. The monotonic clock cannot go backwards, which is why every timing helper uses it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? require "time" puts Time.new(2026, 11, 5, 12, 0, 0, "+03:00").utc.iso8601

    Answer

    2026-11-05T09:00:00Z

    The instant is noon at +03:00, which is 09:00 UTC, and iso8601 writes it with the Z suffix.

  2. What is t.utc_offset for a time created with the offset "+03:00"?

    Answer

    10800

    The offset is in seconds: three hours is 10,800 seconds.

  3. What does getlocal("-05:00") return?

    Answer

    The same instant shown in the -05:00 zone

    The instant never changes; only the clock face does.

  4. What does this program print? start = Process.clock_gettime(Process::CLOCK_MONOTONIC) sleep 0.2 p (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).round(3)

    Answer

    0.2

    The monotonic clock answers seconds as a float, and the difference rounds to 0.2.

  5. Which clock should measure how long a step took?

    Answer

    The monotonic clock

    A monotonic clock never jumps when the system time is adjusted.

  6. What does this program print? require "time" t = Time.new(2026, 11, 5, 12, 0, 0, "+00:00") p (t + 90).strftime("%H:%M:%S") p ((t + 90) - t)

    Answer

    "12:01:30" / 90.0

    Adding a number adds seconds, and subtracting two times gives seconds as a float.

  7. Why is a day not always 86,400 seconds?

    Answer

    Because of daylight saving, a day in a zone can be 82,800 or 90,000 seconds

    Calendar arithmetic belongs to Date, which the next lesson covers.

  8. What does iso8601 give a timestamp that a plain print does not?

    Answer

    The offset, in a form every language parses and that sorts lexically

    A timestamp without an offset is ambiguous in a shared log.

  9. What does this program print? require "time" p Time.at(0).utc.iso8601

    Answer

    "1970-01-01T00:00:00Z"

    Time.at(0) is the epoch, utc converts it, and p prints the string with quotes.

  10. A duration measured with Time.now comes out negative. Why?

    Answer

    The clock was adjusted between the two readings, which a monotonic clock cannot do

    That is why every timing helper uses the monotonic clock.

Article author: Arthur Isaev