Module: VietnameseHoliday::Utils

Defined in:
lib/vietnamese_holiday/utils.rb

Overview

The calculate utils

Class Method Summary collapse

Class Method Details

.check(date) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/vietnamese_holiday/utils.rb', line 29

def self.check(date)
  solar_date = Date.parse(date)

  return true if solar_holiday_names.key?(solar_date.strftime('%-m-%-d').to_sym)

  ldate = lunar_date(solar_date)
  return true if lunar_holiday_names.key?("#{ldate.month}-#{ldate.day}".to_sym)

  false
end

.holidays(dates) ⇒ Object



52
53
54
55
56
# File 'lib/vietnamese_holiday/utils.rb', line 52

def self.holidays(dates)
  return [] if dates.count.zero?

  solar_holidays(dates) + lunar_holidays(dates)
end

.lunar_date(date) ⇒ Object



24
25
26
27
# File 'lib/vietnamese_holiday/utils.rb', line 24

def self.lunar_date(date)
  date = date.to_date if date.is_a? String
  LunarCalendar.at_lunar(date.year, date.month, date.day)
end

.lunar_holiday_namesObject



15
16
17
18
19
20
21
22
# File 'lib/vietnamese_holiday/utils.rb', line 15

def self.lunar_holiday_names
  {
    '1-1': 'Tết nguyên Đán',
    '1-2': 'Tết nguyên Đán',
    '1-3': 'Tết nguyên Đán',
    '3-10': 'Giỗ tổ Hùng Vương'
  }
end

.lunar_holidays(dates) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vietnamese_holiday/utils.rb', line 73

def self.lunar_holidays(dates)
  [].tap do |result|
    date_by_lunars = dates.map do  |date|
      ldate = lunar_date(date)
      ["#{ldate.month}-#{ldate.day}", date]
    end.to_h

    common_keys = date_by_lunars.keys & lunar_holiday_names.keys.map { _1.to_s }
    common_keys.each do |key|
      result << {
        date: date_by_lunars[key],
        name: lunar_holiday_names[key.to_sym]
      }
    end
  end
end

.name(date) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vietnamese_holiday/utils.rb', line 40

def self.name(date)
  [].tap do |result|
    solar_date = Date.parse(date)
    key = solar_date.strftime('%-m-%-d').to_sym
    result << solar_holiday_names[key] if solar_holiday_names.key?(key)

    ldate = lunar_date(solar_date)
    key = "#{ldate.month}-#{ldate.day}".to_sym
    result << lunar_holiday_names[key] if lunar_holiday_names.key?(key)
  end
end

.solar_holiday_namesObject



6
7
8
9
10
11
12
13
# File 'lib/vietnamese_holiday/utils.rb', line 6

def self.solar_holiday_names
  {
    '1-1': 'Tết Dương lịch',
    '4-30': 'Ngày giải phóng miền Nam',
    '5-1': 'Ngày Quốc tế Lao động',
    '2-9': 'Ngày Quốc Khánh'
  }
end

.solar_holidays(dates) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/vietnamese_holiday/utils.rb', line 58

def self.solar_holidays(dates)
  solar_year = dates.first.year

  [].tap do |result|
    date_keys = dates.map { _1.strftime('%-m-%-d').to_sym }
    common_keys = date_keys & solar_holiday_names.keys
    common_keys.each do |key|
      result << {
        date: "#{solar_year}-#{key.to_s}".to_date,
        name: solar_holiday_names[key]
      }
    end
  end
end