Class: Stardate

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(t = Time.now) ⇒ Stardate

Returns a new instance of Stardate.



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
# File 'lib/stardate.rb', line 63

def initialize(t = Time.now)
  case t.class.to_s
  when "Stardate"
    @stardate = t.stardate
    return
  when "Fixnum"
    @stardate = t.to_f
    return
  when "Float"
    @stardate = t
    return
  when "DateTime"
    datetime = t.to_time.utc
  when "Time"
    datetime = t.utc
  when "ActiveSupport::TimeWithZone"
    datetime = t.utc
  when "String"
    datetime = Time.parse(t).utc
  else
    raise "Unknown conversion: #{t.class}"
  end
  y0 = datetime.year
  t0 = Time.utc(y0).to_f
  t1 = Time.utc(y0 + 1).to_f
  @stardate = y0 + (datetime.to_f - t0)/(t1 - t0)
end

Instance Attribute Details

#stardateObject

Returns the value of attribute stardate.



7
8
9
# File 'lib/stardate.rb', line 7

def stardate
  @stardate
end

Class Method Details

.dayObject



29
30
31
# File 'lib/stardate.rb', line 29

def day
  1.0/365.2425
end

.fortnightObject



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

def fortnight
  14.0/365.2425
end

.hourObject



25
26
27
# File 'lib/stardate.rb', line 25

def hour
  1.0/8765.82
end

.lifespanObject



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

def lifespan
  80.0
end

.millisecondObject

Based on 365.2425 days/Gregorian year



13
14
15
# File 'lib/stardate.rb', line 13

def millisecond
  1.0/31556952000.0
end

.minuteObject



21
22
23
# File 'lib/stardate.rb', line 21

def minute
  1.0/525949.2
end

.monthObject



41
42
43
# File 'lib/stardate.rb', line 41

def month
  1.0/12.0
end

.rename(filename, mode = :mtime) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stardate.rb', line 49

def rename(filename, mode = :mtime)
  case mode
  when :mtime
    datetime = File.mtime filename
  when :ctime
    datetime = File.ctime filename
  else
    raise "Unknown mode: #{mode}"
  end
  File.rename filename, datetime.to_stardate.to_filename(filename)
end

.secondObject



17
18
19
# File 'lib/stardate.rb', line 17

def second
  1.0/31556952.0
end

.weekObject



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

def week
  7.0/365.2425
end

Instance Method Details

#-(arg) ⇒ Object



91
92
93
# File 'lib/stardate.rb', line 91

def -(arg)
  StardateInterval.new arg, self
end

#inspectObject



99
100
101
102
103
104
105
106
# File 'lib/stardate.rb', line 99

def inspect
  [
    to_s,
    ' [',
    to_rfc2822,
    ']'
  ].join
end

#shortObject



95
96
97
# File 'lib/stardate.rb', line 95

def short
  sprintf("%.3f", @stardate)
end

#to_datetimeObject



108
109
110
# File 'lib/stardate.rb', line 108

def to_datetime
  to_time.to_datetime
end

#to_dtsObject



112
113
114
# File 'lib/stardate.rb', line 112

def to_dts
  to_time.utc.strftime("%Y%m%d_%H%M%SZ")
end

#to_fObject



116
117
118
# File 'lib/stardate.rb', line 116

def to_f
  @stardate
end

#to_femtoyearsObject



120
121
122
# File 'lib/stardate.rb', line 120

def to_femtoyears
  (@stardate * 1e15).to_i
end

#to_filename(filename) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/stardate.rb', line 124

def to_filename(filename)
  ext = File.extname filename
  [
    filename.chomp(ext),
    '-',
    to_s,
    ext
  ].join
end

#to_iso8601Object

Round to the nearest second



135
136
137
# File 'lib/stardate.rb', line 135

def to_iso8601
  Time.at(to_time.to_f + 0.5).iso8601
end

#to_localdateObject



139
140
141
# File 'lib/stardate.rb', line 139

def to_localdate
  to_time.to_date
end

#to_rfc2822Object

Round to the nearest second



144
145
146
# File 'lib/stardate.rb', line 144

def to_rfc2822
  Time.at(to_time.to_f + 0.5).rfc2822
end

#to_sObject Also known as: canonical



148
149
150
# File 'lib/stardate.rb', line 148

def to_s
  sprintf("%.15f", @stardate)
end

#to_timeObject



153
154
155
156
157
158
# File 'lib/stardate.rb', line 153

def to_time
  y0 = @stardate.to_i
  t0 = Time.utc(y0).to_f
  t1 = Time.utc(y0+1).to_f
  Time.at(t0 + (@stardate-y0)*(t1-t0))
end

#to_utcdateObject



160
161
162
# File 'lib/stardate.rb', line 160

def to_utcdate
  to_time.utc.to_date
end