when_exe 0.2.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/LICENSE.ja.txt +25 -0
- data/LICENSE.txt +31 -0
- data/Rakefile +1 -0
- data/bin/locales.rb +77 -0
- data/doc/COPYING +31 -0
- data/doc/COPYING.ja +25 -0
- data/doc/document_url +1 -0
- data/lib/when_exe.rb +14 -0
- data/lib/when_exe/core/duration.rb +72 -0
- data/lib/when_exe/tmduration.rb +330 -0
- data/lib/when_exe/version.rb +3 -0
- data/test/test.rb +19 -0
- data/test/test/tmobjects.rb +57 -0
- data/when_exe.gemspec +24 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.ja.txt
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
When.exe: A multicultural and multilingualized calendar library
|
2
|
+
|
3
|
+
本プログラムの著作権は須賀隆 <suchowan@box.email.ne.jp> にあります。
|
4
|
+
|
5
|
+
本プログラムは,下記の条件で使用できます.
|
6
|
+
|
7
|
+
1. 必ず、著作権表示や免責事項を含めて、制限なく自由に複製できます.
|
8
|
+
|
9
|
+
2. 以下の条件のいずれかを満たす時に本プログラムを自由に変更できます.
|
10
|
+
|
11
|
+
(a) 変更した本プログラムを自分だけで使う.
|
12
|
+
|
13
|
+
(b) その他の変更条件を作者と書面により合意する.
|
14
|
+
|
15
|
+
3. 本プログラム,変更した本プログラム及び本プログラムから生成した実行形式は,
|
16
|
+
作者との書面による合意がなければ配布できません.
|
17
|
+
|
18
|
+
4. 他のプログラムとの併合はいかなる目的であれ自由です.
|
19
|
+
|
20
|
+
5. 本プログラムへの入力となるデータおよび本プログラムからの出力の権利は
|
21
|
+
本プログラムの作者ではなく,それぞれの入出力を生成した人に属します.
|
22
|
+
|
23
|
+
6. 本プログラムは無保証です.作者は本プログラムをサポートする意志はありますが,
|
24
|
+
プログラム自身のバグあるいは本プログラムの実行などから発生するいかなる損害に
|
25
|
+
対しても責任を持ちません.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
When.exe: A multicultural and multilingualized calendar library
|
2
|
+
|
3
|
+
This library is copyrighted free software by Takashi SUGA <suchowan@box.email.ne.jp>.
|
4
|
+
|
5
|
+
You can use and/or modify the software under the conditions below:
|
6
|
+
|
7
|
+
1. You may make verbatim copies of the software without restriction, provided
|
8
|
+
that you duplicate all of the original copyright notices and associated
|
9
|
+
disclaimers.
|
10
|
+
|
11
|
+
2. You may modify your copy of the software in any way, provided that
|
12
|
+
you do at least ONE of the following:
|
13
|
+
|
14
|
+
a) place your modifications in your private domain.
|
15
|
+
|
16
|
+
b) make other written arrangements with the author.
|
17
|
+
|
18
|
+
3. You cannot distribute the software with/without modification or
|
19
|
+
derived executable binary form without written arrangements with the author.
|
20
|
+
|
21
|
+
4. You may modify and include the part of the software into any other
|
22
|
+
software.
|
23
|
+
|
24
|
+
5. The data supplied as input to or produced as output from the software
|
25
|
+
do not automatically fall under the copyright of the software,
|
26
|
+
but belong to whomever generated them.
|
27
|
+
|
28
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
29
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
30
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
31
|
+
PURPOSE.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/locales.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
=begin
|
4
|
+
Copyright (C) 2011-2012 Takashi SUGA
|
5
|
+
|
6
|
+
You may use and/or modify this file according to the license described in the COPYING file included in this archive.
|
7
|
+
=end
|
8
|
+
|
9
|
+
require 'pp'
|
10
|
+
require 'yaml'
|
11
|
+
require 'fileutils'
|
12
|
+
require 'net/https'
|
13
|
+
|
14
|
+
HEADER = <<HEADER
|
15
|
+
# -*- coding: utf-8 -*-
|
16
|
+
=begin
|
17
|
+
Copyright (C) 2012-2013 Takashi SUGA
|
18
|
+
|
19
|
+
You may use and/or modify this file according to the license described in the COPYING file included in this archive.
|
20
|
+
=end
|
21
|
+
|
22
|
+
module When::BasicTypes
|
23
|
+
class M17n
|
24
|
+
HEADER
|
25
|
+
|
26
|
+
if "1".respond_to?(:force_encoding)
|
27
|
+
Encoding.default_external = 'UTF-8'
|
28
|
+
Encoding.default_internal = 'UTF-8'
|
29
|
+
end
|
30
|
+
yamls = {}
|
31
|
+
["locales/yaml", "locales/locales"].each do |dir|
|
32
|
+
FileUtils.mkdir_p(dir) unless FileTest.exist?(dir)
|
33
|
+
end
|
34
|
+
site = Net::HTTP.new('github.com', '443')
|
35
|
+
site.use_ssl = true
|
36
|
+
site.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
37
|
+
site.get('/svenfuchs/rails-i18n/tree/master/rails/locale').body.scan(/>([^> ]+?)\.yml</) do |match|
|
38
|
+
code = match[0]
|
39
|
+
locale = nil
|
40
|
+
begin
|
41
|
+
open("locales/yaml/#{code}.yml", 'r') do |source|
|
42
|
+
locale = source.read
|
43
|
+
end
|
44
|
+
rescue Errno::ENOENT
|
45
|
+
http = Net::HTTP.new('raw.github.com', '443')
|
46
|
+
http.use_ssl = true
|
47
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
48
|
+
sleep(1)
|
49
|
+
locale = http.get("/svenfuchs/rails-i18n/master/rails/locale/#{code}.yml").body
|
50
|
+
locale = locale.force_encoding('UTF-8') if "1".respond_to?(:force_encoding)
|
51
|
+
open("locales/yaml/#{code}.yml", 'w') do |file|
|
52
|
+
file.write(locale)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
yaml = YAML.load(locale)
|
56
|
+
next unless yaml
|
57
|
+
yaml[code]['time']['formats']['time'] ||= yaml[code]['time']['formats']['default'][/%H.*$/].sub(/\s*%Y.*$/,'')
|
58
|
+
yamls[code.sub(/-/,'_')] = {'date'=>yaml[code]['date'], 'time'=>yaml[code]['time'], 'datetime'=>yaml[code]['datetime']}
|
59
|
+
end
|
60
|
+
|
61
|
+
open("locales/locales/locales.rb", 'w') do |script|
|
62
|
+
script.puts HEADER
|
63
|
+
yamls.each_key do |key|
|
64
|
+
script.puts " autoload :Locale_%-8s 'when_exe/locales/%s'" % [key + ',', key]
|
65
|
+
end
|
66
|
+
script.puts " end\nend"
|
67
|
+
end
|
68
|
+
|
69
|
+
yamls.each_pair do |key, value|
|
70
|
+
open("locales/locales/#{key}.rb", 'w') do |script|
|
71
|
+
script.puts HEADER
|
72
|
+
script.puts "\n # from https://raw.github.com/svenfuchs/rails-i18n/master/rails/locale/#{key.sub(/_/,'-')}.yml"
|
73
|
+
script.puts "\n Locale_#{key} ="
|
74
|
+
script.write(value.pretty_inspect)
|
75
|
+
script.puts " end\nend"
|
76
|
+
end
|
77
|
+
end
|
data/doc/COPYING
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
When.exe: A multicultural and multilingualized calendar library
|
2
|
+
|
3
|
+
This library is copyrighted free software by Takashi SUGA <suchowan@box.email.ne.jp>.
|
4
|
+
|
5
|
+
You can use and/or modify the software under the conditions below:
|
6
|
+
|
7
|
+
1. You may make verbatim copies of the software without restriction, provided
|
8
|
+
that you duplicate all of the original copyright notices and associated
|
9
|
+
disclaimers.
|
10
|
+
|
11
|
+
2. You may modify your copy of the software in any way, provided that
|
12
|
+
you do at least ONE of the following:
|
13
|
+
|
14
|
+
a) place your modifications in your private domain.
|
15
|
+
|
16
|
+
b) make other written arrangements with the author.
|
17
|
+
|
18
|
+
3. You cannot distribute the software with/without modification or
|
19
|
+
derived executable binary form without written arrangements with the author.
|
20
|
+
|
21
|
+
4. You may modify and include the part of the software into any other
|
22
|
+
software.
|
23
|
+
|
24
|
+
5. The data supplied as input to or produced as output from the software
|
25
|
+
do not automatically fall under the copyright of the software,
|
26
|
+
but belong to whomever generated them.
|
27
|
+
|
28
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
29
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
30
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
31
|
+
PURPOSE.
|
data/doc/COPYING.ja
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
When.exe: A multicultural and multilingualized calendar library
|
2
|
+
|
3
|
+
本プログラムの著作権は須賀隆 <suchowan@box.email.ne.jp> にあります。
|
4
|
+
|
5
|
+
本プログラムは,下記の条件で使用できます.
|
6
|
+
|
7
|
+
1. 必ず、著作権表示や免責事項を含めて、制限なく自由に複製できます.
|
8
|
+
|
9
|
+
2. 以下の条件のいずれかを満たす時に本プログラムを自由に変更できます.
|
10
|
+
|
11
|
+
(a) 変更した本プログラムを自分だけで使う.
|
12
|
+
|
13
|
+
(b) その他の変更条件を作者と書面により合意する.
|
14
|
+
|
15
|
+
3. 本プログラム,変更した本プログラム及び本プログラムから生成した実行形式は,
|
16
|
+
作者との書面による合意がなければ配布できません.
|
17
|
+
|
18
|
+
4. 他のプログラムとの併合はいかなる目的であれ自由です.
|
19
|
+
|
20
|
+
5. 本プログラムへの入力となるデータおよび本プログラムからの出力の権利は
|
21
|
+
本プログラムの作者ではなく,それぞれの入出力を生成した人に属します.
|
22
|
+
|
23
|
+
6. 本プログラムは無保証です.作者は本プログラムをサポートする意志はありますが,
|
24
|
+
プログラム自身のバグあるいは本プログラムの実行などから発生するいかなる損害に
|
25
|
+
対しても責任を持ちません.
|
data/doc/document_url
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
See online document http://www2u.biglobe.ne.jp/~suchowan/u/wiki.cgi?Calendar%2FWhen%2FRuby%2F2%2EAPI%E3%81%AE%E4%BD%BF%E7%94%A8%E4%BE%8B%2F4%2E%E6%99%82%E9%96%93%E9%96%93%E9%9A%94%2F%E6%9C%80%E5%B0%8F%E3%82%BB%E3%83%83%E3%83%88
|
data/lib/when_exe.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin
|
3
|
+
|
4
|
+
Copyright (C) 2011-2013 Takashi SUGA
|
5
|
+
|
6
|
+
You may use and/or modify this file according to the license described in the COPYING file included in this archive.
|
7
|
+
=end
|
8
|
+
|
9
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
10
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
11
|
+
|
12
|
+
require 'when_exe/version'
|
13
|
+
require 'when_exe/tmduration'
|
14
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin
|
3
|
+
Copyright (C) 2013 Takashi SUGA
|
4
|
+
|
5
|
+
You may use and/or modify this file according to the license described in the COPYING file included in this archive.
|
6
|
+
=end
|
7
|
+
|
8
|
+
#
|
9
|
+
# When::TM::Duration のための標準クラスの拡張
|
10
|
+
#
|
11
|
+
|
12
|
+
#
|
13
|
+
# Extensions to Time class
|
14
|
+
#
|
15
|
+
# @private
|
16
|
+
class Time
|
17
|
+
|
18
|
+
alias :_plus_ :+
|
19
|
+
def +(other)
|
20
|
+
other.kind_of?(When::TM::Duration) ? self + other.to_f : self._plus_(other)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
alias :_minus_ :-
|
25
|
+
def -(other)
|
26
|
+
other.kind_of?(When::TM::Duration) ? self - other.to_f : self._minus_(other)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Extensions to Numeric class
|
32
|
+
#
|
33
|
+
class Numeric
|
34
|
+
|
35
|
+
#
|
36
|
+
# メソッド名に相当する単位で表した When::TM::Duration
|
37
|
+
# @method unit_duration
|
38
|
+
# @return [When::TM::Duration]
|
39
|
+
# @note unit は second, minute, hour, day, week または
|
40
|
+
# seconds, minutes, hours, days, weeks に読み替える
|
41
|
+
# @note core/duration
|
42
|
+
|
43
|
+
# @private
|
44
|
+
When::TM::Duration::Unit.keys.each do |key|
|
45
|
+
|
46
|
+
module_eval %Q{
|
47
|
+
# for When::TM::Duration
|
48
|
+
def #{key}_duration
|
49
|
+
When::TM::Duration.new(self * When::TM::Duration::Unit['#{key}'])
|
50
|
+
end
|
51
|
+
alias :#{key}s_duration :#{key}_duration
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
#
|
57
|
+
# Extensions to Array class
|
58
|
+
#
|
59
|
+
class Array
|
60
|
+
|
61
|
+
#
|
62
|
+
# self を Array<日, 時, 分, 秒> とみなして When::TM::Duration を生成
|
63
|
+
#
|
64
|
+
# @return [When::TM::Duration]
|
65
|
+
#
|
66
|
+
# @note core/duration
|
67
|
+
#
|
68
|
+
def duration
|
69
|
+
When::TM::Duration.dhms(*self)
|
70
|
+
end
|
71
|
+
alias :to_duration :duration
|
72
|
+
end
|
@@ -0,0 +1,330 @@
|
|
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 COPYING file included in this archive.
|
8
|
+
|
9
|
+
=end
|
10
|
+
|
11
|
+
module When
|
12
|
+
|
13
|
+
# 分解能定数
|
14
|
+
|
15
|
+
YEAR = -2
|
16
|
+
MONTH = -1
|
17
|
+
WEEK = -0.5
|
18
|
+
DAY = 0
|
19
|
+
HOUR = 1
|
20
|
+
MINUTE = 2
|
21
|
+
SECOND = 3
|
22
|
+
# CENTURY = -4
|
23
|
+
# DECADE = -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
|
+
# 文字列化
|
283
|
+
#
|
284
|
+
# @return [String]
|
285
|
+
#
|
286
|
+
def to_s
|
287
|
+
to_dhms.to_s
|
288
|
+
end
|
289
|
+
|
290
|
+
#
|
291
|
+
# When::TM::Duration への変換
|
292
|
+
#
|
293
|
+
# @note 必ずコピーを作る
|
294
|
+
#
|
295
|
+
# @return [When::TM::Duration]
|
296
|
+
#
|
297
|
+
def to_duration
|
298
|
+
Duration.new(duration)
|
299
|
+
end
|
300
|
+
|
301
|
+
#
|
302
|
+
# ActiveSupport::Duration への変換
|
303
|
+
#
|
304
|
+
# @return [ActiveSupport::Duration]
|
305
|
+
#
|
306
|
+
def to_as_duration
|
307
|
+
[[:weeks, WEEK], [:days, DAY], [:hours, HOUR], [:minutes, MINUTE], [:seconds, SECOND]].each do |unit|
|
308
|
+
div, mod = duration.divmod(unit[1])
|
309
|
+
return div.send(unit[0]) if mod == 0
|
310
|
+
end
|
311
|
+
(duration / SECOND).seconds
|
312
|
+
end
|
313
|
+
|
314
|
+
# coerce
|
315
|
+
# @private
|
316
|
+
def coerce(other)
|
317
|
+
[other, @duration]
|
318
|
+
end
|
319
|
+
|
320
|
+
#
|
321
|
+
# Duration オブジェクトの初期化
|
322
|
+
#
|
323
|
+
# @param [Numeric] value Duration値 / 128秒
|
324
|
+
#
|
325
|
+
def initialize(value)
|
326
|
+
@duration = value
|
327
|
+
end
|
328
|
+
end
|
329
|
+
end
|
330
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
=begin
|
4
|
+
Copyright (C) 2011-2013 Takashi SUGA
|
5
|
+
|
6
|
+
You may use and/or modify this file according to the license
|
7
|
+
described in the COPYING file included in this archive.
|
8
|
+
=end
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'minitest/unit'
|
12
|
+
require 'minitest/autorun'
|
13
|
+
Test = MiniTest
|
14
|
+
rescue LoadError
|
15
|
+
require 'test/unit'
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'when_exe'
|
19
|
+
require './test/tmobjects'
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
=begin
|
3
|
+
Copyright (C) 2011-2013 Takashi SUGA
|
4
|
+
|
5
|
+
You may use and/or modify this file according to the license
|
6
|
+
described in the COPYING file included in this archive.
|
7
|
+
=end
|
8
|
+
|
9
|
+
module Test::TM
|
10
|
+
class Duration < Test::Unit::TestCase
|
11
|
+
def test__duration_constructors
|
12
|
+
assert_equal( 1, When::TM::Duration.new(1).duration)
|
13
|
+
assert_equal( 1.0/128, When::TM::Duration.second(1).duration)
|
14
|
+
assert_equal( 60.0/128, When::TM::Duration.minute(1).duration)
|
15
|
+
assert_equal( 3600.0/128, When::TM::Duration.hour(1).duration)
|
16
|
+
assert_equal(86400.0/128, When::TM::Duration.day(1).duration)
|
17
|
+
assert_equal(93784.0/128, When::TM::Duration.dhms(1,2,3,4).duration)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test__duration_values
|
21
|
+
assert_equal(1, When::TM::Duration.new(1).system)
|
22
|
+
assert_equal(2, When::TM::Duration.second(2).second)
|
23
|
+
assert_equal(3, When::TM::Duration.minute(3).minute)
|
24
|
+
assert_equal(4, When::TM::Duration.hour(4).hour)
|
25
|
+
assert_equal(5, When::TM::Duration.day(5).day)
|
26
|
+
assert_equal([1,2,3,4], When::TM::Duration.dhms(1,2,3,4).to_dhms)
|
27
|
+
assert_equal([-2,21,56,56], (-When::TM::Duration.dhms(1,2,3,4)).to_dhms)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test__duration_elements
|
31
|
+
duration = When::TM::Duration.dhms(1,2,3,4.5)
|
32
|
+
assert_equal(1, duration[0])
|
33
|
+
assert_equal(2, duration[1])
|
34
|
+
assert_equal(3, duration[2])
|
35
|
+
assert_equal(4.5, duration[3])
|
36
|
+
end
|
37
|
+
|
38
|
+
def test__duration_arithmetics
|
39
|
+
assert_equal(When::TM::Duration.dhms(-2,21,56,56), -When::TM::Duration.dhms(1,2,3,4))
|
40
|
+
assert_equal(When::TM::Duration.dhms(2,3,4,5), When::TM::Duration.dhms(1,2,3,4) + When::TM::Duration.dhms(1,1,1,1))
|
41
|
+
assert_equal(When::TM::Duration.dhms(0,1,2,3), When::TM::Duration.dhms(1,2,3,4) - When::TM::Duration.dhms(1,1,1,1))
|
42
|
+
assert_equal(When::TM::Duration.dhms(2,4,6,8), When::TM::Duration.dhms(1,2,3,4) * 2)
|
43
|
+
assert_equal(When::TM::Duration.dhms(1,2,3,4), When::TM::Duration.dhms(2,4,6,8) / 2)
|
44
|
+
assert_equal(2, When::TM::Duration.dhms(2,4,6,8) / When::TM::Duration.dhms(1,2,3,4))
|
45
|
+
end
|
46
|
+
|
47
|
+
def test__duration_and_time
|
48
|
+
start = Time.now
|
49
|
+
duration = When::TM::Duration.dhms(1,2,3,4.4)
|
50
|
+
stop = start + duration
|
51
|
+
assert_equal((RUBY_VERSION.to_f<1.9 ? 93784.4 : 93784), stop-start)
|
52
|
+
require 'when_exe/core/duration'
|
53
|
+
stop = start + duration
|
54
|
+
assert_equal(93784.4, stop-start)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/when_exe.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "when_exe/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "when_exe"
|
7
|
+
s.version = When::VERSION
|
8
|
+
s.authors = ["Takashi SUGA"]
|
9
|
+
s.email = ["suchowan@box.email.ne.jp"]
|
10
|
+
s.homepage = "http://www2u.biglobe.ne.jp/~suchowan/u/wiki.cgi?Calendar%2FWhen%2FRuby%2F2%2EAPI%E3%81%AE%E4%BD%BF%E7%94%A8%E4%BE%8B%2F4%2E%E6%99%82%E9%96%93%E9%96%93%E9%9A%94%2F%E6%9C%80%E5%B0%8F%E3%82%BB%E3%83%83%E3%83%88"
|
11
|
+
s.summary = %q{A multicultural and multilingualized calendar library based on ISO 8601, ISO 19108 and RFC 5545}
|
12
|
+
s.description = %q{This version is minimum subset and only supports When::TM::Duration class.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "when_exe"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: when_exe
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.100
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Takashi SUGA
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-04 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: This version is minimum subset and only supports When::TM::Duration class.
|
15
|
+
email:
|
16
|
+
- suchowan@box.email.ne.jp
|
17
|
+
executables:
|
18
|
+
- locales.rb
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.ja.txt
|
25
|
+
- LICENSE.txt
|
26
|
+
- Rakefile
|
27
|
+
- bin/locales.rb
|
28
|
+
- doc/COPYING
|
29
|
+
- doc/COPYING.ja
|
30
|
+
- doc/document_url
|
31
|
+
- lib/when_exe.rb
|
32
|
+
- lib/when_exe/core/duration.rb
|
33
|
+
- lib/when_exe/tmduration.rb
|
34
|
+
- lib/when_exe/version.rb
|
35
|
+
- test/test.rb
|
36
|
+
- test/test/tmobjects.rb
|
37
|
+
- when_exe.gemspec
|
38
|
+
homepage: http://www2u.biglobe.ne.jp/~suchowan/u/wiki.cgi?Calendar%2FWhen%2FRuby%2F2%2EAPI%E3%81%AE%E4%BD%BF%E7%94%A8%E4%BE%8B%2F4%2E%E6%99%82%E9%96%93%E9%96%93%E9%9A%94%2F%E6%9C%80%E5%B0%8F%E3%82%BB%E3%83%83%E3%83%88
|
39
|
+
licenses: []
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project: when_exe
|
58
|
+
rubygems_version: 1.7.2
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: A multicultural and multilingualized calendar library based on ISO 8601,
|
62
|
+
ISO 19108 and RFC 5545
|
63
|
+
test_files: []
|
64
|
+
has_rdoc:
|