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.



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

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.



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

def day_second
  @day_second
end

Class Method Details

.endnightObject



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

def endnight
  ENDNIGHT
end

.from_hour(hour) ⇒ Object



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

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

.from_minute(minute) ⇒ Object



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

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

.from_time(time) ⇒ Object



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

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

.from_timestamp(timestamp) ⇒ Object



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

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



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

def midnight
  MIDNIGHT
end

Instance Method Details

#day_minuteObject



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

def day_minute
  hour * Time.hour_minutes + minute
end

#for_dstObject



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

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

#hourObject



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

def hour
  day_second / Time.hour_seconds
end

#minuteObject



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

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

#secondObject



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

def second
  day_second % Time.minute_seconds
end

#timestampObject



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

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