Class: Miti::NepaliDate

Inherits:
Object
  • Object
show all
Defined in:
lib/miti/nepali_date.rb

Overview

Class for nepali date

Defined Under Namespace

Classes: InvalidSeparatorError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(barsa:, mahina:, gatey:) ⇒ NepaliDate

require barsa, mahina and gatey as attributes



13
14
15
16
17
# File 'lib/miti/nepali_date.rb', line 13

def initialize(barsa:, mahina:, gatey:)
  @barsa = barsa
  @mahina = mahina
  @gatey = gatey
end

Instance Attribute Details

#barsaObject (readonly)

Returns the value of attribute barsa.



9
10
11
# File 'lib/miti/nepali_date.rb', line 9

def barsa
  @barsa
end

#gateyObject (readonly)

Returns the value of attribute gatey.



9
10
11
# File 'lib/miti/nepali_date.rb', line 9

def gatey
  @gatey
end

#mahinaObject (readonly)

Returns the value of attribute mahina.



9
10
11
# File 'lib/miti/nepali_date.rb', line 9

def mahina
  @mahina
end

Class Method Details

.monthsObject



89
90
91
# File 'lib/miti/nepali_date.rb', line 89

def months
  %w[वैशाख ज्येष्ठ आषाढ़ श्रावण भाद्र आश्विन कार्तिक मंसिर पौष माघ फाल्गुण चैत्र]
end

.months_in_englishObject



93
94
95
# File 'lib/miti/nepali_date.rb', line 93

def months_in_english
  %w[Baishakh Jestha Ashadh Shrawan Bhadra Asoj Kartik Mangsir Poush Magh Falgun Chaitra]
end

.parse(date_string) ⇒ Miti::NepaliDate

This method parses date in yyyy/mm/dd to NepaliDate object

Returns:



105
106
107
108
109
110
111
112
113
# File 'lib/miti/nepali_date.rb', line 105

def parse(date_string)
  regex = %r{\A\d{4}[,-/\s]\d{1,2}[,-/\s]\d{1,2}\z}
  raise "Invalid Date Format" unless regex.match(date_string)

  delimiters = ["-", " ", "/", ","]
  barsa, mahina, gatey = date_string.split(Regexp.union(delimiters))
  validate_parsed_date(barsa.to_i, mahina.to_i, gatey.to_i)
  NepaliDate.new(barsa: barsa.to_i, mahina: mahina.to_i, gatey: gatey.to_i)
end

.todayObject



81
82
83
# File 'lib/miti/nepali_date.rb', line 81

def today
  AdToBs.new(Date.today).convert
end

.week_daysObject



85
86
87
# File 'lib/miti/nepali_date.rb', line 85

def week_days
  %w[आइतबार सोमबार मंगलबार बुधबार बिहिबार शुक्रबार शनिबार]
end

.week_days_in_englishObject



97
98
99
# File 'lib/miti/nepali_date.rb', line 97

def week_days_in_english
  %w[Aitabar Somabar Mangalbar Budhabar Bihibar Sukrabar Sanibar]
end

Instance Method Details

#barInteger

Get weekday for a nepali date.

Returns:

  • (Integer)

    (0-6) that represents weekdays starting from 0.



31
32
33
# File 'lib/miti/nepali_date.rb', line 31

def bar
  @bar ||= tarik&.wday
end

#descriptive(nepali: false) ⇒ String

Descriptive output for current date When nepali flag is true, month is returned in nepali font and week day in Nepali

Returns:

  • (String)


62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/miti/nepali_date.rb', line 62

def descriptive(nepali: false)
  month_index = mahina - 1
  if nepali
    month = NepaliDate.months[month_index]
    week_day = "#{NepaliDate.week_days_in_english[bar]}(#{NepaliDate.week_days[bar]})}"
  else
    month = NepaliDate.months_in_english[month_index]
    week_day = tarik.strftime("%A")
  end

  "#{month} #{gatey}, #{barsa} #{week_day}"
end

#tarikDate

Get equivalent tarik(AD) for NepaliDate

Returns:

  • (Date)


23
24
25
# File 'lib/miti/nepali_date.rb', line 23

def tarik
  @tarik ||= BsToAd.new(self).convert
end

#to_hHash

returns details of nepali date in a ruby Hash

Returns:

  • (Hash)


39
40
41
# File 'lib/miti/nepali_date.rb', line 39

def to_h
  { barsa: barsa, mahina: mahina, gatey: gatey, bar: bar, tarik: tarik }
end

#to_s(separator: "-") ⇒ String

Returns Nepali Date in string format(yyyy/mm/dd).

Parameters:

  • separator (- by default) (defaults to: "-")

Returns:

  • (String)

Raises:



49
50
51
52
53
54
55
# File 'lib/miti/nepali_date.rb', line 49

def to_s(separator: "-")
  raise InvalidSeparatorError, "Invalid separator provided." unless [" ", "/", "-"].include?(separator)

  [barsa, mahina, gatey].reduce("") do |final_date, date_element|
    "#{final_date}#{final_date.empty? ? "" : separator}#{date_element < 10 ? 0 : ""}#{date_element}"
  end
end

#ydayObject



75
76
77
78
# File 'lib/miti/nepali_date.rb', line 75

def yday
  days_before_month = Miti::Data::NEPALI_YEAR_MONTH_HASH[barsa].first(mahina - 1).sum
  days_before_month + gatey
end