englishnepalidateconverter 1.0.0 → 2.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 659b8de7ddfe4d8de1bd128e8e8528ca20aed20e580e5b9a96991a5589e62b5d
4
- data.tar.gz: d783218484da57fccd4606f08d233ad5321cd1dbe4096eb2cc253a026321bc8e
3
+ metadata.gz: c5b1bc863beae761d07b77b3e4ff5081c642e7e1aef2dd326bd646de58adaa4f
4
+ data.tar.gz: b9b0641bcb2a06604d94128efca2ccccd70d11cda5902efc69c155d97c73c306
5
5
  SHA512:
6
- metadata.gz: e61c85f805523b0e66a2ba38dc85db696d3e9474d541953d79ec57abca42a314b25495f4cc07266d6af3cb8fc6f9624da948989e6582b6a0f961c4f22d90e9cc
7
- data.tar.gz: a7a10d0992d37e5c7f97283b99abf2ae0bc9d7c649dc4e6ad9ebfb79282bbcc3912867fe36a3f1092b78dc27e33c8e75663f0307357e5d75a80966e22ea89ca1
6
+ metadata.gz: cce984e5a63bb15a2268318ab01eeb5de0f29dc6baad7b3097cb3391ee1b38efb4006e008e0a0b06967b96562e3477d2ea2d225d0bd236c567fd0883d9e5cf06
7
+ data.tar.gz: 5b6eed8c2afc6099fdcb6bfd4e0fa062bf0f29061d832506d1cd7bc8f8fc3e6705b75d2977198a931beb11364e174005532a12595fcb09a3e96e649c315fc55b
@@ -0,0 +1,71 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ tags: ["v*"]
7
+ pull_request:
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ test:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: .ruby-version
20
+ bundler-cache: true
21
+ - name: Run tests
22
+ run: bundle exec rspec
23
+ - name: Run linter
24
+ run: bundle exec rubocop
25
+
26
+ # Runs only when a version tag (e.g. v2.0.1) is pushed, after the tests pass.
27
+ # Publishes the same gem under two names on RubyGems.org.
28
+ release:
29
+ needs: test
30
+ if: startsWith(github.ref, 'refs/tags/v')
31
+ runs-on: ubuntu-latest
32
+ permissions:
33
+ contents: read
34
+ id-token: write # needed for RubyGems.org trusted publishing
35
+ steps:
36
+ - uses: actions/checkout@v4
37
+ - uses: ruby/setup-ruby@v1
38
+ with:
39
+ ruby-version: .ruby-version
40
+ - name: Check tag matches gem version
41
+ run: |
42
+ version=$(ruby -Ilib -e 'require "englishnepalidateconverter/version"; puts EnglishNepaliDateConverter::VERSION')
43
+ if [ "v$version" != "$GITHUB_REF_NAME" ]; then
44
+ echo "Tag $GITHUB_REF_NAME does not match gem version $version"
45
+ exit 1
46
+ fi
47
+ - name: Build gems
48
+ run: |
49
+ ruby -rrubygems/package -e '
50
+ spec = Gem::Specification.load("englishnepalidateconverter.gemspec")
51
+ %w[englishnepalidateconverter english_nepali_date_converter].each do |name|
52
+ copy = spec.dup
53
+ copy.name = name
54
+ Gem::Package.build(copy)
55
+ end
56
+ '
57
+ ls -l *.gem
58
+
59
+ - name: Configure RubyGems credentials (englishnepalidateconverter)
60
+ uses: rubygems/configure-rubygems-credentials@v2.2.0
61
+ - name: Push englishnepalidateconverter
62
+ run: gem push englishnepalidateconverter-*.gem
63
+
64
+ # The first release of a new gem through a pending trusted publisher needs
65
+ # the RubyGems.org account ID. Set it as the repository variable RUBYGEMS_OWNER_ID.
66
+ - name: Configure RubyGems credentials (english_nepali_date_converter)
67
+ uses: rubygems/configure-rubygems-credentials@v2.2.0
68
+ with:
69
+ owner: ${{ vars.RUBYGEMS_OWNER_ID }}
70
+ - name: Push english_nepali_date_converter
71
+ run: gem push english_nepali_date_converter-*.gem
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 3.4
2
+ TargetRubyVersion: 4.0
3
3
 
4
4
  Style/StringLiterals:
5
5
  EnforcedStyle: double_quotes
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 4.0.10
data/README.md CHANGED
@@ -1,89 +1,258 @@
1
1
  # EnglishNepaliDateConverter
2
2
 
3
- This EnglishNepaliDateConverter gem converts between Anno Domini (AD/Gregorian) and Bikram Sambat (BS/Nepali) dates.
3
+ [![CI](https://github.com/dlamichhane/englishnepalidateconverter/actions/workflows/ci.yml/badge.svg)](https://github.com/dlamichhane/englishnepalidateconverter/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/englishnepalidateconverter.svg)](https://rubygems.org/gems/englishnepalidateconverter)
4
5
 
5
- **Supported date ranges:**
6
+ A dependency-free Ruby gem that converts dates between the Gregorian calendar (AD) and the Bikram Sambat calendar (BS), Nepal's official calendar. It can also format BS dates in Nepali script, with month and weekday names in Nepali, romanized Nepali or English.
6
7
 
7
- - AD to BS: 1943-04-14 to 2034-04-13
8
- - BS to AD: 2000-01-01 to 2090-12-30
8
+ ```ruby
9
+ require "englishnepalidateconverter"
10
+
11
+ Converter = EnglishNepaliDateConverter::Converter
12
+
13
+ Converter.to_bs(Date.new(2014, 1, 17)).to_nepali # => "माघ ३, २०७०"
14
+ Converter.to_ad(2070, 10, 3) # => #<Date: 2014-01-17>
15
+ ```
16
+
17
+ ## Contents
18
+
19
+ - [Features](#features)
20
+ - [Supported date range](#supported-date-range)
21
+ - [Installation](#installation)
22
+ - [Usage](#usage)
23
+ - [The converter](#the-converter)
24
+ - [Convert BS to AD](#convert-bs-to-ad)
25
+ - [Convert AD to BS](#convert-ad-to-bs)
26
+ - [Today's date in BS](#todays-date-in-bs)
27
+ - [Month and weekday names](#month-and-weekday-names)
28
+ - [Nepali digits](#nepali-digits)
29
+ - [Error handling](#error-handling)
30
+ - [API reference](#api-reference)
31
+ - [Development](#development)
32
+ - [Contributing](#contributing)
33
+ - [License](#license)
34
+
35
+ ## Features
36
+
37
+ - Convert BS to AD and AD to BS.
38
+ - Get today's date in BS.
39
+ - Month names in Nepali (`माघ`) or romanized Nepali (`Magh`).
40
+ - Weekday names in Nepali (`शुक्रबार`), romanized Nepali (`Shukrabar`) or English (`Friday`).
41
+ - Full Nepali date strings such as `माघ ३, २०७०`.
42
+ - Convert any integer to Nepali digits (`2070` → `२०७०`).
43
+ - No runtime dependencies. It uses only Ruby's standard `Date` library.
44
+
45
+ ## Supported date range
46
+
47
+ Conversion uses a built-in table of BS month lengths, so it only works within this range:
48
+
49
+ | Direction | From | To |
50
+ | --------- | ---------- | ---------- |
51
+ | AD → BS | 1943-04-14 | 2034-04-13 |
52
+ | BS → AD | 2000-01-01 | 2090-12-30 |
53
+
54
+ Dates outside this range raise an error. See [Error handling](#error-handling).
9
55
 
10
56
  ## Installation
11
57
 
12
- Add this line to your application's Gemfile:
58
+ Requires Ruby 4.0 or newer. Version 1.0.0 on RubyGems still supports Ruby 3.4.
13
59
 
14
- gem 'englishnepalidateconverter'
60
+ Add the gem to your application's `Gemfile`:
15
61
 
16
- And then execute:
62
+ ```ruby
63
+ gem "englishnepalidateconverter"
64
+ ```
17
65
 
18
- $ bundle
66
+ Then run:
19
67
 
20
- Or install it yourself as:
68
+ ```sh
69
+ bundle install
70
+ ```
21
71
 
22
- $ gem install englishnepalidateconverter
72
+ Or install it directly:
73
+
74
+ ```sh
75
+ gem install englishnepalidateconverter
76
+ ```
23
77
 
24
78
  ## Usage
25
79
 
26
- ### Basic Date Conversion
80
+ Require the gem once. It loads `date` for you:
81
+
82
+ ```ruby
83
+ require "englishnepalidateconverter"
84
+ ```
85
+
86
+ ### The converter
87
+
88
+ `EnglishNepaliDateConverter::Converter` handles both directions in one place:
27
89
 
28
90
  ```ruby
29
- require 'englishnepalidateconverter'
91
+ Converter = EnglishNepaliDateConverter::Converter
30
92
 
31
- # Create a BS date from year, month, day
93
+ Converter.to_bs(Date.new(2025, 9, 3)) # => BSDate 2082-05-18
94
+ Converter.to_ad(BSDate.new(2082, 5, 18)) # => #<Date: 2025-09-03>
95
+ Converter.to_ad(2082, 5, 18) # => #<Date: 2025-09-03>
96
+ ```
97
+
98
+ Both methods check that the date is within the [supported range](#supported-date-range). `Date#to_bs`, `BSDate#to_ad` and `BSDate.from_ad`, described below, are shortcuts that call the converter.
99
+
100
+ ### Convert BS to AD
101
+
102
+ Create a `BSDate` from a year, month and day, then call `to_ad`. It returns a standard Ruby `Date`:
103
+
104
+ ```ruby
32
105
  bs_date = BSDate.new(2070, 10, 3)
33
- puts bs_date.to_ad # => 2014-01-16
34
106
 
35
- # Create a BS date from an AD date
36
- bs_date = BSDate.from_ad(Date.new(2014, 1, 16))
37
- puts "#{bs_date.year}-#{bs_date.month}-#{bs_date.day}" # => 2070-10-3
107
+ ad_date = bs_date.to_ad # => #<Date: 2014-01-17>
108
+ ad_date.to_s # => "2014-01-17"
109
+ ad_date.strftime("%d %B %Y") # => "17 January 2014"
110
+ ```
38
111
 
39
- # Create current BS date (no arguments)
40
- current_bs = BSDate.new
41
- puts current_bs.to_ad
112
+ Months are numbered 1 to 12, with 1 being Baisakh. See the [month table](#months).
113
+
114
+ ### Convert AD to BS
115
+
116
+ Call `to_bs` on any `Date`:
117
+
118
+ ```ruby
119
+ bs_date = Date.new(2014, 1, 17).to_bs
120
+
121
+ bs_date.year # => 2070
122
+ bs_date.month # => 10
123
+ bs_date.day # => 3
42
124
  ```
43
125
 
44
- ### Using Date Extensions
126
+ `BSDate.from_ad(date)` does the same conversion.
127
+
128
+ ### Today's date in BS
129
+
130
+ Call `BSDate.new` with no arguments to get today's date in BS:
45
131
 
46
132
  ```ruby
47
- # Convert AD date to BS using Date extension
48
- ad_date = Date.new(2014, 1, 16)
49
- bs_date = ad_date.to_bs
50
- puts "#{bs_date.year}-#{bs_date.month}-#{bs_date.day}" # => 2070-10-3
133
+ today = BSDate.new
134
+ today.to_nepali # => e.g. "मंसिर २१, २०८३"
51
135
  ```
52
136
 
53
- ### Nepali Text Formatting
137
+ ### Month and weekday names
54
138
 
55
139
  ```ruby
56
140
  bs_date = BSDate.new(2070, 10, 3)
57
141
 
58
- # Get month name in Nepali
59
- puts bs_date.month_name # => "मंसिर"
142
+ bs_date.month_name # => "माघ"
143
+ bs_date.month_name(romanized: true) # => "Magh"
60
144
 
61
- # Get month name in romanized form
62
- puts bs_date.month_name(romanized: true) # => "Mangsir"
145
+ bs_date.day_name # => "शुक्रबार"
146
+ bs_date.day_name(romanized: true) # => "Shukrabar"
147
+ bs_date.day_name(localized: true) # => "Friday"
63
148
 
64
- # Get day name in Nepali
65
- puts bs_date.day_name # => "बुधबार"
149
+ bs_date.to_nepali # => "माघ ३, २०७०"
150
+ ```
151
+
152
+ `day_name` takes either `romanized: true` or `localized: true`, not both at once.
153
+
154
+ #### Months
155
+
156
+ | # | Nepali | Romanized |
157
+ | --- | ------- | --------- |
158
+ | 1 | बैशाख | Baisakh |
159
+ | 2 | जेठ | Jeth |
160
+ | 3 | असार | Asar |
161
+ | 4 | श्रावण | Shrawan |
162
+ | 5 | भाद्र | Bhadra |
163
+ | 6 | आश्विन | Ashwin |
164
+ | 7 | कार्तिक | Kartik |
165
+ | 8 | मंसिर | Mangsir |
166
+ | 9 | पुष | Poush |
167
+ | 10 | माघ | Magh |
168
+ | 11 | फाल्गुण | Falgun |
169
+ | 12 | चैत | Chaitra |
66
170
 
67
- # Get day name in romanized form
68
- puts bs_date.day_name(romanized: true) # => "Budhbar"
171
+ #### Weekdays
69
172
 
70
- # Get day name in English
71
- puts bs_date.day_name(localized: true) # => "Wednesday"
173
+ | English | Nepali | Romanized |
174
+ | --------- | -------- | --------- |
175
+ | Sunday | आइतबार | Aaitabar |
176
+ | Monday | सोमबार | Sombar |
177
+ | Tuesday | मंगलवार | Mangalbar |
178
+ | Wednesday | बुधबार | Budhbar |
179
+ | Thursday | बिहीबार | Bihibar |
180
+ | Friday | शुक्रबार | Shukrabar |
181
+ | Saturday | शनिवार | Shanibar |
72
182
 
73
- # Get formatted Nepali date string
74
- puts bs_date.to_nepali # => "मंसिर ३, २०७०"
183
+ ### Nepali digits
184
+
185
+ The gem adds `to_nepali` to `Integer`, which returns the number written in Nepali digits:
186
+
187
+ ```ruby
188
+ 2070.to_nepali # => "२०७०"
189
+ 16.to_nepali # => "१६"
190
+ -5.to_nepali # => "-५"
75
191
  ```
76
192
 
77
- ### Integer Extensions for Nepali Numbers
193
+ ### Error handling
194
+
195
+ All of the gem's errors inherit from `EnglishNepaliDateConverter::Error`, so you can catch them all with one `rescue`:
196
+
197
+ | Error | Raised when |
198
+ | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
199
+ | `DateOutOfRangeError` | `Converter.to_bs` (or `Date#to_bs`, `BSDate.from_ad`) gets a date outside 1943-04-14 to 2034-04-13. |
200
+ | `BSDateOutOfRangeError` | `Converter.to_ad` or `BSDate#to_ad` (and methods that use it, such as `day_name`) gets a year outside 2000 to 2090, a month outside 1 to 12, or a day that doesn't exist in that BS month. |
78
201
 
79
202
  ```ruby
80
- # Convert numbers to Nepali digits
81
- puts 2070.to_nepali # => "२०७०"
82
- puts 16.to_nepali # => "१६"
203
+ begin
204
+ Date.new(1900, 1, 1).to_bs
205
+ rescue EnglishNepaliDateConverter::Error => e
206
+ puts e.message
207
+ # => "Date 1900-01-01 is out of supported AD Range (1943-04-14 - 2034-04-13)"
208
+ end
209
+ ```
210
+
211
+ `BSDate.new` doesn't check its arguments when you create it. An invalid date raises an error only when you convert it or ask for its weekday.
212
+
213
+ ## API reference
214
+
215
+ `Converter` lives only inside `EnglishNepaliDateConverter`. `BSDate`, `DateOutOfRangeError` and `BSDateOutOfRangeError` are available at the top level, and also inside `EnglishNepaliDateConverter`.
216
+
217
+ | Method | Returns | Description |
218
+ | ----------------------------------------------------------------- | --------- | ---------------------------------------------------- |
219
+ | `Converter.to_bs(date)` | `BSDate` | Converts a `Date` to BS, with a range check. |
220
+ | `Converter.to_ad(bs_date)` or `Converter.to_ad(year, month, day)` | `Date` | Converts a BS date to AD, with a range check. |
221
+ | `BSDate.new(year, month, day)` | `BSDate` | A BS date. Not validated until it is used. |
222
+ | `BSDate.new` | `BSDate` | Today's date in BS. |
223
+ | `BSDate.from_ad(date)` | `BSDate` | Same as `Converter.to_bs`. |
224
+ | `BSDate#year`, `#month`, `#day` | `Integer` | Parts of the BS date. |
225
+ | `BSDate#to_ad` | `Date` | Converts to AD. |
226
+ | `BSDate#month_name(romanized: false)` | `String` | Month name in Nepali, or romanized Nepali. |
227
+ | `BSDate#day_name(romanized: nil, localized: nil)` | `String` | Weekday name in Nepali, romanized Nepali or English. |
228
+ | `BSDate#to_nepali` | `String` | Formatted date, e.g. `"माघ ३, २०७०"`. |
229
+ | `Date#to_bs` | `BSDate` | Same as `Converter.to_bs`. |
230
+ | `Integer#to_nepali` | `String` | The number in Nepali digits. |
231
+
232
+ ## Development
233
+
234
+ After cloning the repository:
235
+
236
+ ```sh
237
+ bundle install # install development dependencies
238
+ bundle exec rspec # run the tests
239
+ bundle exec rubocop # run the linter
240
+ bundle exec rake install # build and install the gem locally
83
241
  ```
84
242
 
243
+ The BS month-length table is in [`lib/englishnepalidateconverter/bs_calendar.rb`](lib/englishnepalidateconverter/bs_calendar.rb). To support more years, add rows there. The supported AD and BS ranges are derived from the table, and all conversion logic lives in [`lib/englishnepalidateconverter/converter.rb`](lib/englishnepalidateconverter/converter.rb).
244
+
245
+ ## Contributing
246
+
247
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/dlamichhane/englishnepalidateconverter).
248
+
249
+ 1. Fork the repository and create a branch for your change.
250
+ 2. Add tests for it in `spec/`.
251
+ 3. Make sure `bundle exec rspec` and `bundle exec rubocop` pass.
252
+ 4. Open a pull request.
253
+
85
254
  ## License
86
255
 
87
- This project is licensed under the MIT License - see the [LICENSE.txt](LICENSE.txt) file for details.
256
+ This gem is available under the [MIT License](LICENSE.txt).
88
257
 
89
- Copyright (c) 2014 dlamichhane
258
+ Copyright (c) 2027 dlamichhane
@@ -8,7 +8,7 @@ require "englishnepalidateconverter/version"
8
8
  Gem::Specification.new do |spec|
9
9
  spec.name = "englishnepalidateconverter"
10
10
  spec.version = EnglishNepaliDateConverter::VERSION
11
- spec.date = %(2025-09-01)
11
+ spec.date = %(2026-12-06)
12
12
  spec.authors = ["dlamichhane"]
13
13
  spec.email = ["lamichhanedeepak@gmail.com"]
14
14
  spec.description = "Conversion of Gregorian calendar to Nepali Calendar"
@@ -21,13 +21,13 @@ Gem::Specification.new do |spec|
21
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
22
  spec.require_paths = ["lib"]
23
23
 
24
- spec.add_development_dependency "bundler", "~> 2.7"
24
+ spec.add_development_dependency "bundler", "~> 4.0"
25
25
  spec.add_development_dependency "pry-byebug"
26
26
  spec.add_development_dependency "rake"
27
- spec.add_development_dependency "rspec", "~> 3.0"
28
- spec.add_development_dependency "rubocop", "~> 1.8"
29
- spec.add_development_dependency "rubocop-rake"
30
- spec.add_development_dependency "rubocop-rspec"
27
+ spec.add_development_dependency "rspec", "~> 3.13"
28
+ spec.add_development_dependency "rubocop", "~> 1.93"
29
+ spec.add_development_dependency "rubocop-rake", "~> 0.7"
30
+ spec.add_development_dependency "rubocop-rspec", "~> 3.11"
31
31
 
32
- spec.required_ruby_version = ">= 3.4"
32
+ spec.required_ruby_version = ">= 4.0"
33
33
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "bs_calendar"
4
+ require_relative "converter"
4
5
 
5
6
  module EnglishNepaliDateConverter
6
7
  # Base BS Date class
@@ -16,7 +17,7 @@ module EnglishNepaliDateConverter
16
17
  @day = day
17
18
  else
18
19
  # No arguments provided, use current AD date and convert to BS
19
- bs = BSDate.from_ad(Date.today)
20
+ bs = Converter.to_bs(Date.today)
20
21
  @year = bs.year
21
22
  @month = bs.month
22
23
  @day = bs.day
@@ -29,14 +30,12 @@ module EnglishNepaliDateConverter
29
30
 
30
31
  # Convert Bikram Sambat date to Anno Domini (Gregorian) date
31
32
  def to_ad
32
- validate_date!
33
- Date.jd(bs_to_jd(@year, @month, @day))
33
+ Converter.to_ad(self)
34
34
  end
35
35
 
36
36
  # Convert Anno Domini (Gregorian) date to Bikram Sambat date
37
37
  def self.from_ad(ad_date)
38
- year, month, day = jd_to_bs(ad_date.jd)
39
- new(year, month, day)
38
+ Converter.to_bs(ad_date)
40
39
  end
41
40
 
42
41
  def month_name(romanized: false)
@@ -55,57 +54,5 @@ module EnglishNepaliDateConverter
55
54
 
56
55
  WEEKDAYS.keys[ad_date.wday]
57
56
  end
58
-
59
- private
60
-
61
- def validate_date!
62
- return if valid_date?
63
-
64
- raise BSDateOutOfRangeError,
65
- "Year #{year} is out of valid range (#{MONTH_DAYS.keys.first}-#{MONTH_DAYS.keys.last})"
66
- end
67
-
68
- def valid_date?
69
- MONTH_DAYS.key?(year) && (1..12).include?(month) && (1..MONTH_DAYS[year][month - 1]).include?(day)
70
- end
71
-
72
- def bs_to_jd(year, month, day)
73
- julian_day = EPOCH_JD
74
-
75
- # Add days from complete years
76
- (EPOCH_BS[0]...year).each do |year_index|
77
- julian_day += MONTH_DAYS[year_index].sum
78
- end
79
-
80
- # Add days from complete months in the current year
81
- julian_day += MONTH_DAYS[year][0...(month - 1)].sum
82
-
83
- # Add remaining days
84
- julian_day += (day - 1)
85
-
86
- julian_day
87
- end
88
-
89
- def self.jd_to_bs(julian_day)
90
- year = EPOCH_BS[0]
91
- remaining_days = julian_day - EPOCH_JD
92
-
93
- # Find the correct year by subtracting complete years
94
- while remaining_days >= MONTH_DAYS[year].sum
95
- remaining_days -= MONTH_DAYS[year].sum
96
- year += 1
97
- end
98
-
99
- # Find the correct month and day within the year
100
- month = 1
101
- MONTH_DAYS[year].each do |days_in_month|
102
- break if remaining_days < days_in_month
103
-
104
- remaining_days -= days_in_month
105
- month += 1
106
- end
107
-
108
- [year, month, remaining_days + 1]
109
- end
110
57
  end
111
58
  end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require_relative "bs_calendar"
5
+ require_relative "bs_date"
6
+ require_relative "errors"
7
+
8
+ module EnglishNepaliDateConverter
9
+ # Converts dates between the Gregorian (AD) and Bikram Sambat (BS) calendars.
10
+ class Converter
11
+ include BSCalendar
12
+
13
+ AD_RANGE = (EPOCH_AD..(EPOCH_AD + MONTH_DAYS.values.sum(&:sum) - 1))
14
+ BS_RANGE = (EPOCH_BS..[MONTH_DAYS.keys.last, 12, MONTH_DAYS.values.last.last])
15
+
16
+ class << self
17
+ # Convert an AD date (Date, DateTime or Time) to a BSDate.
18
+ # Raises DateOutOfRangeError outside the supported range.
19
+ def to_bs(ad_date)
20
+ ad_date = ad_date.to_date
21
+ unless AD_RANGE.cover?(ad_date)
22
+ raise DateOutOfRangeError,
23
+ "Date #{ad_date} is out of supported AD Range (#{AD_RANGE.begin} - #{AD_RANGE.end})"
24
+ end
25
+
26
+ year, month, day = jd_to_bs(ad_date.jd)
27
+ BSDate.new(year, month, day)
28
+ end
29
+
30
+ # Convert a BS date to an AD Date. Accepts a BSDate or year, month, day.
31
+ # Raises BSDateOutOfRangeError if the BS date does not exist in the table.
32
+ def to_ad(*args)
33
+ year, month, day = bs_parts(*args)
34
+ validate_bs!(year, month, day)
35
+ Date.jd(bs_to_jd(year, month, day))
36
+ end
37
+
38
+ private
39
+
40
+ def bs_parts(*args)
41
+ case args
42
+ in [BSDate => bs_date] then [bs_date.year, bs_date.month, bs_date.day]
43
+ in [Integer, Integer, Integer] then args
44
+ else raise ArgumentError, "Expected a BSDate or year, month, day integers"
45
+ end
46
+ end
47
+
48
+ def validate_bs!(year, month, day)
49
+ return if MONTH_DAYS.key?(year) && (1..12).cover?(month) && (1..MONTH_DAYS[year][month - 1]).cover?(day)
50
+
51
+ raise BSDateOutOfRangeError,
52
+ "BS date #{year}-#{month}-#{day} is out of supported range " \
53
+ "(#{BS_RANGE.begin.join("-")} - #{BS_RANGE.end.join("-")})"
54
+ end
55
+
56
+ def bs_to_jd(year, month, day)
57
+ days_in_prior_years = (EPOCH_BS[0]...year).sum { |y| MONTH_DAYS[y].sum }
58
+ days_in_prior_months = MONTH_DAYS[year].first(month - 1).sum
59
+
60
+ EPOCH_JD + days_in_prior_years + days_in_prior_months + (day - 1)
61
+ end
62
+
63
+ def jd_to_bs(julian_day)
64
+ year = EPOCH_BS[0]
65
+ remaining_days = julian_day - EPOCH_JD
66
+
67
+ while remaining_days >= MONTH_DAYS[year].sum
68
+ remaining_days -= MONTH_DAYS[year].sum
69
+ year += 1
70
+ end
71
+
72
+ month = 1
73
+ MONTH_DAYS[year].each do |days_in_month|
74
+ break if remaining_days < days_in_month
75
+
76
+ remaining_days -= days_in_month
77
+ month += 1
78
+ end
79
+
80
+ [year, month, remaining_days + 1]
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnglishNepaliDateConverter
4
+ class Error < StandardError; end
5
+
6
+ # Date out of range error
7
+ class DateOutOfRangeError < Error
8
+ def initialize(message = "Date out of range")
9
+ super(message)
10
+ end
11
+ end
12
+
13
+ # BS Date out of range error
14
+ class BSDateOutOfRangeError < Error
15
+ def initialize(message = "BS Date out of range")
16
+ super(message)
17
+ end
18
+ end
19
+ end
@@ -1,20 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "date"
4
- require_relative "../bs_date"
4
+ require_relative "../converter"
5
5
 
6
6
  # Extension to Ruby's Date class to add Bikram Sambat conversion methods
7
7
  class Date
8
- EPOCH_AD_START = Date.new(1943, 4, 14)
9
- EPOCH_AD_END = Date.new(2034, 4, 13)
10
- private_constant :EPOCH_AD_START, :EPOCH_AD_END
11
-
12
8
  def to_bs
13
- if self < EPOCH_AD_START || self > EPOCH_AD_END
14
- raise DateOutOfRangeError,
15
- "Date #{self} is out of supported AD Range (1943-04-14 - 2034-04-13)"
16
- end
17
-
18
- BSDate.from_ad(self)
9
+ EnglishNepaliDateConverter::Converter.to_bs(self)
19
10
  end
20
11
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EnglishNepaliDateConverter
4
- VERSION = "1.0.0"
4
+ VERSION = "2.0.1"
5
5
  end
@@ -4,29 +4,13 @@ $LOAD_PATH << File.dirname(__FILE__)
4
4
 
5
5
  require_relative "englishnepalidateconverter/version"
6
6
  require "date"
7
+ require_relative "englishnepalidateconverter/errors"
7
8
  require_relative "englishnepalidateconverter/bs_date"
8
9
  require_relative "englishnepalidateconverter/bs_calendar"
10
+ require_relative "englishnepalidateconverter/converter"
9
11
  require_relative "englishnepalidateconverter/extensions/date"
10
12
  require_relative "englishnepalidateconverter/extensions/integer"
11
13
 
12
- module EnglishNepaliDateConverter
13
- class Error < StandardError; end
14
-
15
- # Date out of range error
16
- class DateOutOfRangeError < Error
17
- def initialize(message = "Date out of range")
18
- super(message)
19
- end
20
- end
21
-
22
- # BS Date out of range error
23
- class BSDateOutOfRangeError < Error
24
- def initialize(message = "BS Date out of range")
25
- super(message)
26
- end
27
- end
28
- end
29
-
30
14
  BSDate = EnglishNepaliDateConverter::BSDate
31
15
  DateOutOfRangeError = EnglishNepaliDateConverter::DateOutOfRangeError
32
16
  BSDateOutOfRangeError = EnglishNepaliDateConverter::BSDateOutOfRangeError
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe EnglishNepaliDateConverter::Converter do
6
+ def bs_parts(bs_date)
7
+ [bs_date.year, bs_date.month, bs_date.day]
8
+ end
9
+
10
+ describe ".to_bs" do
11
+ it "converts an AD date to a BS date" do
12
+ result = described_class.to_bs(Date.new(2025, 9, 3))
13
+
14
+ expect(result).to be_a(EnglishNepaliDateConverter::BSDate)
15
+ expect(bs_parts(result)).to eq([2082, 5, 18])
16
+ end
17
+
18
+ it "converts the first supported AD date" do
19
+ expect(bs_parts(described_class.to_bs(Date.new(1943, 4, 14)))).to eq([2000, 1, 1])
20
+ end
21
+
22
+ it "converts the last supported AD date" do
23
+ expect(bs_parts(described_class.to_bs(Date.new(2034, 4, 13)))).to eq([2090, 12, 30])
24
+ end
25
+
26
+ it "converts a DateTime late on the last supported day" do
27
+ expect(bs_parts(described_class.to_bs(DateTime.new(2034, 4, 13, 23)))).to eq([2090, 12, 30])
28
+ end
29
+
30
+ it "raises DateOutOfRangeError before the supported range" do
31
+ expect { described_class.to_bs(Date.new(1943, 4, 13)) }
32
+ .to raise_error(EnglishNepaliDateConverter::DateOutOfRangeError)
33
+ end
34
+
35
+ it "raises DateOutOfRangeError after the supported range" do
36
+ expect { described_class.to_bs(Date.new(2034, 4, 14)) }
37
+ .to raise_error(EnglishNepaliDateConverter::DateOutOfRangeError)
38
+ end
39
+ end
40
+
41
+ describe ".to_ad" do
42
+ it "converts a BSDate to an AD date" do
43
+ result = described_class.to_ad(EnglishNepaliDateConverter::BSDate.new(2082, 5, 18))
44
+
45
+ expect(result).to eq(Date.new(2025, 9, 3))
46
+ end
47
+
48
+ it "converts year, month and day arguments to an AD date" do
49
+ expect(described_class.to_ad(2070, 10, 3)).to eq(Date.new(2014, 1, 17))
50
+ end
51
+
52
+ it "converts the first supported BS date" do
53
+ expect(described_class.to_ad(2000, 1, 1)).to eq(Date.new(1943, 4, 14))
54
+ end
55
+
56
+ it "converts the last supported BS date" do
57
+ expect(described_class.to_ad(2090, 12, 30)).to eq(Date.new(2034, 4, 13))
58
+ end
59
+
60
+ it "raises BSDateOutOfRangeError for a year outside the table" do
61
+ expect { described_class.to_ad(1999, 12, 30) }
62
+ .to raise_error(EnglishNepaliDateConverter::BSDateOutOfRangeError)
63
+ end
64
+
65
+ it "raises BSDateOutOfRangeError for an invalid month" do
66
+ expect { described_class.to_ad(2080, 13, 1) }
67
+ .to raise_error(EnglishNepaliDateConverter::BSDateOutOfRangeError)
68
+ end
69
+
70
+ it "raises BSDateOutOfRangeError for a day that does not exist in the month" do
71
+ expect { described_class.to_ad(2090, 12, 31) }
72
+ .to raise_error(EnglishNepaliDateConverter::BSDateOutOfRangeError)
73
+ end
74
+ end
75
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  RSpec.describe EnglishNepaliDateConverter do
4
4
  it "should be a valid version" do
5
- expect(EnglishNepaliDateConverter::VERSION).to eq("1.0.0")
5
+ expect(EnglishNepaliDateConverter::VERSION).to match(/\A\d+\.\d+\.\d+\z/)
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: englishnepalidateconverter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dlamichhane
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-09-01 00:00:00.000000000 Z
10
+ date: 2026-12-06 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bundler
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '2.7'
18
+ version: '4.0'
19
19
  type: :development
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '2.7'
25
+ version: '4.0'
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: pry-byebug
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -57,56 +57,56 @@ dependencies:
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '3.0'
60
+ version: '3.13'
61
61
  type: :development
62
62
  prerelease: false
63
63
  version_requirements: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '3.0'
67
+ version: '3.13'
68
68
  - !ruby/object:Gem::Dependency
69
69
  name: rubocop
70
70
  requirement: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '1.8'
74
+ version: '1.93'
75
75
  type: :development
76
76
  prerelease: false
77
77
  version_requirements: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: '1.8'
81
+ version: '1.93'
82
82
  - !ruby/object:Gem::Dependency
83
83
  name: rubocop-rake
84
84
  requirement: !ruby/object:Gem::Requirement
85
85
  requirements:
86
- - - ">="
86
+ - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: '0'
88
+ version: '0.7'
89
89
  type: :development
90
90
  prerelease: false
91
91
  version_requirements: !ruby/object:Gem::Requirement
92
92
  requirements:
93
- - - ">="
93
+ - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: '0'
95
+ version: '0.7'
96
96
  - !ruby/object:Gem::Dependency
97
97
  name: rubocop-rspec
98
98
  requirement: !ruby/object:Gem::Requirement
99
99
  requirements:
100
- - - ">="
100
+ - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: '0'
102
+ version: '3.11'
103
103
  type: :development
104
104
  prerelease: false
105
105
  version_requirements: !ruby/object:Gem::Requirement
106
106
  requirements:
107
- - - ">="
107
+ - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: '3.11'
110
110
  description: Conversion of Gregorian calendar to Nepali Calendar
111
111
  email:
112
112
  - lamichhanedeepak@gmail.com
@@ -114,9 +114,11 @@ executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
+ - ".github/workflows/ci.yml"
117
118
  - ".gitignore"
118
119
  - ".rspec"
119
120
  - ".rubocop.yml"
121
+ - ".ruby-version"
120
122
  - Gemfile
121
123
  - LICENSE.txt
122
124
  - README.md
@@ -125,10 +127,13 @@ files:
125
127
  - lib/englishnepalidateconverter.rb
126
128
  - lib/englishnepalidateconverter/bs_calendar.rb
127
129
  - lib/englishnepalidateconverter/bs_date.rb
130
+ - lib/englishnepalidateconverter/converter.rb
131
+ - lib/englishnepalidateconverter/errors.rb
128
132
  - lib/englishnepalidateconverter/extensions/date.rb
129
133
  - lib/englishnepalidateconverter/extensions/integer.rb
130
134
  - lib/englishnepalidateconverter/version.rb
131
135
  - spec/englishnepalidateconverter/bs_date_spec.rb
136
+ - spec/englishnepalidateconverter/converter_spec.rb
132
137
  - spec/englishnepalidateconverter/version_spec.rb
133
138
  - spec/spec_helper.rb
134
139
  homepage: https://github.com/dlamichhane/englishnepalidateconverter
@@ -142,17 +147,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
142
147
  requirements:
143
148
  - - ">="
144
149
  - !ruby/object:Gem::Version
145
- version: '3.4'
150
+ version: '4.0'
146
151
  required_rubygems_version: !ruby/object:Gem::Requirement
147
152
  requirements:
148
153
  - - ">="
149
154
  - !ruby/object:Gem::Version
150
155
  version: '0'
151
156
  requirements: []
152
- rubygems_version: 3.6.9
157
+ rubygems_version: 4.0.20
153
158
  specification_version: 4
154
159
  summary: Converts the English calendar date to Nepali calendar date
155
160
  test_files:
156
161
  - spec/englishnepalidateconverter/bs_date_spec.rb
162
+ - spec/englishnepalidateconverter/converter_spec.rb
157
163
  - spec/englishnepalidateconverter/version_spec.rb
158
164
  - spec/spec_helper.rb