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, format = nil) ⇒ 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, format = nil) ⇒ 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 83 84 85 86 87 88 89 90 |
# File 'lib/barometer/data/local_datetime.rb', line 55 def self.parse(string, format=nil) 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) begin datetime = if format Time.strptime(string, format) else DateTime.parse(string) end year = datetime.year month = datetime.mon day = datetime.day hour = datetime.hour min = datetime.min sec = datetime.sec rescue ArgumentError return nil end end Data::LocalDateTime.new(year, month, day, hour, min, sec) end |
Instance Method Details
#<=>(other) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/barometer/data/local_datetime.rb', line 104 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)) || 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
140 141 142 |
# File 'lib/barometer/data/local_datetime.rb', line 140 def _total_days self.to_d.yday + (@year * 366) end |
#nil? ⇒ Boolean
135 |
# File 'lib/barometer/data/local_datetime.rb', line 135 def nil?; @year == 0 && @month == 0 && @day == 0 && super; end |
#parse(string, format = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/barometer/data/local_datetime.rb', line 43 def parse(string, format=nil) return unless string new_date = Data::LocalDateTime.parse(string, format) @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
94 95 96 |
# File 'lib/barometer/data/local_datetime.rb', line 94 def to_d Date.civil(@year, @month, @day) end |
#to_dt ⇒ Object
convert to a DateTime class
100 101 102 |
# File 'lib/barometer/data/local_datetime.rb', line 100 def to_dt DateTime.new(@year, @month, @day, @hour, @min, @sec) end |
#to_s(time = false) ⇒ Object
129 130 131 132 133 |
# File 'lib/barometer/data/local_datetime.rb', line 129 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 |