wareki 0.2.0 → 0.2.1
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.
- checksums.yaml +4 -4
- data/ChangeLog +5 -0
- data/README.rdoc +11 -7
- data/lib/wareki/common.rb +3 -0
- data/lib/wareki/date.rb +27 -28
- data/lib/wareki/utils.rb +41 -34
- data/lib/wareki/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 024750af6c60493ab9b817b2ac9e81a7cffd96f5
|
4
|
+
data.tar.gz: 61981fbbe3734009c05bbaabe56370c9b083cbcf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bdbf8c2f81dc966ede8c24633fb8a6c541e4e2b30907d3d804da4e01a43387420b534811b79f7e7c6b8482630b59afd58eabed2f301089f2f4302c2f6d7185e
|
7
|
+
data.tar.gz: 5b7093d249fad9fe6389620104f04f40aeebbd14e000a5c8c3fa120f45bbf14ad562ef48c9a57f111a7ac0a9ec779c4a8b67f54fe68eb837324bc8ae3d103586
|
data/ChangeLog
CHANGED
data/README.rdoc
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
{<img src="https://badge.fury.io/rb/wareki.svg" alt="Gem Version" />}[https://badge.fury.io/rb/wareki]
|
4
4
|
{<img src="https://travis-ci.org/sugi/wareki.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/sugi/wareki]
|
5
5
|
{<img src="https://coveralls.io/repos/sugi/wareki/badge.svg?branch=master&service=github" alt="Coverage Status" />}[https://coveralls.io/github/sugi/wareki?branch=master]
|
6
|
+
{<img src="https://api.codeclimate.com/v1/badges/c9209422700b526d2b45/maintainability" />}[https://codeclimate.com/github/sugi/wareki/maintainability]
|
6
7
|
|
7
8
|
== 概要
|
8
9
|
|
@@ -13,13 +14,13 @@
|
|
13
14
|
== 機能
|
14
15
|
|
15
16
|
* 和暦文字列のパース
|
16
|
-
*
|
17
|
-
*
|
17
|
+
* 旧字体、1文字元号のサポート (慶應、萬延、㍻ など)
|
18
|
+
* 全角数字、漢数字、全角ゼロのサポート (10月、十月、一〇月)
|
18
19
|
* 閏月サポート
|
19
|
-
*
|
20
|
+
* 月の別名のサポート (如月、弥生、師走など)
|
20
21
|
* 元年、正月、朔日、晦日、廿一日、卅日 などの特殊な表記の日付サポート
|
21
22
|
* Date クラスの拡張
|
22
|
-
* strftime
|
23
|
+
* 標準の strftime に和暦へのフォーマット文字列を追加
|
23
24
|
* to_wareki_date を追加
|
24
25
|
* Date::JAPAN (明治改暦日)の追加
|
25
26
|
|
@@ -37,14 +38,17 @@ Gemfile に以下のようにするか、
|
|
37
38
|
|
38
39
|
require 'wareki'
|
39
40
|
|
41
|
+
# 和暦へのフォーマット
|
40
42
|
Date.today.strftime("%JF") # => "平成二十七年八月二十二日"
|
41
43
|
Date.civil(1311, 7, 20).strftime("%JF") # => "応長元年閏六月四日"
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
+
# 和暦のパース
|
46
|
+
Date.parse("正嘉元年 うるう3月 12日") # => 標準 Date インスタンス 1257-04-27
|
45
47
|
|
48
|
+
# Wareki::Date を直接扱う場合
|
49
|
+
Date.today.to_wareki_date # => Wareki::Date インスタンス
|
46
50
|
Wareki::Date.parse("正嘉元年 うるう3月 12日") # => Wareki::Date インスタンス
|
47
|
-
Date.
|
51
|
+
Wareki::Date.new("明治", 8, 2, 1).to_date # => 標準 Date インスタンス 1875-02-01
|
48
52
|
|
49
53
|
== フォーマット文字列
|
50
54
|
|
data/lib/wareki/common.rb
CHANGED
@@ -17,6 +17,9 @@ module Wareki
|
|
17
17
|
alt_era_name == era_name and next
|
18
18
|
ERA_BY_NAME[alt_era_name] = ERA_BY_NAME[era_name]
|
19
19
|
end
|
20
|
+
{'㍾' => '明治', '㍽' => '大正', '㍼' => '昭和', '㍻' => '平成'}.each do |short, canon|
|
21
|
+
ERA_BY_NAME[short] = ERA_BY_NAME[canon]
|
22
|
+
end
|
20
23
|
ERA_BY_NAME.freeze
|
21
24
|
NUM_CHARS = "零〇一二三四五六七八九十卄廿卅丗卌肆百皕千万億兆01234567890123456789"
|
22
25
|
ALT_MONTH_NAME = %w(睦月 如月 弥生 卯月 皐月 水無月 文月 葉月 長月 神無月 霜月 師走).freeze
|
data/lib/wareki/date.rb
CHANGED
@@ -11,19 +11,27 @@ module Wareki
|
|
11
11
|
jd(::Date.today.jd)
|
12
12
|
end
|
13
13
|
|
14
|
+
def self._check_invalid_date(era, year, month, day)
|
15
|
+
month == 12 or return true
|
16
|
+
day > 2 or return true
|
17
|
+
(era == "明治" && year == 5 ||
|
18
|
+
era.to_s == "" && year == GREGORIAN_START_YEAR - 1 ||
|
19
|
+
%w(皇紀 神武天皇即位紀元).member?(era) &&
|
20
|
+
year == GREGORIAN_START_YEAR - IMPERIAL_START_YEAR - 1) or
|
21
|
+
return true
|
22
|
+
false
|
23
|
+
end
|
24
|
+
|
14
25
|
def self._parse(str)
|
15
|
-
match = REGEX.match(str.to_s.gsub(/[[:space:]]/, ''))
|
16
|
-
if !match || !match[:year]
|
26
|
+
match = REGEX.match(str.to_s.gsub(/[[:space:]]/, '')) or
|
17
27
|
raise ArgumentError, "Invaild Date: #{str}"
|
18
|
-
end
|
19
28
|
era = match[:era_name]
|
20
|
-
year = Utils.kan_to_i(match[:year])
|
21
|
-
|
22
|
-
day = 1
|
29
|
+
(year = Utils.kan_to_i(match[:year])) > 0 or
|
30
|
+
raise ArgumentError, "Invalid year: #{str}"
|
31
|
+
month = day = 1
|
23
32
|
|
24
|
-
|
33
|
+
era.to_s != "" && era.to_s != "紀元前" && !ERA_BY_NAME[era] and
|
25
34
|
raise ArgumentError, "Date parse failed: Invalid era name '#{match[:era_name]}'"
|
26
|
-
end
|
27
35
|
|
28
36
|
if match[:month]
|
29
37
|
month = Utils.kan_to_i(match[:month])
|
@@ -31,7 +39,7 @@ module Wareki
|
|
31
39
|
month = Utils.alt_month_name_to_i(match[:alt_month])
|
32
40
|
end
|
33
41
|
|
34
|
-
month > 12 || month <
|
42
|
+
month > 12 || month < 1 and
|
35
43
|
raise ArgumentError, "Invalid month: #{str}"
|
36
44
|
|
37
45
|
if match[:day]
|
@@ -42,13 +50,8 @@ module Wareki
|
|
42
50
|
end
|
43
51
|
end
|
44
52
|
|
45
|
-
|
46
|
-
era.to_s == "" && year == GREGORIAN_START_YEAR - 1 ||
|
47
|
-
(era == "皇紀" || era == "神武天皇即位紀元") &&
|
48
|
-
year == GREGORIAN_START_YEAR - IMPERIAL_START_YEAR - 1) &&
|
49
|
-
month == 12 && day > 2
|
53
|
+
_check_invalid_date(era, year, month, day) or
|
50
54
|
raise ArgumentError, "Invaild Date: #{str}"
|
51
|
-
end
|
52
55
|
|
53
56
|
{era: era, year: year, month: month, day: day, is_leap: !!match[:is_leap]}
|
54
57
|
end
|
@@ -234,23 +237,19 @@ module Wareki
|
|
234
237
|
end
|
235
238
|
|
236
239
|
def -(other)
|
237
|
-
|
238
|
-
raise NotImplementedError, "Date calcration with ActiveSupport::Duration currently is not supported. Please use numeric."
|
239
|
-
else
|
240
|
-
other.respond_to?(:to_date) and other = other.to_date
|
241
|
-
other.respond_to?(:jd) and other = other.jd
|
242
|
-
self.class.jd jd - other
|
243
|
-
end
|
240
|
+
self.class.jd jd - _to_jd_for_calc(other)
|
244
241
|
end
|
245
242
|
|
246
243
|
def +(other)
|
247
|
-
|
244
|
+
self.class.jd jd + _to_jd_for_calc(other)
|
245
|
+
end
|
246
|
+
|
247
|
+
def _to_jd_for_calc(other)
|
248
|
+
other.class.to_s == "ActiveSupport::Duration" and
|
248
249
|
raise NotImplementedError, "Date calcration with ActiveSupport::Duration currently is not supported. Please use numeric."
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
self.class.jd jd + other
|
253
|
-
end
|
250
|
+
other.respond_to?(:to_date) and other = other.to_date
|
251
|
+
other.respond_to?(:jd) and other = other.jd
|
252
|
+
other
|
254
253
|
end
|
255
254
|
end
|
256
255
|
end
|
data/lib/wareki/utils.rb
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
module Wareki
|
3
3
|
module Utils
|
4
|
+
UNIT_SUFFIX1 = {
|
5
|
+
'千' => 1000,
|
6
|
+
'百' => 100,
|
7
|
+
'十' => 10,
|
8
|
+
'' => 1,
|
9
|
+
}
|
10
|
+
UNIT_SUFFIX2 = {
|
11
|
+
'京' => 10000000000000000,
|
12
|
+
'兆' => 1000000000000,
|
13
|
+
'億' => 100000000,
|
14
|
+
'万' => 10000,
|
15
|
+
'' => 1,
|
16
|
+
}
|
4
17
|
module_function
|
5
18
|
def kan_to_i(str)
|
6
19
|
ret3 = 0
|
@@ -53,13 +66,7 @@ module Wareki
|
|
53
66
|
|
54
67
|
def i_to_kan(num, opts = {})
|
55
68
|
ret = ''
|
56
|
-
|
57
|
-
'京' => 10000000000000000,
|
58
|
-
'兆' => 1000000000000,
|
59
|
-
'億' => 100000000,
|
60
|
-
'万' => 10000,
|
61
|
-
'' => 1,
|
62
|
-
}.each do |unit4, rank4|
|
69
|
+
UNIT_SUFFIX2.each do |unit4, rank4|
|
63
70
|
i4 = (num / rank4).to_i % 10000
|
64
71
|
if i4 == 0
|
65
72
|
next
|
@@ -67,12 +74,7 @@ module Wareki
|
|
67
74
|
ret += "一#{unit4}"
|
68
75
|
next
|
69
76
|
end
|
70
|
-
|
71
|
-
'千' => 1000,
|
72
|
-
'百' => 100,
|
73
|
-
'十' => 10,
|
74
|
-
'' => 1,
|
75
|
-
}.each do |unit1, rank1|
|
77
|
+
UNIT_SUFFIX1.each do |unit1, rank1|
|
76
78
|
i1 = (i4 / rank1).to_i % 10
|
77
79
|
if i1 == 0
|
78
80
|
next
|
@@ -93,25 +95,32 @@ module Wareki
|
|
93
95
|
|
94
96
|
def last_day_of_month(year, month, is_leap)
|
95
97
|
if year >= GREGORIAN_START_YEAR
|
96
|
-
|
97
|
-
tmp_m = month
|
98
|
-
if month == 12
|
99
|
-
tmp_y += 1
|
100
|
-
tmp_m = 1
|
101
|
-
else
|
102
|
-
tmp_m += 1
|
103
|
-
end
|
104
|
-
day = (::Date.new(tmp_y, tmp_m, 1, ::Date::GREGORIAN)-1).day
|
98
|
+
_last_day_of_month_gregorian(year, month, is_leap)
|
105
99
|
else
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
100
|
+
_last_day_of_month_from_defs(year, month, is_leap)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def _last_day_of_month_gregorian(year, month, is_leap)
|
105
|
+
tmp_y = year
|
106
|
+
tmp_m = month
|
107
|
+
if month == 12
|
108
|
+
tmp_y += 1
|
109
|
+
tmp_m = 1
|
110
|
+
else
|
111
|
+
tmp_m += 1
|
113
112
|
end
|
114
|
-
day
|
113
|
+
(::Date.new(tmp_y, tmp_m, 1, ::Date::GREGORIAN)-1).day
|
114
|
+
end
|
115
|
+
|
116
|
+
def _last_day_of_month_from_defs(year, month, is_leap)
|
117
|
+
yobj = YEAR_BY_NUM[year] or
|
118
|
+
raise UnsupportedDateRange, "Cannot find year #{self.inspect}"
|
119
|
+
month_idx = month - 1
|
120
|
+
if is_leap || yobj.leap_month && yobj.leap_month < month
|
121
|
+
month_idx += 1
|
122
|
+
end
|
123
|
+
yobj.month_days[month_idx]
|
115
124
|
end
|
116
125
|
|
117
126
|
def alt_month_name_to_i(name)
|
@@ -145,9 +154,8 @@ module Wareki
|
|
145
154
|
|
146
155
|
def find_date_ary(d)
|
147
156
|
d = _to_date(d).new_start(::Date::GREGORIAN)
|
148
|
-
|
157
|
+
d.jd >= GREGORIAN_START and
|
149
158
|
return [d.year, d.month, d.day, false]
|
150
|
-
end
|
151
159
|
|
152
160
|
yobj = find_year(d) or raise UnsupportedDateRange, "Unsupported date: #{d.inspect}"
|
153
161
|
month = 0
|
@@ -159,9 +167,8 @@ module Wareki
|
|
159
167
|
end
|
160
168
|
month_start = yobj.month_starts[month-1]
|
161
169
|
is_leap = (yobj.leap_month == (month - 1))
|
162
|
-
|
170
|
+
yobj.leap_month && yobj.leap_month < month and
|
163
171
|
month -= 1
|
164
|
-
end
|
165
172
|
[yobj.year, month, d.jd - month_start +1, is_leap]
|
166
173
|
end
|
167
174
|
|
data/lib/wareki/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wareki
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuki Sugiura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-08-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
93
|
rubyforge_project:
|
94
|
-
rubygems_version: 2.2.2
|
94
|
+
rubygems_version: 2.5.2.2
|
95
95
|
signing_key:
|
96
96
|
specification_version: 4
|
97
97
|
summary: Pure ruby library of Wareki (Japanese calendar date)
|