Class: EnglishNepaliDateConverter::BSDate

Inherits:
Object
  • Object
show all
Includes:
BSCalendar
Defined in:
lib/englishnepalidateconverter/bs_date.rb

Overview

Base BS Date class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BSCalendar

included

Constructor Details

#initialize(year = nil, month = nil, day = nil) ⇒ BSDate

Returns a new instance of BSDate.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/englishnepalidateconverter/bs_date.rb', line 12

def initialize(year = nil, month = nil, day = nil)
  if year && month && day
    @year = year
    @month = month
    @day = day
  else
    # No arguments provided, use current AD date and convert to BS
    bs = BSDate.from_ad(Date.today)
    @year = bs.year
    @month = bs.month
    @day = bs.day
  end
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



10
11
12
# File 'lib/englishnepalidateconverter/bs_date.rb', line 10

def day
  @day
end

#monthObject (readonly)

Returns the value of attribute month.



10
11
12
# File 'lib/englishnepalidateconverter/bs_date.rb', line 10

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



10
11
12
# File 'lib/englishnepalidateconverter/bs_date.rb', line 10

def year
  @year
end

Class Method Details

.from_ad(ad_date) ⇒ Object

Convert Anno Domini (Gregorian) date to Bikram Sambat date



37
38
39
40
# File 'lib/englishnepalidateconverter/bs_date.rb', line 37

def self.from_ad(ad_date)
  year, month, day = jd_to_bs(ad_date.jd)
  new(year, month, day)
end

Instance Method Details

#day_name(romanized: nil, localized: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/englishnepalidateconverter/bs_date.rb', line 46

def day_name(romanized: nil, localized: nil)
  if [romanized, localized].reject { |v| v.nil? || v == false }.size > 1
    raise ArgumentError, "You must provide exactly one of :romanized or :localized"
  end

  ad_date = to_ad

  return WEEKDAYS.values[ad_date.wday] if romanized
  return Date::DAYNAMES[ad_date.wday] if localized

  WEEKDAYS.keys[ad_date.wday]
end

#month_name(romanized: false) ⇒ Object



42
43
44
# File 'lib/englishnepalidateconverter/bs_date.rb', line 42

def month_name(romanized: false)
  romanized ? MONTHS.values[@month - 1] : MONTHS.keys[@month - 1]
end

#to_adObject

Convert Bikram Sambat date to Anno Domini (Gregorian) date



31
32
33
34
# File 'lib/englishnepalidateconverter/bs_date.rb', line 31

def to_ad
  validate_date!
  Date.jd(bs_to_jd(@year, @month, @day))
end

#to_nepaliObject



26
27
28
# File 'lib/englishnepalidateconverter/bs_date.rb', line 26

def to_nepali
  "#{month_name} #{day.to_nepali}, #{year.to_nepali}"
end