What it you need a
time without an
associated day?
Slide 41
Slide 41 text
Other Kinds of Time
Moment
November 14th, 2023 at 11:30AM PST
Duration
45 minutes
Time of Day
5:30 PM
Slide 42
Slide 42 text
Time of Day
Points on a circular timeline
Slide 43
Slide 43 text
How does ToD interact
with other types?
Slide 44
Slide 44 text
Time of Day
Our 3 questions still work!
Slide 45
Slide 45 text
Extract from a Time
Slide 46
Slide 46 text
Combine ToD with
Date to get a Time
Slide 47
Slide 47 text
Third Party Solutions
GEMS!
Slide 48
Slide 48 text
DURATION
ActiveSupport
third party gems
Slide 49
Slide 49 text
DURATION
1.day
Slide 50
Slide 50 text
POINT IN TIME
1.day.ago
Slide 51
Slide 51 text
Sugar
Time.now - (1 * SECONDS_PER_DAY)
Slide 52
Slide 52 text
TIME OF DAY
jackc/tod
third party gems
Slide 53
Slide 53 text
How to Implement Time?
Slide 54
Slide 54 text
Warning
Slide 55
Slide 55 text
Avoid human-readable integers like 1030
Slide 56
Slide 56 text
Invalid times like 3176
Slide 57
Slide 57 text
Breaks Math (1025 - 0925 = 100)
Slide 58
Slide 58 text
Avoid multi-part values
like {hour: 10, minute: 30}
Slide 59
Slide 59 text
Epoch + Counter
Slide 60
Slide 60 text
Epoch: when is 0?
Slide 61
Slide 61 text
Counter: how many
since 0?
Slide 62
Slide 62 text
Common Implementations
UNIX Time
milliseconds since Jan 1, 1970
Postgres Time of Day
microseconds since midnight
Ruby Date
days since Jan 1, 4713 BCE
Slide 63
Slide 63 text
Just a big counter
class TimeOfDay
def initialize(microseconds_since_midnight)
@counter =
(microseconconds_since_midnight % MS_PER_DAY)
end
end
Slide 64
Slide 64 text
Custom constructor
class TimeOfDay
def self.from_parts(hours:, minutes:, seconds:)
total =
(hours * MICROSECONDS_PER_HOUR) +
(minutes * MICROSECONDS_PER_MINUTE) +
(seconds * MICROSECONDS_PER_SECOND)
new(total)
end
end
Slide 65
Slide 65 text
Operators
class TimeOfDay
def +(duration_microseconds)
self.class.new(@counter + duration_microseconds)
end
end
Slide 66
Slide 66 text
Case Study
Slide 67
Slide 67 text
No content
Slide 68
Slide 68 text
When working with time
Think About
What kind of time you need
moment, duration, tod, etc
What operations make sense
(and which ones don't!)
both within and across types
What resolution you need
e.g. Time vs Date class
Slide 69
Slide 69 text
Joël Quenneville
Joël Quenneville
Principal Developer
thoughtbot
@joelquen
bikeshed.fm
co-host