Class: Snafu::Models::GlitchTime

Inherits:
Object
  • Object
show all
Defined in:
lib/snafu/models/glitch_time.rb

Overview

Class representing a Glitch date and time

there are 4435200 real seconds in a game year there are 115200 real seconds in a game week there are 14400 real seconds in a game day there are 600 real seconds in a game hour there are 10 real seconds in a game minute

Defined Under Namespace

Classes: InvalidTimestampError

Constant Summary collapse

GLITCH_EPOCH =
1238562000
Y_SECS =
4435200
D_SECS =
14400
H_SECS =
600
M_SECS =
10
DAYS_IN_MONTH =
[29, 3, 53, 17, 73, 19, 13, 37, 5, 47, 11, 1]
MONTH_NAMES =
%w(Primuary Spork Bruise Candy Fever Junuary Septa Remember Doom Widdershins Eleventy Recurse)
DAY_NAMES =
%w(Hairday Moonday Twoday Weddingday Theday Fryday Standday Fabday Recurse)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_timestamp = Time.now) ⇒ GlitchTime

Returns a new instance of GlitchTime.



25
26
27
28
29
30
# File 'lib/snafu/models/glitch_time.rb', line 25

def initialize(new_timestamp = Time.now)
  @timestamp = new_timestamp.to_i
  raise InvalidTimestampError if @timestamp < GLITCH_EPOCH
  @timestamp = timestamp
  @seconds_since_epoch = @timestamp - GLITCH_EPOCH
end

Instance Attribute Details

#seconds_since_epochObject (readonly)

Returns the value of attribute seconds_since_epoch.



23
24
25
# File 'lib/snafu/models/glitch_time.rb', line 23

def seconds_since_epoch
  @seconds_since_epoch
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



23
24
25
# File 'lib/snafu/models/glitch_time.rb', line 23

def timestamp
  @timestamp
end

Instance Method Details

#day_of_monthObject

Returns the 0-based day of the month



57
58
59
60
# File 'lib/snafu/models/glitch_time.rb', line 57

def day_of_month
  return self.day_of_year if self.month == 0
  self.day_of_year - DAYS_IN_MONTH.slice(0, (month)).inject(:+)
end

#day_of_weekObject

Returns the 0-based day of the week



63
64
65
66
# File 'lib/snafu/models/glitch_time.rb', line 63

def day_of_week
  return -1 if day_of_year == 307
  days_since_epoch % 8
end

#day_of_yearObject

Returns the 0-based day of the current Glitch year



38
39
40
# File 'lib/snafu/models/glitch_time.rb', line 38

def day_of_year
  (seconds_since_start_of_year / D_SECS).to_i
end

#days_since_epochObject

Returns the number of game days since epoch



74
75
76
# File 'lib/snafu/models/glitch_time.rb', line 74

def days_since_epoch
  self.day_of_year + (307 * year)
end

#hourObject

Returns the hour of the day



79
80
81
82
# File 'lib/snafu/models/glitch_time.rb', line 79

def hour
  # seconds_since_start_of_day / H_SECS
  (seconds_since_start_of_day / H_SECS).to_i
end

#minute(padded = false) ⇒ Object

Returns the minute of the hour

Options

:padded - If true, will return a zero-padded string if the minute value is less than 10



89
90
91
92
93
94
95
# File 'lib/snafu/models/glitch_time.rb', line 89

def minute(padded = false)
  min = (seconds_since_start_of_hour / M_SECS).to_i
  if padded && min < 10
    min = "0#{min}"
  end
  min
end

#monthObject

Returns the 0-based month of the year



43
44
45
46
47
48
49
# File 'lib/snafu/models/glitch_time.rb', line 43

def month
  running_days = -1
  DAYS_IN_MONTH.each_with_index do |days, idx|
    running_days += days
    return idx if day_of_year <= running_days
  end
end

#name_of_dayObject

Returns the name of the day



69
70
71
# File 'lib/snafu/models/glitch_time.rb', line 69

def name_of_day
  DAY_NAMES[day_of_week]
end

#name_of_monthObject

Returns the name of the month



52
53
54
# File 'lib/snafu/models/glitch_time.rb', line 52

def name_of_month
  MONTH_NAMES[self.month]
end

#to_sObject



97
98
99
# File 'lib/snafu/models/glitch_time.rb', line 97

def to_s
  "#{self.hour}:#{self.minute(padded: true)}, #{self.name_of_day} #{self.day_of_month + 1} of #{self.name_of_month}, year #{self.year}"
end

#yearObject

Returns the number of years since the Glitch epoch



33
34
35
# File 'lib/snafu/models/glitch_time.rb', line 33

def year
  (seconds_since_epoch / Y_SECS).to_i
end