when_exe 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +36 -33
- data/bin/locales.rb +1 -1
- data/bin/make_ttl.rb.config +1 -1
- data/lib/when_exe.rb +27 -16
- data/lib/when_exe/basictypes.rb +772 -771
- data/lib/when_exe/calendartypes.rb +1485 -1453
- data/lib/when_exe/coordinates.rb +5 -0
- data/lib/when_exe/core/compatibility.rb +1 -1
- data/lib/when_exe/core/duration.rb +147 -116
- data/lib/when_exe/core/extension.rb +499 -497
- data/lib/when_exe/ephemeris.rb +1952 -1951
- data/lib/when_exe/ephemeris/eclipse.rb +5 -4
- data/lib/when_exe/ephemeris/notes.rb +457 -421
- data/lib/when_exe/ephemeris/planets.rb +585 -585
- data/lib/when_exe/ephemeris/sun.rb +214 -214
- data/lib/when_exe/google_api.rb +153 -0
- data/lib/when_exe/icalendar.rb +1640 -1632
- data/lib/when_exe/inspect.rb +42 -20
- data/lib/when_exe/linkeddata.rb +28 -7
- data/lib/when_exe/locales/autoload.rb +2 -1
- data/lib/when_exe/locales/locale.rb +35 -15
- data/lib/when_exe/locales/zh.rb +77 -0
- data/lib/when_exe/mini_application.rb +3 -1
- data/lib/when_exe/{googlecalendar.rb → obsolete/googlecalendar.rb} +144 -144
- data/lib/when_exe/parts/enumerator.rb +498 -486
- data/lib/when_exe/parts/geometric_complex.rb +397 -397
- data/lib/when_exe/parts/timezone.rb +246 -241
- data/lib/when_exe/region/armenian.rb +55 -56
- data/lib/when_exe/region/babylonian.rb +406 -405
- data/lib/when_exe/region/bahai.rb +107 -106
- data/lib/when_exe/region/balinese.rb +624 -622
- data/lib/when_exe/region/chinese.rb +1071 -1026
- data/lib/when_exe/region/chinese/epochs.rb +28 -28
- data/lib/when_exe/region/chinese/notes.rb +219 -0
- data/lib/when_exe/region/chinese/twins.rb +803 -803
- data/lib/when_exe/region/christian.rb +21 -15
- data/lib/when_exe/region/coptic.rb +107 -106
- data/lib/when_exe/region/discordian.rb +218 -218
- data/lib/when_exe/region/east_asian.rb +1 -1
- data/lib/when_exe/region/french.rb +126 -56
- data/lib/when_exe/region/geologicalage.rb +639 -639
- data/lib/when_exe/region/goddess.rb +60 -58
- data/lib/when_exe/region/hanke_henry.rb +2 -2
- data/lib/when_exe/region/indian.rb +1225 -1222
- data/lib/when_exe/region/international_fixed.rb +96 -97
- data/lib/when_exe/region/iranian.rb +206 -203
- data/lib/when_exe/region/islamic.rb +102 -102
- data/lib/when_exe/region/japanese.rb +126 -71
- data/lib/when_exe/region/japanese/epochs.rb +426 -426
- data/lib/when_exe/region/japanese/notes.rb +101 -81
- data/lib/when_exe/region/japanese/residues.rb +1345 -1311
- data/lib/when_exe/region/japanese/twins.rb +225 -225
- data/lib/when_exe/region/japanese/weeks.rb +112 -112
- data/lib/when_exe/region/javanese.rb +230 -230
- data/lib/when_exe/region/jewish.rb +130 -131
- data/lib/when_exe/region/m17n.rb +114 -114
- data/lib/when_exe/region/martian.rb +258 -258
- data/lib/when_exe/region/mayan.rb +11 -8
- data/lib/when_exe/region/pax.rb +4 -5
- data/lib/when_exe/region/pope.rb +1 -1
- data/lib/when_exe/region/positivist.rb +100 -100
- data/lib/when_exe/region/residue.rb +162 -162
- data/lib/when_exe/region/roman.rb +333 -333
- data/lib/when_exe/region/{soviet.rb → russian.rb} +221 -209
- data/lib/when_exe/region/shire.rb +222 -223
- data/lib/when_exe/region/symmetry.rb +50 -50
- data/lib/when_exe/region/thai.rb +336 -336
- data/lib/when_exe/region/tibetan.rb +315 -316
- data/lib/when_exe/region/tranquility.rb +207 -208
- data/lib/when_exe/region/vanishing_leprechaun.rb +3 -1
- data/lib/when_exe/region/vietnamese.rb +449 -440
- data/lib/when_exe/region/weekdate.rb +80 -80
- data/lib/when_exe/region/world.rb +170 -171
- data/lib/when_exe/region/world_season.rb +89 -89
- data/lib/when_exe/region/yerm.rb +3 -3
- data/lib/when_exe/region/zoroastrian.rb +205 -205
- data/lib/when_exe/timestandard.rb +708 -707
- data/lib/when_exe/tmduration.rb +338 -338
- data/lib/when_exe/tmobjects.rb +1356 -1356
- data/lib/when_exe/tmposition.rb +66 -31
- data/lib/when_exe/version.rb +16 -2
- data/test/examples/Residue.m17n +83 -83
- data/test/examples/Terms.m17n +2 -2
- data/test/test.rb +2 -2
- data/test/test/google_api.rb +65 -0
- data/test/test/linkeddata.rb +1 -1
- data/test/test/{googlecalendar.rb → obsolete/googlecalendar.rb} +194 -194
- data/test/test/region/indian.rb +90 -85
- data/test/test/region/m17n.rb +7 -7
- data/test/test/region/mayan.rb +195 -195
- data/test/test/region/residue.rb +153 -153
- data/test/test/tmposition.rb +11 -1
- data/when_exe.gemspec +2 -2
- metadata +95 -8
- data/test/test.rb.config +0 -1
data/lib/when_exe/tmduration.rb
CHANGED
@@ -1,338 +1,338 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
=begin
|
3
|
-
Minimum subset - A multicultural and multilingualized calendar library based on ISO 8601, ISO 19108 and RFC 5545
|
4
|
-
|
5
|
-
Copyright (C) 2013 Takashi SUGA
|
6
|
-
|
7
|
-
You may use and/or modify this file according to the license described in the LICENSE.txt file included in this archive.
|
8
|
-
|
9
|
-
=end
|
10
|
-
|
11
|
-
module When
|
12
|
-
|
13
|
-
# 分解能定数
|
14
|
-
|
15
|
-
CENTURY = -4
|
16
|
-
DECADE = -3
|
17
|
-
YEAR = -2
|
18
|
-
MONTH = -1
|
19
|
-
WEEK = -0.5
|
20
|
-
DAY = 0
|
21
|
-
HOUR = 1
|
22
|
-
MINUTE = 2
|
23
|
-
SECOND = 3
|
24
|
-
STRING = 5
|
25
|
-
SYSTEM = (Float::MANT_DIG*0.3).to_i
|
26
|
-
|
27
|
-
#
|
28
|
-
# (5.2) Temporal Objects Package
|
29
|
-
#
|
30
|
-
#
|
31
|
-
module TM
|
32
|
-
|
33
|
-
# 時間次元における長さ又は距離を記述するために使うデータ型
|
34
|
-
#
|
35
|
-
# see {gml schema}[link:http://schemas.opengis.net/gml/3.1.1/base/temporal.xsd#duration]
|
36
|
-
#
|
37
|
-
class Duration
|
38
|
-
|
39
|
-
# 物理的な時間間隔の定数
|
40
|
-
#
|
41
|
-
# 時間の「秒」を Float で表現して丸め誤差が発生しない範囲で、
|
42
|
-
# もっとも大きな時間間隔(86400s を 2 の因数で割りつくした値)
|
43
|
-
# を単位 SYSTEM とする
|
44
|
-
#
|
45
|
-
SYSTEM = 1.0
|
46
|
-
DAY = SYSTEM * 675
|
47
|
-
YEAR = DAY * 365.2425
|
48
|
-
MONTH = YEAR / 12
|
49
|
-
WEEK = DAY * 7
|
50
|
-
HOUR = DAY / 24
|
51
|
-
MINUTE = HOUR / 60
|
52
|
-
SECOND = MINUTE / 60
|
53
|
-
|
54
|
-
UnitName = {YEAR=>'year', MONTH =>'month', WEEK =>'week', DAY =>'day',
|
55
|
-
HOUR=>'hour', MINUTE=>'minute', SECOND=>'second', SYSTEM=>'system'}
|
56
|
-
Unit = UnitName.invert
|
57
|
-
DurationUnits = [DAY, HOUR, MINUTE, SECOND]
|
58
|
-
|
59
|
-
#
|
60
|
-
# オブジェクト生成用クラスメソッド
|
61
|
-
#
|
62
|
-
class << self
|
63
|
-
#
|
64
|
-
# 日時分秒からのオブジェクト生成
|
65
|
-
#
|
66
|
-
# @param [Array<Numeric>] value ( 日, 時, 分, 秒 ) 下位の桁は省略可
|
67
|
-
#
|
68
|
-
# @return [When::TM::Duration]
|
69
|
-
#
|
70
|
-
def dhms(*value)
|
71
|
-
units = DurationUnits.dup
|
72
|
-
Duration.new(value.inject(0) {|s,v| s + v * units.shift})
|
73
|
-
end
|
74
|
-
|
75
|
-
#
|
76
|
-
# メソッド名に相当する単位で表した value に対応する When::TM::Duration を生成する
|
77
|
-
# @method unit
|
78
|
-
# @param [Numeric] value
|
79
|
-
# @return [When::TM::Duration]
|
80
|
-
# @note unit は second, minute, hour, day, week に読み替える
|
81
|
-
|
82
|
-
# @private
|
83
|
-
Unit.keys.each do |key|
|
84
|
-
module_eval %Q{
|
85
|
-
def #{key}(value)
|
86
|
-
new(value * Unit['#{key}'])
|
87
|
-
end
|
88
|
-
}
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
# 時間間隔の長さ (128秒単位)
|
93
|
-
#
|
94
|
-
# @return [Numeric]
|
95
|
-
#
|
96
|
-
attr_accessor :duration
|
97
|
-
|
98
|
-
#
|
99
|
-
# メソッド名に相当する単位で表した時間間隔の大きさ
|
100
|
-
# @method unit
|
101
|
-
# @return [Numeric]
|
102
|
-
# @note unit は second, minute, hour, day, week に読み替える
|
103
|
-
|
104
|
-
# @private
|
105
|
-
Unit.keys.each do |key|
|
106
|
-
module_eval %Q{
|
107
|
-
def #{key}
|
108
|
-
duration / Unit['#{key}']
|
109
|
-
end
|
110
|
-
}
|
111
|
-
end
|
112
|
-
|
113
|
-
# 時間間隔の長さ / 秒
|
114
|
-
#
|
115
|
-
# @return [Float]
|
116
|
-
#
|
117
|
-
def to_f
|
118
|
-
duration / SECOND
|
119
|
-
end
|
120
|
-
alias :to_float :to_f
|
121
|
-
|
122
|
-
# 時間間隔の長さ / 秒
|
123
|
-
#
|
124
|
-
# @return [Integer] (四捨五入値)
|
125
|
-
#
|
126
|
-
def to_i
|
127
|
-
to_f.round
|
128
|
-
end
|
129
|
-
alias :to_int :to_i
|
130
|
-
|
131
|
-
# 符号反転
|
132
|
-
#
|
133
|
-
# @return [When::TM::Duration]
|
134
|
-
#
|
135
|
-
def -@
|
136
|
-
Duration.new(-@duration)
|
137
|
-
end
|
138
|
-
|
139
|
-
# 絶対値
|
140
|
-
#
|
141
|
-
# @return [When::TM::Duration]
|
142
|
-
#
|
143
|
-
def abs
|
144
|
-
sign >= 0 ? self.dup : -self
|
145
|
-
end
|
146
|
-
|
147
|
-
# 符号
|
148
|
-
#
|
149
|
-
# @return [Integer] 0 との比較により、負,0,正の値を返す
|
150
|
-
#
|
151
|
-
def sign
|
152
|
-
@duration <=> 0
|
153
|
-
end
|
154
|
-
|
155
|
-
# 比較
|
156
|
-
#
|
157
|
-
# @param [Numeric, When::TM::Duration] other
|
158
|
-
#
|
159
|
-
# @return [Integer] other との比較により、負,0,正の値を返す
|
160
|
-
#
|
161
|
-
def <=>(other)
|
162
|
-
self.to_f <=> other.to_f
|
163
|
-
end
|
164
|
-
|
165
|
-
# オブジェクトの同値
|
166
|
-
#
|
167
|
-
# @param [Object] other
|
168
|
-
#
|
169
|
-
# @return [Boolean]
|
170
|
-
# [ true - 同値 ]
|
171
|
-
# [ false - 非同値 ]
|
172
|
-
#
|
173
|
-
def ==(other)
|
174
|
-
return false unless other.instance_of?(self.class)
|
175
|
-
return self.duration == other.duration
|
176
|
-
end
|
177
|
-
|
178
|
-
#
|
179
|
-
# 加算
|
180
|
-
#
|
181
|
-
# @param [Numeric, When::TM::Duration] other 秒数とみなす
|
182
|
-
# @param [その他] other 日時とみなす
|
183
|
-
#
|
184
|
-
# @return [When::TM::Duration] (other が Numeric, When::TM::Duration の場合)
|
185
|
-
# @return [other と同じクラス] (other がその他の場合)
|
186
|
-
#
|
187
|
-
def +(other)
|
188
|
-
case other
|
189
|
-
when Duration ; Duration.new(@duration + other.duration)
|
190
|
-
when Numeric ; Duration.new(@duration + other * SECOND)
|
191
|
-
else ; other + self
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
#
|
196
|
-
# 減算
|
197
|
-
#
|
198
|
-
# @param [Numeric, When::TM::Duration] other 秒数とみなす
|
199
|
-
#
|
200
|
-
# @return [When::TM::Duration]
|
201
|
-
#
|
202
|
-
def -(other)
|
203
|
-
Duration.new(@duration - (other.kind_of?(Duration) ? other.duration : other * SECOND))
|
204
|
-
end
|
205
|
-
|
206
|
-
#
|
207
|
-
# 乗算
|
208
|
-
#
|
209
|
-
# @param [Numeric] other
|
210
|
-
#
|
211
|
-
# @return [When::TM::Duration]
|
212
|
-
#
|
213
|
-
def *(other)
|
214
|
-
Duration.new(@duration * other)
|
215
|
-
end
|
216
|
-
|
217
|
-
#
|
218
|
-
# 除算
|
219
|
-
#
|
220
|
-
# @param [Numeric, When::TM::Duration] other 秒数とみなす
|
221
|
-
#
|
222
|
-
# @return [When::TM::Duration] (other が Numeric の場合)
|
223
|
-
# @return [Numeric] (other が When::TM::Duration の場合)
|
224
|
-
#
|
225
|
-
def /(other)
|
226
|
-
other.kind_of?(Duration) ? @duration / other.duration : Duration.new(@duration / other)
|
227
|
-
end
|
228
|
-
|
229
|
-
#
|
230
|
-
# 時間間隔を日時分秒を表すArrayに変換する
|
231
|
-
#
|
232
|
-
# @param [Integer] n 最下位要素のインデクス(デフォルト When::SECOND)
|
233
|
-
#
|
234
|
-
# @return [Array] [ 日, 時, 分, 秒 ]
|
235
|
-
#
|
236
|
-
def to_dhms(n=When::SECOND)
|
237
|
-
a = []
|
238
|
-
m = @duration
|
239
|
-
n.times do |i|
|
240
|
-
d, m = m.divmod(DurationUnits[i])
|
241
|
-
a << d
|
242
|
-
end
|
243
|
-
a << m / DurationUnits[n]
|
244
|
-
a
|
245
|
-
end
|
246
|
-
|
247
|
-
#
|
248
|
-
# 時間間隔の要素を取り出す
|
249
|
-
#
|
250
|
-
# @param [Integer] n 要素のインデクス
|
251
|
-
#
|
252
|
-
# @return [Numeric] (秒は Float, その他は Integer)
|
253
|
-
#
|
254
|
-
def [](n)
|
255
|
-
to_dhms([n+1,When::SECOND].min)[n]
|
256
|
-
end
|
257
|
-
|
258
|
-
#
|
259
|
-
# 指定時刻よりselfの時間間隔だけ後の時刻オブジェクト
|
260
|
-
#
|
261
|
-
# @param [::Time, When::TM::TemporalPosition] time 指定時刻
|
262
|
-
#
|
263
|
-
# @return [引数と同種の時刻オブジェクト]
|
264
|
-
#
|
265
|
-
def after(time=Time.now)
|
266
|
-
time + self
|
267
|
-
end
|
268
|
-
alias :since :after
|
269
|
-
|
270
|
-
#
|
271
|
-
# 指定時刻よりselfの時間間隔だけ前の時刻オブジェクト
|
272
|
-
#
|
273
|
-
# @param [::Time, When::TM::TemporalPosition] time 指定時刻
|
274
|
-
#
|
275
|
-
# @return [引数と同種の時刻オブジェクト]
|
276
|
-
#
|
277
|
-
def before(time=Time.now)
|
278
|
-
time - self
|
279
|
-
end
|
280
|
-
alias :ago :before
|
281
|
-
|
282
|
-
# When::TM::Duration オブジェクトを分かりやすい文字列にして返します
|
283
|
-
#
|
284
|
-
# @return [String] to_s と同様
|
285
|
-
#
|
286
|
-
def inspect
|
287
|
-
to_s
|
288
|
-
end
|
289
|
-
|
290
|
-
# 文字列化
|
291
|
-
#
|
292
|
-
# @return [String]
|
293
|
-
#
|
294
|
-
def to_s
|
295
|
-
to_dhms.to_s
|
296
|
-
end
|
297
|
-
|
298
|
-
#
|
299
|
-
# When::TM::Duration への変換
|
300
|
-
#
|
301
|
-
# @note 必ずコピーを作る
|
302
|
-
#
|
303
|
-
# @return [When::TM::Duration]
|
304
|
-
#
|
305
|
-
def to_duration
|
306
|
-
Duration.new(duration)
|
307
|
-
end
|
308
|
-
|
309
|
-
#
|
310
|
-
# ActiveSupport::Duration への変換
|
311
|
-
#
|
312
|
-
# @return [ActiveSupport::Duration]
|
313
|
-
#
|
314
|
-
def to_as_duration
|
315
|
-
[[:weeks, WEEK], [:days, DAY], [:hours, HOUR], [:minutes, MINUTE], [:seconds, SECOND]].each do |unit|
|
316
|
-
div, mod = duration.divmod(unit[1])
|
317
|
-
return div.send(unit[0]) if mod == 0
|
318
|
-
end
|
319
|
-
(duration / SECOND).seconds
|
320
|
-
end
|
321
|
-
|
322
|
-
# coerce
|
323
|
-
# @private
|
324
|
-
def coerce(other)
|
325
|
-
[other, @duration]
|
326
|
-
end
|
327
|
-
|
328
|
-
#
|
329
|
-
# Duration オブジェクトの初期化
|
330
|
-
#
|
331
|
-
# @param [Numeric] value Duration値 / 128秒
|
332
|
-
#
|
333
|
-
def initialize(value)
|
334
|
-
@duration = value
|
335
|
-
end
|
336
|
-
end
|
337
|
-
end
|
338
|
-
end
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin
|
3
|
+
Minimum subset - A multicultural and multilingualized calendar library based on ISO 8601, ISO 19108 and RFC 5545
|
4
|
+
|
5
|
+
Copyright (C) 2013-2015 Takashi SUGA
|
6
|
+
|
7
|
+
You may use and/or modify this file according to the license described in the LICENSE.txt file included in this archive.
|
8
|
+
|
9
|
+
=end
|
10
|
+
|
11
|
+
module When
|
12
|
+
|
13
|
+
# 分解能定数
|
14
|
+
|
15
|
+
CENTURY = -4
|
16
|
+
DECADE = -3
|
17
|
+
YEAR = -2
|
18
|
+
MONTH = -1
|
19
|
+
WEEK = -0.5
|
20
|
+
DAY = 0
|
21
|
+
HOUR = 1
|
22
|
+
MINUTE = 2
|
23
|
+
SECOND = 3
|
24
|
+
STRING = 5
|
25
|
+
SYSTEM = (Float::MANT_DIG*0.3).to_i
|
26
|
+
|
27
|
+
#
|
28
|
+
# (5.2) Temporal Objects Package
|
29
|
+
#
|
30
|
+
#
|
31
|
+
module TM
|
32
|
+
|
33
|
+
# 時間次元における長さ又は距離を記述するために使うデータ型
|
34
|
+
#
|
35
|
+
# see {gml schema}[link:http://schemas.opengis.net/gml/3.1.1/base/temporal.xsd#duration]
|
36
|
+
#
|
37
|
+
class Duration
|
38
|
+
|
39
|
+
# 物理的な時間間隔の定数
|
40
|
+
#
|
41
|
+
# 時間の「秒」を Float で表現して丸め誤差が発生しない範囲で、
|
42
|
+
# もっとも大きな時間間隔(86400s を 2 の因数で割りつくした値)
|
43
|
+
# を単位 SYSTEM とする
|
44
|
+
#
|
45
|
+
SYSTEM = 1.0
|
46
|
+
DAY = SYSTEM * 675
|
47
|
+
YEAR = DAY * 365.2425
|
48
|
+
MONTH = YEAR / 12
|
49
|
+
WEEK = DAY * 7
|
50
|
+
HOUR = DAY / 24
|
51
|
+
MINUTE = HOUR / 60
|
52
|
+
SECOND = MINUTE / 60
|
53
|
+
|
54
|
+
UnitName = {YEAR=>'year', MONTH =>'month', WEEK =>'week', DAY =>'day',
|
55
|
+
HOUR=>'hour', MINUTE=>'minute', SECOND=>'second', SYSTEM=>'system'}
|
56
|
+
Unit = UnitName.invert
|
57
|
+
DurationUnits = [DAY, HOUR, MINUTE, SECOND]
|
58
|
+
|
59
|
+
#
|
60
|
+
# オブジェクト生成用クラスメソッド
|
61
|
+
#
|
62
|
+
class << self
|
63
|
+
#
|
64
|
+
# 日時分秒からのオブジェクト生成
|
65
|
+
#
|
66
|
+
# @param [Array<Numeric>] value ( 日, 時, 分, 秒 ) 下位の桁は省略可
|
67
|
+
#
|
68
|
+
# @return [When::TM::Duration]
|
69
|
+
#
|
70
|
+
def dhms(*value)
|
71
|
+
units = DurationUnits.dup
|
72
|
+
Duration.new(value.inject(0) {|s,v| s + v * units.shift})
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# メソッド名に相当する単位で表した value に対応する When::TM::Duration を生成する
|
77
|
+
# @method unit
|
78
|
+
# @param [Numeric] value
|
79
|
+
# @return [When::TM::Duration]
|
80
|
+
# @note unit は second, minute, hour, day, week に読み替える
|
81
|
+
|
82
|
+
# @private
|
83
|
+
Unit.keys.each do |key|
|
84
|
+
module_eval %Q{
|
85
|
+
def #{key}(value)
|
86
|
+
new(value * Unit['#{key}'])
|
87
|
+
end
|
88
|
+
}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# 時間間隔の長さ (128秒単位)
|
93
|
+
#
|
94
|
+
# @return [Numeric]
|
95
|
+
#
|
96
|
+
attr_accessor :duration
|
97
|
+
|
98
|
+
#
|
99
|
+
# メソッド名に相当する単位で表した時間間隔の大きさ
|
100
|
+
# @method unit
|
101
|
+
# @return [Numeric]
|
102
|
+
# @note unit は second, minute, hour, day, week に読み替える
|
103
|
+
|
104
|
+
# @private
|
105
|
+
Unit.keys.each do |key|
|
106
|
+
module_eval %Q{
|
107
|
+
def #{key}
|
108
|
+
duration / Unit['#{key}']
|
109
|
+
end
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
# 時間間隔の長さ / 秒
|
114
|
+
#
|
115
|
+
# @return [Float]
|
116
|
+
#
|
117
|
+
def to_f
|
118
|
+
duration / SECOND
|
119
|
+
end
|
120
|
+
alias :to_float :to_f
|
121
|
+
|
122
|
+
# 時間間隔の長さ / 秒
|
123
|
+
#
|
124
|
+
# @return [Integer] (四捨五入値)
|
125
|
+
#
|
126
|
+
def to_i
|
127
|
+
to_f.round
|
128
|
+
end
|
129
|
+
alias :to_int :to_i
|
130
|
+
|
131
|
+
# 符号反転
|
132
|
+
#
|
133
|
+
# @return [When::TM::Duration]
|
134
|
+
#
|
135
|
+
def -@
|
136
|
+
Duration.new(-@duration)
|
137
|
+
end
|
138
|
+
|
139
|
+
# 絶対値
|
140
|
+
#
|
141
|
+
# @return [When::TM::Duration]
|
142
|
+
#
|
143
|
+
def abs
|
144
|
+
sign >= 0 ? self.dup : -self
|
145
|
+
end
|
146
|
+
|
147
|
+
# 符号
|
148
|
+
#
|
149
|
+
# @return [Integer] 0 との比較により、負,0,正の値を返す
|
150
|
+
#
|
151
|
+
def sign
|
152
|
+
@duration <=> 0
|
153
|
+
end
|
154
|
+
|
155
|
+
# 比較
|
156
|
+
#
|
157
|
+
# @param [Numeric, When::TM::Duration] other
|
158
|
+
#
|
159
|
+
# @return [Integer] other との比較により、負,0,正の値を返す
|
160
|
+
#
|
161
|
+
def <=>(other)
|
162
|
+
self.to_f <=> other.to_f
|
163
|
+
end
|
164
|
+
|
165
|
+
# オブジェクトの同値
|
166
|
+
#
|
167
|
+
# @param [Object] other
|
168
|
+
#
|
169
|
+
# @return [Boolean]
|
170
|
+
# [ true - 同値 ]
|
171
|
+
# [ false - 非同値 ]
|
172
|
+
#
|
173
|
+
def ==(other)
|
174
|
+
return false unless other.instance_of?(self.class)
|
175
|
+
return self.duration == other.duration
|
176
|
+
end
|
177
|
+
|
178
|
+
#
|
179
|
+
# 加算
|
180
|
+
#
|
181
|
+
# @param [Numeric, When::TM::Duration] other 秒数とみなす
|
182
|
+
# @param [その他] other 日時とみなす
|
183
|
+
#
|
184
|
+
# @return [When::TM::Duration] (other が Numeric, When::TM::Duration の場合)
|
185
|
+
# @return [other と同じクラス] (other がその他の場合)
|
186
|
+
#
|
187
|
+
def +(other)
|
188
|
+
case other
|
189
|
+
when Duration ; Duration.new(@duration + other.duration)
|
190
|
+
when Numeric ; Duration.new(@duration + other * SECOND)
|
191
|
+
else ; other + self
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
#
|
196
|
+
# 減算
|
197
|
+
#
|
198
|
+
# @param [Numeric, When::TM::Duration] other 秒数とみなす
|
199
|
+
#
|
200
|
+
# @return [When::TM::Duration]
|
201
|
+
#
|
202
|
+
def -(other)
|
203
|
+
Duration.new(@duration - (other.kind_of?(Duration) ? other.duration : other * SECOND))
|
204
|
+
end
|
205
|
+
|
206
|
+
#
|
207
|
+
# 乗算
|
208
|
+
#
|
209
|
+
# @param [Numeric] other
|
210
|
+
#
|
211
|
+
# @return [When::TM::Duration]
|
212
|
+
#
|
213
|
+
def *(other)
|
214
|
+
Duration.new(@duration * other)
|
215
|
+
end
|
216
|
+
|
217
|
+
#
|
218
|
+
# 除算
|
219
|
+
#
|
220
|
+
# @param [Numeric, When::TM::Duration] other 秒数とみなす
|
221
|
+
#
|
222
|
+
# @return [When::TM::Duration] (other が Numeric の場合)
|
223
|
+
# @return [Numeric] (other が When::TM::Duration の場合)
|
224
|
+
#
|
225
|
+
def /(other)
|
226
|
+
other.kind_of?(Duration) ? @duration / other.duration : Duration.new(@duration / other)
|
227
|
+
end
|
228
|
+
|
229
|
+
#
|
230
|
+
# 時間間隔を日時分秒を表すArrayに変換する
|
231
|
+
#
|
232
|
+
# @param [Integer] n 最下位要素のインデクス(デフォルト When::SECOND)
|
233
|
+
#
|
234
|
+
# @return [Array] [ 日, 時, 分, 秒 ]
|
235
|
+
#
|
236
|
+
def to_dhms(n=When::SECOND)
|
237
|
+
a = []
|
238
|
+
m = @duration
|
239
|
+
n.times do |i|
|
240
|
+
d, m = m.divmod(DurationUnits[i])
|
241
|
+
a << d
|
242
|
+
end
|
243
|
+
a << m / DurationUnits[n]
|
244
|
+
a
|
245
|
+
end
|
246
|
+
|
247
|
+
#
|
248
|
+
# 時間間隔の要素を取り出す
|
249
|
+
#
|
250
|
+
# @param [Integer] n 要素のインデクス
|
251
|
+
#
|
252
|
+
# @return [Numeric] (秒は Float, その他は Integer)
|
253
|
+
#
|
254
|
+
def [](n)
|
255
|
+
to_dhms([n+1,When::SECOND].min)[n]
|
256
|
+
end
|
257
|
+
|
258
|
+
#
|
259
|
+
# 指定時刻よりselfの時間間隔だけ後の時刻オブジェクト
|
260
|
+
#
|
261
|
+
# @param [::Time, When::TM::TemporalPosition] time 指定時刻
|
262
|
+
#
|
263
|
+
# @return [引数と同種の時刻オブジェクト]
|
264
|
+
#
|
265
|
+
def after(time=Time.now)
|
266
|
+
time + self
|
267
|
+
end
|
268
|
+
alias :since :after
|
269
|
+
|
270
|
+
#
|
271
|
+
# 指定時刻よりselfの時間間隔だけ前の時刻オブジェクト
|
272
|
+
#
|
273
|
+
# @param [::Time, When::TM::TemporalPosition] time 指定時刻
|
274
|
+
#
|
275
|
+
# @return [引数と同種の時刻オブジェクト]
|
276
|
+
#
|
277
|
+
def before(time=Time.now)
|
278
|
+
time - self
|
279
|
+
end
|
280
|
+
alias :ago :before
|
281
|
+
|
282
|
+
# When::TM::Duration オブジェクトを分かりやすい文字列にして返します
|
283
|
+
#
|
284
|
+
# @return [String] to_s と同様
|
285
|
+
#
|
286
|
+
def inspect
|
287
|
+
to_s
|
288
|
+
end
|
289
|
+
|
290
|
+
# 文字列化
|
291
|
+
#
|
292
|
+
# @return [String]
|
293
|
+
#
|
294
|
+
def to_s
|
295
|
+
to_dhms.to_s
|
296
|
+
end
|
297
|
+
|
298
|
+
#
|
299
|
+
# When::TM::Duration への変換
|
300
|
+
#
|
301
|
+
# @note 必ずコピーを作る
|
302
|
+
#
|
303
|
+
# @return [When::TM::Duration]
|
304
|
+
#
|
305
|
+
def to_duration
|
306
|
+
Duration.new(duration)
|
307
|
+
end
|
308
|
+
|
309
|
+
#
|
310
|
+
# ActiveSupport::Duration への変換
|
311
|
+
#
|
312
|
+
# @return [ActiveSupport::Duration]
|
313
|
+
#
|
314
|
+
def to_as_duration
|
315
|
+
[[:weeks, WEEK], [:days, DAY], [:hours, HOUR], [:minutes, MINUTE], [:seconds, SECOND]].each do |unit|
|
316
|
+
div, mod = duration.divmod(unit[1])
|
317
|
+
return div.send(unit[0]) if mod == 0
|
318
|
+
end
|
319
|
+
(duration / SECOND).seconds
|
320
|
+
end
|
321
|
+
|
322
|
+
# coerce
|
323
|
+
# @private
|
324
|
+
def coerce(other)
|
325
|
+
[other, @duration]
|
326
|
+
end
|
327
|
+
|
328
|
+
#
|
329
|
+
# Duration オブジェクトの初期化
|
330
|
+
#
|
331
|
+
# @param [Numeric] value Duration値 / 128秒
|
332
|
+
#
|
333
|
+
def initialize(value)
|
334
|
+
@duration = value
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|