when_exe 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +210 -171
  3. data/bin/irb.rc +1 -0
  4. data/lib/when_exe.rb +78 -53
  5. data/lib/when_exe/basictypes.rb +27 -8
  6. data/lib/when_exe/calendarnote.rb +848 -805
  7. data/lib/when_exe/calendartypes.rb +110 -240
  8. data/lib/when_exe/coordinates.rb +2440 -2175
  9. data/lib/when_exe/core/compatibility.rb +1 -1
  10. data/lib/when_exe/core/duration.rb +13 -11
  11. data/lib/when_exe/core/extension.rb +38 -45
  12. data/lib/when_exe/ephemeris.rb +43 -14
  13. data/lib/when_exe/ephemeris/eclipse.rb +149 -0
  14. data/lib/when_exe/ephemeris/notes.rb +39 -7
  15. data/lib/when_exe/icalendar.rb +2 -6
  16. data/lib/when_exe/inspect.rb +1408 -1399
  17. data/lib/when_exe/parts/enumerator.rb +486 -477
  18. data/lib/when_exe/parts/resource.rb +1101 -1069
  19. data/lib/when_exe/parts/timezone.rb +6 -5
  20. data/lib/when_exe/region/babylonian.rb +405 -405
  21. data/lib/when_exe/region/bahai.rb +21 -61
  22. data/lib/when_exe/region/chinese/epochs.rb +81 -81
  23. data/lib/when_exe/region/chinese/twins.rb +51 -51
  24. data/lib/when_exe/region/christian.rb +7 -2
  25. data/lib/when_exe/region/coptic.rb +1 -1
  26. data/lib/when_exe/region/discordian.rb +9 -16
  27. data/lib/when_exe/region/french.rb +1 -1
  28. data/lib/when_exe/region/hanke_henry.rb +57 -0
  29. data/lib/when_exe/region/indian.rb +41 -73
  30. data/lib/when_exe/region/international_fixed.rb +97 -0
  31. data/lib/when_exe/region/iranian.rb +203 -203
  32. data/lib/when_exe/region/japanese.rb +13 -13
  33. data/lib/when_exe/region/japanese/eclipses.rb +1194 -0
  34. data/lib/when_exe/region/japanese/notes.rb +1482 -1383
  35. data/lib/when_exe/region/japanese/residues.rb +726 -721
  36. data/lib/when_exe/region/japanese/twins.rb +37 -37
  37. data/lib/when_exe/region/jewish.rb +2 -2
  38. data/lib/when_exe/region/pax.rb +60 -0
  39. data/lib/when_exe/region/positivist.rb +100 -0
  40. data/lib/when_exe/region/roman.rb +333 -334
  41. data/lib/when_exe/region/shire.rb +3 -20
  42. data/lib/when_exe/region/thai.rb +2 -2
  43. data/lib/when_exe/region/tibetan.rb +3 -3
  44. data/lib/when_exe/region/tranquility.rb +208 -0
  45. data/lib/when_exe/region/vanishing_leprechaun.rb +53 -0
  46. data/lib/when_exe/region/vietnamese.rb +4 -4
  47. data/lib/when_exe/region/world.rb +9 -13
  48. data/lib/when_exe/region/world_season.rb +89 -0
  49. data/lib/when_exe/region/zoroastrian.rb +4 -2
  50. data/lib/when_exe/tmobjects.rb +14 -4
  51. data/lib/when_exe/tmposition.rb +239 -81
  52. data/lib/when_exe/tmreference.rb +57 -28
  53. data/lib/when_exe/version.rb +1 -1
  54. data/link_to_online_documents +6 -3
  55. data/test/examples/today.rb +1 -1
  56. data/test/scripts.rb +23 -0
  57. data/test/scripts/2.ext.rb +169 -0
  58. data/test/scripts/2.rb +169 -0
  59. data/test/scripts/3.ext.rb +133 -0
  60. data/test/scripts/3.rb +134 -0
  61. data/test/scripts/4.ext.rb +112 -0
  62. data/test/scripts/4.min.rb +65 -0
  63. data/test/scripts/4.rb +136 -0
  64. data/test/scripts/5.ext.rb +78 -0
  65. data/test/scripts/5.rb +81 -0
  66. data/test/scripts/6.gcal.rb +131 -0
  67. data/test/scripts/6.rb +205 -0
  68. data/test/scripts/6.tz.rb +105 -0
  69. data/test/scripts/7.phase.rb +109 -0
  70. data/test/scripts/7.rb +95 -0
  71. data/test/scripts/7.term.rb +128 -0
  72. data/test/scripts/7.week.rb +84 -0
  73. data/test/scripts/8.ext.rb +61 -0
  74. data/test/scripts/8.rb +62 -0
  75. data/test/scripts/9.ext.rb +131 -0
  76. data/test/scripts/9.rb +130 -0
  77. data/test/scripts/chinese-luni-solar.rb +52 -0
  78. data/test/{examples → scripts}/geometric_complex.rb +41 -41
  79. data/test/scripts/geometric_complex.txt +18 -0
  80. data/test/scripts/korea.rb +59 -0
  81. data/test/scripts/thai-reviewed.txt +211 -0
  82. data/test/scripts/thai.rb +36 -0
  83. data/test/scripts/thai.txt +210 -0
  84. data/test/test.rb +7 -0
  85. data/test/test/basictypes.rb +22 -0
  86. data/test/test/coordinates.rb +2 -1
  87. data/test/test/ephemeris.rb +34 -2
  88. data/test/test/icalendar.rb +12 -0
  89. data/test/test/inspect.rb +37 -1
  90. data/test/test/parts.rb +4 -3
  91. data/test/test/region/armenian.rb +20 -0
  92. data/test/test/region/bahai.rb +58 -0
  93. data/test/test/region/chinese.rb +14 -3
  94. data/test/test/region/christian.rb +16 -35
  95. data/test/test/region/discordian.rb +20 -0
  96. data/test/test/region/indian.rb +30 -2
  97. data/test/test/region/japanese.rb +24 -0
  98. data/test/test/region/jewish.rb +2 -0
  99. data/test/test/region/m17n.rb +9 -0
  100. data/test/test/region/reforms.rb +121 -0
  101. data/test/test/region/residue.rb +17 -11
  102. data/test/test/region/shire.rb +58 -0
  103. data/test/test/region/swedish.rb +45 -0
  104. data/test/test/region/zoroastrian.rb +58 -0
  105. data/test/test/tmobjects.rb +74 -0
  106. data/test/test/tmposition.rb +468 -397
  107. data/when_exe.gemspec +2 -2
  108. metadata +49 -6
@@ -0,0 +1,133 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin
3
+ Copyright (C) 2014 Takashi SUGA
4
+
5
+ You may use and/or modify this file according to the license described in the LICENSE.txt file included in this archive.
6
+ =end
7
+
8
+ # Calendar/When/Ruby/2.APIの使用例/3.年号の扱い/コア拡張
9
+
10
+ # コアクラスを require 'when_exe/core/extension' で拡張した場合の記法を確認
11
+
12
+ # *準備
13
+ # when_exe Ruby 版の When モジュールを include する
14
+ require 'when_exe'
15
+ require 'when_exe/core/extension'
16
+ include When
17
+
18
+ # 使用可能な &yard(CalendarEra) は http://hosi.org/frames/When/TM/CalendarEra.html 参照
19
+
20
+ # *グレゴリオ暦 ⇔ 現代日本暦日
21
+ # **最近の日付
22
+ date = [2013, 5, 25].tm_pos
23
+ p date #=> 2013-05-25
24
+ p 'ModernJapanese'.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/ModernJapanese"
25
+ list = date ^ 'ModernJapanese'.calendar_era
26
+ p list #=> [H25(2013).05.25]
27
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian?note=DefaultNotes"
28
+ date = ['H', 25, 5, 25].tm_pos
29
+ p date #=> H25(2013).05.25
30
+ p 'Gregorian'.calendar ^ date #=> 2013-05-25
31
+ date = 'H25.5.25'.when?
32
+ p date #=> H25(2013).05.25
33
+ p 'Gregorian'.calendar ^ date #=> 2013-05-25
34
+
35
+ # *グレゴリオ暦 ⇔ 西暦(CE/BCE)
36
+ # **グレゴリオ暦改暦直後
37
+ date = '1582-10-15'.when?
38
+ p date #=> 1582-10-15
39
+ p 'Common'.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/Common"
40
+ list = date ^ 'Common'.calendar_era
41
+ p list #=> [CE1582.10.15]
42
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian"
43
+ date = ['CE', 1582, 10, 15].tm_pos
44
+ p date #=> CE1582.10.15
45
+ p 'Gregorian'.calendar ^ date #=> 1582-10-15
46
+ date = 'CE1582.10.15'.when?
47
+ p date #=> CE1582.10.15
48
+ p 'Gregorian'.calendar ^ date #=> 1582-10-15
49
+
50
+ # **グレゴリオ暦改暦直前
51
+ date = '1582-10-14'.when?
52
+ p date #=> 1582-10-14
53
+ list = date ^ 'Common'.calendar_era
54
+ p list #=> [CE1582.10.04]
55
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Julian"
56
+ date = ['CE', 1582, 10, 4].tm_pos
57
+ p date #=> CE1582.10.04
58
+ p 'Gregorian'.calendar ^ date #=> 1582-10-14
59
+ date = 'CE1582.10.4'.when?
60
+ p date #=> CE1582.10.04
61
+ p 'Gregorian'.calendar ^ date #=> 1582-10-14
62
+
63
+ # **紀元前45年
64
+ date = '-44-03-13'.when?
65
+ p date #=> -00044-03-13
66
+ list = date ^ 'Common'.calendar_era
67
+ p list #=> [BCE45(-044).03.15]
68
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Julian"
69
+ date = ['BCE', 45, 3, 15].tm_pos
70
+ p date #=> BCE45(-044).03.15
71
+ p 'Gregorian'.calendar ^ date #=> -00044-03-13
72
+ date = 'BCE45.3.15'.when?
73
+ p date #=> BCE45(-044).03.15
74
+ p 'Gregorian'.calendar ^ date #=> -00044-03-13
75
+
76
+ # *天保年号のバリエーション
77
+ # **日本
78
+ date = '天保2.3.4'.when? # ここで日本暦日をメモリに展開するため、少々時間がかかる
79
+ p date #=> 天保02(1831).03.04
80
+ p date.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/Japanese::江戸時代::天保"
81
+ # **南朝の後梁
82
+ date = '天保2.3.4'.when?(:count=>2) # ここで中国暦日をメモリに展開するため、さらに時間がかかる
83
+ p date #=> 天保02(0563).03.04
84
+ p date.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/Chinese::南朝::後梁::天保"
85
+ date = '後梁::天保2.3.4'.when?
86
+ p date #=> 天保02(0563).03.04
87
+ # **北朝の北斉
88
+ date = '天保2.3.4'.when?(:count=>3)
89
+ p date #=> 天保02(0551).03.04
90
+ p date.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/Chinese::北朝::北斉::天保"
91
+ date = '北斉::天保2.3.4'.when?
92
+ p date #=> 天保02(0551).03.04
93
+ # **四つ目はない
94
+ begin
95
+ date = '天保2.3.4'.when?(:count=>4)
96
+ rescue => e
97
+ p e.to_s #=> ArgumentError: CalendarEraName doesn't exist: 天保
98
+ end
99
+
100
+ # *明治改元
101
+ # **改元前
102
+ date = '1868-10-22'.when?
103
+ p date #=> 1868-10-22
104
+ list = date ^ 'Japanese'.calendar_era
105
+ p list #=> [慶応04(1868).09.07, 明治01(1868).09.07]
106
+ p list[0].calendar_name[3] #=> nil
107
+ p list[1].calendar_name[3] #=> true (改元前)
108
+ # **改元後
109
+ date = '1868-10-23'.when?
110
+ p date #=> 1868-10-23
111
+ list = date ^ 'Japanese'.calendar_era
112
+ p list #=> [明治01(1868).09.08]
113
+ p list[0].calendar_name[3] #=> nil
114
+ p list[0].calendar_era.reference_date #=> 01(1868)
115
+
116
+ # 年号の reference_date の“精度”が“年”なら、年初に遡って検索をヒットさせる
117
+
118
+ # *大正改元
119
+ # **改元前
120
+ date = '1912-7-29'.when?
121
+ p date #=> 1912-07-29
122
+ list = date ^ 'Japanese'.calendar_era
123
+ p list #=> [明治45(1912).07.29]
124
+ p list[0].calendar_name[3] #=> nil
125
+ # **改元後
126
+ date = '1912-7-30'.when?
127
+ p date #=> 1912-07-30
128
+ list = date ^ 'Japanese'.calendar_era
129
+ p list #=> [大正01(1912).07.30]
130
+ p list[0].calendar_name[3] #=> nil
131
+ p list[0].calendar_era.reference_date #=> 01(1912).07.30
132
+
133
+ # 年号の reference_date の“精度”が“日”なら、年初に遡って検索をヒットさせない
data/test/scripts/3.rb ADDED
@@ -0,0 +1,134 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin
3
+ Copyright (C) 2014 Takashi SUGA
4
+
5
+ You may use and/or modify this file according to the license described in the LICENSE.txt file included in this archive.
6
+ =end
7
+
8
+ # Calendar/When/Ruby/2.APIの使用例/3.年号の扱い
9
+
10
+ # →関連クラス図 [[Calendar/When/Ruby/3.クラス図/13.暦及び時計]]
11
+
12
+ # *準備
13
+ # when_exe Ruby 版の When モジュールを include する
14
+ require 'when_exe'
15
+ include When
16
+
17
+ # コア拡張を利用する場合は[[Calendar/When/Ruby/2.APIの使用例/3.年号の扱い/コア拡張]]を参照
18
+
19
+ # 使用可能な &yard(CalendarEra) は http://hosi.org/frames/When/TM/CalendarEra.html 参照
20
+
21
+ # *グレゴリオ暦 ⇔ 現代日本暦日
22
+ # **最近の日付
23
+ date = tm_pos(2013, 5, 25)
24
+ p date #=> 2013-05-25
25
+ p CalendarEra('ModernJapanese').iri #=> "http://hosi.org/When/TM/CalendarEra/ModernJapanese"
26
+ list = date ^ CalendarEra('ModernJapanese')
27
+ p list #=> [H25(2013).05.25]
28
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian?note=DefaultNotes"
29
+ date = tm_pos('H', 25, 5, 25)
30
+ p date #=> H25(2013).05.25
31
+ p Calendar('Gregorian') ^ date #=> 2013-05-25
32
+ date = when?('H25.5.25')
33
+ p date #=> H25(2013).05.25
34
+ p Calendar('Gregorian') ^ date #=> 2013-05-25
35
+
36
+ # *グレゴリオ暦 ⇔ 西暦(CE/BCE)
37
+ # **グレゴリオ暦改暦直後
38
+ date = when?('1582-10-15')
39
+ p date #=> 1582-10-15
40
+ p CalendarEra('Common').iri #=> "http://hosi.org/When/TM/CalendarEra/Common"
41
+ list = date ^ CalendarEra('Common')
42
+ p list #=> [CE1582.10.15]
43
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Gregorian"
44
+ date = tm_pos('CE', 1582, 10, 15)
45
+ p date #=> CE1582.10.15
46
+ p Calendar('Gregorian') ^ date #=> 1582-10-15
47
+ date = when?('CE1582.10.15')
48
+ p date #=> CE1582.10.15
49
+ p Calendar('Gregorian') ^ date #=> 1582-10-15
50
+
51
+ # **グレゴリオ暦改暦直前
52
+ date = when?('1582-10-14')
53
+ p date #=> 1582-10-14
54
+ list = date ^ CalendarEra('Common')
55
+ p list #=> [CE1582.10.04]
56
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Julian"
57
+ date = tm_pos('CE', 1582, 10, 4)
58
+ p date #=> CE1582.10.04
59
+ p Calendar('Gregorian') ^ date #=> 1582-10-14
60
+ date = when?('CE1582.10.4')
61
+ p date #=> CE1582.10.04
62
+ p Calendar('Gregorian') ^ date #=> 1582-10-14
63
+
64
+ # **紀元前45年
65
+ date = when?('-44-03-13')
66
+ p date #=> -00044-03-13
67
+ list = date ^ CalendarEra('Common')
68
+ p list #=> [BCE45(-044).03.15]
69
+ p list[0].frame.iri #=> "http://hosi.org/When/CalendarTypes/Julian"
70
+ date = tm_pos('BCE', 45, 3, 15)
71
+ p date #=> BCE45(-044).03.15
72
+ p Calendar('Gregorian') ^ date #=> -00044-03-13
73
+ date = when?('BCE45.3.15')
74
+ p date #=> BCE45(-044).03.15
75
+ p Calendar('Gregorian') ^ date #=> -00044-03-13
76
+
77
+ # *天保年号のバリエーション
78
+ # **日本
79
+ date = when?('天保2.3.4') # ここで日本暦日をメモリに展開するため、少々時間がかかる
80
+ p date #=> 天保02(1831).03.04
81
+ p date.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/Japanese::江戸時代::天保"
82
+ # **南朝の後梁
83
+ date = when?('天保2.3.4', :count=>2) # ここで中国暦日をメモリに展開するため、さらに時間がかかる
84
+ p date #=> 天保02(0563).03.04
85
+ p date.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/Chinese::南朝::後梁::天保"
86
+ date = when?('後梁::天保2.3.4')
87
+ p date #=> 天保02(0563).03.04
88
+ # **北朝の北斉
89
+ date = when?('天保2.3.4', :count=>3)
90
+ p date #=> 天保02(0551).03.04
91
+ p date.calendar_era.iri #=> "http://hosi.org/When/TM/CalendarEra/Chinese::北朝::北斉::天保"
92
+ date = when?('北斉::天保2.3.4')
93
+ p date #=> 天保02(0551).03.04
94
+ # **四つ目はない
95
+ begin
96
+ date = when?('天保2.3.4', :count=>4)
97
+ rescue => e
98
+ p e.to_s #=> ArgumentError: CalendarEraName doesn't exist: 天保
99
+ end
100
+
101
+ # *明治改元
102
+ # **改元前
103
+ date = when?('1868-10-22')
104
+ p date #=> 1868-10-22
105
+ list = date ^ CalendarEra('Japanese')
106
+ p list #=> [慶応04(1868).09.07, 明治01(1868).09.07]
107
+ p list[0].calendar_name[3] #=> nil
108
+ p list[1].calendar_name[3] #=> true (改元前)
109
+ # **改元後
110
+ date = when?('1868-10-23')
111
+ p date #=> 1868-10-23
112
+ list = date ^ CalendarEra('Japanese')
113
+ p list #=> [明治01(1868).09.08]
114
+ p list[0].calendar_name[3] #=> nil
115
+ p list[0].calendar_era.reference_date #=> 01(1868)
116
+
117
+ # 年号の reference_date の“精度”が“年”なら、年初に遡って検索をヒットさせる
118
+
119
+ # *大正改元
120
+ # **改元前
121
+ date = when?('1912-7-29')
122
+ p date #=> 1912-07-29
123
+ list = date ^ CalendarEra('Japanese')
124
+ p list #=> [明治45(1912).07.29]
125
+ p list[0].calendar_name[3] #=> nil
126
+ # **改元後
127
+ date = when?('1912-7-30')
128
+ p date #=> 1912-07-30
129
+ list = date ^ CalendarEra('Japanese')
130
+ p list #=> [大正01(1912).07.30]
131
+ p list[0].calendar_name[3] #=> nil
132
+ p list[0].calendar_era.reference_date #=> 01(1912).07.30
133
+
134
+ # 年号の reference_date の“精度”が“日”なら、年初に遡って検索をヒットさせない
@@ -0,0 +1,112 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin
3
+ Copyright (C) 2014 Takashi SUGA
4
+
5
+ You may use and/or modify this file according to the license described in the LICENSE.txt file included in this archive.
6
+ =end
7
+
8
+ # Calendar/When/Ruby/2.APIの使用例/4.時間間隔/コア拡張
9
+
10
+ # コアクラスを require 'when_exe/core/extension' で拡張した場合の記法を確認
11
+
12
+ # *準備
13
+ # when_exe Ruby 版の When モジュールを include する
14
+ require 'when_exe'
15
+ require 'when_exe/core/extension'
16
+ include When
17
+
18
+ # *Period Duration
19
+ # **四則
20
+ date = when?('2013-03-25')
21
+ p date #=> 2013-03-25
22
+ duration = 2.months_period_duration
23
+ p duration #=> P2M
24
+ p date + duration #=> 2013-05-25
25
+ p date - duration #=> 2013-01-25
26
+ duration2 = duration * 2
27
+ p duration2 #=> P4M
28
+ p duration2 + duration #=> P6M
29
+ p duration2 - duration #=> P2M
30
+ p date + duration2 #=> 2013-07-25
31
+ p duration2 / 2 #=> P2M
32
+ begin
33
+ p duration2 / duration
34
+ rescue => e #=> TypeError: nil can't be coerced into Float
35
+ p e
36
+ end
37
+
38
+ # **ブロック
39
+ start = when?('2013-03-25')
40
+ p start #=> 2013-03-25
41
+ stop = when?('2013-09-01')
42
+ p stop #=> 2013-09-01
43
+ duration = 2.months_period_duration
44
+ (start ^ duration).each do |date|
45
+ break if date >= stop
46
+ p date #=> 2013-03-25,2013-05-25,2013-07-25
47
+ end
48
+ duration.enum_for(start, :forward, 3).each do |date|
49
+ p date #=> 2013-03-25,2013-05-25,2013-07-25
50
+ end
51
+
52
+ # *Interval Length
53
+ # **四則
54
+ time = when?('2013-03-25T01:23:45')
55
+ p time #=> 2013-03-25T01:23:45Z
56
+ duration = 3.seconds_interval_length
57
+ p duration #=> 3s
58
+ p time + duration #=> 2013-03-25T01:23:48Z
59
+ p time - duration #=> 2013-03-25T01:23:42Z
60
+ duration2 = duration * 2
61
+ p duration2 #=> 6s
62
+ p duration2 + duration #=> 9.0s
63
+ p duration2 - duration #=> 3.0s
64
+ p time + duration2 #=> 2013-03-25T01:23:51Z
65
+ p duration2 / 2 #=> 3.0s
66
+ p duration2 / duration #=> 2.0
67
+
68
+ # **ブロック
69
+ start = when?('2013-03-25T01:23:45')
70
+ p start #=> 2013-03-25T01:23:45Z
71
+ stop = when?('2013-03-25T01:23:54')
72
+ p stop #=> 2013-03-25T01:23:54Z
73
+ duration = 3.seconds_interval_length
74
+ (start ^ duration).each do |time|
75
+ break if time >= stop
76
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
77
+ end
78
+ duration.enum_for(start, :forward, 3).each do |time|
79
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
80
+ end
81
+
82
+
83
+ # *Duration
84
+ # **四則
85
+ time = when?('2013-03-25T01:23:45')
86
+ p time #=> 2013-03-25T01:23:45Z
87
+ duration = 3.seconds_duration
88
+ p duration #=> [0, 0, 0, 3.0]
89
+ p time + duration #=> 2013-03-25T01:23:48Z
90
+ p time - duration #=> 2013-03-25T01:23:42Z
91
+ duration2 = duration * 2
92
+ p duration2 #=> [0, 0, 0, 6.0]
93
+ p duration2 + duration #=> [0, 0, 0, 9.0]
94
+ p duration2 - duration #=> [0, 0, 0, 3.0]
95
+ p time + duration2 #=> 2013-03-25T01:23:51Z
96
+ p duration2 / 2 #=> [0, 0, 0, 3.0]
97
+ p duration2 / duration #=> 2.0
98
+
99
+ # **ブロック
100
+ start = when?('2013-03-25T01:23:45')
101
+ p start #=> 2013-03-25T01:23:45Z
102
+ stop = when?('2013-03-25T01:23:54')
103
+ p stop #=> 2013-03-25T01:23:54Z
104
+ duration = 3.seconds_duration
105
+ (start ^ duration).each do |time|
106
+ break if time >= stop
107
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
108
+ end
109
+ duration.enum_for(start, :forward, 3).each do |time|
110
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
111
+ end
112
+
@@ -0,0 +1,65 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin
3
+ Copyright (C) 2014 Takashi SUGA
4
+
5
+ You may use and/or modify this file according to the license described in the LICENSE.txt file included in this archive.
6
+ =end
7
+
8
+ # Calendar/When/Ruby/2.APIの使用例/4.時間間隔/最小セット
9
+
10
+ # *準備
11
+ require 'when_exe/tmduration'
12
+ require 'when_exe/core/duration' # コア拡張する場合のみ
13
+
14
+ # *Duration
15
+ # **生成
16
+ # ***コア拡張に依存しない生成方式
17
+ p When::TM::Duration.week(1) #=> [7, 0, 0, 0.0]
18
+ p When::TM::Duration.day(2) #=> [2, 0, 0, 0.0]
19
+ p When::TM::Duration.hour(3) #=> [0, 3, 0, 0.0]
20
+ p When::TM::Duration.minute(4) #=> [0, 0, 4, 0.0]
21
+ p When::TM::Duration.second(5) #=> [0, 0, 0, 5.0]
22
+ p When::TM::Duration.dhms(2,3,4,5) #=> [2, 3, 4, 5.0]
23
+
24
+ # ***コア拡張時のみ可能な生成方式
25
+ p 1.week_duration #=> [7, 0, 0, 0.0]
26
+ p 2.days_duration #=> [2, 0, 0, 0.0]
27
+ p 3.hours_duration #=> [0, 3, 0, 0.0]
28
+ p 4.minutes_duration #=> [0, 0, 4, 0.0]
29
+ p 5.seconds_duration #=> [0, 0, 0, 5.0]
30
+ p [2,3,4,5].duration #=> [2, 3, 4, 5.0]
31
+
32
+ # 単数(~)、複数(~s)は同義
33
+
34
+ # **四則
35
+ # ***コア拡張に依存しない生成方式を使用
36
+ time = Time.at(1369475972)
37
+ p time #=> 2013-05-25 18:59:32 +0900
38
+ duration = When::TM::Duration.second(3)
39
+ p duration #=> [0, 0, 0, 3.0]
40
+ p time + duration #=> 2013-05-25 18:59:35 +0900
41
+ p time - duration #=> 2013-05-25 18:59:29 +0900
42
+ duration2 = duration * 2
43
+ p duration2 #=> [0, 0, 0, 6.0]
44
+ p duration2 + duration #=> [0, 0, 0, 9.0]
45
+ p duration2 - duration #=> [0, 0, 0, 3.0]
46
+ p time + duration2 #=> 2013-05-25 18:59:38 +0900
47
+ p duration2 / 2 #=> [0, 0, 0, 3.0]
48
+ p duration2 / duration #=> 2.0
49
+ p duration2 + When::TM::Duration.dhms(1,2,3) #=> [1, 2, 3, 6.0]
50
+
51
+ # ***コア拡張時のみ可能な生成方式を使用
52
+ time = Time.at(1369475972)
53
+ p time #=> 2013-05-25 18:59:32 +0900
54
+ duration = 3.seconds_duration
55
+ p duration #=> [0, 0, 0, 3.0]
56
+ p time + duration #=> 2013-05-25 18:59:35 +0900
57
+ p time - duration #=> 2013-05-25 18:59:29 +0900
58
+ duration2 = duration * 2
59
+ p duration2 #=> [0, 0, 0, 6.0]
60
+ p duration2 + duration #=> [0, 0, 0, 9.0]
61
+ p duration2 - duration #=> [0, 0, 0, 3.0]
62
+ p time + duration2 #=> 2013-05-25 18:59:38 +0900
63
+ p duration2 / 2 #=> [0, 0, 0, 3.0]
64
+ p duration2 / duration #=> 2.0
65
+ p duration2 + [1,2,3].duration #=> [1, 2, 3, 6.0]
data/test/scripts/4.rb ADDED
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+ =begin
3
+ Copyright (C) 2014 Takashi SUGA
4
+
5
+ You may use and/or modify this file according to the license described in the LICENSE.txt file included in this archive.
6
+ =end
7
+
8
+ # Calendar/When/Ruby/2.APIの使用例/4.時間間隔
9
+
10
+ # →関連クラス図 [[Calendar/When/Ruby/3.クラス図/10.Duration]]
11
+
12
+ # *準備
13
+ # when_exe Ruby 版の When モジュールを include する
14
+ require 'when_exe'
15
+ include When
16
+
17
+ # コア拡張を利用する場合は[[Calendar/When/Ruby/2.APIの使用例/4.時間間隔/コア拡張]]を参照
18
+
19
+ # When::TM::Durationのみを利用する場合は[[Calendar/When/Ruby/2.APIの使用例/4.時間間隔/最小セット]]を参照
20
+
21
+ # *Period Duration
22
+ # **四則
23
+ date = when?('2013-03-25')
24
+ p date #=> 2013-03-25
25
+ duration = Duration('P1Y2M3D')
26
+ p duration #=> P1Y2M3D
27
+ p date + duration #=> 2014-05-28
28
+ p date - duration #=> 2012-01-22
29
+ duration2 = duration * 2
30
+ p duration2 #=> P2Y4M6D
31
+ p duration2 + duration #=> P3Y6M9D
32
+ p duration2 - duration #=> P1Y2M3D
33
+ p date + duration2 #=> 2015-07-31
34
+ p duration2 / 2 #=> P1Y2M3D
35
+ begin
36
+ p duration2 / duration
37
+ rescue => e #=> TypeError: nil can't be coerced into Float
38
+ p e
39
+ end
40
+
41
+ # **ブロック
42
+ start = when?('2013-03-25')
43
+ p start #=> 2013-03-25
44
+ stop = when?('2016-01-01')
45
+ p stop #=> 2016-01-01
46
+ duration = Duration('P1Y2M3D')
47
+ (start ^ duration).each do |date|
48
+ break if date >= stop
49
+ p date #=> 2013-03-25,2014-05-28,2015-07-31
50
+ end
51
+ duration.enum_for(start, :forward, 3).each do |date|
52
+ p date #=> 2013-03-25,2014-05-28,2015-07-31
53
+ end
54
+
55
+ # *Interval Length
56
+ # **四則
57
+ time = when?('2013-03-25T01:23:45')
58
+ p time #=> 2013-03-25T01:23:45Z
59
+ duration = Duration('3s')
60
+ p duration #=> 3s
61
+ p time + duration #=> 2013-03-25T01:23:48Z
62
+ p time - duration #=> 2013-03-25T01:23:42Z
63
+ duration2 = duration * 2
64
+ p duration2 #=> 6s
65
+ p duration2 + duration #=> 9.0s
66
+ p duration2 - duration #=> 3.0s
67
+ p time + duration2 #=> 2013-03-25T01:23:51Z
68
+ p duration2 / 2 #=> 3.0s
69
+ p duration2 / duration #=> 2.0
70
+
71
+ # **ブロック
72
+ start = when?('2013-03-25T01:23:45')
73
+ p start #=> 2013-03-25T01:23:45Z
74
+ stop = when?('2013-03-25T01:23:54')
75
+ p stop #=> 2013-03-25T01:23:54Z
76
+ duration = Duration('3s')
77
+ (start ^ duration).each do |time|
78
+ break if time >= stop
79
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
80
+ end
81
+ duration.enum_for(start, :forward, 3).each do |time|
82
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
83
+ end
84
+
85
+
86
+ # *Duration
87
+ # **四則
88
+ time = when?('2013-03-25T01:23:45')
89
+ p time #=> 2013-03-25T01:23:45Z
90
+ duration = TM::Duration.second(3)
91
+ p duration #=> [0, 0, 0, 3.0]
92
+ p time + duration #=> 2013-03-25T01:23:48Z
93
+ p time - duration #=> 2013-03-25T01:23:42Z
94
+ duration2 = duration * 2
95
+ p duration2 #=> [0, 0, 0, 6.0]
96
+ p duration2 + duration #=> [0, 0, 0, 9.0]
97
+ p duration2 - duration #=> [0, 0, 0, 3.0]
98
+ p time + duration2 #=> 2013-03-25T01:23:51Z
99
+ p duration2 / 2 #=> [0, 0, 0, 3.0]
100
+ p duration2 / duration #=> 2.0
101
+
102
+ # **ブロック
103
+ start = when?('2013-03-25T01:23:45')
104
+ p start #=> 2013-03-25T01:23:45Z
105
+ stop = when?('2013-03-25T01:23:54')
106
+ p stop #=> 2013-03-25T01:23:54Z
107
+ duration = TM::Duration.second(3)
108
+ (start ^ duration).each do |time|
109
+ break if time >= stop
110
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
111
+ end
112
+ duration.enum_for(start, :forward, 3).each do |time|
113
+ p time #=> 2013-03-25T01:23:45Z,2013-03-25T01:23:48Z,2013-03-25T01:23:51Z
114
+ end
115
+
116
+ # *Temporal Position の差
117
+ time1 = when?('2013-03-25T01:23:45')
118
+ time2 = when?('2013-03-26T01:23:45')
119
+ duration = time2 - time1
120
+ p [duration, duration.class] # => [86400.0s, When::TM::IntervalLength]
121
+
122
+ # 分解能が「日」よりも細かい時間位置の差は&yard(When::TM::IntervalLength)になる。
123
+
124
+ date1 = when?('2013-03-25')
125
+ date2 = when?('2013-03-26')
126
+ duration = date2 - date1
127
+ p [duration, duration.class] # => [P1D, When::TM::PeriodDuration]
128
+
129
+ # 分解能が「日」またはそれより粗い時間位置の差は「日」を単位とする&yard(When::TM::PeriodDuration)になる。
130
+
131
+ date3 = when?('215-20-17^^Darian')
132
+ duration = date3 - date1
133
+ p [duration, duration.class] # => [14434.504952669144s, When::TM::IntervalLength]
134
+
135
+ # グレゴリオ暦と火星暦のように歩度の異なる暦で表された時間位置の差は分解能に関係なく&yard(When::TM::IntervalLength)になる。
136
+