Class Chronos::Duration::Gregorian
In: lib/chronos/duration/gregorian.rb
Parent: ::Chronos::Duration

Methods

Constants

FormatToS = "%dps %d months (%s)".freeze
FormatInspect = "#<%s:0x%08x %dps %d months (%s)>".freeze
ShortTime = ["%d".freeze, "%02d".freeze, "%02d".freeze, "%02d".freeze].freeze
MinSecondsInMonths = [ 0, 2419200, 5097600, 7689600, 10368000, 12960000, 15638400, 18316800, 20908800, 23587200, 26179200, 28857600, 31536000   if you want to estimate minimum seconds in months
MaxSecondsInMonths = [ 0, # 0 days 2678401, # 31 days + leapsecond (june/december) 5356800, # 62 days (july, august) 7948801, # 92 days + leapsecond (june, july, august) 10627201, # 123 days + leapsecond 13219201, 15897601, 18489602, 21168002, 23760002, 26438402, 29116802, 31622402, ]   if you want to estimate maximum seconds in months
After = Hash.new { |h,(a,b)| raise ArgumentError, "Can't get #{a} after #{b}" }.merge({ [:picoseconds, :nanoseconds] => 1_000, [:picoseconds, :microseconds] => 1_000_000, [:picoseconds, :milliseconds] => 1_000_000_000, [:picoseconds, :seconds] => 1_000_000_000_000, [:picoseconds, :minutes] => 60_000_000_000_000, [:picoseconds, :hours] => 3_600_000_000_000_000, [:picoseconds, :days] => 86_400_000_000_000_000, [:picoseconds, :weeks] => 345_600_000_000_000_000, [:nanoseconds, :microseconds] => 1_000, [:nanoseconds, :milliseconds] => 1_000_000, [:nanoseconds, :seconds] => 1_000_000_000, [:nanoseconds, :minutes] => 60_000_000_000, [:nanoseconds, :hours] => 3_600_000_000_000, [:nanoseconds, :days] => 86_400_000_000_000, [:nanoseconds, :weeks] => 345_600_000_000_000, [:microseconds, :milliseconds] => 1_000, [:microseconds, :seconds] => 1_000_000, [:microseconds, :minutes] => 60_000_000, [:microseconds, :hours] => 3_600_000_000, [:microseconds, :days] => 86_400_000_000, [:microseconds, :weeks] => 345_600_000_000, [:milliseconds, :seconds] => 1_000, [:milliseconds, :minutes] => 60_000, [:milliseconds, :hours] => 3_600_000, [:milliseconds, :days] => 86_400_000, [:milliseconds, :weeks] => 345_600_000, [:seconds, :minutes] => 60, [:seconds, :hours] => 3_600, [:seconds, :days] => 86_400, [:seconds, :weeks] => 345_600, [:minutes, :hours] => 60, [:minutes, :days] => 1_440, [:minutes, :weeks] => 10_080, [:hours, :days] => 24, [:hours, :weeks] => 168, [:days, :weeks] => 7, [:months, :years] => 12, })

Public Class methods

[Source]

     # File lib/chronos/duration/gregorian.rb, line 117
117:       def self.import(duration)
118:         duration.respond_to?(:to_gregorian_duration) ? duration.to_gregorian_duration : super
119:       end

seconds+months

[Source]

     # File lib/chronos/duration/gregorian.rb, line 122
122:       def initialize(months, days, picoseconds, language=nil)
123:         super(days, picoseconds, language)
124:         @months = months
125:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 96
 96:       def self.with(parts)
 97:         y,m,w,d,h,min,s,ms,us,ns,ps = *Hash.new(0).merge(parts).values_at(
 98:           :years,
 99:           :months,
100:           :weeks,
101:           :days,
102:           :hours,
103:           :minutes,
104:           :seconds,
105:           :milliseconds,
106:           :microseconds,
107:           :nanoseconds,
108:           :picoseconds
109:         )
110:         seconds  = s+min*60+h*3600+d*86400+w*604800
111:         ps      += seconds*1_000_000_000_000+ms*1_000_000_000+us*1_000_000+ns*1_000
112:         days, ps = ps.divmod(PS_IN_DAY)
113:         months   = m+y*12
114:         new(months, days, ps, parts[:language])
115:       end

Public Instance methods

[Source]

     # File lib/chronos/duration/gregorian.rb, line 175
175:       def centuries
176:         @months.quo(1200)
177:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 155
155:       def days(after=nil)
156:         after ? days%After[[:days, after]] : picoseconds.quo(PS_IN_DAY)
157:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 171
171:       def decades
172:         @months.quo(144)
173:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 151
151:       def hours(after=nil)
152:         after ? hours%After[[:hours, after]] : picoseconds.quo(PS_IN_HOUR)
153:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 135
135:       def microseconds(after=nil)
136:         after ? microseconds%After[[:microseconds, after]] : picoseconds.quo(PS_IN_MICROSECOND)
137:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 139
139:       def milliseconds(after=nil)
140:         after ? milliseconds%After[[:milliseconds, after]] : picoseconds.quo(PS_IN_MILLISECOND)
141:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 147
147:       def minutes(after=nil)
148:         after ? minutes%After[[:minutes, after]] : picoseconds.quo(PS_IN_MINUTE)
149:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 163
163:       def months(after=nil)
164:         after ? days%After[[:days, after]] : @months
165:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 131
131:       def nanoseconds(after=nil)
132:         after ? nanoseconds%After[[:nanoseconds, after]] : picoseconds.quo(PS_IN_NANOSECOND)
133:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 127
127:       def picoseconds(after=nil)
128:         after ? picoseconds%After[[:picoseconds, after]] : @picoseconds+@days*PS_IN_DAY
129:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 143
143:       def seconds(after=nil)
144:         after ? seconds%After[[:seconds, after]] : picoseconds.quo(PS_IN_SECOND)
145:       end

Return a String in form of DDD:HH:MM:SS.fff

fraction_digits:How many digits to display after the ., defaults to 0
num_elements:How many elements to display at least
 * 1 is only SS
 * 2 is MM:SS
 * 3 is HH:MM:SS
 * 4 is DDD:HH:SS

[Source]

     # File lib/chronos/duration/gregorian.rb, line 215
215:       def short_time(fraction_digits=nil, num_elements=nil)
216:         elements = [
217:           days.floor,
218:           hours(:days).floor,
219:           minutes(:hours).floor,
220:           seconds(:minutes)
221:         ]
222:         elements.shift while (elements.size > num_elements && elements.first.first.zero?)
223:         display = ShortTime[-elements.size..-1]
224:         display[-1] = "%#{fraction_digits+3}.#{fraction_digits}f" if (fraction_digits && fraction_digits > 0)
225:         sprintf(display.join(":"), *elements)
226:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 187
187:       def to_a(exclude_language=nil)
188:         exclude_language ? [@picoseconds, @months] : [@picoseconds, @months, @language]
189:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 179
179:       def to_duration
180:         Duration.new(@days, @picoseconds, @language)
181:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 183
183:       def to_gregorian_duration
184:         self
185:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 191
191:       def to_hash
192:         {
193:           :years        => years,
194:           :months       => @months,
195:           :weeks        => weeks,
196:           :days         => days,
197:           :hours        => hours,
198:           :minutes      => minutes,
199:           :seconds      => seconds,
200:           :milliseconds => milliseconds,
201:           :microseconds => microseconds,
202:           :nanoseconds  => nanoseconds,
203:           :picoseconds  => @picoseconds,
204:           :language     => @language,
205:         }
206:       end

return a readable representation

[Source]

     # File lib/chronos/duration/gregorian.rb, line 229
229:       def to_s(drop=:all_zeros, language=nil)
230:         elements1 = @months.zero? ? [] : [
231:           [years.floor,             :year],
232:           [days(:weeks).floor,      :month],
233:         ]
234:         elements2 = @picoseconds.zero? ? [] : [
235:           [weeks.floor,                      :week],
236:           [days(:weeks).floor,               :day],
237:           [hours(:days).floor,               :hour],
238:           [minutes(:hours).floor,            :minute],
239:           [seconds(:minutes).floor,          :second],
240:           [milliseconds(:seconds).floor,     :millisecond],
241:           [microseconds(:milliseconds).floor, :microseconds],
242:           [nanoseconds(:microseconds).floor,  :nanosecond],
243:           [picoseconds(:nanoseconds).floor,   :picosecond],
244:         ]
245:         elements = elements1+elements2
246:         case drop
247:           when :all_zeros
248:             elements.reject! { |count,*rest| count.zero? }
249:           when :leading_zeros
250:             elements.shift while elements.first.first.zero?
251:           when nil
252:           else
253:             raise ArgumentError, "Unknown directive, #{drop.inspect}"
254:         end
255:         elements.empty? ? "0" : elements.map { |count, unit|
256:           "#{count} #{Chronos.string(language ? Chronos.language(language) : @language, unit, count)}"
257:         }.join(", ")
258:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 159
159:       def weeks
160:         picoseconds.quo(PS_IN_WEEK)
161:       end

[Source]

     # File lib/chronos/duration/gregorian.rb, line 167
167:       def years
168:         @months.quo(12)
169:       end

[Validate]