Class: DarianCalendar::Date

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/darian_calendar/date.rb

Overview

The date is a particular day of a Darian calendar year

Direct Known Subclasses

Time

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sols = nil, type = DarianCalendar::CalendarTypes::MARTIANA) ⇒ DarianCalendar::Date

Converts a number of martian sols to mars date.

Parameters:

  • sols (defaults to: nil)

    optional [Float] Number of martian sols. Default is the number of sols of the the current date.

  • type (defaults to: DarianCalendar::CalendarTypes::MARTIANA)

    optional [DarianCalendar::CalendarTypes] calendar type.



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_typeString (readonly)

Returns name of the calendar type ("Martiana").

Returns:

  • (String)

    name of the calendar type ("Martiana")



32
33
34
# File 'lib/darian_calendar/date.rb', line 32

def calendar_type
  @calendar_type
end

#monthInteger (readonly)

Returns month of the year.

Returns:

  • (Integer)

    month of the year



12
13
14
# File 'lib/darian_calendar/date.rb', line 12

def month
  @month
end

#month_nameString (readonly)

Returns full month name ("Mithuna").

Returns:

  • (String)

    full month name ("Mithuna")



16
17
18
# File 'lib/darian_calendar/date.rb', line 16

def month_name
  @month_name
end

#month_of_seasonInteger (readonly)

Returns month of the season.

Returns:

  • (Integer)

    month of the season



28
29
30
# File 'lib/darian_calendar/date.rb', line 28

def month_of_season
  @month_of_season
end

#seasonInteger (readonly)

Returns season of the year.

Returns:

  • (Integer)

    season of the year



24
25
26
# File 'lib/darian_calendar/date.rb', line 24

def season
  @season
end

#solInteger (readonly) Also known as: day

Returns sol of the month.

Returns:

  • (Integer)

    sol of the month



14
15
16
# File 'lib/darian_calendar/date.rb', line 14

def sol
  @sol
end

#sol_of_seasonInteger (readonly)

Returns sol of the season.

Returns:

  • (Integer)

    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_yearInteger (readonly)

Returns sol of the year.

Returns:

  • (Integer)

    sol of the year



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

def sol_of_year
  @sol_of_year
end

#total_solsInteger (readonly)

Returns number of sols since the earth date 0-0-0.

Returns:

  • (Integer)

    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_solString (readonly) Also known as: week_day

Returns sol of the week.

Returns:

  • (String)

    sol of the week



22
23
24
# File 'lib/darian_calendar/date.rb', line 22

def week_sol
  @week_sol
end

#week_sol_nameString (readonly)

Returns full weeksol name ("Sol Jovis").

Returns:

  • (String)

    full weeksol name ("Sol Jovis")



18
19
20
# File 'lib/darian_calendar/date.rb', line 18

def week_sol_name
  @week_sol_name
end

#yearInteger (readonly)

Returns year.

Returns:

  • (Integer)

    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.

Parameters:

  • year (Integer) (defaults to: nil)

    year

  • month (Integer) (defaults to: 1)

    month

  • sol (Integer) (defaults to: 1)

    sol

  • type (DarianCalendar::CalendarTypes) (defaults to: DarianCalendar::CalendarTypes::MARTIANA)

    Calendar type

Returns:



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

Parameters:

  • earth_date (::Date)

    Earth date

  • type (DarianCalendar::CalendarTypes) (defaults to: DarianCalendar::CalendarTypes::MARTIANA)

    Calendar type

Returns:



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.

Parameters:

  • string (String)

    JSON string

Returns:



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

Parameters:

  • string (String)

    String with a date

  • type (DarianCalendar::CalendarTypes) (defaults to: DarianCalendar::CalendarTypes::MARTIANA)

    Calendar type

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:

  • (Integer)

    Compare result



179
180
181
# File 'lib/darian_calendar/date.rb', line 179

def <=>(another)
  @total_sols.floor <=> another.total_sols.floor
end

#as_jsonObject

Returns a hash representing the model.

Returns:

  • 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

Returns:

  • (Boolean)

    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_yearInteger

Return the number of sols in the given year

Returns:

  • (Integer)

    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_earthDate

Converts the given mars date to earth date

Returns:

  • (Date)

    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_jsonObject

Returns a JSON string representing the model.

Returns:

  • 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_sString

Returns a string in an ISO 8601 format (This method doesn’t use the expanded representations).

Returns:

  • (String)

    Date as a string in an ISO 8601 format



205
206
207
# File 'lib/darian_calendar/date.rb', line 205

def to_s
  sprintf('%d-%02d-%02d', @year, @month, @sol)
end