Class: Redistat::Date
- Inherits:
-
Object
- Object
- Redistat::Date
- Defined in:
- lib/redistat/date.rb
Constant Summary collapse
- DEPTHS =
[:year, :month, :day, :hour, :min, :sec, :usec]
Instance Attribute Summary collapse
-
#day ⇒ Object
Returns the value of attribute day.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#hour ⇒ Object
Returns the value of attribute hour.
-
#min ⇒ Object
Returns the value of attribute min.
-
#month ⇒ Object
Returns the value of attribute month.
-
#sec ⇒ Object
Returns the value of attribute sec.
-
#usec ⇒ Object
Returns the value of attribute usec.
-
#year ⇒ Object
Returns the value of attribute year.
Instance Method Summary collapse
-
#initialize(input, depth = nil) ⇒ Date
constructor
A new instance of Date.
- #to_d ⇒ Object (also: #to_date)
- #to_i ⇒ Object (also: #to_integer)
- #to_s(depth = nil) ⇒ Object (also: #to_string)
- #to_t ⇒ Object (also: #to_time)
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
#day ⇒ Object
Returns the value of attribute day.
6 7 8 |
# File 'lib/redistat/date.rb', line 6 def day @day end |
#depth ⇒ Object
Returns the value of attribute depth.
11 12 13 |
# File 'lib/redistat/date.rb', line 11 def depth @depth end |
#hour ⇒ Object
Returns the value of attribute hour.
7 8 9 |
# File 'lib/redistat/date.rb', line 7 def hour @hour end |
#min ⇒ Object
Returns the value of attribute min.
8 9 10 |
# File 'lib/redistat/date.rb', line 8 def min @min end |
#month ⇒ Object
Returns the value of attribute month.
5 6 7 |
# File 'lib/redistat/date.rb', line 5 def month @month end |
#sec ⇒ Object
Returns the value of attribute sec.
9 10 11 |
# File 'lib/redistat/date.rb', line 9 def sec @sec end |
#usec ⇒ Object
Returns the value of attribute usec.
10 11 12 |
# File 'lib/redistat/date.rb', line 10 def usec @usec end |
#year ⇒ Object
Returns the value of attribute year.
4 5 6 |
# File 'lib/redistat/date.rb', line 4 def year @year end |
Instance Method Details
#to_d ⇒ Object 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_i ⇒ Object 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_t ⇒ Object 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 |