warekky 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +26 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/warekky/core_ext.rb +49 -0
- data/lib/warekky/era.rb +44 -0
- data/lib/warekky/era_group.rb +138 -0
- data/lib/warekky/ja.rb +36 -0
- data/lib/warekky.rb +49 -0
- data/spec/core_ext/with_date_spec.rb +152 -0
- data/spec/core_ext/with_date_time_spec.rb +156 -0
- data/spec/core_ext/with_time_spec.rb +156 -0
- data/spec/ja/with_date_spec.rb +142 -0
- data/spec/ja/with_date_time_spec.rb +133 -0
- data/spec/ja/with_time_spec.rb +133 -0
- data/spec/rcov.opts +1 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/warekky/era_group_spec.rb +121 -0
- data/spec/warekky/era_spec.rb +100 -0
- data/spec/warekky_spec.rb +32 -0
- data/warekky.gemspec +76 -0
- metadata +115 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Takeshi AKIMA
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= warekky
|
2
|
+
|
3
|
+
warekkyは和暦サポートのためのライブラリです。
|
4
|
+
和暦の日付と西暦の日付を相互に変換できます。
|
5
|
+
|
6
|
+
== Install
|
7
|
+
$ [sudo] gem install warekky
|
8
|
+
|
9
|
+
== Simple Usage
|
10
|
+
$ irb
|
11
|
+
require 'warekky'
|
12
|
+
#=> true
|
13
|
+
|
14
|
+
Date.new(2010, 6, 10).strftime("%g%n/%m/%d")
|
15
|
+
#=> "H22/06/10"
|
16
|
+
|
17
|
+
Time.parse("H22/06/10 19:07:20")
|
18
|
+
#=> Thu Jun 10 19:07:20 0900 2010
|
19
|
+
|
20
|
+
Time.now.era.name
|
21
|
+
#=> "heisei"
|
22
|
+
|
23
|
+
|
24
|
+
== Copyright
|
25
|
+
|
26
|
+
Copyright (c) 2010 Takeshi AKIMA. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "warekky"
|
9
|
+
gem.summary = %Q{Japanese Era(年号) library and Time, Date, DateTime extensions.}
|
10
|
+
gem.description = %Q{Japanese Era(年号) library and Time, Date, DateTime extensions.}
|
11
|
+
gem.email = "akm2000@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/akm/warekky"
|
13
|
+
gem.authors = ["Takeshi AKIMA"]
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
spec.rcov_opts = lambda do
|
33
|
+
IO.readlines("spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
task :spec => :check_dependencies
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "warekky #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'warekky'
|
2
|
+
|
3
|
+
module Warekky
|
4
|
+
module CoreExt
|
5
|
+
def self.included(klass)
|
6
|
+
klass.extend(ClassMethods)
|
7
|
+
unless klass.respond_to?(:parse_without_warekky)
|
8
|
+
klass.instance_eval do
|
9
|
+
alias :parse_without_warekky :parse
|
10
|
+
alias :parse :parse_with_warekky
|
11
|
+
end
|
12
|
+
end
|
13
|
+
unless klass.instance_methods.include?('strftime_without_warekky')
|
14
|
+
klass.module_eval do
|
15
|
+
alias_method :strftime_without_warekky, :strftime
|
16
|
+
alias_method :strftime, :strftime_with_warekky
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def eras
|
23
|
+
Warekky.era_group
|
24
|
+
end
|
25
|
+
|
26
|
+
def parse_with_warekky(str)
|
27
|
+
eras.parse(str, :class => self)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def strftime_with_warekky(format)
|
32
|
+
self.class.eras.strftime(self, format)
|
33
|
+
end
|
34
|
+
|
35
|
+
def era
|
36
|
+
self.class.eras[self]
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.setup
|
40
|
+
[::Time, ::Date].each do |klass|
|
41
|
+
klass.send(:include, self)
|
42
|
+
end
|
43
|
+
::DateTime.instance_eval do
|
44
|
+
alias :parse :parse_with_warekky
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
data/lib/warekky/era.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'warekky'
|
2
|
+
|
3
|
+
module Warekky
|
4
|
+
class Era
|
5
|
+
attr_reader :key, :sign, :first, :last, :options
|
6
|
+
def initialize(key, sign, first, last, options = {})
|
7
|
+
@key, @sign = key.to_sym, sign.to_s.freeze
|
8
|
+
@first, @last = to_date(first), to_date(last)
|
9
|
+
@options = (options || {}).freeze
|
10
|
+
end
|
11
|
+
|
12
|
+
def name
|
13
|
+
key.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def match?(d)
|
17
|
+
d = to_date(d)
|
18
|
+
(@first ? @first <= d : true) && (@last ? @last >= d : true)
|
19
|
+
end
|
20
|
+
|
21
|
+
def [](option_key)
|
22
|
+
@options[option_key]
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_ad_year(era_year)
|
26
|
+
era_year = era_year.to_i
|
27
|
+
@first.year - 1 + era_year
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_era_year(ad_year)
|
31
|
+
ad_year = ad_year.to_i
|
32
|
+
ad_year - @first.year + 1
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def to_date(obj)
|
37
|
+
return nil unless obj
|
38
|
+
return obj if obj.is_a?(Date)
|
39
|
+
return Date.new(obj.year, obj.month, obj.day) if obj.is_a?(Time)
|
40
|
+
return obj.to_date if respond_to?(:to_date)
|
41
|
+
Warekky.try_without(Date, :parse, obj.to_s)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'warekky'
|
3
|
+
|
4
|
+
module Warekky
|
5
|
+
class EraGroup
|
6
|
+
def initialize
|
7
|
+
@formats = self.class.formats.dup
|
8
|
+
end
|
9
|
+
|
10
|
+
class_method_proxy_methods = %w[eras name_to_era era_replacements
|
11
|
+
formats formats_regexp replacements_before_parse]
|
12
|
+
class_method_proxy_methods.each do |method_name|
|
13
|
+
module_eval(<<-"EOS")
|
14
|
+
def #{method_name}
|
15
|
+
self.class.#{method_name}
|
16
|
+
end
|
17
|
+
EOS
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse(str, options = {})
|
21
|
+
options = {
|
22
|
+
:class => Date
|
23
|
+
}.update(options || {})
|
24
|
+
str = str.dup
|
25
|
+
replacements_before_parse.each_with_index do |dic, idx|
|
26
|
+
str.gsub!(replacements_regexp_before_parse[idx]){|s| dic[s]}
|
27
|
+
end
|
28
|
+
era_dic = name_to_era
|
29
|
+
str.gsub!(era_replacements) do
|
30
|
+
md = Regexp.last_match
|
31
|
+
h = Hash[*md.captures]
|
32
|
+
h.delete(nil)
|
33
|
+
s = h.keys.first
|
34
|
+
era = era_dic[s]
|
35
|
+
raise ArgumentError, "Era not found for #{s.inspect}" unless era
|
36
|
+
era.to_ad_year(h[s]).to_s
|
37
|
+
end
|
38
|
+
Warekky.try_without(options[:class], :parse, str)
|
39
|
+
end
|
40
|
+
|
41
|
+
def strftime(d, format)
|
42
|
+
return Warekky.try_without(d, :strftime, format) unless formats_regexp.match(format)
|
43
|
+
era = self[d]
|
44
|
+
era_year = era ? era.to_era_year(d.year) : d.year
|
45
|
+
format = format.dup
|
46
|
+
format.gsub!(formats_regexp) do
|
47
|
+
md = Regexp.last_match
|
48
|
+
s = md.captures.compact.first
|
49
|
+
rep = formats[s]
|
50
|
+
raise ArgumentError, "replacement not found for #{s.inspect}" unless rep
|
51
|
+
res = rep.call(era, era_year)
|
52
|
+
res
|
53
|
+
end
|
54
|
+
Warekky.try_without(d, :strftime, format)
|
55
|
+
end
|
56
|
+
|
57
|
+
def [](era_name_or_date_or_time)
|
58
|
+
return nil unless era_name_or_date_or_time
|
59
|
+
case era_name_or_date_or_time
|
60
|
+
when Symbol, String then
|
61
|
+
name_to_era[era_name_or_date_or_time]
|
62
|
+
when Time, Date then
|
63
|
+
eras.detect{|era| era.match?(era_name_or_date_or_time)}
|
64
|
+
else
|
65
|
+
raise ArgumentError, "#{self.class.name}#[] doesn't support #{era_name_or_date_or_time.inspect}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def replacements_regexp_before_parse
|
70
|
+
@replacements_regexp_before_parse ||=
|
71
|
+
replacements_before_parse.map{|replacements|
|
72
|
+
Regexp.union(*replacements.keys.map{|s| /(#{Regexp.escape(s)})/})}
|
73
|
+
end
|
74
|
+
|
75
|
+
class << self
|
76
|
+
def eras
|
77
|
+
@eras ||= []
|
78
|
+
end
|
79
|
+
def name_to_era
|
80
|
+
unless @name_to_era
|
81
|
+
@name_to_era = eras.inject({}) do |d, era|
|
82
|
+
d[era.key] = era
|
83
|
+
d[era.key.to_s] = era
|
84
|
+
d[era.sign] = era
|
85
|
+
era_extra_names.each do |extra_name|
|
86
|
+
d[era[extra_name]] = era
|
87
|
+
end
|
88
|
+
d
|
89
|
+
end
|
90
|
+
end
|
91
|
+
@name_to_era
|
92
|
+
end
|
93
|
+
|
94
|
+
def era(first, last, key, sign, options = {})
|
95
|
+
key = key.to_sym
|
96
|
+
result = Era.new(key, sign, first, last, options).freeze
|
97
|
+
eras << result
|
98
|
+
result
|
99
|
+
end
|
100
|
+
|
101
|
+
def era_extra_names
|
102
|
+
eras.map{|era| era.options.keys}.flatten.uniq
|
103
|
+
end
|
104
|
+
|
105
|
+
attr_accessor :parse_regexp_builder
|
106
|
+
def parse_regexp(&block)
|
107
|
+
@parse_regexp_builder = block
|
108
|
+
end
|
109
|
+
|
110
|
+
def replacements_before_parse
|
111
|
+
@replacements_before_parse ||= []
|
112
|
+
end
|
113
|
+
|
114
|
+
def replace_before_parse(*replacements)
|
115
|
+
@replacements_before_parse = replacements
|
116
|
+
end
|
117
|
+
|
118
|
+
def era_replacements
|
119
|
+
@era_replacements ||= Regexp.union(eras.map(&parse_regexp_builder).flatten)
|
120
|
+
end
|
121
|
+
|
122
|
+
def formats
|
123
|
+
@formats ||= {}
|
124
|
+
end
|
125
|
+
|
126
|
+
def format(pattern, &block)
|
127
|
+
formats[pattern] = block
|
128
|
+
end
|
129
|
+
|
130
|
+
def formats_regexp
|
131
|
+
@formats_regexp ||=
|
132
|
+
Regexp.union(*formats.map{|(k,v)| /(#{Regexp.escape(k)})/})
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
data/lib/warekky/ja.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'warekky'
|
3
|
+
|
4
|
+
module Warekky
|
5
|
+
class Ja < EraGroup
|
6
|
+
# http://ja.wikipedia.org/wiki/明治
|
7
|
+
# 明治(めいじ)は、日本の元号の一つ。慶応の後、大正の前。明治元年1月1日(1868年1月25日)から
|
8
|
+
# 明治45年(大正元年、1912年)7月30日までの期間を指す。明治天皇在位期間とほぼ一致する。
|
9
|
+
# ただし、実際に改元の詔書が出されたのは慶応4年9月8日(1868年10月23日)で、同年1月1日に遡って明治元年とすると定めた。
|
10
|
+
#
|
11
|
+
# ※明治5年までは旧暦を使用していたため、西暦(グレゴリオ暦)の年とは厳密には一致しない。詳細は明治元年〜5年の各年の項目を参照。
|
12
|
+
era('1868/01/01', '1912/07/29', :meiji , 'M', :long => '明治', :short => "明") # 1 - 45
|
13
|
+
era('1912/07/30', '1926/12/24', :taisho, 'T', :long => '大正', :short => "大") # 1 - 15
|
14
|
+
era('1926/12/25', '1989/01/07', :showa , 'S', :long => '昭和', :short => "昭") # 1 - 64
|
15
|
+
era('1989/01/08', nil , :heisei, 'H', :long => '平成', :short => "平") # 1 -
|
16
|
+
|
17
|
+
# strftimeで使える記号
|
18
|
+
format('%G'){|era, era_year| era[:long] if era} # 明治/大正/昭和/平成
|
19
|
+
format('%g'){|era, era_year| era.sign if era} # M/T/S/H
|
20
|
+
format('%n'){|era, era_year| '%02d' % era_year} # (元号での)年度
|
21
|
+
|
22
|
+
# parseで使われる元号毎の正規表現
|
23
|
+
parse_regexp do |era|
|
24
|
+
[ /(#{Regexp.escape(era.name)})(\d{1,2})/,
|
25
|
+
/(#{Regexp.escape(era.sign)})(\d{1,2})/,
|
26
|
+
/(#{Regexp.escape(era[:short])})(\d{1,2})/,
|
27
|
+
/(#{Regexp.escape(era[:long])})(\d{1,2})/]
|
28
|
+
end
|
29
|
+
|
30
|
+
# parseを実行前に適用される置換
|
31
|
+
replace_before_parse(
|
32
|
+
{"元年" => "1年"},
|
33
|
+
{"年" => ".", "月" => ".", "日" => "", "時" => ":", "分" => ":", "秒" => ""}
|
34
|
+
)
|
35
|
+
end
|
36
|
+
end
|
data/lib/warekky.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
module Warekky
|
5
|
+
autoload :Era, 'warekky/era'
|
6
|
+
autoload :EraGroup, 'warekky/era_group'
|
7
|
+
autoload :Ja, 'warekky/ja'
|
8
|
+
autoload :CoreExt, 'warekky/core_ext'
|
9
|
+
|
10
|
+
class << self
|
11
|
+
attr_writer :era_group
|
12
|
+
attr_writer :era_group_class
|
13
|
+
|
14
|
+
def era_group
|
15
|
+
@era_group ||= (era_group_class ? era_group_class.new : nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
def era_group_class
|
19
|
+
@era_group_class = ::Warekky::Ja unless defined?(@era_group_class)
|
20
|
+
@era_group_class
|
21
|
+
end
|
22
|
+
|
23
|
+
# d: Date or Time
|
24
|
+
def strftime(d, format)
|
25
|
+
era_group ?
|
26
|
+
era_group.strftime(d, format) :
|
27
|
+
d.strftime(format)
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse(str, options = {})
|
31
|
+
era_group ?
|
32
|
+
era_group.parse(str, options) :
|
33
|
+
try_without(DateTime, :parse, str)
|
34
|
+
end
|
35
|
+
|
36
|
+
def [](era_name_or_date_or_time)
|
37
|
+
era_group[era_name_or_date_or_time]
|
38
|
+
end
|
39
|
+
|
40
|
+
def try_without(obj, method_name, *args, &block)
|
41
|
+
without_m = "#{method_name}_without_warekky"
|
42
|
+
return obj.send(without_m, *args, &block) if obj.respond_to?(without_m)
|
43
|
+
obj.send(method_name, *args, &block)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
Warekky::CoreExt.setup
|
@@ -0,0 +1,152 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
3
|
+
|
4
|
+
describe "Warekky" do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
Warekky.era_group_class = Warekky::Ja
|
8
|
+
end
|
9
|
+
|
10
|
+
describe :strftime do
|
11
|
+
it "without era name (元号の指定なし)" do
|
12
|
+
Date.new(1867,12,31).strftime('%Y.%m.%d').should == "1867.12.31"
|
13
|
+
Date.new(1868, 1, 1).strftime('%Y.%m.%d').should == "1868.01.01"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "with alphabet era name (アルファベット表記の元号)" do
|
17
|
+
fmt = '%g%n/%m/%d'
|
18
|
+
Date.new(1867,12,31).strftime(fmt).should == "1867/12/31"
|
19
|
+
Date.new(1868, 1, 1).strftime(fmt).should == "M01/01/01"
|
20
|
+
Date.new(1912, 7,29).strftime(fmt).should == "M45/07/29"
|
21
|
+
Date.new(1912, 7,30).strftime(fmt).should == "T01/07/30"
|
22
|
+
Date.new(1926,12,24).strftime(fmt).should == "T15/12/24"
|
23
|
+
Date.new(1926,12,25).strftime(fmt).should == "S01/12/25"
|
24
|
+
Date.new(1989, 1, 7).strftime(fmt).should == "S64/01/07"
|
25
|
+
Date.new(1989, 1, 8).strftime(fmt).should == "H01/01/08"
|
26
|
+
Date.new(2010, 6, 9).strftime(fmt).should == "H22/06/09"
|
27
|
+
Date.new(2050,12,31).strftime(fmt).should == "H62/12/31"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "with chinese charactor era name (漢字表記の元号)" do
|
31
|
+
fmt = '%G%n/%m/%d'
|
32
|
+
Date.new(1867,12,31).strftime(fmt).should == "1867/12/31"
|
33
|
+
Date.new(1868, 1, 1).strftime(fmt).should == "明治01/01/01"
|
34
|
+
Date.new(1912, 7,29).strftime(fmt).should == "明治45/07/29"
|
35
|
+
Date.new(1912, 7,30).strftime(fmt).should == "大正01/07/30"
|
36
|
+
Date.new(1926,12,24).strftime(fmt).should == "大正15/12/24"
|
37
|
+
Date.new(1926,12,25).strftime(fmt).should == "昭和01/12/25"
|
38
|
+
Date.new(1989, 1, 7).strftime(fmt).should == "昭和64/01/07"
|
39
|
+
Date.new(1989, 1, 8).strftime(fmt).should == "平成01/01/08"
|
40
|
+
Date.new(2010, 6, 9).strftime(fmt).should == "平成22/06/09"
|
41
|
+
Date.new(2050,12,31).strftime(fmt).should == "平成62/12/31"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
describe :era do
|
47
|
+
it "should return an era for date" do
|
48
|
+
Date.new(1867,12,31).era.should == nil
|
49
|
+
Date.new(1868, 1, 1).era.name.should == 'meiji'
|
50
|
+
Date.new(1912, 7,29).era.name.should == 'meiji'
|
51
|
+
Date.new(1912, 7,30).era.name.should == 'taisho'
|
52
|
+
Date.new(1926,12,24).era.name.should == 'taisho'
|
53
|
+
Date.new(1926,12,25).era.name.should == 'showa'
|
54
|
+
Date.new(1989, 1, 7).era.name.should == 'showa'
|
55
|
+
Date.new(1989, 1, 8).era.name.should == 'heisei'
|
56
|
+
Date.new(2010, 6, 9).era.name.should == 'heisei'
|
57
|
+
Date.new(2050,12,31).era.name.should == 'heisei'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe :parse do
|
62
|
+
it "without era name (元号の指定なし)" do
|
63
|
+
Date.parse("1867/12/31").should == Date.new(1867,12,31)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "with alphabet era name (アルファベット表記の元号)" do
|
67
|
+
Date.parse("M01/01/01").should == Date.new(1868, 1, 1)
|
68
|
+
Date.parse("M45/07/29").should == Date.new(1912, 7,29)
|
69
|
+
Date.parse("T01/07/30").should == Date.new(1912, 7,30)
|
70
|
+
Date.parse("T15/12/24").should == Date.new(1926,12,24)
|
71
|
+
Date.parse("S01/12/25").should == Date.new(1926,12,25)
|
72
|
+
Date.parse("S64/01/07").should == Date.new(1989, 1, 7)
|
73
|
+
Date.parse("H01/01/08").should == Date.new(1989, 1, 8)
|
74
|
+
Date.parse("H22/06/09").should == Date.new(2010, 6, 9)
|
75
|
+
Date.parse("H62/12/31").should == Date.new(2050,12,31)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "区切りなし" do
|
79
|
+
Date.parse("M010101").should == Date.new(1868, 1, 1)
|
80
|
+
Date.parse("M450729").should == Date.new(1912, 7,29)
|
81
|
+
Date.parse("T010730").should == Date.new(1912, 7,30)
|
82
|
+
Date.parse("T151224").should == Date.new(1926,12,24)
|
83
|
+
Date.parse("S011225").should == Date.new(1926,12,25)
|
84
|
+
Date.parse("S640107").should == Date.new(1989, 1, 7)
|
85
|
+
Date.parse("H010108").should == Date.new(1989, 1, 8)
|
86
|
+
Date.parse("H220609").should == Date.new(2010, 6, 9)
|
87
|
+
Date.parse("H621231").should == Date.new(2050,12,31)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "with chinese charactor era name (漢字表記の元号)" do
|
91
|
+
Date.parse("明治元年1月1日").should == Date.new(1868, 1, 1)
|
92
|
+
Date.parse("明治1年1月1日").should == Date.new(1868, 1, 1)
|
93
|
+
Date.parse("明治01年01月01日").should == Date.new(1868, 1, 1)
|
94
|
+
Date.parse("明治45年07月29日").should == Date.new(1912, 7,29)
|
95
|
+
Date.parse("大正01年07月30日").should == Date.new(1912, 7,30)
|
96
|
+
Date.parse("大正15年12月24日").should == Date.new(1926,12,24)
|
97
|
+
Date.parse("昭和01年12月25日").should == Date.new(1926,12,25)
|
98
|
+
Date.parse("昭和64年01月07日").should == Date.new(1989, 1, 7)
|
99
|
+
Date.parse("平成01年01月08日").should == Date.new(1989, 1, 8)
|
100
|
+
Date.parse("平成22年06月09日").should == Date.new(2010, 6, 9)
|
101
|
+
Date.parse("平成62年12月31日").should == Date.new(2050,12,31)
|
102
|
+
end
|
103
|
+
|
104
|
+
it "with chinese charactor short era name (漢字省略表記の元号)" do
|
105
|
+
Date.parse("明元年1月1日").should == Date.new(1868, 1, 1)
|
106
|
+
Date.parse("明1年1月1日").should == Date.new(1868, 1, 1)
|
107
|
+
Date.parse("明01年01月01日").should == Date.new(1868, 1, 1)
|
108
|
+
Date.parse("明45年07月29日").should == Date.new(1912, 7,29)
|
109
|
+
Date.parse("大01年07月30日").should == Date.new(1912, 7,30)
|
110
|
+
Date.parse("大15年12月24日").should == Date.new(1926,12,24)
|
111
|
+
Date.parse("昭01年12月25日").should == Date.new(1926,12,25)
|
112
|
+
Date.parse("昭64年01月07日").should == Date.new(1989, 1, 7)
|
113
|
+
Date.parse("平01年01月08日").should == Date.new(1989, 1, 8)
|
114
|
+
Date.parse("平22年06月09日").should == Date.new(2010, 6, 9)
|
115
|
+
Date.parse("平62年12月31日").should == Date.new(2050,12,31)
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
describe :[] do
|
121
|
+
describe "should return an Era object" do
|
122
|
+
it "for era_name" do
|
123
|
+
[:meiji, :taisho, :showa, :heisei].each do |era|
|
124
|
+
Date.eras[era].class.should == Warekky::Era
|
125
|
+
Date.eras[era.to_s].class.should == Warekky::Era
|
126
|
+
end
|
127
|
+
Date.eras[nil].should == nil
|
128
|
+
Date.eras[''].should == nil
|
129
|
+
Date.eras[:unexist_era].should == nil
|
130
|
+
end
|
131
|
+
|
132
|
+
it "for era_name with kanji" do
|
133
|
+
%w[M T S H 明治 大正 昭和 平成 明 大 昭 平].each do |era|
|
134
|
+
Date.eras[era].class.should == Warekky::Era
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
it "for date" do
|
139
|
+
Date.eras[Date.new(1867,12,31)].should == nil
|
140
|
+
Date.eras[Date.new(1868, 1, 1)].name.should == 'meiji'
|
141
|
+
Date.eras[Date.new(1912, 7,29)].name.should == 'meiji'
|
142
|
+
Date.eras[Date.new(1912, 7,30)].name.should == 'taisho'
|
143
|
+
Date.eras[Date.new(1926,12,24)].name.should == 'taisho'
|
144
|
+
Date.eras[Date.new(1926,12,25)].name.should == 'showa'
|
145
|
+
Date.eras[Date.new(1989, 1, 7)].name.should == 'showa'
|
146
|
+
Date.eras[Date.new(1989, 1, 8)].name.should == 'heisei'
|
147
|
+
Date.eras[Date.new(2010, 6, 9)].name.should == 'heisei'
|
148
|
+
Date.eras[Date.new(2050,12,31)].name.should == 'heisei'
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|