class Teasy::FloatingTime

Attributes

time[R]

Public Class Methods

from_time(time) click to toggle source

rubocop:enable Metrics/ParameterLists

# 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
new(year, month = nil, day = nil, hour = nil, minute = nil, second = nil, usec_with_frac = nil) click to toggle source

rubocop:disable Metrics/ParameterLists

# 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

Public Instance Methods

+(other) click to toggle source
# File lib/teasy/floating_time.rb, line 73
def +(other)
  FloatingTime.from_time(time + other)
end
-(other) click to toggle source
# 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) click to toggle source
# 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
asctime() click to toggle source
# File lib/teasy/floating_time.rb, line 52
def asctime
  strftime('%a %b %e %T %Y')
end
Also aliased as: ctime
ctime()
Alias for: asctime
eql?(other) click to toggle source
# File lib/teasy/floating_time.rb, line 65
def eql?(other)
  hash == other.hash
end
hash() click to toggle source
# File lib/teasy/floating_time.rb, line 69
def hash
  (to_a << self.class).hash
end
in_time_zone(zone = Teasy.default_zone) click to toggle source
# 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
inspect() click to toggle source
# File lib/teasy/floating_time.rb, line 41
def inspect
  strftime('%Y-%m-%d %H:%M:%S')
end
Also aliased as: to_s
round(*args) click to toggle source
# File lib/teasy/floating_time.rb, line 37
def round(*args)
  dup.round!(*args)
end
round!(*args) click to toggle source
# File lib/teasy/floating_time.rb, line 32
def round!(*args)
  @time = time.round(*args)
  self
end
strftime(format) click to toggle source
# 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_a() click to toggle source
# File lib/teasy/floating_time.rb, line 87
def to_a
  time.to_a[0..7]
end
to_s()
Alias for: inspect
to_time() click to toggle source
# File lib/teasy/floating_time.rb, line 91
def to_time
  time.dup
end
Also aliased as: utc
utc()
Alias for: to_time
utc_offset() click to toggle source
# File lib/teasy/floating_time.rb, line 97
def utc_offset
  0
end

Private Instance Methods

includes_zone_directive?(format) click to toggle source
# File lib/teasy/floating_time.rb, line 111
def includes_zone_directive?(format)
  zone_directives_matcher =~ format
end
prefix_zone_info(format) click to toggle source
# File lib/teasy/floating_time.rb, line 115
def prefix_zone_info(format)
  # prefixes zone directives with a % s.t. they are ignored in strftime
  format.gsub(zone_directives_matcher) { |m| "%#{m}" }
end
zone_directives_matcher() click to toggle source
# File lib/teasy/floating_time.rb, line 103
def zone_directives_matcher
  @zone_directives_matcher ||= Regexp.union(
    /(?<!%)%Z/, /(?<!%)%z/, /(?<!%)%:z/, /(?<!%)%::z/
  )
end