0Pricing
Ruby Academy · Lesson

Time Zones

Handling zones.

Time Zones is a free Ruby Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Time Zones Matter

The same instant looks different depending on location. Mishandling zones causes off-by-hours bugs, so you must be explicit about which zone a time uses.

t = Time.new(2026, 5, 30, 12, 0, 0, '+03:00')
puts(t.zone || t.utc_offset)
puts(t.utc_offset)

UTC: The Reference Zone

UTC (Coordinated Universal Time) is the global reference. Store and compute in UTC, then convert for display.

t = Time.utc(2026, 5, 30, 9, 0, 0)
puts(t.utc?)
puts(t.hour)

Local Time

Time.now and Time.local use the machine's local zone. The offset from UTC is given by utc_offset in seconds.

t = Time.new(2026, 5, 30, 12, 0, 0, '+03:00')
puts(t.utc_offset)
puts(t.utc_offset / 3600)

Converting to UTC

getutc (alias utc) returns the same instant expressed in UTC. The wall-clock changes but the moment is identical.

t = Time.new(2026, 5, 30, 12, 0, 0, '+03:00')
puts(t.hour)
puts(t.getutc.hour)

Converting to a Local Offset

getlocal accepts an explicit offset string and returns the instant in that zone.

t = Time.utc(2026, 5, 30, 9, 0, 0)
istanbul = t.getlocal('+03:00')
puts(istanbul.hour)

Specifying Offset on Creation

Pass an offset string as the last argument to Time.new to fix the zone at creation.

ny = Time.new(2026, 5, 30, 8, 0, 0, '-04:00')
puts(ny.utc_offset / 3600)
puts(ny.getutc.hour)

Same Instant, Different Clocks

Two times in different zones can represent the exact same instant. Comparison uses the underlying moment, so they are equal.

a = Time.new(2026, 5, 30, 12, 0, 0, '+03:00')
b = Time.new(2026, 5, 30, 9, 0, 0, '+00:00')
puts(a == b)

Offsets in ISO 8601

ISO 8601 timestamps embed the offset (for example +03:00 or Z for UTC), making them unambiguous across systems.

require 'time'
t = Time.new(2026, 5, 30, 12, 0, 0, '+03:00')
puts(t.iso8601)
puts(t.getutc.iso8601)

Parsing Zoned Strings

Time.parse respects an offset present in the input string.

require 'time'
t = Time.parse('2026-05-30T12:00:00+03:00')
puts(t.utc_offset / 3600)
puts(t.getutc.hour)

Best Practice: Store UTC

The safest pattern is: store UTC, do arithmetic in UTC, and convert to the user's zone only at display time. This avoids ambiguity entirely.

stored = Time.utc(2026, 5, 30, 9, 0, 0)
display = stored.getlocal('+03:00')
puts("UTC #{stored.hour} -> Local #{display.hour}")

DST Caution

A fixed offset like +03:00 ignores daylight saving time. For named zones with DST rules, libraries such as the tzinfo gem are needed; the core Time class only knows fixed offsets.

t = Time.new(2026, 7, 1, 12, 0, 0, '+02:00')
puts(t.utc_offset / 3600)

Quick Check

What is the recommended strategy for handling time zones in an application?

Recap: Time Zones

You learned to handle zones correctly:

  • UTC is the global reference
  • getutc and getlocal convert without changing the instant
  • Offsets are set at creation or in ISO 8601 strings
  • Same instant in different zones compares equal
  • Store UTC, display local; fixed offsets ignore DST
require 'time'
puts(Time.utc(2026,5,30,9).getlocal('+03:00').hour)

Frequently asked questions

Is the “Time Zones” lesson free?

Yes — the full text of “Time Zones” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “Time Zones”?

Handling zones. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Time Zones” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Time and Date Classes
  2. Parsing and Formatting
  3. Time Arithmetic
  4. Time Zones
← Back to Ruby Academy