Class: Redistat::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/redistat/date.rb

Constant Summary collapse

DEPTHS =
[:year, :month, :day, :hour, :min, :sec, :usec]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, depth = nil) ⇒ Date

Returns a new instance of Date.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redistat/date.rb', line 15

def initialize(input, depth = nil)
  @depth = depth
  if input.is_a?(::Time)
    from_time(input)
  elsif input.is_a?(::Date)
    from_date(input)
  elsif input.is_a?(::String)
    from_string(input)
  elsif input.is_a?(::Fixnum)
    from_integer(input)
  elsif input.is_a?(::Bignum)
    from_integer(input)
  end
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



6
7
8
# File 'lib/redistat/date.rb', line 6

def day
  @day
end

#depthObject

Returns the value of attribute depth.



11
12
13
# File 'lib/redistat/date.rb', line 11

def depth
  @depth
end

#hourObject

Returns the value of attribute hour.



7
8
9
# File 'lib/redistat/date.rb', line 7

def hour
  @hour
end

#minObject

Returns the value of attribute min.



8
9
10
# File 'lib/redistat/date.rb', line 8

def min
  @min
end

#monthObject

Returns the value of attribute month.



5
6
7
# File 'lib/redistat/date.rb', line 5

def month
  @month
end

#secObject

Returns the value of attribute sec.



9
10
11
# File 'lib/redistat/date.rb', line 9

def sec
  @sec
end

#usecObject

Returns the value of attribute usec.



10
11
12
# File 'lib/redistat/date.rb', line 10

def usec
  @usec
end

#yearObject

Returns the value of attribute year.



4
5
6
# File 'lib/redistat/date.rb', line 4

def year
  @year
end

Instance Method Details

#to_dObject Also known as: to_date



35
36
37
# File 'lib/redistat/date.rb', line 35

def to_d
  ::Date.civil(@year, @month, @day)
end

#to_iObject Also known as: to_integer



40
41
42
# File 'lib/redistat/date.rb', line 40

def to_i
  to_time.to_i
end

#to_s(depth = nil) ⇒ Object Also known as: to_string



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/redistat/date.rb', line 45

def to_s(depth = nil)
  depth ||= @depth ||= :sec
  output = ""
  DEPTHS.each_with_index do |current, i|
    break if self.send(current).nil?
    if current != :usec
      output << self.send(current).to_s.rjust((i <= 0) ? 4 : 2, '0')
    else
      output << "." + self.send(current).to_s.rjust(6, '0')
    end
    break if current == depth
  end
  output
end

#to_tObject Also known as: to_time



30
31
32
# File 'lib/redistat/date.rb', line 30

def to_t
  ::Time.local(@year, @month, @day, @hour, @min, @sec, @usec)
end