Class: Biz::DayTime

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/biz/day_time.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(day_second) ⇒ DayTime

Returns a new instance of DayTime.



54
55
56
57
58
# File 'lib/biz/day_time.rb', line 54

def initialize(day_second)
  @day_second = Integer(day_second)

  fail ArgumentError, 'second not within a day' unless valid_second?
end

Instance Attribute Details

#day_secondObject (readonly)

Returns the value of attribute day_second.



60
61
62
# File 'lib/biz/day_time.rb', line 60

def day_second
  @day_second
end

Class Method Details

.endnightObject



48
49
50
# File 'lib/biz/day_time.rb', line 48

def endnight
  ENDNIGHT
end

.from_hour(hour) ⇒ Object



30
31
32
# File 'lib/biz/day_time.rb', line 30

def from_hour(hour)
  new(hour * Time.hour_seconds)
end

.from_minute(minute) ⇒ Object



26
27
28
# File 'lib/biz/day_time.rb', line 26

def from_minute(minute)
  new(minute * Time.minute_seconds)
end

.from_time(time) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/biz/day_time.rb', line 18

def from_time(time)
  new(
    time.hour * Time.hour_seconds +
      time.min * Time.minute_seconds +
      time.sec
  )
end

.from_timestamp(timestamp) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/biz/day_time.rb', line 34

def from_timestamp(timestamp)
  timestamp.match(Timestamp::PATTERN) { |match|
    new(
      match[:hour].to_i * Time.hour_seconds +
        match[:minute].to_i * Time.minute_seconds +
        match[:second].to_i
    )
  }
end

.midnightObject



44
45
46
# File 'lib/biz/day_time.rb', line 44

def midnight
  MIDNIGHT
end

Instance Method Details

#day_minuteObject



74
75
76
# File 'lib/biz/day_time.rb', line 74

def day_minute
  hour * Time.hour_minutes + minute
end

#for_dstObject



78
79
80
# File 'lib/biz/day_time.rb', line 78

def for_dst
  self.class.new((day_second + Time.hour_seconds) % Time.day_seconds)
end

#hourObject



62
63
64
# File 'lib/biz/day_time.rb', line 62

def hour
  day_second / Time.hour_seconds
end

#minuteObject



66
67
68
# File 'lib/biz/day_time.rb', line 66

def minute
  day_second % Time.hour_seconds / Time.minute_seconds
end

#secondObject



70
71
72
# File 'lib/biz/day_time.rb', line 70

def second
  day_second % Time.minute_seconds
end

#timestampObject



82
83
84
# File 'lib/biz/day_time.rb', line 82

def timestamp
  format(Timestamp::FORMAT, hour, minute)
end