Class: Teasy::FloatingTime

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/teasy/floating_time.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month = nil, day = nil, hour = nil, minute = nil, second = nil, usec_with_frac = nil) ⇒ FloatingTime

rubocop:disable Metrics/ParameterLists



17
18
19
20
# File 'lib/teasy/floating_time.rb', line 17

def initialize(year, month = nil, day = nil,
               hour = nil, minute = nil, second = nil, usec_with_frac = nil)
  @time = Time.utc(year, month, day, hour, minute, second, usec_with_frac)
end

Class Method Details

.from_time(time) ⇒ Object

rubocop:enable Metrics/ParameterLists



23
24
25
26
# File 'lib/teasy/floating_time.rb', line 23

def self.from_time(time)
  new(time.year, time.mon, time.day,
      time.hour, time.min, time.sec, time.nsec / 1_000.0)
end

Instance Method Details

#+(other) ⇒ Object



73
74
75
# File 'lib/teasy/floating_time.rb', line 73

def +(other)
  FloatingTime.from_time(time + other)
end

#-(other) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/teasy/floating_time.rb', line 77

def -(other)
  if other.is_a? Numeric
    FloatingTime.from_time(time - other)
  elsif other.respond_to? :to_time
    to_time - other.to_time
  else
    raise TypeError, "#{other.class} can't be coerced into FloatingTime"
  end
end

#<=>(other) ⇒ Object



58
59
60
61
62
63
# File 'lib/teasy/floating_time.rb', line 58

def <=>(other)
  return nil unless other.respond_to?(:to_time) &&
                    other.respond_to?(:utc_offset)

  to_time - other.utc_offset <=> other.to_time.utc
end

#asctimeObject Also known as: ctime



52
53
54
# File 'lib/teasy/floating_time.rb', line 52

def asctime
  strftime('%a %b %e %T %Y')
end

#eql?(other) ⇒ Boolean



65
66
67
# File 'lib/teasy/floating_time.rb', line 65

def eql?(other)
  hash == other.hash
end

#hashObject



69
70
71
# File 'lib/teasy/floating_time.rb', line 69

def hash
  (to_a << self.class).hash
end

#in_time_zone(zone = Teasy.default_zone) ⇒ Object



28
29
30
# File 'lib/teasy/floating_time.rb', line 28

def in_time_zone(zone = Teasy.default_zone)
  Teasy.with_zone(zone) { TimeWithZone.from_time(self) }
end

#inspectObject Also known as: to_s



41
42
43
# File 'lib/teasy/floating_time.rb', line 41

def inspect
  strftime('%Y-%m-%d %H:%M:%S')
end

#round(*args) ⇒ Object



37
38
39
# File 'lib/teasy/floating_time.rb', line 37

def round(*args)
  dup.round!(*args)
end

#round!(*args) ⇒ Object



32
33
34
35
# File 'lib/teasy/floating_time.rb', line 32

def round!(*args)
  @time = time.round(*args)
  self
end

#strftime(format) ⇒ Object



47
48
49
50
# File 'lib/teasy/floating_time.rb', line 47

def strftime(format)
  format = prefix_zone_info(format) if includes_zone_directive?(format)
  time.strftime(format)
end

#to_aObject



87
88
89
# File 'lib/teasy/floating_time.rb', line 87

def to_a
  time.to_a[0..7]
end

#to_timeObject Also known as: utc



91
92
93
# File 'lib/teasy/floating_time.rb', line 91

def to_time
  time.dup
end

#utc_offsetObject



97
98
99
# File 'lib/teasy/floating_time.rb', line 97

def utc_offset
  0
end