Class: Stardate
- Inherits:
-
Object
- Object
- Stardate
- Defined in:
- lib/stardate.rb
Instance Attribute Summary collapse
-
#stardate ⇒ Object
Returns the value of attribute stardate.
Class Method Summary collapse
- .day ⇒ Object
- .fortnight ⇒ Object
- .hour ⇒ Object
- .lifespan ⇒ Object
-
.millisecond ⇒ Object
Based on 365.2425 days/Gregorian year.
- .minute ⇒ Object
- .month ⇒ Object
- .rename(filename, mode = :mtime) ⇒ Object
- .second ⇒ Object
- .week ⇒ Object
Instance Method Summary collapse
- #-(arg) ⇒ Object
-
#initialize(t = Time.now) ⇒ Stardate
constructor
A new instance of Stardate.
- #inspect ⇒ Object
- #short ⇒ Object
- #to_datetime ⇒ Object
- #to_dts ⇒ Object
- #to_f ⇒ Object
- #to_femtoyears ⇒ Object
- #to_filename(filename) ⇒ Object
-
#to_iso8601 ⇒ Object
Round to the nearest second.
- #to_localdate ⇒ Object
-
#to_rfc2822 ⇒ Object
Round to the nearest second.
- #to_s ⇒ Object (also: #canonical)
- #to_time ⇒ Object
- #to_utcdate ⇒ Object
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
#stardate ⇒ Object
Returns the value of attribute stardate.
7 8 9 |
# File 'lib/stardate.rb', line 7 def stardate @stardate end |
Class Method Details
.day ⇒ Object
29 30 31 |
# File 'lib/stardate.rb', line 29 def day 1.0/365.2425 end |
.fortnight ⇒ Object
37 38 39 |
# File 'lib/stardate.rb', line 37 def fortnight 14.0/365.2425 end |
.hour ⇒ Object
25 26 27 |
# File 'lib/stardate.rb', line 25 def hour 1.0/8765.82 end |
.lifespan ⇒ Object
45 46 47 |
# File 'lib/stardate.rb', line 45 def lifespan 80.0 end |
.millisecond ⇒ Object
Based on 365.2425 days/Gregorian year
13 14 15 |
# File 'lib/stardate.rb', line 13 def millisecond 1.0/31556952000.0 end |
.minute ⇒ Object
21 22 23 |
# File 'lib/stardate.rb', line 21 def minute 1.0/525949.2 end |
.month ⇒ Object
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 |
.second ⇒ Object
17 18 19 |
# File 'lib/stardate.rb', line 17 def second 1.0/31556952.0 end |
.week ⇒ Object
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 |
#inspect ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/stardate.rb', line 99 def inspect [ to_s, ' [', to_rfc2822, ']' ].join end |
#short ⇒ Object
95 96 97 |
# File 'lib/stardate.rb', line 95 def short sprintf("%.3f", @stardate) end |
#to_datetime ⇒ Object
108 109 110 |
# File 'lib/stardate.rb', line 108 def to_datetime to_time.to_datetime end |
#to_dts ⇒ Object
112 113 114 |
# File 'lib/stardate.rb', line 112 def to_dts to_time.utc.strftime("%Y%m%d_%H%M%SZ") end |
#to_f ⇒ Object
116 117 118 |
# File 'lib/stardate.rb', line 116 def to_f @stardate end |
#to_femtoyears ⇒ Object
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_iso8601 ⇒ Object
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_localdate ⇒ Object
139 140 141 |
# File 'lib/stardate.rb', line 139 def to_localdate to_time.to_date end |
#to_rfc2822 ⇒ Object
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_s ⇒ Object Also known as: canonical
148 149 150 |
# File 'lib/stardate.rb', line 148 def to_s sprintf("%.15f", @stardate) end |
#to_time ⇒ Object
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_utcdate ⇒ Object
160 161 162 |
# File 'lib/stardate.rb', line 160 def to_utcdate to_time.utc.to_date end |