Class: EnglishNepaliDateConverter::DateConversion

Inherits:
Object
  • Object
show all
Defined in:
lib/english_nepali_date_converter/date_conversion.rb

Constant Summary collapse

EN_DAY =
{ 1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday" }
NP_DAY =
{ 1 => "Aaitabar", 2 => "Sombar", 3 => "Mangalbar", 4 => "Budhabar", 5 => "Bihibar", 6 => "Sukrabar", 7 => "Sanibar" }
NP_MONTH =
{ 1 => "Baisakh",	2 => "Jestha", 3 => "Asar", 4 => "Shrawan",	5 => "Bhadra", 6 => "Ashoj",
7 => "Kartik", 8 => "Mangsir",	9 => "Poush", 10 => "Magh", 11 => "Falgun", 12 => "Chaitra"	}
EN_MONTH =
{ 1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June",
7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#day_in_monthObject

Returns the value of attribute day_in_month.



4
5
6
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 4

def day_in_month
  @day_in_month
end

#day_of_weekObject

Returns the value of attribute day_of_week.



4
5
6
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 4

def day_of_week
  @day_of_week
end

#monthObject

Returns the value of attribute month.



4
5
6
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 4

def month
  @month
end

#month_numberObject

Returns the value of attribute month_number.



4
5
6
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 4

def month_number
  @month_number
end

#week_dayObject

Returns the value of attribute week_day.



4
5
6
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 4

def week_day
  @week_day
end

#yearObject

Returns the value of attribute year.



4
5
6
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 4

def year
  @year
end

Class Method Details

.get_day_of_week(day, type) ⇒ Object



141
142
143
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 141

def self.get_day_of_week(day, type)
	type == "en" ? self::EN_DAY[day] : self::NP_DAY[day]
end

.get_month_name(month, type) ⇒ Object



145
146
147
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 145

def self.get_month_name(month, type)
	type == "en" ? self::EN_MONTH[month] : self::NP_MONTH[month]
end

Instance Method Details

#eng_to_nep(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 15

def eng_to_nep(*args)

	# Normal days in month except leap year
	enDaysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
	return "Enter appropriate month" unless (1..12).include?(args[1])

	if Date.leap?(args[2].to_i)
		return "Enter appropriate day of the month" unless (1.. + enDaysInMonth[args[1].to_i - 1] + 1).include?(args[0])
	else
		return "Enter appropriate day of the month" unless (1.. + enDaysInMonth[args[1].to_i - 1]).include?(args[0])
	end

	formated_date = args[0].to_s + "-" + args[1].to_s + "-" + args[2].to_s
	starting_date = DateTime.parse("01-01-1944").mjd.to_i
	ending_date = DateTime.parse("31-12-2033").mjd
	date_to_convert = DateTime.parse(formated_date).mjd
	return "Enter a valid date from 01-01-1944 to 31-12-2033" unless (starting_date..ending_date).include?(date_to_convert)

	enTotalDays = date_to_convert - starting_date

	# Conversion of 01-01-1944 AD will be 17.01.2000  BS. Take this BS as a base date
	@year = npStartYear = 2000
	@month_number = npStartMth = 9
	@day_in_month = npStartDay = 17

	startingIndexOfBSDate = 0
	@day_of_week = 7 # Saturday as Day 7 and Sunday as Day 1 of the week

	while enTotalDays !=0
		@day_in_month += 1
		@day_of_week += 1

		if @day_in_month > BsDate::BS_DATES[startingIndexOfBSDate][npStartMth]
			@month_number += 1
			@day_in_month = 1
			npStartMth += 1
		end

		@day_of_week = 1 if @day_of_week > 7

		if @month_number > 12
			@year += 1
			@month_number = 1
		end

		if npStartMth > 12
			npStartMth = 1
			startingIndexOfBSDate += 1
		end

		enTotalDays -= 1
	end

	@week_day = self.class.get_day_of_week(@day_of_week, 'np')
	@month = self.class.get_month_name(@month_number, 'np')
	return self
end

#nep_to_eng(*args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/english_nepali_date_converter/date_conversion.rb', line 73

def nep_to_eng(*args)

	return "Enter appropriate month" unless (1..12).include?(args[1])

	check_year = []
	for i in 0..90
		check_year << BsDate::BS_DATES[i][0]
	end

	return "Enter appropriate year" unless check_year.index(args[2])

	return "Enter appropriate day of the month" if BsDate::BS_DATES[check_year.index(args[2])][args[1]] < args[0]

	enStartYear = 1943;	enStartMonth = 4; enStartDay = 14 - 1

	npStartYear = 2000;	npStartMth = 1;	npStartDay = 1

	@day_of_week = 4 - 1
	# (2000..2089).include?(args[2]) && (01..12).include?(args[1]) && (01..32).include?(args[0])
	# 2000-01-01 to 2089-12-32
	enDaysInMonth = [0,31,28,31,30,31,30,31,31,30,31,30,31]
	leapEnDaysInMonth = [0,31,29,31,30,31,30,31,31,30,31,30,31]

	npYearDiff = args[2] - npStartYear - 1
	npTotalDays = 0

	for i in 0.. + npYearDiff
		 BsDate::BS_DATES[i].shift
		 npTotalDays += BsDate::BS_DATES[i].inject(:+)
	end

	npMonthDiff = args[1] -1

	for i in 1.. + npMonthDiff
		npTotalDays += BsDate::BS_DATES[args[2] - npStartYear][i]
	end

	npTotalDays += args[0]

	@day_in_month = enStartDay
	@month_number = enStartMonth;
	@year = enStartYear;

	while npTotalDays != 0

		total_month_days = Date.leap?(year) ? leapEnDaysInMonth[@month_number] : enDaysInMonth[@month_number]

		@day_in_month += 1
		@day_of_week += 1

		if @day_in_month > total_month_days
			@month_number += 1
			@day_in_month = 1

			if @month_number > 12
				@year += 1
				@month_number = 1
			end
		end

		@day_of_week = 1 if @day_of_week > 7
		npTotalDays -= 1
	end
	@week_day = self.class.get_day_of_week(@day_of_week, 'en')
	@month = self.class.get_month_name(@month_number, 'en')
	return self
end