diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 3527577..2041eb7 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -1,10 +1,6 @@ name: Ruby -on: - push: - branches: [master] - pull_request: - branches: [master] +on: [push, pull_request] permissions: contents: read @@ -14,10 +10,13 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ["2.6", "2.7", "3.0", "3.1"] + ruby-version: + - '3.1' + - '3.2' + - '3.3' steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ec7d2d --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# Parsi Date + +The Parsi Date library is an implementation of the [Solar Hijri Calendar](http://en.wikipedia.org/wiki/Solar_Hijri_calendar), also known as the Jalali or Persian Calendar, which is the official calendar of Iran. This library aims to create a Solar Hijri date library that closely resembles Ruby's built-in date library, providing a seamless experience for users who need to work with the Solar Hijri dates. The conversion algorithm used in this library is adopted from [FarsiWeb](http://www.farsiweb.info/jalali/jalali.c). + +## Features + +- **Parsi::Date** and **Parsi::DateTime**: These objects can be used just like Ruby's `Date` and `DateTime` objects. +- **Date Conversion:** Easily convert between Gregorian and Solar Hijri dates. +- **Leap Year Calculation:** Determine if a given Solar Hijri year is a leap year. +- **Date Parsing and Formatting:** Parse Solar Hijri date strings and format dates in various styles. + +## Usage + +### Basic Operations + +You can use `Parsi::Date` and `Parsi::DateTime` objects in the same way you use `Date` and `DateTime` objects in Ruby. Here are some examples: + +```ruby +# Get today's Solar Hijri date +a = Parsi::Date.today +# => # + +# Add 12 months to it +b = a >> 12 +# => # + +# Count the number of Sundays between two dates +a.upto(b).select{ |d| d.sunday? }.count +# => 52 + +# Check if a given year is a leap year +Parsi::Date.leap?(1391) +# => true +``` + +### Parsing and Formatting Dates + +You can parse Solar Hijri date strings and format them in various styles: + +```ruby +# Parse a Solar Hijri date string +c = Parsi::Date.parse("1391/10/12") +# => # + +# Format the date in Persian +c.strftime("%A %d %B %Y") +# => "سه‌شنبه 12 دی 1391" + +# Format the date in English +c.strftime("%^EA %d %^EB %Y") +# => "Seshambe 12 Day 1391" +``` + +### Converting Between Calendars + +You can easily convert between Gregorian and Solar Hijri dates: + +```ruby +# Convert a Gregorian date to Solar Hijri +d = Date.civil(2012, 10, 30).to_parsi +# => # + +# Convert a Solar Hijri date back to Gregorian +d.to_gregorian +# => # +``` + +## Installation + +Add this line to your application's Gemfile: + +```ruby +gem 'parsi_date' +``` + +And then execute: + +```sh +bundle install +``` + +Or install it yourself as: + +```sh +gem install parsi_date +``` + +## License + +The Parsi Date library is open-source software licensed under the MIT license. + +## Contributing + +If you would like to contribute to the development of the Parsi Date library, please feel free to fork the repository and submit pull requests. Contributions, bug reports, and feature requests are welcome and appreciated. diff --git a/README.rdoc b/README.rdoc deleted file mode 100644 index 54463de..0000000 --- a/README.rdoc +++ /dev/null @@ -1,31 +0,0 @@ -= Parsi Date - -This is an implementation of {Solar Hijri Calendar}[http://en.wikipedia.org/wiki/Solar_Hijri_calendar] -(some times referred to as Jalali or Persian Calendar) which is Iran's official calendar. -Main aim of this gem is to create a Solar Hijri date library as close as possible to the built-in date library. -Conversion algorithm originally adopted from {here}[http://www.fourmilab.ch/documents/calendar/]. - -== Usage - -You can use Parsi::Date<\tt> and Parsi::DateTime<\tt> objects as +Date+ and +DateTime+ objects -For example: - - a = Parsi::Date.today # => # - b = a >> 12 # => # - a.upto(b).select{ |d| d.sunday? }.count # => 52 - - Parsi::Date.leap? 1391 # => true - - c = Parsi::Date.parse "1391/10/12" # => # - c.strftime "%A %d %B %Y" # => "سه‌شنبه 12 دی 1391" - c.strftime "%^EA %d %^EB %Y" # => "Seshambe 12 Day 1391" - -For converting a +Date+ or +DateTime+ object just call +to_parsi+ (aliased to +jalali+ and +to_jalali+) on it. -To convert back use +to_gregorian+. - - d = Date.civil(2012, 10, 30).to_parsi # => # - d.to_gregorian # => # - - - -Copyright (c) 2012 Hassan Zamani, released under the MIT license. diff --git a/lib/parsi-date.rb b/lib/parsi-date.rb index 44c4e4a..8ef4c2c 100644 --- a/lib/parsi-date.rb +++ b/lib/parsi-date.rb @@ -135,11 +135,11 @@ class Date JALALI_EPOCH_IN_AJD = Rational(3896641, 2) # :nodoc: MJD_EPOCH_IN_AJD = Rational(4800001, 2) # 1858-11-17 # :nodoc: UNIX_EPOCH_IN_AJD = Rational(4881175, 2) # 1970-01-01 # :nodoc: - JALALI_EPOCH_IN_CJD = 1948321 # :nodoc: + JALALI_EPOCH_IN_CJD = 1948320 # :nodoc: MJD_EPOCH_IN_CJD = 2400001 # :nodoc: UNIX_EPOCH_IN_CJD = 2440588 # :nodoc: LD_EPOCH_IN_CJD = 2299160 # :nodoc: - DAYS_IN_MONTH = [nil, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29] # :nodoc: + DAYS_IN_MONTH = [0, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29] # :nodoc: LEAP_REMINDERS = [1, 5, 9, 13, 17, 22, 26, 30].freeze @@ -153,38 +153,43 @@ def leap? year private - DAYS_TO_FIRST_OF_MONTH = [nil, 0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336] # :nodoc: - # Convert a Civil Date to a Julian Day Number and returns the corresponding Julian Day Number. - def civil_to_jd year, month, day # :nodoc: - epbase = year - 474 - epyear = 474 + (epbase % 2820) - - day + DAYS_TO_FIRST_OF_MONTH[month] + - (epyear * 682 - 110) / 2816 + - (epyear - 1) * 365 + - (epbase / 2820 * 1029983) + - (JALALI_EPOCH_IN_CJD - 1) + def civil_to_jd(year, month, day) # :nodoc: + year -= 1 + month -= 1 + day -= 1 + + jd = 365*year + (year/33)*8 + (year%33+3)/4; + while month > 0 + jd += DAYS_IN_MONTH[month] + month -= 1 + end + + jd + day + JALALI_EPOCH_IN_CJD end # Convert a Julian Day Number to a Civil Date. +jday+ is the Julian Day Number. # # Returns the corresponding [year, month, day_of_month] as a three-element array. - def jd_to_civil jday - depoch = (jday - first_day_of_year(475)) - cycle, cyear = depoch.divmod 1029983 + def jd_to_civil jd + jd -= JALALI_EPOCH_IN_CJD - if cyear == 1029982 - ycycle = 2820 - else - aux1, aux2 = cyear.divmod 366 - ycycle = (2134 * aux1 + 2816 * aux2 + 2815) / 1028522 + aux1 + 1 + n, jd = jd.divmod 12053 + m, jd = jd.divmod 1461 + year = 33*n + 4*m + 1 + + if jd >= 366 + n, jd = (jd - 1).divmod 365 + year += n + end + + month = 1 + while month < 12 && jd >= DAYS_IN_MONTH[month] + jd -= DAYS_IN_MONTH[month] + month += 1 end - year = ycycle + 2820 * cycle + 474 - yday = jday - first_day_of_year(year) + 1 - month = ((yday <= 186) ? yday / 31.0 : (yday - 6) / 30.0).ceil - day = (jday - first_day_of_month(year, month) + 1) - [year, month, day] + + [year, month, jd + 1] end # Do +year+, +month+, and day-of-month +day+ make a valid Civil Date? @@ -332,7 +337,6 @@ def valid_civil? year, month, day # Parsi::Date.jd # => # # def jd jday=0 - jd = _valid_jd? jday new! jd_to_ajd(jday, 0, 0), 0 end @@ -733,7 +737,7 @@ def to_s() format('%.4d-%02d-%02d', year, mon, mday) end def marshal_dump() [@ajd, @offset] end # Load from Marshal format. - def marshal_load(a) @ajd, @of, = a end + def marshal_load(a) @ajd, @offset, = a end end end diff --git a/parsi-date.gemspec b/parsi-date.gemspec index 02d1099..8b011a7 100644 --- a/parsi-date.gemspec +++ b/parsi-date.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |s| s.require_paths = ['lib'] s.add_development_dependency("bundler", "~> 2.0") + s.add_development_dependency("json", "~> 2.0") s.add_development_dependency("rake", "~> 13.0") s.add_development_dependency("rspec", "~> 3.0") s.add_development_dependency("activerecord", "~> 6.0") diff --git a/spec/parsi-date/accessor_spec.rb b/spec/parsi-date/accessor_spec.rb index 20e514c..8916c02 100644 --- a/spec/parsi-date/accessor_spec.rb +++ b/spec/parsi-date/accessor_spec.rb @@ -1,57 +1,66 @@ describe "Parsi::Date#ajd" do it "determines the Astronomical Julian day" do - expect Parsi::Date.civil(1391, 8, 6).ajd == Rational(4912455, 2) + expect(Parsi::Date.civil(1391, 8, 6).ajd).to be == Rational(4912455, 2) + expect(Parsi::Date.civil(1403, 12, 30).ajd).to be == Rational(4921509, 2) end end describe "Parsi::Date#amjd" do it "determines the Astronomical Modified Julian day" do - expect Parsi::Date.civil(1391, 8, 6).amjd == 56227 + expect(Parsi::Date.civil(1391, 8, 6).amjd).to be == 56227 + expect(Parsi::Date.civil(1403, 12, 30).amjd).to be == 60754 end end describe "Parsi::Date#mjd" do it "determines the Modified Julian day" do - expect Parsi::Date.civil(1391, 8, 6).mjd == 56227 + expect(Parsi::Date.civil(1391, 8, 6).mjd).to be == 56227 + expect(Parsi::Date.civil(1403, 12, 30).mjd).to be == 60754 end end describe "Parsi::Date#ld" do - it "determines the Modified Julian day" do - expect Parsi::Date.civil(1391, 8, 6).ld == 157068 + it "determines the number of days since the Day of Calendar Reform" do + expect(Parsi::Date.civil(1391, 8, 6).ld).to be == 157068 + expect(Parsi::Date.civil(1403, 12, 30).ld).to be == 161595 end end describe "Parsi::Date#year" do it "determines the year" do - expect Parsi::Date.civil(1391, 8, 6).year == 1391 + expect(Parsi::Date.civil(1391, 8, 6).year).to be == 1391 + expect(Parsi::Date.civil(1403, 12, 30).year).to be == 1403 end end describe "Parsi::Date#yday" do it "determines the year" do - expect Parsi::Date.civil(1391, 1, 17).yday == 17 - expect Parsi::Date.civil(1391, 8, 6).yday == 222 + expect(Parsi::Date.civil(1391, 1, 17).yday).to be == 17 + expect(Parsi::Date.civil(1391, 8, 6).yday).to be == 222 + expect(Parsi::Date.civil(1403, 12, 30).yday).to be == 366 end end describe "Parsi::Date#mon" do it "determines the month" do - expect Parsi::Date.civil(1391, 1, 17).mon == 1 - expect Parsi::Date.civil(1391, 8, 6).mon == 8 + expect(Parsi::Date.civil(1391, 1, 17).mon).to be == 1 + expect(Parsi::Date.civil(1391, 8, 6).mon).to be == 8 + expect(Parsi::Date.civil(1403, 12, 30).mon).to be == 12 end end describe "Parsi::Date#mday" do it "determines the day of the month" do - expect Parsi::Date.civil(1391, 1, 17).mday == 17 - expect Parsi::Date.civil(1391, 10, 28).mday == 28 + expect(Parsi::Date.civil(1391, 1, 17).mday).to be == 17 + expect(Parsi::Date.civil(1391, 10, 28).mday).to be == 28 + expect(Parsi::Date.civil(1403, 12, 30).mday).to be == 30 end end describe "Parsi::Date#wday" do it "determines the week day" do - expect Parsi::Date.civil(1391, 1, 17).wday == 4 - expect Parsi::Date.civil(1391, 8, 6).wday == 6 + expect(Parsi::Date.civil(1391, 1, 17).wday).to be == 4 + expect(Parsi::Date.civil(1391, 8, 6).wday).to be == 6 + expect(Parsi::Date.civil(1403, 12, 30).wday).to be == 4 end end diff --git a/spec/parsi-date/accessors_helper_spec.rb b/spec/parsi-date/accessors_helper_spec.rb index 1d5b268..7a8f3ae 100644 --- a/spec/parsi-date/accessors_helper_spec.rb +++ b/spec/parsi-date/accessors_helper_spec.rb @@ -1,5 +1,6 @@ require 'active_record' require 'sqlite3' +require 'json' ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") diff --git a/spec/parsi-date/add_month_spec.rb b/spec/parsi-date/add_month_spec.rb index 7ac9f21..a5b4bb1 100644 --- a/spec/parsi-date/add_month_spec.rb +++ b/spec/parsi-date/add_month_spec.rb @@ -1,10 +1,12 @@ describe "Parsi::Date#>>" do it "adds the number of months to a Parsi::Date" do - expect (Parsi::Date.civil(1391, 2, 27) >> 10) == Parsi::Date.civil(1391, 12, 27) + expect(Parsi::Date.civil(1391, 2, 27) >> 10).to be == Parsi::Date.civil(1391, 12, 27) + expect(Parsi::Date.civil(1403, 11, 30) >> 1).to be == Parsi::Date.civil(1403, 12, 30) end it "sets the day to the last day of a month if the day doesn't exist" do - expect (Parsi::Date.civil(1391, 6, 31) >> 1) == Parsi::Date.civil(1391, 7, 30) + expect(Parsi::Date.civil(1391, 6, 31) >> 1).to be == Parsi::Date.civil(1391, 7, 30) + expect(Parsi::Date.civil(1402, 11, 30) >> 1).to be == Parsi::Date.civil(1402, 12, 29) end it "raise a TypeError when passed a Symbol" do diff --git a/spec/parsi-date/add_spec.rb b/spec/parsi-date/add_spec.rb index 6b2a504..672eae5 100644 --- a/spec/parsi-date/add_spec.rb +++ b/spec/parsi-date/add_spec.rb @@ -1,10 +1,12 @@ describe "Parsi::Date#+" do it "adds the number of days to a Parsi::Date" do - expect Parsi::Date.civil(1391, 2, 27) + 10 == Parsi::Date.civil(1391, 3, 6) + expect(Parsi::Date.civil(1391, 2, 27) + 10).to be == Parsi::Date.civil(1391, 3, 6) + expect(Parsi::Date.civil(1403, 12, 29) + 1).to be == Parsi::Date.civil(1403, 12, 30) + expect(Parsi::Date.civil(1403, 12, 30) + 1).to be == Parsi::Date.civil(1404, 1, 1) end it "adds a negative number of days to a Parsi::Date" do - expect Parsi::Date.civil(1391, 2, 27) + (-10) == Parsi::Date.civil(1391, 2, 17) + expect(Parsi::Date.civil(1391, 2, 27) + (-10)).to be == Parsi::Date.civil(1391, 2, 17) end it "raises a TypeError when passed a Symbol" do diff --git a/spec/parsi-date/comp_spec.rb b/spec/parsi-date/comp_spec.rb index 0baf471..fa23714 100644 --- a/spec/parsi-date/comp_spec.rb +++ b/spec/parsi-date/comp_spec.rb @@ -2,29 +2,29 @@ describe "Parsi::Date#<=>" do it "returns 0 when two dates are equal" do - expect (Parsi::Date.civil(1391, 4, 6) <=> Parsi::Date.civil(1391, 4, 6)) == 0 - expect (Parsi::Date.civil(1391, 4, 6) <=> Date.civil(2012, 6, 26)) == 0 + expect(Parsi::Date.civil(1391, 4, 6) <=> Parsi::Date.civil(1391, 4, 6)).to be == 0 + expect(Parsi::Date.civil(1391, 4, 6) <=> Date.civil(2012, 6, 26)).to be == 0 end it "returns -1 when self is less than another date" do - expect (Parsi::Date.civil(1391, 4, 5) <=> Parsi::Date.civil(1391, 4, 6)) == -1 - expect (Parsi::Date.civil(1391, 4, 5) <=> Date.civil(2012, 6, 26)) == -1 + expect(Parsi::Date.civil(1391, 4, 5) <=> Parsi::Date.civil(1391, 4, 6)).to be == -1 + expect(Parsi::Date.civil(1391, 4, 5) <=> Date.civil(2012, 6, 26)).to be == -1 end it "returns 1 when self is greater than another date" do - expect (Parsi::Date.civil(1392, 4, 7) <=> Parsi::Date.civil(1391, 4, 6)) == 1 - expect (Parsi::Date.civil(1391, 4, 7) <=> Date.civil(2012, 6, 26)) == 1 + expect(Parsi::Date.civil(1392, 4, 7) <=> Parsi::Date.civil(1391, 4, 6)).to be == 1 + expect(Parsi::Date.civil(1391, 4, 7) <=> Date.civil(2012, 6, 26)).to be == 1 end it "returns 0 when self is equal to a Numeric" do - expect (Parsi::Date.civil(1391, 4, 6) <=> Rational(4912209,2)) == 0 + expect(Parsi::Date.civil(1391, 4, 6) <=> Rational(4912209,2)).to be == 0 end it "returns -1 when self is less than a Numeric" do - expect (Parsi::Date.civil(1391, 4, 6) <=> Rational(4912210,2)) == -1 + expect(Parsi::Date.civil(1391, 4, 6) <=> Rational(4912210,2)).to be == -1 end it "returns 1 when self is greater than a Numeric" do - expect (Parsi::Date.civil(1391, 4, 6) <=> Rational(4912208,2)) == 1 + expect(Parsi::Date.civil(1391, 4, 6) <=> Rational(4912208,2)).to be == 1 end end diff --git a/spec/parsi-date/constants_spec.rb b/spec/parsi-date/constants_spec.rb index 03019e5..c06d35b 100644 --- a/spec/parsi-date/constants_spec.rb +++ b/spec/parsi-date/constants_spec.rb @@ -1,34 +1,38 @@ # encoding: utf-8 describe "Date constants" do it "defines MONTHNAMES" do - expect Parsi::Date::MONTHNAMES == [nil] + - %w(فروردین اردیبهشت خرداد تیر مرداد شهریور مهر آبان آذر دی بهمن اسفند) + expect(Parsi::Date::MONTHNAMES).to be == + [nil] + %w(فروردین اردیبهشت خرداد تیر مرداد شهریور مهر آبان آذر دی بهمن اسفند) end it "defines EN_MONTHNAMES" do - expect Parsi::Date::EN_MONTHNAMES == [nil] + - %w(farvardin ordibehesht khordad tir mordad shahrivar mehr aban azar day bahman esfand) + expect(Parsi::Date::EN_MONTHNAMES).to be == + [nil] + %w(farvardin ordibehesht khordad tir mordad shahrivar mehr aban azar day bahman esfand) end it "defines ABBR_MONTHNAMES" do - expect Parsi::Date::ABBR_MONTHNAMES == [nil] + - %w(far ord kho tir mor sha meh abn azr day bah esf) + expect(Parsi::Date::ABBR_MONTHNAMES).to be == + [nil] + %w(far ord kho tir mor sha meh abn azr day bah esf) end it "defines DAYNAMES" do - expect Parsi::Date::DAYNAMES == %w(یک‌شنبه دوشنبه سه‌شنبه چهارشنبه پنج‌شنبه جمعه شنبه) + expect(Parsi::Date::DAYNAMES).to be == + %w(یک‌شنبه دوشنبه سه‌شنبه چهارشنبه پنج‌شنبه جمعه شنبه) end it "defines EN_DAYNAMES" do - expect Parsi::Date::EN_DAYNAMES == %w(yekshanbe doshanbe seshanbe chaharshanbe panjshanbe jomee shanbe) + expect(Parsi::Date::EN_DAYNAMES).to be == + %w(yekshanbe doshanbe seshanbe chaharshanbe panjshanbe jomee shanbe) end it "defines ABBR_DAYNAMES" do - expect Parsi::Date::ABBR_DAYNAMES == %w(۱ش ۲ش ۳ش ۴ش ۵ش ج ش) + expect(Parsi::Date::ABBR_DAYNAMES).to be == + %w(۱ش ۲ش ۳ش ۴ش ۵ش ج ش) end it "defines ABBR_EN_DAYNAMES" do - expect Parsi::Date::ABBR_EN_DAYNAMES == %w(ye do se ch pj jo sh) + expect(Parsi::Date::ABBR_EN_DAYNAMES).to be == + %w(ye do se ch pj jo sh) end it "freezes MONTHNAMES, DAYNAMES, EN_DAYNAMES, ABBR_MONTHNAMES, ABBR_DAYSNAMES" do diff --git a/spec/parsi-date/construction_spec.rb b/spec/parsi-date/construction_spec.rb index 7074366..5da9d41 100644 --- a/spec/parsi-date/construction_spec.rb +++ b/spec/parsi-date/construction_spec.rb @@ -21,17 +21,18 @@ it "constructs a Date for 1/1/1 by default" do date = Parsi::Date.civil - expect date.year == 1 - expect date.month == 1 - expect date.day == 1 + expect(date.year ).to be == 1 + expect(date.month).to be == 1 + expect(date.day ).to be == 1 end end context "ordinal" do it "constructs a Date object from an ordinal date" do - expect Parsi::Date.ordinal(1390) == Parsi::Date.civil(1390, 1, 1) - expect Parsi::Date.ordinal(1390,7) == Parsi::Date.civil(1390, 1, 7) - expect Parsi::Date.ordinal(1390,100) == Parsi::Date.civil(1390, 4, 7) + expect(Parsi::Date.ordinal(1390) ).to be == Parsi::Date.civil(1390, 1, 1) + expect(Parsi::Date.ordinal(1390, 7) ).to be == Parsi::Date.civil(1390, 1, 7) + expect(Parsi::Date.ordinal(1390, 100)).to be == Parsi::Date.civil(1390, 4, 7) + expect(Parsi::Date.ordinal(1403, 366)).to be == Parsi::Date.civil(1403, 12, 30) end end @@ -39,20 +40,20 @@ it "parses date from strings" do ['1391/8/6', '1391-8-6', '1391 8 6', '1391 8 6', '13910806'].each do |date_string| date = Parsi::Date.parse date_string - expect [date.year, date.month, date.day] == [1391, 8, 6] + expect([date.year, date.month, date.day]).to be == [1391, 8, 6] end end it "completes century when second arg is true" do allow(Date).to receive(:today) { Date.new 2012, 10, 26 } date = Parsi::Date.parse '91/8/5', true - expect [date.year, date.month, date.day] == [1391, 8, 5] + expect([date.year, date.month, date.day]).to be == [1391, 8, 5] end it "raises ArgumentError on invalid date string" do - expect { date = Parsi::Date.parse '1390/12/30' }.to raise_error(ArgumentError) + expect { date = Parsi::Date.parse '1390/12/30' }.to raise_error(ArgumentError) expect { date = Parsi::Date.parse 'bad date string' }.to raise_error(ArgumentError) - expect { date = Parsi::Date.parse '12-30-1390' }.to raise_error(ArgumentError) + expect { date = Parsi::Date.parse '12-30-1390' }.to raise_error(ArgumentError) end end end diff --git a/spec/parsi-date/conversion_spec.rb b/spec/parsi-date/conversion_spec.rb index 66f78f1..cc5fcc2 100644 --- a/spec/parsi-date/conversion_spec.rb +++ b/spec/parsi-date/conversion_spec.rb @@ -1,21 +1,26 @@ describe "Parsi::Date.jd" do it "constructs a date form given Chronological Julian day number" do - expect Parsi::Date.jd(2456228) == Parsi::Date.civil(1391, 8, 6) - expect Parsi::Date.jd(2456229) == Parsi::Date.civil(1391, 8, 7) + expect(Parsi::Date.jd(2456228)).to be == Parsi::Date.civil(1391, 8, 6) + expect(Parsi::Date.jd(2456229)).to be == Parsi::Date.civil(1391, 8, 7) + expect(Parsi::Date.jd(2456229)).to be == Parsi::Date.civil(1391, 8, 7) + expect(Parsi::Date.jd(2460755)).to be == Parsi::Date.civil(1403, 12, 30) + expect(Parsi::Date.jd(2460756)).to be == Parsi::Date.civil(1404, 1, 1) end it "returns a Date object representing Julian day 0 if no arguments passed"do - expect Parsi::Date.jd == Parsi::Date.civil(-5334, 9, 1) + expect(Parsi::Date.jd).to be == Parsi::Date.civil(-5334, 9, 3) end it "constructs a Date object if passed a negative number" do - expect Parsi::Date.jd(-1) == Parsi::Date.civil(-5334, 8, 30) + expect(Parsi::Date.jd(-1)).to be == Parsi::Date.civil(-5334, 9, 2) end end describe "Parsi::Date#jd" do it "determines the Julian day for a Date object" do - expect Parsi::Date.civil(1391, 8, 7).jd == 2456229 + expect(Parsi::Date.civil(1391, 8, 7).jd ).to be == 2456229 + expect(Parsi::Date.civil(1403, 12, 30).jd).to be == 2460755 + expect(Parsi::Date.civil(1404, 1, 1).jd ).to be == 2460756 end end @@ -23,7 +28,10 @@ it "converts date to Gregorian date" do date = Parsi::Date.civil(1391, 8, 7).to_gregorian expect(date).to be_a(Date) - expect date == Date.civil(2012, 10, 28) + expect(date).to be == Date.civil(2012, 10, 28) + + expect(Parsi::Date.civil(1366, 11, 14).to_gregorian).to be == Date.civil(1988, 2, 3) + expect(Parsi::Date.civil(1403, 12, 30).to_gregorian).to be == Date.civil(2025, 3, 20) end end @@ -31,6 +39,9 @@ it "converts date to Parsi date" do date = Date.civil(2012, 10, 28).to_parsi expect(date).to be_a(Parsi::Date) - expect date == Parsi::Date.civil(1391, 8, 7) + expect(date).to be == Parsi::Date.civil(1391, 8, 7) + + expect(Date.civil(1988, 2, 3).to_parsi ).to be == Parsi::Date.civil(1366, 11, 14) + expect(Date.civil(2025, 3, 20).to_parsi).to be == Parsi::Date.civil(1403, 12, 30) end end diff --git a/spec/parsi-date/leap_spec.rb b/spec/parsi-date/leap_spec.rb index 38dea5b..c1cc001 100644 --- a/spec/parsi-date/leap_spec.rb +++ b/spec/parsi-date/leap_spec.rb @@ -1,15 +1,15 @@ describe "Parsi::Date#leap?" do it "returns true if a year is a leap year in the Parsi (Jalali) calendar" do - expect Parsi::Date.leap?(1387) == true - expect Parsi::Date.leap?(1391) == true - expect Parsi::Date.leap?(1395) == true - expect Parsi::Date.leap?(1403) == true + expect(Parsi::Date.leap?(1387)).to be true + expect(Parsi::Date.leap?(1391)).to be true + expect(Parsi::Date.leap?(1395)).to be true + expect(Parsi::Date.leap?(1403)).to be true end it "returns false if a year is not a leap year in the Parsi (Jalali) calendar" do - expect Parsi::Date.leap?(1390) == false - expect Parsi::Date.leap?(1392) == false - expect Parsi::Date.leap?(1400) == false - expect Parsi::Date.leap?(1404) == false + expect(Parsi::Date.leap?(1390)).to be false + expect(Parsi::Date.leap?(1392)).to be false + expect(Parsi::Date.leap?(1400)).to be false + expect(Parsi::Date.leap?(1404)).to be false end end diff --git a/spec/parsi-date/step_spec.rb b/spec/parsi-date/step_spec.rb index 61656af..a10ae81 100644 --- a/spec/parsi-date/step_spec.rb +++ b/spec/parsi-date/step_spec.rb @@ -5,23 +5,23 @@ de = Parsi::Date.civil(1391, 9, 29) count = 0 de.step(ds) do |d| - expect d <= ds - expect d >= de + expect(d).to be <= ds + expect(d).to be >= de count += 1 end - expect count == 13 + expect(count).to be == 13 count = 0 de.step(ds, 5) do |d| - expect d <= ds - expect d >= de + expect(d).to be <= ds + expect(d).to be >= de count += 1 end - expect count == 3 + expect(count).to be == 3 count = 0 ds.step(de) do |d|; count += 1; end - expect count == 0 + expect(count).to be == 0 end it "steps backward in time" do @@ -29,22 +29,22 @@ de = Parsi::Date.civil(1390, 3, 29) count = 0 ds.step(de, -1) do |d| - expect d <= ds - expect d >= de + expect(d).to be <= ds + expect(d).to be >= de count += 1 end - expect count == 17 + expect(count).to be == 17 count = 0 ds.step(de, -5) do |d| - expect d <= ds - expect d >= de + expect(d).to be <= ds + expect(d).to be >= de count += 1 end - expect count == 4 + expect(count).to be == 4 count = 0 de.step(ds, -1) do |d|; count += 1; end - expect count == 0 + expect(count).to be == 0 end end diff --git a/spec/parsi-date/strftime_spec.rb b/spec/parsi-date/strftime_spec.rb index 649eef8..dc21100 100644 --- a/spec/parsi-date/strftime_spec.rb +++ b/spec/parsi-date/strftime_spec.rb @@ -2,78 +2,78 @@ describe "Parsi::Date#strftime" do it "is able to print the date" do - expect Parsi::Date.civil(1390, 4, 6).strftime == "1390/04/06" + expect(Parsi::Date.civil(1390, 4, 6).strftime).to be == "1390/04/06" end it "is able to print the full day name" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%A") == "دوشنبه" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%A")).to be == "دوشنبه" end it "is able to print the short day name" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%a") == "۲ش" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%a")).to be == "۲ش" end it "is able to print the full month name" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%B") == "تیر" - expect Parsi::Date.civil(1390, 4, 6).strftime("%EB") == "tir" - expect Parsi::Date.civil(1390, 4, 6).strftime("%^EB") == "Tir" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%B") ).to be == "تیر" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%EB") ).to be == "tir" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%^EB")).to be == "Tir" end it "is able to print the short month name" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%b") == "tir" - expect Parsi::Date.civil(1390, 4, 6).strftime("%h") == "tir" - expect Parsi::Date.civil(1390, 4, 6).strftime("%^b") == "Tir" - expect Parsi::Date.civil(1390, 4, 6).strftime("%^h") == "Tir" - expect Parsi::Date.civil(1390, 4, 6).strftime("%b") == Parsi::Date.civil(1390, 4, 6).strftime("%h") + expect(Parsi::Date.civil(1390, 4, 6).strftime("%b") ).to be == "tir" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%h") ).to be == "tir" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%^b")).to be == "Tir" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%^h")).to be == "Tir" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%b") ).to be == Parsi::Date.civil(1390, 4, 6).strftime("%h") end it "is able to print the century" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%C") == "13" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%C")).to be == "13" end it "is able to print the month day with leading zeroes" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%d") == "06" - expect Parsi::Date.civil(1390, 4, 16).strftime("%d") == "16" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%d")).to be == "06" + expect(Parsi::Date.civil(1390, 4, 16).strftime("%d")).to be == "16" end it "is able to print the month day with leading spaces and without em" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%-d") == "6" - expect Parsi::Date.civil(1390, 4, 6).strftime("%e") == " 6" - expect Parsi::Date.civil(1390, 4, 16).strftime("%e") == "16" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%-d")).to be == "6" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%e") ).to be == " 6" + expect(Parsi::Date.civil(1390, 4, 16).strftime("%e") ).to be == "16" end it "is able to print the month with leading zeroes, spaces and none" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%m") == "04" - expect Parsi::Date.civil(1390, 11, 6).strftime("%m") == "11" - expect Parsi::Date.civil(1390, 4, 6).strftime("%_m") == " 4" - expect Parsi::Date.civil(1390, 11, 6).strftime("%_m") == "11" - expect Parsi::Date.civil(1390, 4, 6).strftime("%-m") == "4" - expect Parsi::Date.civil(1390, 11, 6).strftime("%-m") == "11" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%m") ).to be == "04" + expect(Parsi::Date.civil(1390, 11, 6).strftime("%m") ).to be == "11" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%_m")).to be == " 4" + expect(Parsi::Date.civil(1390, 11, 6).strftime("%_m")).to be == "11" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%-m")).to be == "4" + expect(Parsi::Date.civil(1390, 11, 6).strftime("%-m")).to be == "11" end it "is able to add a newline" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%n") == "\n" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%n")).to be == "\n" end it "is able to add a tab" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%t") == "\t" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%t")).to be == "\t" end it "is able to show the week day" do - expect Parsi::Date.civil(1390, 4, 11).strftime("%w") == "6" - expect Parsi::Date.civil(1390, 4, 12).strftime("%w") == "0" + expect(Parsi::Date.civil(1390, 4, 11).strftime("%w")).to be == "6" + expect(Parsi::Date.civil(1390, 4, 12).strftime("%w")).to be == "0" end it "is able to show the year in YYYY format" do - expect Parsi::Date.civil(1390, 4, 9).strftime("%Y") == "1390" + expect(Parsi::Date.civil(1390, 4, 9).strftime("%Y")).to be == "1390" end it "is able to show the year in YY format" do - expect Parsi::Date.civil(1390, 4, 9).strftime("%y") == "90" + expect(Parsi::Date.civil(1390, 4, 9).strftime("%y")).to be == "90" end it "is able to escape the % character" do - expect Parsi::Date.civil(1390, 4, 9).strftime("%%") == "%" + expect(Parsi::Date.civil(1390, 4, 9).strftime("%%")).to be == "%" end ############################ @@ -81,28 +81,28 @@ ############################ it "is able to print the date in full" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%c") == "۲ش 6 تیر 1390" - expect Parsi::Date.civil(1390, 4, 6).strftime("%c") == Parsi::Date.civil(1390, 4, 6).strftime('%a %-d %B %Y') + expect(Parsi::Date.civil(1390, 4, 6).strftime("%c")).to be == "۲ش 6 تیر 1390" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%c")).to be == Parsi::Date.civil(1390, 4, 6).strftime('%a %-d %B %Y') end it "is able to print the date with slashes" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%D") == "90/04/06" - expect Parsi::Date.civil(1390, 4, 6).strftime("%D") == Parsi::Date.civil(1390, 4, 6).strftime('%y/%m/%d') + expect(Parsi::Date.civil(1390, 4, 6).strftime("%D")).to be == "90/04/06" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%D")).to be == Parsi::Date.civil(1390, 4, 6).strftime('%y/%m/%d') end it "is able to print the date as YYYY-MM-DD" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%F") == "1390-04-06" - expect Parsi::Date.civil(1390, 4, 6).strftime("%F") == Parsi::Date.civil(1390, 4, 6).strftime('%Y-%m-%d') + expect(Parsi::Date.civil(1390, 4, 6).strftime("%F")).to be == "1390-04-06" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%F")).to be == Parsi::Date.civil(1390, 4, 6).strftime('%Y-%m-%d') end it "is able to show the commercial week" do - expect Parsi::Date.civil(1390, 4, 9).strftime("%v") == " 9-تیر-1390" - expect Parsi::Date.civil(1390, 4, 9).strftime("%v") == Parsi::Date.civil(1390, 4, 9).strftime('%e-%B-%Y') + expect(Parsi::Date.civil(1390, 4, 9).strftime("%v")).to be == " 9-تیر-1390" + expect(Parsi::Date.civil(1390, 4, 9).strftime("%v")).to be == Parsi::Date.civil(1390, 4, 9).strftime('%e-%B-%Y') end it "is able to show YY/MM/DD" do - expect Parsi::Date.civil(1390, 4, 6).strftime("%x") == "90/04/06" - expect Parsi::Date.civil(1390, 4, 6).strftime("%x") == Parsi::Date.civil(1390, 4, 6).strftime('%y/%m/%d') + expect(Parsi::Date.civil(1390, 4, 6).strftime("%x")).to be == "90/04/06" + expect(Parsi::Date.civil(1390, 4, 6).strftime("%x")).to be == Parsi::Date.civil(1390, 4, 6).strftime('%y/%m/%d') end end diff --git a/spec/parsi-date/week_methods_spec.rb b/spec/parsi-date/week_methods_spec.rb index d5dd1bd..4ac56c5 100644 --- a/spec/parsi-date/week_methods_spec.rb +++ b/spec/parsi-date/week_methods_spec.rb @@ -1,17 +1,17 @@ describe "Parsi::Date#cwday?" do it "returns the day of calendar week (1-7, Monday is 1)" do - expect Parsi::Date.civil(1393, 12, 3).cwday == 7 - expect Parsi::Date.civil(1394, 1, 3).cwday == 1 + expect(Parsi::Date.civil(1393, 12, 3).cwday).to be == 7 + expect(Parsi::Date.civil(1394, 1, 3).cwday).to be == 1 end end describe "Parsi::Date#cweek?" do it "returns the calendar week number (1-53)" do - expect Parsi::Date.civil(1393, 11, 30).cweek == 48 - expect Parsi::Date.civil(1393, 12, 1).cweek == 48 - expect Parsi::Date.civil(1393, 12, 29).cweek == 52 - expect Parsi::Date.civil(1394, 1, 1).cweek == 1 - expect Parsi::Date.civil(1394, 1, 7).cweek == 1 - expect Parsi::Date.civil(1394, 1, 8).cweek == 2 + expect(Parsi::Date.civil(1393, 11, 30).cweek).to be == 48 + expect(Parsi::Date.civil(1393, 12, 1).cweek ).to be == 48 + expect(Parsi::Date.civil(1393, 12, 29).cweek).to be == 52 + expect(Parsi::Date.civil(1394, 1, 1).cweek ).to be == 1 + expect(Parsi::Date.civil(1394, 1, 7).cweek ).to be == 1 + expect(Parsi::Date.civil(1394, 1, 8).cweek ).to be == 2 end end