Class: Fugit::Duration

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

Defined Under Namespace

Modules: IsoParser, Parser

Constant Summary collapse

KEYS =
{
yea: { a: 'Y', r: 'y', i: 'Y', s: YEAR_S, x: 0, l: 'year' },
mon: { a: 'M', r: 'M', i: 'M', s: 30 * DAY_S, x: 1, l: 'month' },
wee: { a: 'W', r: 'w', i: 'W', s: 7 * DAY_S, I: true, l: 'week' },
day: { a: 'D', r: 'd', i: 'D', s: DAY_S, I: true, l: 'day' },
hou: { a: 'h', r: 'h', i: 'H', s: 3600, I: true, l: 'hour' },
min: { a: 'm', r: 'm', i: 'M', s: 60, I: true, l: 'minute' },
sec: { a: 's', r: 's', i: 'S', s: 1, I: true, l: 'second' } }.freeze
SECOND_ROUND =

Round float seconds to 9 decimals when deflating

9

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hObject (readonly)

Returns the value of attribute h.



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

def h
  @h
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#originalObject (readonly)

Returns the value of attribute original.



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

def original
  @original
end

Class Method Details

.common_rewrite_dur(t) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/fugit/duration.rb', line 51

def common_rewrite_dur(t)

  t
    .subgather(nil)
    .inject({}) { |h, tt|
      v = tt.string; v = v.index('.') ? v.to_f : v.to_i
        # drops ending ("y", "m", ...) by itself
      h[tt.name] = (h[tt.name] || 0) + v
      h }
end

.do_parse(s, opts = {}) ⇒ Object



41
42
43
44
45
# File 'lib/fugit/duration.rb', line 41

def do_parse(s, opts={})

  parse(s, opts) ||
  fail(ArgumentError.new("not a duration #{s.inspect}"))
end

.new(s) ⇒ Object



11
12
13
14
# File 'lib/fugit/duration.rb', line 11

def new(s)

  parse(s)
end

.parse(s, opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fugit/duration.rb', line 16

def parse(s, opts={})

  return s if s.is_a?(self)

  original = s

  s = "#{s}s" if s.is_a?(Numeric)

  return nil unless s.is_a?(String)

  s = s.strip
#p [ original, s ]; Raabro.pp(Parser.parse(s, debug: 3), colours: true)

  h =
    if opts[:iso]
      IsoParser.parse(opts[:stricter] ? s : s.upcase)
    elsif opts[:plain]
      Parser.parse(s)
    else
      Parser.parse(s) || IsoParser.parse(opts[:stricter] ? s : s.upcase)
    end

  h ? self.allocate.send(:init, original, opts, h) : nil
end

.to_iso_s(o) ⇒ Object



48
# File 'lib/fugit/duration.rb', line 48

def to_iso_s(o); do_parse(o).deflate.to_iso_s; end

.to_long_s(o, opts = {}) ⇒ Object



49
# File 'lib/fugit/duration.rb', line 49

def to_long_s(o, opts={}); do_parse(o).deflate.to_long_s(opts); end

.to_plain_s(o) ⇒ Object



47
# File 'lib/fugit/duration.rb', line 47

def to_plain_s(o); do_parse(o).deflate.to_plain_s; end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



281
282
283
284
# File 'lib/fugit/duration.rb', line 281

def ==(o)

  o.is_a?(Fugit::Duration) && o.h == @h
end

#add(a) ⇒ Object Also known as: +



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fugit/duration.rb', line 255

def add(a)

  case a
  when Numeric then add_numeric(a)
  when Fugit::Duration then add_duration(a)
  when String then add_duration(self.class.parse(a))
  when ::Time, ::EtOrbi::EoTime then add_to_time(a)
  else fail ArgumentError.new(
    "cannot add #{a.class} instance to a Fugit::Duration")
  end
end

#add_duration(d) ⇒ Object



216
217
218
219
220
221
# File 'lib/fugit/duration.rb', line 216

def add_duration(d)

  params = d.h.inject(@h.dup) { |h, (k, v)| h[k] = (h[k] || 0) + v; h }

  self.class.allocate.init(nil, {}, params)
end

#add_numeric(n) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/fugit/duration.rb', line 208

def add_numeric(n)

  h = @h.dup
  h[:sec] = (h[:sec] || 0) + n.to_i

  self.class.allocate.init(nil,{}, h)
end

#add_to_time(t) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/fugit/duration.rb', line 223

def add_to_time(t)

  t = ::EtOrbi.make_time(t)

  INFLA_KEYS.each do |k, a|

    v = @h[k]; next unless v

    t = t + v * a[:s]
  end

  NON_INFLA_KEYS.each do |k, a|

    v = @h[k]; next unless v
    at = [ t.year, t.month, t.day, t.hour, t.min, t.sec ]

    at[a[:x]] += v

    if at[1] > 12
      n, m = at[1] / 12, at[1] % 12
      at[0], at[1] = at[0] + n, m
    elsif at[1] < 1
      n, m = (-at[1]) / 12 + 1, (11+at[1]) % 12 + 1
      at[0], at[1] = at[0] - n, m
    end

    t = ::EtOrbi.make_time(at, t.zone)
  end

  t
end

#deflate(options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fugit/duration.rb', line 163

def deflate(options={})

  id = inflate
  h = id.h.dup
  s = h.delete(:sec) || 0

  keys = INFLA_KEYS

  mon = options[:month]
  yea = options[:year]
  keys = keys.dup if mon || yea

  if mon
    mon = 30 if mon == true
    mon = "#{mon}d" if mon.is_a?(Integer)
    keys.unshift([ :mon, { s: Fugit::Duration.parse(mon).to_sec } ])
  end
  if yea
    yea = 365 if yea == true
    yea = "#{yea}d" if yea.is_a?(Integer)
    keys.unshift([ :yea, { s: Fugit::Duration.parse(yea).to_sec } ])
  end

  keys[0..-2].each do |k, v|

    vs = v[:s]; next if s < vs

    h[k] = (h[k] || 0) + s.to_i / vs
    s = s % vs
  end

  h[:sec] = s.is_a?(Integer) ? s : s.round(SECOND_ROUND)

  self.class.allocate.init(@original, {}, h)
end

#drop_secondsObject

Returns a copy of this duration, omitting its seconds.



299
300
301
302
303
304
305
306
# File 'lib/fugit/duration.rb', line 299

def drop_seconds

  h = @h.dup
  h.delete(:sec)
  h[:min] = 0 if h.empty?

  self.class.allocate.init(nil, { literal: true }, h)
end

#hashObject



287
288
289
290
# File 'lib/fugit/duration.rb', line 287

def hash

  @h.hash
end

#inflateObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/fugit/duration.rb', line 143

def inflate

  params =
    @h.inject({ sec: 0 }) { |h, (k, v)|
      a = KEYS[k]
      if a[:I]
        h[:sec] += (v * a[:s])
      else
        h[k] = v
      end
      h
    }

  self.class.allocate.init(@original, {}, params)
end

#next_time(from = ::EtOrbi::EoTime.now) ⇒ Object



292
293
294
295
# File 'lib/fugit/duration.rb', line 292

def next_time(from=::EtOrbi::EoTime.now)

  add(from)
end

#oppositeObject Also known as: -@



199
200
201
202
203
204
# File 'lib/fugit/duration.rb', line 199

def opposite

  params = @h.inject({}) { |h, (k, v)| h[k] = -v; h }

  self.class.allocate.init(nil, {}, params)
end

#subtract(a) ⇒ Object Also known as: -



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/fugit/duration.rb', line 268

def subtract(a)

  case a
  when Numeric then add_numeric(-a)
  when Fugit::Duration then add_duration(-a)
  when String then add_duration(-self.class.parse(a))
  when ::Time, ::EtOrbi::EoTime then opposite.add_to_time(a)
  else fail ArgumentError.new(
    "cannot subtract #{a.class} instance to a Fugit::Duration")
  end
end

#to_hObject

For now, let’s alias to #h



128
# File 'lib/fugit/duration.rb', line 128

def to_h; h; end

#to_iso_sObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/fugit/duration.rb', line 90

def to_iso_s

  t = false

  s = StringIO.new
  s << 'P'

  KEYS.each_with_index do |(k, a), i|
    v = @h[k]; next unless v
    if i > 3 && t == false
      t = true
      s << 'T'
    end
    s << v.to_s; s << a[:i]
  end

  s.string
end

#to_long_s(opts = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fugit/duration.rb', line 109

def to_long_s(opts={})

  s = StringIO.new
  adn = [ false, 'no' ].include?(opts[:oxford]) ? ' and ' : ', and '

  a = @h.to_a
  while kv = a.shift
    k, v = kv
    aa = KEYS[k]
    s << v.to_i
    s << ' '; s << aa[:l]; s << 's' if v > 1
    s << (a.size == 1 ? adn : ', ') if a.size > 0
  end

  s.string
end

#to_plain_sObject



87
# File 'lib/fugit/duration.rb', line 87

def to_plain_s; _to_s(:a); end

#to_rufus_hObject



130
131
132
133
# File 'lib/fugit/duration.rb', line 130

def to_rufus_h

  KEYS.inject({}) { |h, (ks, kh)| v = @h[ks]; h[kh[:r].to_sym] = v if v; h }
end

#to_rufus_sObject



88
# File 'lib/fugit/duration.rb', line 88

def to_rufus_s; _to_s(:r); end

#to_secObject

Warning: this is an “approximation”, months are 30 days and years are 365 days, …



138
139
140
141
# File 'lib/fugit/duration.rb', line 138

def to_sec

  KEYS.inject(0) { |s, (k, a)| v = @h[k]; next s unless v; s += v * a[:s] }
end