Class: Chronos::Duration

Inherits:
Object
  • Object
show all
Defined in:
lib/chronos/duration.rb,
lib/chronos/duration/gregorian.rb

Overview

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

Direct Known Subclasses

Gregorian

Defined Under Namespace

Classes: Gregorian

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(days, picoseconds, language = nil) ⇒ Duration

Create a Duration of given picoseconds length



47
48
49
50
51
# File 'lib/chronos/duration.rb', line 47

def initialize(days, picoseconds, language=nil)
	@days, @picoseconds = *picoseconds.divmod(PS_IN_DAY)
	@days += days
	@language    = Chronos.language(language)
end

Instance Attribute Details

#daysObject (readonly)

Returns the value of attribute days.



34
35
36
# File 'lib/chronos/duration.rb', line 34

def days
  @days
end

#languageObject (readonly)

Returns the value of attribute language.



36
37
38
# File 'lib/chronos/duration.rb', line 36

def language
  @language
end

#picosecondsObject (readonly)

Returns the value of attribute picoseconds.



35
36
37
# File 'lib/chronos/duration.rb', line 35

def picoseconds
  @picoseconds
end

Class Method Details

.days(n, language = nil) ⇒ Object



38
39
40
# File 'lib/chronos/duration.rb', line 38

def self.days(n, language=nil)
	new(n, 0, language)
end

.import(duration) ⇒ Object



30
31
32
# File 'lib/chronos/duration.rb', line 30

def self.import(duration)
	duration.to_duration
end

.seconds(n, language = nil) ⇒ Object



42
43
44
# File 'lib/chronos/duration.rb', line 42

def self.seconds(n, language=nil)
	new(0, n*PS_IN_SECOND, language)
end

.with(parts) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/chronos/duration.rb', line 22

def self.with(parts)
	new(
		parts[:days] || parts[:d] || 0,
		parts[:picoseconds] || parts[:ps] || 0,
		parts[:language]
	)
end

Instance Method Details

#%(other) ⇒ Object



85
86
87
88
89
# File 'lib/chronos/duration.rb', line 85

def %(other)
	raise "Not yet implemented"
	# Duration % Duration -> modulo per unit, e.g. duration % 1.hour -> ps % (1*PS_IN_HOUR)
	# Duration % Symbol -> shortcut, e.g. duration % :hour -> duration % 1.hour
end

#*(other) ⇒ Object



69
70
71
# File 'lib/chronos/duration.rb', line 69

def *(other)
	self.class.new(*(self.to_a(true).map { |e| e*other }+[@language]))
end

#+(other) ⇒ Object



61
62
63
# File 'lib/chronos/duration.rb', line 61

def +(other)
	self.class.new(*(self.to_a(true).zip(other.to_a).map { |a,b| a+b }+[@language]))
end

#+@Object



53
54
55
# File 'lib/chronos/duration.rb', line 53

def +@
	self
end

#-(other) ⇒ Object



65
66
67
# File 'lib/chronos/duration.rb', line 65

def -(other)
	self.class.new(*(self.to_a(true).zip(other.to_a).map { |a,b| a-b }+[@language]))
end

#-@Object



57
58
59
# File 'lib/chronos/duration.rb', line 57

def -@
	self.class.new(*(self.to_a(true).map { |e| -e }+[@language]))
end

#/(other) ⇒ Object



73
74
75
# File 'lib/chronos/duration.rb', line 73

def /(other)
	self.class.new(*(self.to_a(true).map { |e| e/other }+[@language]))
end

#div(other) ⇒ Object



77
78
79
# File 'lib/chronos/duration.rb', line 77

def div(other)
	self.class.new(*(self.to_a(true).map { |e| e.div(other) }+[@language]))
end

#durations_at(*keys) ⇒ Object



126
127
128
129
130
# File 'lib/chronos/duration.rb', line 126

def durations_at(*keys)
	keys.zip(values_at(*keys)).map { |key, value|
		self.class.with(key => value, :language => @language)
	}
end

#inspectObject

:nodoc:



137
138
139
# File 'lib/chronos/duration.rb', line 137

def inspect # :nodoc:
	sprintf(self.class::FormatInspect, self.class, object_id<<1, *self)
end

#quo(other) ⇒ Object



81
82
83
# File 'lib/chronos/duration.rb', line 81

def quo(other)
	self.class.new(*(self.to_a(true).map { |e| e.quo(other) }+[@language]))
end

#splitObject

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



92
93
94
95
96
97
98
99
100
101
# File 'lib/chronos/duration.rb', line 92

def split
	lang  = [@language]
	klass = self.class
	ary   = to_a(true)
	(0...(ary.size)).zip(ary).map { |i,e|
		init = Array.new(ary.size, 0)+lang
		init[i] = e
		klass.new(*init)
	}
end

#to_a(exclude_language = nil) ⇒ Object

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



104
105
106
# File 'lib/chronos/duration.rb', line 104

def to_a(exclude_language=nil)
	exclude_language ? [@days, @picoseconds] : [@days, @picoseconds, @language]
end

#to_durationObject



118
119
120
# File 'lib/chronos/duration.rb', line 118

def to_duration
	self
end

#to_hashObject



108
109
110
111
112
113
114
115
116
# File 'lib/chronos/duration.rb', line 108

def to_hash
	{
		:days        => @days,
		:d           => @days,
		:ps          => @picoseconds,
		:picoseconds => @picoseconds,
		:language    => @language,
	}
end

#to_sObject

return a readable representation



133
134
135
# File 'lib/chronos/duration.rb', line 133

def to_s
	sprintf(self.class::FormatToS, *self)
end

#values_at(*keys) ⇒ Object



122
123
124
# File 'lib/chronos/duration.rb', line 122

def values_at(*keys)
	to_hash.values_at(*keys)
end