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

An immutable class representing the amount of intervening time in a time interval. A duration has no start- nor end-point. Also see Interval

Methods

%   *   +   +@   -   -@   /   days   div   durations_at   import   new   quo   seconds   split   to_a   to_duration   to_hash   to_s   values_at   with  

Classes and Modules

Class Chronos::Duration::Gregorian

Constants

FormatToS = "%dd %dps (%s)".freeze
FormatInspect = "#<%s:0x%08x %dd %dps (%s)>".freeze

Attributes

days  [R] 
language  [R] 
picoseconds  [R] 

Public Class methods

[Source]

    # File lib/chronos/duration.rb, line 38
38:     def self.days(n, language=nil)
39:       new(n, 0, language)
40:     end

[Source]

    # File lib/chronos/duration.rb, line 30
30:     def self.import(duration)
31:       duration.to_duration
32:     end

Create a Duration of given picoseconds length

[Source]

    # File lib/chronos/duration.rb, line 47
47:     def initialize(days, picoseconds, language=nil)
48:       @days, @picoseconds = *picoseconds.divmod(PS_IN_DAY)
49:       @days += days
50:       @language    = Chronos.language(language)
51:     end

[Source]

    # File lib/chronos/duration.rb, line 42
42:     def self.seconds(n, language=nil)
43:       new(0, n*PS_IN_SECOND, language)
44:     end

[Source]

    # File lib/chronos/duration.rb, line 22
22:     def self.with(parts)
23:       new(
24:         parts[:days] || parts[:d] || 0,
25:         parts[:picoseconds] || parts[:ps] || 0,
26:         parts[:language]
27:       )
28:     end

Public Instance methods

[Source]

    # File lib/chronos/duration.rb, line 85
85:     def %(other)
86:       raise "Not yet implemented"
87:       # Duration % Duration -> modulo per unit, e.g. duration % 1.hour -> ps % (1*PS_IN_HOUR)
88:       # Duration % Symbol -> shortcut, e.g. duration % :hour -> duration % 1.hour
89:     end

[Source]

    # File lib/chronos/duration.rb, line 69
69:     def *(other)
70:       self.class.new(*(self.to_a(true).map { |e| e*other }+[@language]))
71:     end

[Source]

    # File lib/chronos/duration.rb, line 61
61:     def +(other)
62:       self.class.new(*(self.to_a(true).zip(other.to_a).map { |a,b| a+b }+[@language]))
63:     end

[Source]

    # File lib/chronos/duration.rb, line 53
53:     def +@
54:       self
55:     end

[Source]

    # File lib/chronos/duration.rb, line 65
65:     def -(other)
66:       self.class.new(*(self.to_a(true).zip(other.to_a).map { |a,b| a-b }+[@language]))
67:     end

[Source]

    # File lib/chronos/duration.rb, line 57
57:     def -@
58:       self.class.new(*(self.to_a(true).map { |e| -e }+[@language]))
59:     end

[Source]

    # File lib/chronos/duration.rb, line 73
73:     def /(other)
74:       self.class.new(*(self.to_a(true).map { |e| e/other }+[@language]))
75:     end

[Source]

    # File lib/chronos/duration.rb, line 77
77:     def div(other)
78:       self.class.new(*(self.to_a(true).map { |e| e.div(other) }+[@language]))
79:     end

[Source]

     # File lib/chronos/duration.rb, line 126
126:     def durations_at(*keys)
127:       keys.zip(values_at(*keys)).map { |key, value|
128:         self.class.with(key => value, :language => @language)
129:       }
130:     end

[Source]

    # File lib/chronos/duration.rb, line 81
81:     def quo(other)
82:       self.class.new(*(self.to_a(true).map { |e| e.quo(other) }+[@language]))
83:     end

Split the duration into durations with each only one of the atomic units set

[Source]

     # File lib/chronos/duration.rb, line 92
 92:     def split
 93:       lang  = [@language]
 94:       klass = self.class
 95:       ary   = to_a(true)
 96:       (0...(ary.size)).zip(ary).map { |i,e|
 97:         init = Array.new(ary.size, 0)+lang
 98:         init[i] = e
 99:         klass.new(*init)
100:       }
101:     end

An array with the atomic units and the language of this Duration

[Source]

     # File lib/chronos/duration.rb, line 104
104:     def to_a(exclude_language=nil)
105:       exclude_language ? [@days, @picoseconds] : [@days, @picoseconds, @language]
106:     end

[Source]

     # File lib/chronos/duration.rb, line 118
118:     def to_duration
119:       self
120:     end

[Source]

     # File lib/chronos/duration.rb, line 108
108:     def to_hash
109:       {
110:         :days        => @days,
111:         :d           => @days,
112:         :ps          => @picoseconds,
113:         :picoseconds => @picoseconds,
114:         :language    => @language,
115:       }
116:     end

return a readable representation

[Source]

     # File lib/chronos/duration.rb, line 133
133:     def to_s
134:       sprintf(self.class::FormatToS, *self)
135:     end

[Source]

     # File lib/chronos/duration.rb, line 122
122:     def values_at(*keys)
123:       to_hash.values_at(*keys)
124:     end

[Validate]