Times done right: Time and Date
So far timelog computes with strings: "08:30" is taken apart by hand, and as_hours turns it into a number. That works well – until an entry runs past midnight or you want to know which weekday September 15 falls on. Ruby has two classes for this: Time for a point in time (date and time of day) and Date for a calendar day.
start = Time.new(2026, 9, 15, 8, 30)
finish = Time.new(2026, 9, 15, 12, 0)
seconds = finish - start
seconds / 3600Subtracting two points in time gives the seconds between them, as a Float; divided by 3600 that's hours. The other way round, you add seconds to a point in time – a 45-minute lunch break, for example:
back = finish + 45 * 60
back.strftime("%H:%M")strftime (“string format time”) turns a point in time into text, using placeholders: %Y year, %m month, %d day, %H hour, %M minute, %A weekday. Anything that doesn't start with % stays as it is:
puts start.strftime("%d.%m.%Y")
puts start.strftime("%Y-%m-%d %H:%M")
puts start.strftime("%A")
start.wday # the weekday as a number: Sunday = 0, Monday = 1 ...The opposite way, from text to a point in time, is Time.strptime (“string parse time”) – with the same placeholders. It comes from the library time. This is how a timelog line becomes a real duration:
require "time"
line = "2026-09-15 08:30-12:00 ProjectX"
date, times = line.split(" ")
from, to = times.split("-")
begins = Time.strptime("#{date} #{from}", "%Y-%m-%d %H:%M")
ends = Time.strptime("#{date} #{to}", "%Y-%m-%d %H:%M")
(ends - begins) / 3600For whole days without a time of day there's Date from the library date. Date computes in days directly: + 1 is tomorrow, and subtracting two dates gives the days between them (as a fraction – to_i makes a whole number of it). And you can walk through a range of dates just like a range of numbers:
require "date"
day = Date.new(2026, 9, 15)
puts day + 1
until_christmas = (Date.new(2026, 12, 24) - day).to_i
puts "#{until_christmas} days until Christmas"
week = day..(day + 6)
week.count { |d| d.saturday? || d.sunday? }More helpers for reports: Date.parse("2026-09-15") reads a date from text, day.cweek gives the calendar week, Date.today and Time.now today's date and this very moment. Those last two are awkward in tests, though: they return something different on every run. So rather hand a method the time as an argument than let it ask Time.now inside.
And a word on time zones: Time.new without one takes the computer's time zone – here in the browser that's always UTC. If a program runs on servers in different countries, you state the zone explicitly: Time.new(2026, 9, 15, 8, 30, 0, "+02:00").
duration_in_hours(date, from, to): the method turns the date and the two times of day into two Time objects and returns the hours between them. If an entry runs past midnight (to is earlier than from), it ends on the next day – then you add a day, that is 24 * 3600 seconds. duration_in_hours("2026-09-15", "22:00", "01:30") gives 3.5.require "time"
# def duration_in_hours(date, from, to)
# begins = Time.strptime(...)
# ...
# end
# duration_in_hours("2026-09-15", "22:00", "01:30")