chinese_lunar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -f nested --color
2
+ --order rand
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://ruby.taobao.org/'
2
+
3
+ # Specify your gem's dependencies in chinese_lunar.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Lanvige Jiang
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ChineseLunar
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'chinese_lunar'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install chinese_lunar
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+
3
+ require "bundler"
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new :spec
8
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ require File.expand_path('../lib/chinese_lunar/version', __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "chinese_lunar"
7
+ s.version = ChineseLunar::VERSION
8
+
9
+ s.authors = ["Lanvige Jiang"]
10
+ s.email = ["lanvige@gmail.com"]
11
+ s.description = %q{Generate the Lunar date}
12
+ s.summary = %q{Generate the Lunar date}
13
+ s.homepage = ""
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
18
+ s.require_path = "lib"
19
+
20
+ s.required_rubygems_version = ">= 1.3.6"
21
+
22
+ s.add_development_dependency "bundler", ">= 1.1.0"
23
+ s.add_development_dependency "rspec", "~> 2.9.0"
24
+ end
@@ -0,0 +1,231 @@
1
+ # encoding: utf-8
2
+ # Modify from http://my.oschina.net/tomsu/blog/700
3
+
4
+ module ChineseLunar
5
+ class Lunar
6
+
7
+ @@lunar_info = [0x04bd8, 0x04ae0, 0x0a570,
8
+ 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,
9
+ 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0,
10
+ 0x0ada2, 0x095b0, 0x14977, 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50,
11
+ 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970, 0x06566,
12
+ 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0,
13
+ 0x1c8d7, 0x0c950, 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4,
14
+ 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557, 0x06ca0, 0x0b550,
15
+ 0x15355, 0x04da0, 0x0a5d0, 0x14573, 0x052d0, 0x0a9a8, 0x0e950,
16
+ 0x06aa0, 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260,
17
+ 0x0f263, 0x0d950, 0x05b57, 0x056a0, 0x096d0, 0x04dd5, 0x04ad0,
18
+ 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b5a0, 0x195a6,
19
+ 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40,
20
+ 0x0af46, 0x0ab60, 0x09570, 0x04af5, 0x04970, 0x064b0, 0x074a3,
21
+ 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0, 0x0c960,
22
+ 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0,
23
+ 0x092d0, 0x0cab5, 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9,
24
+ 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930, 0x07954, 0x06aa0,
25
+ 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65,
26
+ 0x0d530, 0x05aa0, 0x076a3, 0x096d0, 0x04bd7, 0x04ad0, 0x0a4d0,
27
+ 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45, 0x0b5a0, 0x056d0, 0x055b2,
28
+ 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0]
29
+
30
+ @@nstr = ["", "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"]
31
+ @@gan = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
32
+ @@zhi = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
33
+ @@nnimals = ["鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"]
34
+
35
+ attr_reader :Date
36
+
37
+ def initialize(date = Date.today)
38
+ @date = date
39
+ end
40
+
41
+ # Get Lundar date in Chinese text
42
+ def lunar_date_in_chinese()
43
+ l = convert(@date.year, @date.month, @date.day)
44
+ cyclical_year(l[0]) + "年 " + @@nstr[l[1]] + "月 " + get_day_in_chinese(l[2])
45
+ end
46
+
47
+ def lunar_date_year_in_chinese()
48
+ l = convert(@date.year, @date.month, @date.day)
49
+ cyclical_year(l[0])
50
+ end
51
+
52
+ # Get the Lundar date in 'xxxx-xx-xx' fromat
53
+ def lunar_date()
54
+ l = convert(@date.year, @date.month, @date.day)
55
+ l[0].to_s + "-" + l[1].to_s + "-" + (/^\d+/.match(l[2].to_s)).to_s
56
+ end
57
+
58
+ private
59
+
60
+ def cyclical_year(y)
61
+ num = y - 1900 + 36
62
+ cyclicalm(num)
63
+ end
64
+
65
+ def cyclicalm(num)
66
+ @@gan[num % 10] + @@zhi[num % 12]
67
+ end
68
+
69
+ # 传出y年m月d日对应的农历.
70
+ # year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6
71
+ def convert(y, m, d)
72
+ nongDate = []
73
+ i = 0
74
+ temp = 0
75
+ leap = 0
76
+ baseDate = Date.new(0 + 1900, 1, 31)
77
+
78
+ objDate = Date.new(y, m, d);
79
+ offset = objDate - baseDate
80
+ nongDate[5] = offset + 40;
81
+ nongDate[4] = 14;
82
+
83
+ i = 1900
84
+ while (i < 2050 && offset > 0)
85
+ temp = days_in_lunar_date(i)
86
+ offset -= temp
87
+ nongDate[4] += 12
88
+
89
+ i += 1
90
+ end
91
+
92
+ if (offset < 0)
93
+ offset += temp
94
+ i -= 1
95
+ nongDate[4] -= 12
96
+ end
97
+
98
+ nongDate[0] = i
99
+ nongDate[3] = i - 1864
100
+ leap = leap_month(i) #闰哪个月
101
+ nongDate[6] = 0
102
+
103
+ i = 1
104
+ while(i<13 && offset > 0)
105
+ #闰月
106
+ if (leap > 0 && i == (leap + 1) && nongDate[6] == 0)
107
+ i -= 1
108
+ nongDate[6] = 1
109
+ temp = leap_days( nongDate[0])
110
+ else
111
+ temp = monthDays( nongDate[0], i)
112
+ end
113
+
114
+ #解除闰月
115
+ if (nongDate[6] == 1 && i == (leap + 1))
116
+ nongDate[6] = 0
117
+ end
118
+ offset -= temp
119
+ if (nongDate[6] == 0)
120
+ nongDate[4] += 1
121
+ end
122
+
123
+ i += 1
124
+ end
125
+
126
+ if (offset == 0 && leap > 0 && i == leap + 1)
127
+ if (nongDate[6] == 1)
128
+ nongDate[6] = 0
129
+ else
130
+ nongDate[6] = 1
131
+ i -= 1
132
+ nongDate[4] -= 1
133
+ end
134
+ end
135
+
136
+ if (offset < 0)
137
+ offset += temp
138
+ i -= 1
139
+ nongDate[4] -= 1
140
+ end
141
+
142
+ nongDate[1] = i
143
+ nongDate[2] = offset + 1
144
+
145
+ nongDate
146
+ end
147
+
148
+ def get_day_in_chinese(day)
149
+ a = ""
150
+ if (day == 10)
151
+ return "初十"
152
+ elsif (day == 20)
153
+ return "二十"
154
+ elsif (day == 30)
155
+ return "三十"
156
+ end
157
+
158
+ two = ((day) / 10).to_i()
159
+
160
+ if (two == 0)
161
+ a = "初"
162
+ elsif (two == 1)
163
+ a = "十"
164
+ elsif (two == 2)
165
+ a = "廿"
166
+ elsif (two == 3)
167
+ a = "三"
168
+ else
169
+ a = "ERROR"
170
+ end
171
+
172
+ one = (day % 10)
173
+
174
+ case one
175
+ when 1 then a += "一"
176
+ when 2 then a += "二"
177
+ when 3 then a += "三"
178
+ when 4 then a += "四"
179
+ when 5 then a += "五"
180
+ when 6 then a += "六"
181
+ when 7 then a += "七"
182
+ when 8 then a += "八"
183
+ when 9 then a += "九"
184
+ end
185
+
186
+ return a
187
+ end
188
+
189
+ # Return the days in lunar of y year.
190
+ def days_in_lunar_date(y)
191
+ sum = 348
192
+ i = 0x8000
193
+ while i > 0x8
194
+ if ((@@lunar_info[y - 1900] & i) != 0)
195
+ sum += 1
196
+ end
197
+ i >>= 1
198
+ end
199
+
200
+ sum + leap_days(y)
201
+ end
202
+
203
+ # Return the leap days in y year.
204
+ def leap_days(y)
205
+ if (leap_month(y) != 0)
206
+ if ((@@lunar_info[y - 1900] & 0x10000) != 0)
207
+ return 30
208
+ else
209
+ return 29
210
+ end
211
+ else
212
+ return 0
213
+ end
214
+ end
215
+
216
+ #传回农历 y年闰哪个月 1-12 , 没闰传回 0
217
+ def leap_month(y)
218
+ @@lunar_info[y - 1900] & 0xf
219
+ end
220
+
221
+ # Return the days of m month in y year.
222
+ def monthDays(y, m)
223
+ if ((@@lunar_info[y - 1900] & (0x10000 >> m)) == 0)
224
+ return 29
225
+ else
226
+ return 30
227
+ end
228
+ end
229
+
230
+ end
231
+ end
@@ -0,0 +1,3 @@
1
+ module ChineseLunar
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ require "chinese_lunar/version"
4
+ require "chinese_lunar/convert"
5
+
6
+ module ChineseLunar
7
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ require "chinese_lunar/convert"
6
+
7
+ module ChineseLunar
8
+ describe Lunar do
9
+
10
+ it "should convert to chinese lunar success" do
11
+ luanr = Lunar.new(Date.new(2012, 3, 26))
12
+ luanr.lunar_date_in_chinese.should eq "壬辰年 三月 初五"
13
+ end
14
+
15
+ it "should convert 2012 year to 壬辰 success" do
16
+ luanr = Lunar.new(Date.new(2012, 3, 26))
17
+ luanr.lunar_date_year_in_chinese.should eq "壬辰"
18
+ end
19
+
20
+ it "should convert 2012 year to 壬辰 success" do
21
+ luanr = Lunar.new(Date.new(2012, 3, 26))
22
+ luanr.lunar_date_year_in_chinese.should eq "壬辰"
23
+ end
24
+
25
+ it "should convert 1983 success" do
26
+ luanr = Lunar.new(Date.new(1983, 7, 23))
27
+ luanr.lunar_date.should eq "1983-6-14"
28
+ end
29
+
30
+ it "should convert 1985 success" do
31
+ luanr = Lunar.new(Date.new(1985, 2, 2))
32
+ luanr.lunar_date.should eq "1984-12-13"
33
+ end
34
+
35
+ it "should convert 1993 success" do
36
+ luanr = Lunar.new(Date.new(1993, 8, 11))
37
+ luanr.lunar_date.should eq "1993-6-24"
38
+ end
39
+
40
+ it "should convert 2012 success" do
41
+ luanr = Lunar.new(Date.new(2012, 3, 26))
42
+ luanr.lunar_date.should eq "2012-3-5"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |config|
4
+ config.mock_with :rspec
5
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinese_lunar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lanvige Jiang
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &7446000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.1.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *7446000
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &7444680 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.9.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *7444680
36
+ description: Generate the Lunar date
37
+ email:
38
+ - lanvige@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - chinese_lunar.gemspec
50
+ - lib/chinese_lunar.rb
51
+ - lib/chinese_lunar/convert.rb
52
+ - lib/chinese_lunar/version.rb
53
+ - spec/chinese_lunar/convert_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: ''
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: 1.3.6
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.16
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Generate the Lunar date
79
+ test_files:
80
+ - spec/chinese_lunar/convert_spec.rb
81
+ - spec/spec_helper.rb