chinese_lunar_calendar 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e03d804a6733131ae76562cc16b71d0fa0ef67ff
4
+ data.tar.gz: b6819a34bc6d690bdd312e5b99e260616f054dab
5
+ SHA512:
6
+ metadata.gz: d96cad790bf6f6bac4da89e35b36137af8b4ef34544a00fcfd5db0a280dfe5db7e402dba79819f4ceda5b9128f89207db941af1354fa5265bfaa97b9b9dd1c1c
7
+ data.tar.gz: e820b914733ca6778700a2840c63eeb77b93e182d2c96bf473d367124887a170aa8745d04d83599d34157aecb4d3f91b319ecb206fc00d6ecd37834c58082f78
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 'https://rubygems.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_calendar/version', __FILE__)
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "chinese_lunar_calendar"
7
+ s.version = ChineseLunarCalendar::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 = "https://github.com/y1feng200156/chinese_lunar"
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,7 @@
1
+ # encoding: utf-8
2
+
3
+ require "chinese_lunar_calendar/version"
4
+ require "chinese_lunar_calendar/lunar"
5
+
6
+ module ChineseLunar
7
+ end
@@ -0,0 +1,255 @@
1
+ # encoding: utf-8
2
+ # Modify from http://my.oschina.net/tomsu/blog/700
3
+
4
+ module ChineseLunarCalendar
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
+ @@animals = ["猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗"]
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 date_in_chinese
43
+ @l ||= convert(@date.year, @date.month, @date.day)
44
+ pp @l
45
+ @year ||= cyclical_year(@l[0])
46
+ @month ||= @@nstr[@l[1]]
47
+ @day ||= get_day_in_chinese(@l[2])
48
+ @animal ||= __animal @l
49
+ "#{@year}(#{@animal})年 #{@month}月 #{@day}"
50
+ end
51
+
52
+ def year_in_chinese
53
+ date_in_chinese
54
+ @year
55
+ end
56
+
57
+ # Get the Lundar date in 'xxxx-xx-xx' fromat
58
+ def lunar_date()
59
+ date_in_chinese
60
+ @l[0].to_s + "-" + @l[1].to_s + "-" + (/^\d+/.match(@l[2].to_s)).to_s
61
+ end
62
+
63
+ def month_in_chinese
64
+ date_in_chinese
65
+ @month
66
+ end
67
+
68
+ def day_in_chinese
69
+ date_in_chinese
70
+ @day
71
+ end
72
+
73
+ def animal
74
+ date_in_chinese
75
+ @animal
76
+ end
77
+
78
+ private
79
+
80
+ def __animal(l)
81
+ @@animals[(l[0] - 1899) % 12]
82
+ end
83
+
84
+ def cyclical_year(y)
85
+ num = y - 1900 + 36
86
+ cyclicalm(num)
87
+ end
88
+
89
+ def cyclicalm(num)
90
+ @@gan[num % 10] + @@zhi[num % 12]
91
+ end
92
+
93
+ # 传出y年m月d日对应的农历.
94
+ # year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6
95
+ def convert(y, m, d)
96
+ nongDate = []
97
+ i = 0
98
+ temp = 0
99
+ leap = 0
100
+ baseDate = Date.new(0 + 1900, 1, 31)
101
+
102
+ objDate = Date.new(y, m, d)
103
+ offset = objDate - baseDate
104
+ nongDate[5] = offset + 40
105
+ nongDate[4] = 14
106
+
107
+ i = 1900
108
+ while (i < 2050 && offset > 0)
109
+ temp = days_in_lunar_date(i)
110
+ offset -= temp
111
+ nongDate[4] += 12
112
+
113
+ i += 1
114
+ end
115
+
116
+ if (offset < 0)
117
+ offset += temp
118
+ i -= 1
119
+ nongDate[4] -= 12
120
+ end
121
+
122
+ nongDate[0] = i
123
+ nongDate[3] = i - 1864
124
+ leap = leap_month(i) #闰哪个月
125
+ nongDate[6] = 0
126
+
127
+ i = 1
128
+ while(i<13 && offset > 0)
129
+ #闰月
130
+ if (leap > 0 && i == (leap + 1) && nongDate[6] == 0)
131
+ i -= 1
132
+ nongDate[6] = 1
133
+ temp = leap_days(nongDate[0])
134
+ else
135
+ temp = monthDays(nongDate[0], i)
136
+ end
137
+
138
+ #解除闰月
139
+ if (nongDate[6] == 1 && i == (leap + 1))
140
+ nongDate[6] = 0
141
+ end
142
+ offset -= temp
143
+ if (nongDate[6] == 0)
144
+ nongDate[4] += 1
145
+ end
146
+
147
+ i += 1
148
+ end
149
+
150
+ if (offset == 0 && leap > 0 && i == leap + 1)
151
+ if (nongDate[6] == 1)
152
+ nongDate[6] = 0
153
+ else
154
+ nongDate[6] = 1
155
+ i -= 1
156
+ nongDate[4] -= 1
157
+ end
158
+ end
159
+
160
+ if (offset < 0)
161
+ offset += temp
162
+ i -= 1
163
+ nongDate[4] -= 1
164
+ end
165
+
166
+ nongDate[1] = i
167
+ nongDate[2] = offset + 1
168
+
169
+ nongDate
170
+ end
171
+
172
+ def get_day_in_chinese(day)
173
+ a = ""
174
+ if (day == 10)
175
+ return "初十"
176
+ elsif (day == 20)
177
+ return "二十"
178
+ elsif (day == 30)
179
+ return "三十"
180
+ end
181
+
182
+ two = ((day) / 10).to_i()
183
+
184
+ if (two == 0)
185
+ a = "初"
186
+ elsif (two == 1)
187
+ a = "十"
188
+ elsif (two == 2)
189
+ a = "廿"
190
+ elsif (two == 3)
191
+ a = "三"
192
+ else
193
+ a = "ERROR"
194
+ end
195
+
196
+ one = (day % 10)
197
+
198
+ case one
199
+ when 1 then a += "一"
200
+ when 2 then a += "二"
201
+ when 3 then a += "三"
202
+ when 4 then a += "四"
203
+ when 5 then a += "五"
204
+ when 6 then a += "六"
205
+ when 7 then a += "七"
206
+ when 8 then a += "八"
207
+ when 9 then a += "九"
208
+ end
209
+
210
+ return a
211
+ end
212
+
213
+ # Return the days in lunar of y year.
214
+ def days_in_lunar_date(y)
215
+ sum = 348
216
+ i = 0x8000
217
+ while i > 0x8
218
+ if ((@@lunar_info[y - 1900] & i) != 0)
219
+ sum += 1
220
+ end
221
+ i >>= 1
222
+ end
223
+
224
+ sum + leap_days(y)
225
+ end
226
+
227
+ # Return the leap days in y year.
228
+ def leap_days(y)
229
+ if (leap_month(y) != 0)
230
+ if ((@@lunar_info[y - 1900] & 0x10000) != 0)
231
+ return 30
232
+ else
233
+ return 29
234
+ end
235
+ else
236
+ return 0
237
+ end
238
+ end
239
+
240
+ #传回农历 y年闰哪个月 1-12 , 没闰传回 0
241
+ def leap_month(y)
242
+ @@lunar_info[y - 1900] & 0xf
243
+ end
244
+
245
+ # Return the days of m month in y year.
246
+ def monthDays(y, m)
247
+ if ((@@lunar_info[y - 1900] & (0x10000 >> m)) == 0)
248
+ return 29
249
+ else
250
+ return 30
251
+ end
252
+ end
253
+
254
+ end
255
+ end
@@ -0,0 +1,3 @@
1
+ module ChineseLunarCalendar
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,60 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ require 'date'
6
+ require "chinese_lunar_calendar/lunar"
7
+
8
+ module ChineseLunarCalendar
9
+ describe Lunar do
10
+
11
+ it "should convert to chinese lunar success" do
12
+ luanr = Lunar.new(Date.new(2012, 3, 26))
13
+ luanr.date_in_chinese.should eq "壬辰(龙)年 三月 初五"
14
+ luanr = Lunar.new(Date.new(1900, 1, 31))
15
+ luanr.date_in_chinese.should eq "庚子(鼠)年 正月 初一"
16
+ end
17
+
18
+ it "should convert 2012 year to 壬辰 success" do
19
+ luanr = Lunar.new(Date.new(2012, 3, 26))
20
+ luanr.year_in_chinese.should eq "壬辰"
21
+ end
22
+
23
+ it "should convert 2012 year to 龙 success" do
24
+ luanr = Lunar.new(Date.new(2012, 3, 26))
25
+ luanr.animal.should eq "龙"
26
+ end
27
+
28
+ it "should convert 2012 month to 三 success" do
29
+ luanr = Lunar.new(Date.new(2012, 3, 26))
30
+ luanr.month_in_chinese.should eq "三"
31
+ end
32
+
33
+ it "should convert 2012 year to 初五 success" do
34
+ luanr = Lunar.new(Date.new(2012, 3, 26))
35
+ luanr.day_in_chinese.should eq "初五"
36
+ end
37
+
38
+ it "should convert 1983 success" do
39
+ luanr = Lunar.new(Date.new(1983, 7, 23))
40
+ luanr.lunar_date.should eq "1983-6-14"
41
+ end
42
+
43
+ it "should convert 1985 success" do
44
+ luanr = Lunar.new(Date.new(1985, 2, 2))
45
+ luanr.lunar_date.should eq "1984-12-13"
46
+ end
47
+
48
+ it "should convert 1993 success" do
49
+ luanr = Lunar.new(::Date.new(1993, 8, 11))
50
+ luanr.lunar_date.should eq "1993-6-24"
51
+ luanr = Lunar.new(::Date.new(1900, 1, 31))
52
+ luanr.lunar_date.should eq "1900-1-1"
53
+ end
54
+
55
+ it "should convert 2012 success" do
56
+ luanr = Lunar.new(Date.new(2012, 3, 26))
57
+ luanr.lunar_date.should eq "2012-3-5"
58
+ end
59
+ end
60
+ 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,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinese_lunar_calendar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lanvige Jiang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.9.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.9.0
41
+ description: Generate the Lunar date
42
+ email:
43
+ - lanvige@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - chinese_lunar_calendar.gemspec
55
+ - lib/chinese_lunar_calendar.rb
56
+ - lib/chinese_lunar_calendar/lunar.rb
57
+ - lib/chinese_lunar_calendar/version.rb
58
+ - spec/chinese_lunar_calendar/lunar_spec.rb
59
+ - spec/spec_helper.rb
60
+ homepage: https://github.com/y1feng200156/chinese_lunar
61
+ licenses: []
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.3.6
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.6.12
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Generate the Lunar date
83
+ test_files:
84
+ - spec/chinese_lunar_calendar/lunar_spec.rb
85
+ - spec/spec_helper.rb