four-pillars 0.1.12 → 0.1.14

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/four-pillars.rb +137 -50
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df9278f927d44ce10526177dd5525427f429b9da2357683c00612ee00591950a
4
- data.tar.gz: 8ee588fe565c0a5fa0fd9a1be13a039f1882c1ae4de115a9d624f05ce1303629
3
+ metadata.gz: c3208005abb1c52cad09b0e9b335a22351553193308d8716b535eedde2ffd9aa
4
+ data.tar.gz: 570bf5d0a00163dc68202f42f76d07a7f746f2a54d52c6309e95ca01094dfc7c
5
5
  SHA512:
6
- metadata.gz: cb9c9c92a8d3b8dbd13f40f9bcf6981a4ce37cf2ad4e318851bc539654c922c30da2839cc606f1d1b4bfd70848c84596bc132ad861946cfc8ae3857fb78204fe
7
- data.tar.gz: 397bb11c87ea091baac4c0039d90ab6fbd34b3c9d8669b697cb7c2e5fc3426de73a0b010ea346d90747d09a9cdbb02ac5ec4cd73c13399c40cde8e20d0af549f
6
+ metadata.gz: c60b16002946f6e495eb72e2abe5230285e4b25b2ed6c5cc784cd26a28c02e70e97d4f66e6918b16b16273650add5c9047a0e2d1181e459e5b689cf142d3300f
7
+ data.tar.gz: 5c9f536213509c42325111551d35f3c9aa1e0891fde699ab358c0ee5f464e9e6a215659d62c98a0a6d967ee841d8f11e2ab59b128f7dc78437e6aa561bf8efa2
data/lib/four-pillars.rb CHANGED
@@ -118,12 +118,17 @@ class FourPillarsLogic
118
118
  # 生年月日時間, 性別(大運の向きに使用)
119
119
  attr_reader :birth_dt, :gender
120
120
 
121
- def initialize(birth_dt,gender)
122
- @birth_dt = birth_dt.map {|v| v.to_i }
121
+ def initialize(birth_dt,gender,with_time:false)
122
+ @birth_dt = birth_dt.map {|v| v.nil? ? nil : v.to_i }
123
123
  @gender = gender
124
+ @with_time = with_time
124
125
  raise "Incorrect birth date: #{birth_dt}" if @birth_dt.count != 5
125
126
  raise "Gender must be m,f or o(other): #{gender}" unless ['o','m','f'].include? @gender
126
127
  raise "Year must be larger than 1863" if @birth_dt[0] < 1864
128
+ h = @birth_dt[3]
129
+ if !h.nil? && (h < 0 || h > 23)
130
+ raise "Invalid hour: #{birth_dt}"
131
+ end
127
132
  end
128
133
 
129
134
  # 生年月日と性別
@@ -137,7 +142,12 @@ class FourPillarsLogic
137
142
  else
138
143
  g = ""
139
144
  end
140
- "#{y}年#{m}月#{d}日#{h}時#{i}分生 #{g}"
145
+ if h.nil? || i.nil?
146
+ t = ""
147
+ else
148
+ t = "#{h}時#{i}分"
149
+ end
150
+ "#{y}年#{m}月#{d}日#{t}生 #{g}"
141
151
  end
142
152
 
143
153
  # 前月の日数
@@ -196,7 +206,8 @@ class FourPillarsLogic
196
206
  setsuiri[0] == d
197
207
  end
198
208
 
199
- # 干支(日,月,年)
209
+ # 干支((時,)日,月,年)
210
+ # with_time=trueの時、時柱を含め長さ4の配列を返す
200
211
  def kanshi
201
212
  y,m,d,h,i = @birth_dt
202
213
  sd, st = setsuiri
@@ -206,7 +217,17 @@ class FourPillarsLogic
206
217
  md -= 1 if d < sd || (d == sd && h*60+i < st)
207
218
  dd = Date.new(y,m,d) - Date.new(1863,12,31) # 1923.10.18 = 甲子
208
219
 
209
- return [KANSHI_ARRAY[dd % 60],KANSHI_ARRAY[md % 60],KANSHI_ARRAY[yd % 60]]
220
+ return [KANSHI_ARRAY[dd % 60],KANSHI_ARRAY[md % 60],KANSHI_ARRAY[yd % 60]] unless @with_time
221
+
222
+ dp = KANSHI_ARRAY[dd % 60] # 日柱
223
+ if h.nil?
224
+ tp = nil
225
+ else
226
+ jyunishi_idx = h == 23 ? 0 : ((h + 1) / 2)
227
+ jikkan_idx = ((JIKKAN.index(dp[0]) % 5) * 2 + jyunishi_idx) % 10
228
+ tp = JIKKAN[jikkan_idx] + JYUNISHI[jyunishi_idx]
229
+ end
230
+ return [tp,dp,KANSHI_ARRAY[md % 60],KANSHI_ARRAY[yd % 60]]
210
231
  end
211
232
 
212
233
  # 干支(数字)
@@ -236,7 +257,7 @@ class FourPillarsLogic
236
257
  zn = zokan_number
237
258
  z = []
238
259
  kanshi.each do |k|
239
- j = k[1] # 十二支
260
+ j = k&.slice(1) # 十二支
240
261
  if zokan_hash1.keys.include? j
241
262
  z += [zokan_hash1[j]]
242
263
  elsif zokan_hash2.keys.include? j
@@ -255,6 +276,8 @@ class FourPillarsLogic
255
276
  else
256
277
  z += [arr[4]]
257
278
  end
279
+ else # j == nil
280
+ z += [nil]
258
281
  end
259
282
  end
260
283
  return z
@@ -262,52 +285,103 @@ class FourPillarsLogic
262
285
 
263
286
  # 通変星(nil,月,年)
264
287
  def tsuhensei
265
- m = FourPillarsLogic::tsuhensei(kanshi[0][0],kanshi[1][0])
266
- y = FourPillarsLogic::tsuhensei(kanshi[0][0],kanshi[2][0])
267
- [nil,m,y]
288
+ o = @with_time ? 1 : 0
289
+ m = FourPillarsLogic::tsuhensei(kanshi[o][0],kanshi[o + 1][0])
290
+ y = FourPillarsLogic::tsuhensei(kanshi[o][0],kanshi[o + 2][0])
291
+ if @with_time
292
+ tk = kanshi[0]&.slice(0)
293
+ if tk.nil?
294
+ t = nil
295
+ else
296
+ t = FourPillarsLogic::tsuhensei(kanshi[o][0],tk)
297
+ end
298
+ [t,nil,m,y]
299
+ else
300
+ [nil,m,y]
301
+ end
268
302
  end
269
303
 
270
304
  # 蔵干通変星
271
305
  def zokan_tsuhensei
272
- j = JIKKAN.index(kanshi[0][0])
273
- if j % 2 == 0 # 陽
274
- jikkan = JIKKAN
275
- else #
276
- jikkan = JIKKAN_IN
306
+ if @with_time
307
+ j = JIKKAN.index(kanshi[1][0])
308
+ if j % 2 == 0 # 陽
309
+ jikkan = JIKKAN
310
+ else #
311
+ jikkan = JIKKAN_IN
312
+ end
313
+ j = jikkan.index(kanshi[1][0])
314
+ j_time = jikkan.index(zokan[0])
315
+ j_day = jikkan.index(zokan[1])
316
+ j_month = jikkan.index(zokan[2])
317
+ j_year = jikkan.index(zokan[3])
318
+ if j_time.nil?
319
+ t_time = nil
320
+ else
321
+ t_time = j_time - j
322
+ t_time += 10 if t_time < 0
323
+ end
324
+ t_day = j_day - j
325
+ t_day += 10 if t_day < 0
326
+ t_month = j_month - j
327
+ t_month += 10 if t_month < 0
328
+ t_year = j_year - j
329
+ t_year += 10 if t_year < 0
330
+ [(t_time.nil? ? nil : TSUHENSEI[t_time]),TSUHENSEI[t_day],TSUHENSEI[t_month],TSUHENSEI[t_year]]
331
+ else
332
+ j = JIKKAN.index(kanshi[0][0])
333
+ if j % 2 == 0 # 陽
334
+ jikkan = JIKKAN
335
+ else # 陰
336
+ jikkan = JIKKAN_IN
337
+ end
338
+ j = jikkan.index(kanshi[0][0])
339
+ j_day = jikkan.index(zokan[0])
340
+ j_month = jikkan.index(zokan[1])
341
+ j_year = jikkan.index(zokan[2])
342
+ t_day = j_day - j
343
+ t_day += 10 if t_day < 0
344
+ t_month = j_month - j
345
+ t_month += 10 if t_month < 0
346
+ t_year = j_year - j
347
+ t_year += 10 if t_year < 0
348
+ [TSUHENSEI[t_day],TSUHENSEI[t_month],TSUHENSEI[t_year]]
277
349
  end
278
- j = jikkan.index(kanshi[0][0])
279
- j_day = jikkan.index(zokan[0])
280
- j_month = jikkan.index(zokan[1])
281
- j_year = jikkan.index(zokan[2])
282
- t_day = j_day - j
283
- t_day += 10 if t_day < 0
284
- t_month = j_month - j
285
- t_month += 10 if t_month < 0
286
- t_year = j_year - j
287
- t_year += 10 if t_year < 0
288
- [TSUHENSEI[t_day],TSUHENSEI[t_month],TSUHENSEI[t_year]]
289
350
  end
290
351
 
291
352
  # 十二運星
292
353
  def jyuniunsei
293
- d = FourPillarsLogic::jyuniunsei(kanshi[0][0],kanshi[0][1])
294
- m = FourPillarsLogic::jyuniunsei(kanshi[0][0],kanshi[1][1])
295
- y = FourPillarsLogic::jyuniunsei(kanshi[0][0],kanshi[2][1])
296
- [d,m,y]
354
+ o = @with_time ? 1 : 0
355
+ d = FourPillarsLogic::jyuniunsei(kanshi[o][0],kanshi[0 + o][1])
356
+ m = FourPillarsLogic::jyuniunsei(kanshi[o][0],kanshi[1 + o][1])
357
+ y = FourPillarsLogic::jyuniunsei(kanshi[o][0],kanshi[2 + o][1])
358
+ return [d,m,y] unless @with_time
359
+ if @birth_dt[3].nil? # when hour is nil
360
+ t = nil
361
+ else
362
+ t = FourPillarsLogic::jyuniunsei(kanshi[o][0],kanshi[0][1])
363
+ end
364
+ [t,d,m,y]
297
365
  end
298
366
 
299
- # 十二運星エネルギー
367
+ # 十二運星エネルギー 時中は使わない?
300
368
  def jyuniunsei_energy
301
369
  jyuniunsei.map {|v| JYUNIUNSEI_ENERGY[v] }
302
370
  end
303
371
 
304
372
  # 空亡 = 天中殺
305
373
  def kuubou
306
- k_day = (kanshi_as_number[0] - 1) / 10
307
- k_year = (kanshi_as_number[2] - 1) / 10
374
+ o = @with_time ? 1 : 0
375
+ k_day = (kanshi_as_number[0 + o] - 1) / 10
376
+ k_year = (kanshi_as_number[2 + o] - 1) / 10
308
377
  [KUUBOU[k_day],KUUBOU[k_year]]
309
378
  end
310
379
 
380
+ # 日柱のインデックス 時柱を求める場合は1になる
381
+ def offset_for_day
382
+ @with_time ? 1 : 0
383
+ end
384
+
311
385
  # テーブル表示用に配列にして返す (デバッグ用)
312
386
  # 0:干支, 1:干支数字, 2:蔵干, 3:通変星, 4:蔵干通変星, 5:十二運星
313
387
  # 五行、天中殺、十二運星エネルギーは別途
@@ -324,9 +398,14 @@ class FourPillarsLogic
324
398
  # 五行(十干)
325
399
  def gogyo_jikkan
326
400
  arr = []
327
- 3.times do |i|
328
- j = JIKKAN.index(kanshi[i][0])
329
- arr += [(j % 2 == 0 ? "+" : "-") + GOGYO[j / 2]]
401
+ kanshi.length.times do |i|
402
+ k = kanshi[i]&.slice(0)
403
+ if k.nil?
404
+ arr += [nil]
405
+ else
406
+ j = JIKKAN.index(k)
407
+ arr += [(j % 2 == 0 ? "+" : "-") + GOGYO[j / 2]]
408
+ end
330
409
  end
331
410
  return arr
332
411
  end
@@ -335,9 +414,14 @@ class FourPillarsLogic
335
414
  def gogyo_jyunishi
336
415
  arr = []
337
416
  gogyo_j = ["水","土","木","木","土","火","火","土","金","金","土","水"]
338
- 3.times do |i|
339
- j = JYUNISHI.index(kanshi[i][1])
340
- arr += [(j % 2 == 0 ? "+" : "-") + gogyo_j[j]]
417
+ kanshi.length.times do |i|
418
+ jj = kanshi[i]&.slice(1)
419
+ if jj.nil?
420
+ arr += [nil]
421
+ else
422
+ j = JYUNISHI.index(jj)
423
+ arr += [(j % 2 == 0 ? "+" : "-") + gogyo_j[j]]
424
+ end
341
425
  end
342
426
  return arr
343
427
  end
@@ -345,8 +429,8 @@ class FourPillarsLogic
345
429
  # 守護神
346
430
  def shugoshin
347
431
  # 日柱の十干と月柱の十二支
348
- x = JIKKAN.index(kanshi[0][0])
349
- y = JYUNISHI.index(kanshi[1][1])
432
+ x = JIKKAN.index(kanshi[0 + offset_for_day][0])
433
+ y = JYUNISHI.index(kanshi[1 + offset_for_day][1])
350
434
  SHUGOSHIN[y][x].split("")
351
435
  end
352
436
 
@@ -354,7 +438,9 @@ class FourPillarsLogic
354
438
  def shukumei
355
439
  s = []
356
440
  t = kuubou # 天中殺 0:上段, 1:下段
357
- k = kanshi.map {|v| v[1] } # 日,月,年柱の干支
441
+ ka = kanshi
442
+ ka.delete_at(0) if @with_time
443
+ k = ka.map {|v| v[1] } # 日,月,年柱の干支
358
444
  if t[0].include? k[2]
359
445
  s += ["生年中殺"]
360
446
  end
@@ -372,10 +458,10 @@ class FourPillarsLogic
372
458
  end
373
459
 
374
460
  def equivalent_kanshi(only_jikkan=false)
461
+ k = kanshi
462
+ k.delete_at(0) if @with_time
375
463
  if only_jikkan
376
- k = kanshi.map {|v| v[0] } # 日,月,年柱の十干
377
- else
378
- k = kanshi
464
+ k = k.map {|v| v[0] } # 日,月,年柱の十干
379
465
  end
380
466
  if k[0] == k[1] && k[1] == k[2]
381
467
  return [[0,1],[0,2],[1,2]]
@@ -407,8 +493,8 @@ class FourPillarsLogic
407
493
  v = []
408
494
  eq = equivalent_kanshi(true)
409
495
  eq.each do |e|
410
- k0 = kanshi[e[0]][1]
411
- k1 = kanshi[e[1]][1]
496
+ k0 = kanshi[e[0] + offset_for_day][1]
497
+ k1 = kanshi[e[1] + offset_for_day][1]
412
498
  v += [e] if on_distance?(k0,k1,6)
413
499
  end
414
500
  return v
@@ -420,13 +506,13 @@ class FourPillarsLogic
420
506
  v = []
421
507
  eq = equivalent_kanshi(true)
422
508
  eq.each do |e|
423
- k0 = kanshi[e[0]][1]
424
- k1 = kanshi[e[1]][1]
509
+ k0 = kanshi[e[0] + offset_for_day][1]
510
+ k1 = kanshi[e[1] + offset_for_day][1]
425
511
  v += [e] if on_distance?(k0,k1,4)
426
512
  end
427
513
  eq.each do |e|
428
- k0 = kanshi[e[0]][1]
429
- k1 = kanshi[e[1]][1]
514
+ k0 = kanshi[e[0] + offset_for_day][1]
515
+ k1 = kanshi[e[1] + offset_for_day][1]
430
516
  v += [e] if on_distance?(k0,k1,8)
431
517
  end
432
518
  return v
@@ -436,6 +522,7 @@ class FourPillarsLogic
436
522
  def taiun
437
523
  return [nil,nil] unless ['m','f'].include? @gender
438
524
  k = gogyo_jikkan[2][0]
525
+ # TODO: 時刻不明の場合は?
439
526
  t = @birth_dt[3] * 60 + @birth_dt[4] # 生まれた時間
440
527
  if (@gender == 'm' && k == "+") || (@gender == 'f' && k == '-')
441
528
  order = "順行"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: four-pillars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yosei Ito
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-27 00:00:00.000000000 Z
11
+ date: 2024-08-22 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A class which tells fortune by Four Pillar astrology(四柱推命).
14
14
  email: y-itou@lumber-mill.co.jp
@@ -37,7 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
- rubygems_version: 3.3.7
40
+ rubygems_version: 3.5.16
41
41
  signing_key:
42
42
  specification_version: 4
43
43
  summary: Four Pillar astrology