Class: DarianCalendar::Date
- Inherits:
-
Object
- Object
- DarianCalendar::Date
- Includes:
- Comparable
- Defined in:
- lib/darian_calendar/date.rb
Overview
The date is a particular day of a Darian calendar year
Direct Known Subclasses
Instance Attribute Summary collapse
-
#calendar_type ⇒ String
readonly
Name of the calendar type ("Martiana").
-
#month ⇒ Integer
readonly
Month of the year.
-
#month_name ⇒ String
readonly
Full month name ("Mithuna").
-
#month_of_season ⇒ Integer
readonly
Month of the season.
-
#season ⇒ Integer
readonly
Season of the year.
-
#sol ⇒ Integer
(also: #day)
readonly
Sol of the month.
-
#sol_of_season ⇒ Integer
readonly
Sol of the season.
-
#sol_of_year ⇒ Integer
readonly
Sol of the year.
-
#total_sols ⇒ Integer
readonly
Number of sols since the earth date 0-0-0.
-
#week_sol ⇒ String
(also: #week_day)
readonly
Sol of the week.
-
#week_sol_name ⇒ String
readonly
Full weeksol name ("Sol Jovis").
-
#year ⇒ Integer
readonly
Year.
Class Method Summary collapse
-
.by_digits(year = nil, month = 1, sol = 1, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Creates a date object by year, month and sol.
-
.from_earth(earth_date, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Converts a date object to a mars date object.
-
.from_json(string) ⇒ DarianCalendar::Date
Sets the model attributes from a JSON string.
-
.parse_earth(string, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Parses the given representation of a date, and converts it to a mars date.
-
.today(type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Creates a date object denoting the present mars day.
Instance Method Summary collapse
-
#<=>(another) ⇒ Integer
Compares two dates and returns -1, zero, 1 or nil.
-
#as_json ⇒ Object
Returns a hash representing the model.
-
#initialize(sols = nil, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
constructor
Converts a number of martian sols to mars date.
-
#leap? ⇒ Boolean
Returns true if the given year is a leap year.
-
#sols_in_year ⇒ Integer
Return the number of sols in the given year.
-
#to_earth ⇒ Date
Converts the given mars date to earth date.
-
#to_json ⇒ Object
Returns a JSON string representing the model.
-
#to_s ⇒ String
Returns a string in an ISO 8601 format (This method doesn’t use the expanded representations).
Constructor Details
#initialize(sols = nil, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Converts a number of martian sols to mars date.
230 231 232 233 |
# File 'lib/darian_calendar/date.rb', line 230 def initialize(sols=nil, type=DarianCalendar::CalendarTypes::MARTIANA) total_sols = sols.to_f != 0 ? sols.to_f : DarianCalendar.sols_from_earth(::Date.today) self.set_attributes(total_sols.floor, type) end |
Instance Attribute Details
#calendar_type ⇒ String (readonly)
Returns name of the calendar type ("Martiana").
32 33 34 |
# File 'lib/darian_calendar/date.rb', line 32 def calendar_type @calendar_type end |
#month ⇒ Integer (readonly)
Returns month of the year.
12 13 14 |
# File 'lib/darian_calendar/date.rb', line 12 def month @month end |
#month_name ⇒ String (readonly)
Returns full month name ("Mithuna").
16 17 18 |
# File 'lib/darian_calendar/date.rb', line 16 def month_name @month_name end |
#month_of_season ⇒ Integer (readonly)
Returns month of the season.
28 29 30 |
# File 'lib/darian_calendar/date.rb', line 28 def month_of_season @month_of_season end |
#season ⇒ Integer (readonly)
Returns season of the year.
24 25 26 |
# File 'lib/darian_calendar/date.rb', line 24 def season @season end |
#sol ⇒ Integer (readonly) Also known as: day
Returns sol of the month.
14 15 16 |
# File 'lib/darian_calendar/date.rb', line 14 def sol @sol end |
#sol_of_season ⇒ Integer (readonly)
Returns sol of the season.
26 27 28 |
# File 'lib/darian_calendar/date.rb', line 26 def sol_of_season @sol_of_season end |
#sol_of_year ⇒ Integer (readonly)
Returns sol of the year.
30 31 32 |
# File 'lib/darian_calendar/date.rb', line 30 def sol_of_year @sol_of_year end |
#total_sols ⇒ Integer (readonly)
Returns number of sols since the earth date 0-0-0.
20 21 22 |
# File 'lib/darian_calendar/date.rb', line 20 def total_sols @total_sols end |
#week_sol ⇒ String (readonly) Also known as: week_day
Returns sol of the week.
22 23 24 |
# File 'lib/darian_calendar/date.rb', line 22 def week_sol @week_sol end |
#week_sol_name ⇒ String (readonly)
Returns full weeksol name ("Sol Jovis").
18 19 20 |
# File 'lib/darian_calendar/date.rb', line 18 def week_sol_name @week_sol_name end |
#year ⇒ Integer (readonly)
Returns year.
10 11 12 |
# File 'lib/darian_calendar/date.rb', line 10 def year @year end |
Class Method Details
.by_digits(year = nil, month = 1, sol = 1, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Creates a date object by year, month and sol. If you pass the year with nothing else time will default to the first month 1 of that year.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/darian_calendar/date.rb', line 158 def self.by_digits(year=nil, month=1, sol=1, type=DarianCalendar::CalendarTypes::MARTIANA) if year.nil? raise ArgumentError, 'Invalid year' end if (month < 1) || (month >24) raise ArgumentError, 'Invalid month' end if (sol < 1) || (sol > 28) raise ArgumentError, 'Invalid sol' end if ((month % 6) == 0) && (sol == 28) && !((month == 24) && DarianCalendar::is_mars_leap_year?(year)) raise ArgumentError, 'Invalid sol for this month' end sols = sol + ((month-1) * 28) - ((month-1)/6).floor + 668 * year + (year / 2).floor + ((year-1) / 10).floor - ((year-1) / 100).floor + ((year-1) / 1000).floor return self.new(sols) end |
.from_earth(earth_date, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Converts a date object to a mars date object
123 124 125 |
# File 'lib/darian_calendar/date.rb', line 123 def self.from_earth(earth_date, type=DarianCalendar::CalendarTypes::MARTIANA) self.new(DarianCalendar.sols_from_earth(earth_date), type) end |
.from_json(string) ⇒ DarianCalendar::Date
Sets the model attributes from a JSON string. Returns self.
145 146 147 148 149 |
# File 'lib/darian_calendar/date.rb', line 145 def self.from_json(string) json = JSON::parse(string) type = json['calendar_type'].to_s.downcase.to_sym rescue nil self.new(json['total_sols'].to_f, type) end |
.parse_earth(string, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Parses the given representation of a date, and converts it to a mars date
131 132 133 |
# File 'lib/darian_calendar/date.rb', line 131 def self.parse_earth(string, type=DarianCalendar::CalendarTypes::MARTIANA) self.from_earth(::Date.parse(string), type) end |
.today(type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date
Creates a date object denoting the present mars day.
138 139 140 |
# File 'lib/darian_calendar/date.rb', line 138 def self.today(type=DarianCalendar::CalendarTypes::MARTIANA) self.from_earth(::Date.today, type) end |
Instance Method Details
#<=>(another) ⇒ Integer
Compares two dates and returns -1, zero, 1 or nil. The other should be a mars date object.
179 180 181 |
# File 'lib/darian_calendar/date.rb', line 179 def <=>(another) @total_sols.floor <=> another.total_sols.floor end |
#as_json ⇒ Object
Returns a hash representing the model.
217 218 219 220 221 222 223 224 |
# File 'lib/darian_calendar/date.rb', line 217 def as_json json = {} self.instance_variables.sort.each do |attr| field = attr.to_s.gsub('@', '') json[field] = self.send(field) end return json end |
#leap? ⇒ Boolean
Returns true if the given year is a leap year
191 192 193 |
# File 'lib/darian_calendar/date.rb', line 191 def leap? DarianCalendar::is_mars_leap_year?(@year) end |
#sols_in_year ⇒ Integer
Return the number of sols in the given year
185 186 187 |
# File 'lib/darian_calendar/date.rb', line 185 def sols_in_year self.leap? ? 669 : 668 end |
#to_earth ⇒ Date
Converts the given mars date to earth date
197 198 199 200 201 |
# File 'lib/darian_calendar/date.rb', line 197 def to_earth earth_days = (@total_sols * DarianCalendar::MARS_TO_EARTH_DAYS) + DarianCalendar::EPOCH_OFFSET + DarianCalendar::ROUND_UP_SECOND earth_seconds = ((earth_days - DarianCalendar::E_DAYS_TIL_UNIX) * DarianCalendar::SECONDS_A_DAY) - 1 ::Time.at(earth_seconds).to_date end |
#to_json ⇒ Object
Returns a JSON string representing the model.
211 212 213 |
# File 'lib/darian_calendar/date.rb', line 211 def to_json self.as_json.to_json end |
#to_s ⇒ String
Returns a string in an ISO 8601 format (This method doesn’t use the expanded representations).
205 206 207 |
# File 'lib/darian_calendar/date.rb', line 205 def to_s sprintf('%d-%02d-%02d', @year, @month, @sol) end |