Class: Darian::Time

Inherits:
Object
  • Object
show all
Defined in:
lib/darian/time.rb

Overview

Darian Mars calendar time converter.

mars = Darian::Time.from_earth(Time.now)
puts mars
puts mars.sol_name

Constant Summary collapse

MARS_TO_EARTH_DAYS =
1.027491251
EPOCH_OFFSET =
587744.77817
ROUND_UP_SECOND =
1/86400

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sols) ⇒ Time

Create Mars time by sols since 0 year.

It is internal contructor. Use ‘Darian.from_earth`.



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/darian/time.rb', line 61

def initialize(sols)
  sD  = (sols / 334296).floor
  doD = (sols - (sD * 334296)).floor

  sC   = 0
  doC  = doD
  sC   = ((doD - 1) / 66859).floor if doD != 0
  doC -= (sC * 66859) + 1 if sC != 0

  sX  = 0
  doX = doC
  if sC != 0 # century that does not begin with leap day
    sX   = ((doC + 1) / 6686).floor
    doX -= sX * 6686 - 1 if sX != 0
  else
    sX   = (doC / 6686).floor
    doX -= sX * 6686 if sX != 0
  end

  sII = 0
  doII = doX
  if sC != 0 and sX == 0 # decade that does not begin with leap day
    sII   = (doX / 1337).floor
    doII -= sII * 1337 if sII != 0
  else  # 1338, 1337, 1337, 1337 …
    sII   = ((doX - 1) / 1337).floor if doX != 0
    doII -= (sII * 1337 + 1) if sII != 0
  end

  sI  = 0
  doI = doII
  if sII == 0 and (sX != 0 or (sX == 0 and sC == 0))
    sI   = (doII / 669).floor
    doI -= 669 if sI != 0
  else # 668, 669
    sI   = ((doII + 1) / 669).floor
    doI -= 668 if sI != 0
  end

  @year = 500 * sD + 100 * sC + 10 * sX + 2 * sII + sI
  @season = if doI < 167
    0
  elsif doI < 334
    1
  elsif doI < 501
    2
  else
    3
  end
  @sol_of_season   = doI - 167 * @season
  @month_of_season = (@sol_of_season / 28).floor

  @month = @month_of_season + 6 * @season + 1

  @sol      = doI - ((@month - 1) * 28 - @season) + 1
  @week_sol = (@sol - 1) % 7 + 1

  hour = (sols - sols.floor) * 24
  min  = (hour - hour.floor) * 60

  @hour = hour.floor
  @min  = min.floor
  @sec  = ((min - min.floor) * 60).floor
end

Instance Attribute Details

#hourObject (readonly)

Returns the value of attribute hour.



38
39
40
# File 'lib/darian/time.rb', line 38

def hour
  @hour
end

#minObject (readonly)

Returns the value of attribute min.



39
40
41
# File 'lib/darian/time.rb', line 39

def min
  @min
end

#monthObject (readonly)

Returns the value of attribute month.



34
35
36
# File 'lib/darian/time.rb', line 34

def month
  @month
end

#month_nameObject (readonly)

Returns the value of attribute month_name.



35
36
37
# File 'lib/darian/time.rb', line 35

def month_name
  @month_name
end

#month_of_seasonObject (readonly)

Returns the value of attribute month_of_season.



46
47
48
# File 'lib/darian/time.rb', line 46

def month_of_season
  @month_of_season
end

#seasonObject (readonly)

Returns the value of attribute season.



33
34
35
# File 'lib/darian/time.rb', line 33

def season
  @season
end

#secObject (readonly)

Returns the value of attribute sec.



40
41
42
# File 'lib/darian/time.rb', line 40

def sec
  @sec
end

#solObject (readonly) Also known as: day

Returns the value of attribute sol.



36
37
38
# File 'lib/darian/time.rb', line 36

def sol
  @sol
end

#sol_of_seasonObject (readonly)

Returns the value of attribute sol_of_season.



45
46
47
# File 'lib/darian/time.rb', line 45

def sol_of_season
  @sol_of_season
end

#week_solObject (readonly) Also known as: week_day

Returns the value of attribute week_sol.



37
38
39
# File 'lib/darian/time.rb', line 37

def week_sol
  @week_sol
end

#yearObject (readonly)

Returns the value of attribute year.



32
33
34
# File 'lib/darian/time.rb', line 32

def year
  @year
end

Class Method Details

.from_earth(time) ⇒ Object

Return Mars time converted from Earth time.

Darian.from_earth(Time.now)


51
52
53
54
55
56
# File 'lib/darian/time.rb', line 51

def self.from_earth(time)
  # 719527 is days until 0 year for Unix Epoch
  days = (time.to_i / 86400.0) + 719527
  sols = (days - EPOCH_OFFSET) / MARS_TO_EARTH_DAYS
  self.new(sols)
end

Instance Method Details

#to_sObject



168
169
170
171
# File 'lib/darian/time.rb', line 168

def to_s
  sprintf '%d-%02d-%02d %02d:%02d:%02d',
          @year, @month, @sol, @hour, @min, @sec
end

#week_sol_nameObject Also known as: week_day_name



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/darian/time.rb', line 155

def week_sol_name
  case @week_sol
    when 1 then 'Sol Solis'
  	when 2 then 'Sol Lunae'
  	when 3 then 'Sol Martis'
  	when 4 then 'Sol Mercurii'
  	when 5 then 'Sol Jovis'
  	when 6 then 'Sol Veneris'
  	when 7 then 'Sol Saturni'
  end
end