Class: Data::LocalDateTime
- Inherits:
-
LocalTime
- Object
- LocalTime
- Data::LocalDateTime
- Defined in:
- lib/barometer/data/local_datetime.rb
Overview
A simple DateTime class
A time class that represents the local date_time … it has no concept of time zone
Instance Attribute Summary collapse
-
#day ⇒ Object
Returns the value of attribute day.
-
#month ⇒ Object
Returns the value of attribute month.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#_total_days ⇒ Object
this assumes all years have 366 days (which only is true for leap years) but since this is just for comparisons, this will be accurate.
-
#initialize(y, mon, d, h = 0, m = 0, s = 0) ⇒ LocalDateTime
constructor
A new instance of LocalDateTime.
- #nil? ⇒ Boolean
- #parse(string) ⇒ Object
-
#to_d ⇒ Object
convert to a Date class.
-
#to_dt ⇒ Object
convert to a DateTime class.
- #to_s(time = false) ⇒ Object
Constructor Details
#initialize(y, mon, d, h = 0, m = 0, s = 0) ⇒ LocalDateTime
Returns a new instance of LocalDateTime.
13 14 15 16 17 18 19 20 |
# File 'lib/barometer/data/local_datetime.rb', line 13 def initialize(y,mon,d,h=0,m=0,s=0) raise(ArgumentError, "invalid date") unless y && mon && d && Date.civil(y,mon,d) @year = y @month = mon @day = d super(h,m,s) self end |
Instance Attribute Details
#day ⇒ Object
Returns the value of attribute day.
11 12 13 |
# File 'lib/barometer/data/local_datetime.rb', line 11 def day @day end |
#month ⇒ Object
Returns the value of attribute month.
11 12 13 |
# File 'lib/barometer/data/local_datetime.rb', line 11 def month @month end |
#year ⇒ Object
Returns the value of attribute year.
11 12 13 |
# File 'lib/barometer/data/local_datetime.rb', line 11 def year @year end |
Class Method Details
.parse(string) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/barometer/data/local_datetime.rb', line 55 def self.parse(string) return nil unless string return string if string.is_a?(Data::LocalDateTime) year = nil; month = nil; day = nil; hour = nil; min = nil; sec = nil; if string.is_a?(Time) || string.is_a?(DateTime) year = string.year month = string.mon day = string.day hour = string.hour min = string.min sec = string.sec elsif string.is_a?(Date) year = string.year month = string.mon day = string.day elsif string.is_a?(String) datetime = Time.parse(string) year = datetime.year month = datetime.mon day = datetime.day hour = datetime.hour min = datetime.min sec = datetime.sec end Data::LocalDateTime.new(year, month, day, hour, min, sec) end |
Instance Method Details
#<=>(other) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/barometer/data/local_datetime.rb', line 96 def <=>(other) if other.is_a?(String) || other.is_a?(Time) || other.is_a?(DateTime) || other.is_a?(Date) the_other = Data::LocalDateTime.parse(other) else the_other = other end raise ArgumentError unless the_other.is_a?(Data::LocalDateTime) || the_other.is_a?(Data::LocalTime) if (other.is_a?(String) || other.is_a?(Time) || other.is_a?(DateTime)) && the_other.is_a?(Data::LocalDateTime) # we are counting days + seconds if (_total_days <=> the_other._total_days) == 0 return total_seconds <=> the_other.total_seconds else return _total_days <=> the_other._total_days end elsif other.is_a?(Date) && the_other.is_a?(Data::LocalDateTime) # we are counting days return _total_days <=> the_other._total_days elsif the_other.is_a?(Data::LocalTime) # we are counting seconds return total_seconds <=> the_other.total_seconds end end |
#_total_days ⇒ Object
this assumes all years have 366 days (which only is true for leap years) but since this is just for comparisons, this will be accurate
132 133 134 |
# File 'lib/barometer/data/local_datetime.rb', line 132 def _total_days self.to_d.yday + (@year * 366) end |
#nil? ⇒ Boolean
127 |
# File 'lib/barometer/data/local_datetime.rb', line 127 def nil?; @year == 0 && @month == 0 && @day == 0 && super; end |
#parse(string) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/barometer/data/local_datetime.rb', line 43 def parse(string) return unless string new_date = Data::LocalDateTime.parse(string) @year = new_date.year @month = new_date.month @day = new_date.day @hour = new_date.hour @min = new_date.min @sec = new_date.sec self end |
#to_d ⇒ Object
convert to a Date class
86 87 88 |
# File 'lib/barometer/data/local_datetime.rb', line 86 def to_d Date.civil(@year, @month, @day) end |
#to_dt ⇒ Object
convert to a DateTime class
92 93 94 |
# File 'lib/barometer/data/local_datetime.rb', line 92 def to_dt DateTime.new(@year, @month, @day, @hour, @min, @sec) end |
#to_s(time = false) ⇒ Object
121 122 123 124 125 |
# File 'lib/barometer/data/local_datetime.rb', line 121 def to_s(time=false) datetime = self.to_dt format = (time ? "%Y-%m-%d %I:%M:%S %p" : "%Y-%m-%d") datetime.strftime(format).downcase end |