era_ja 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile CHANGED
@@ -2,7 +2,7 @@
2
2
  # A sample Guardfile
3
3
  # More info at https://github.com/guard/guard#readme
4
4
 
5
- guard 'rspec', :version => 2, :cli => '--color', :all_after_pass => false do
5
+ guard 'rspec', :cli => '--color', :all_after_pass => false do
6
6
  watch(%r{^spec/.+_spec\.rb$})
7
7
  watch(%r{^lib/era_ja/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
8
8
  watch('lib/era_ja.rb') { "spec" }
data/README.ja.md CHANGED
@@ -34,6 +34,7 @@ EraJa#to_eraメソッドでの追加のフォーマット文字列は以下の
34
34
  require 'era_ja/time'
35
35
  Time.mktime(2012,4,29).to_era # => "H24.4.29"
36
36
  Time.mktime(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
37
+ Time.mktime(2012,4,29).to_era("%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
37
38
  ```
38
39
 
39
40
  ### Dateインタンスを和暦に変換します。
@@ -42,6 +43,15 @@ EraJa#to_eraメソッドでの追加のフォーマット文字列は以下の
42
43
  require 'era_ja/date'
43
44
  Date.new(2012,4,29).to_era # => "H24.4.29"
44
45
  Date.new(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
46
+ Date.new(2012,4,29).to_era("%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
47
+ ```
48
+
49
+ ### 数字を漢数字に変換します。 ###
50
+
51
+
52
+ ```ruby
53
+ Time.mktime(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
54
+ Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
45
55
  ```
46
56
 
47
57
  ## Support
data/README.md CHANGED
@@ -27,6 +27,7 @@ EraJa#to_era of extra format string is follow.
27
27
  * %o: era(alphabet)
28
28
  * %O: era(kanzi)
29
29
  * %E: era year
30
+ * %J: kanzi number
30
31
 
31
32
  ### Time instance to japanese era
32
33
 
@@ -34,6 +35,7 @@ EraJa#to_era of extra format string is follow.
34
35
  require 'era_ja/time'
35
36
  Time.mktime(2012,4,29).to_era # => "H24.4.29"
36
37
  Time.mktime(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
38
+ Time.mktime(2012,4,29).to_era("%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
37
39
  ```
38
40
 
39
41
  ### Date instance to japanese era
@@ -42,6 +44,14 @@ EraJa#to_era of extra format string is follow.
42
44
  require 'era_ja/date'
43
45
  Date.new(2012,4,29).to_era # => "H24.4.29"
44
46
  Date.new(2012,4,29).to_era("%O%E年%m月%d日") # => "平成24年4月29日"
47
+ Date.new(2012,4,29).to_era("%JE年%Jm月%Jd日") # => "平成二十四年四月二十九日"
48
+ ```
49
+
50
+ ### Change number to kansuuzi ###
51
+
52
+ ```ruby
53
+ Time.mktime(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
54
+ Date.new(2012,4,29).to_era("%Jy年%Jm月%Jd日") # => "二千十二年四月二十九日"
45
55
  ```
46
56
 
47
57
  ## Support
data/era_ja.gemspec CHANGED
@@ -15,8 +15,9 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = EraJa::VERSION
17
17
 
18
- gem.add_development_dependency "rake", '>= 0.9.2'
19
- gem.add_development_dependency 'rspec', '>= 2.9.0'
20
- gem.add_development_dependency 'growl', '>= 1.0.3'
21
- gem.add_development_dependency "guard-rspec", '>= 0.7.0'
18
+ gem.add_development_dependency "rake", '~> 10.0.3'
19
+ gem.add_development_dependency 'rspec', '~> 2.12.0'
20
+ gem.add_development_dependency "guard-rspec", '~> 2.4.0'
21
+ gem.add_development_dependency 'rb-fsevent', '~> 0.9.3'
22
+ gem.add_development_dependency 'terminal-notifier-guard', '~> 1.5.3'
22
23
  end
data/lib/era_ja.rb CHANGED
@@ -12,35 +12,66 @@ module EraJa
12
12
  # * %o - era(alphabet)
13
13
  # * %O - era(kanzi)
14
14
  # * %E - era year
15
+ # * %J - kanzi number
15
16
  # @return [String]
16
17
  def to_era(format = "%o%E.%m.%d")
17
- format.sub!(/%o/i) { |m| m + ' ' }
18
- format.sub!(/%E/) { |m| m + ' ' }
19
- @era_format = format[/%o/i]
20
- if self.to_time < ::Time.mktime(1868,9,8)
21
- raise "#to_era is expeted later in 1968,9,8"
22
- elsif self.to_time < ::Time.mktime(1912,7,30)
23
- strftime(format).sub(/%E /, format_year(year - 1867)).sub(/%o /i) { format_era('M') }
24
- elsif self.to_time < ::Time.mktime(1926,12,25)
25
- strftime(format).sub(/%E /, format_year(year - 1911)).sub(/%o /i) { format_era('T') }
26
- elsif self.to_time < ::Time.mktime(1989,1,8)
27
- strftime(format).sub(/%E /, format_year(year - 1925)).sub(/%o /i) { format_era('S') }
28
- else
29
- strftime(format).sub(/%E /, format_year(year - 1988)).sub(/%o /i) { format_era('H') }
18
+ @era_format = format
19
+ @era_format.gsub!(/%J/, "%J%")
20
+ @era_format.sub!(/%o/i) { |m| m + ' ' }
21
+ @era_format.sub!(/%E/) { |m| m + ' ' }
22
+ str_time = strftime(@era_format)
23
+ if @era_format =~ /%E/
24
+ if self.to_time < ::Time.mktime(1868,9,8)
25
+ raise "#to_era is expeted later in 1968,9,8"
26
+ elsif self.to_time < ::Time.mktime(1912,7,30)
27
+ str_time = era_year(year - 1867, 'M')
28
+ elsif self.to_time < ::Time.mktime(1926,12,25)
29
+ str_time = era_year(year - 1911, 'T')
30
+ elsif self.to_time < ::Time.mktime(1989,1,8)
31
+ str_time = era_year(year - 1925, 'S')
32
+ else
33
+ str_time = era_year(year - 1988, 'H')
34
+ end
30
35
  end
36
+ str_time.gsub(/%J(\d+)/) { to_kanzi($1) }
31
37
  end
32
38
 
33
39
  private
40
+ def era_year(year, era)
41
+ strftime(@era_format).sub(/(%J)?%E /) { format_year(year, $1) }.sub(/%o /i) { format_era(era) }
42
+ end
43
+
34
44
  def format_era(string)
35
- if @era_format == "%o"
45
+ if @era_format =~ /%o/
36
46
  return "#{ERA.detect{ |i| i[0] == string}[0]}"
37
- elsif @era_format == "%O"
47
+ elsif @era_format =~ /%O/
38
48
  return "#{ERA.detect{ |i| i[0] == string}[1]}"
39
49
  end
40
50
  end
41
51
 
42
- def format_year(year)
43
- sprintf("%02d", year)
52
+ def format_year(year, match)
53
+ era_year = sprintf("%02d", year)
54
+ if match == "%J"
55
+ era_year = year == 1 ? "元" : to_kanzi(era_year)
56
+ end
57
+ era_year
58
+ end
59
+
60
+ def to_kanzi(numeric, kanzi = "")
61
+ figures = (10 ** numeric.to_s.size) / 10
62
+ numeric = numeric.to_i
63
+ kanzi_string = [nil, "一", "二", "三", "四", "五", "六", "七", "八", "九"]
64
+ figures_string = { 1000 => "千", 100 => "百", 10 => "十", 1 => "" }
65
+ return kanzi + kanzi_string[numeric] if figures == 1
66
+
67
+ numeral = numeric / figures
68
+ if numeral > 1
69
+ to_kanzi(numeric - numeral * figures, "#{kanzi}#{kanzi_string[numeral]}#{figures_string[figures]}")
70
+ elsif numeral == 1
71
+ to_kanzi(numeric - numeral * figures, "#{kanzi}#{figures_string[figures]}")
72
+ else
73
+ to_kanzi(numeric - numeral * figures, kanzi)
74
+ end
44
75
  end
45
76
 
46
77
  end
@@ -1,3 +1,3 @@
1
1
  module EraJa
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -33,8 +33,32 @@ shared_examples "should equal '平成240429'" do
33
33
  end
34
34
  end
35
35
 
36
+ shared_examples "should equal '平成二十四年四月二十九日'" do
37
+ context "with '%O%JE年%Jm月%Jd日'" do
38
+ it { subject.to_era('%O%JE年%Jm月%Jd日').should eq '平成二十四年四月二十九日' }
39
+ end
40
+ end
41
+
42
+ shared_examples "should equal '2012年04月29日'" do
43
+ context "with '%y年%m月%d日'" do
44
+ it { subject.to_era('%Y年%m月%d日').should eq '2012年04月29日' }
45
+ end
46
+ end
47
+
48
+ shared_examples "should equal '二千十二年四月二十九日'" do
49
+ context "with '%Jy年%Jm月%Jd日'" do
50
+ it { subject.to_era('%JY年%Jm月%Jd日').should eq '二千十二年四月二十九日' }
51
+ end
52
+ end
53
+
36
54
  shared_examples "should equal 'H01.01.08'" do
37
- it { subject.to_era.should eq "H01.01.08" }
55
+ context "without argument" do
56
+ it { subject.to_era.should eq "H01.01.08" }
57
+ end
58
+
59
+ context "with '%O%JE.%Jm.%Jd'" do
60
+ it { subject.to_era('%O%JE.%Jm.%Jd').should eq "平成元.一.八" }
61
+ end
38
62
  end
39
63
 
40
64
  shared_examples "should equal 'S64.01.07'" do
@@ -80,6 +104,9 @@ shared_examples "2012,4,29" do
80
104
  include_examples "should equal '24.04.29'"
81
105
  include_examples "should equal '2404'"
82
106
  include_examples "should equal '平成240429'"
107
+ include_examples "should equal '2012年04月29日'"
108
+ include_examples "should equal '平成二十四年四月二十九日'"
109
+ include_examples "should equal '二千十二年四月二十九日'"
83
110
  end
84
111
 
85
112
  shared_examples "1989,1,8" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: era_ja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,72 +9,88 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-14 00:00:00.000000000 Z
12
+ date: 2013-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.9.2
21
+ version: 10.0.3
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 0.9.2
29
+ version: 10.0.3
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 2.9.0
37
+ version: 2.12.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 2.9.0
45
+ version: 2.12.0
46
46
  - !ruby/object:Gem::Dependency
47
- name: growl
47
+ name: guard-rspec
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 1.0.3
53
+ version: 2.4.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 1.0.3
61
+ version: 2.4.0
62
62
  - !ruby/object:Gem::Dependency
63
- name: guard-rspec
63
+ name: rb-fsevent
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.9.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: terminal-notifier-guard
64
80
  requirement: !ruby/object:Gem::Requirement
65
81
  none: false
66
82
  requirements:
67
- - - ! '>='
83
+ - - ~>
68
84
  - !ruby/object:Gem::Version
69
- version: 0.7.0
85
+ version: 1.5.3
70
86
  type: :development
71
87
  prerelease: false
72
88
  version_requirements: !ruby/object:Gem::Requirement
73
89
  none: false
74
90
  requirements:
75
- - - ! '>='
91
+ - - ~>
76
92
  - !ruby/object:Gem::Version
77
- version: 0.7.0
93
+ version: 1.5.3
78
94
  description: Convert to Japanese era.
79
95
  email:
80
96
  - tomiacannondale@gmail.com
@@ -113,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
129
  version: '0'
114
130
  segments:
115
131
  - 0
116
- hash: -2746383159837241495
132
+ hash: -1339535651453117489
117
133
  required_rubygems_version: !ruby/object:Gem::Requirement
118
134
  none: false
119
135
  requirements:
@@ -122,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
138
  version: '0'
123
139
  segments:
124
140
  - 0
125
- hash: -2746383159837241495
141
+ hash: -1339535651453117489
126
142
  requirements: []
127
143
  rubyforge_project:
128
144
  rubygems_version: 1.8.24