PersianCalender
A Ruby gem that provides Persian (Jalali) calendar functionality for Ruby and Rails applications. This gem allows you to convert between Gregorian and Persian dates, format Persian dates, and use Persian dates in Rails applications.
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add persian_calenderOr add it to your Gemfile manually:
gem 'persian_calender'If bundler is not being used to manage dependencies, install the gem by executing:
gem install persian_calenderUsage
Basic Usage
# Create a new Persian date
persian_date = PersianCalender.new(1402, 12, 15)
# Convert from Gregorian to Persian
persian_date = PersianCalender.from_gregorian(2024, 3, 5)
# Convert from Persian to Gregorian
gregorian_date = persian_date.to_gregorian
# => { year: 2024, month: 3, day: 5 }
# Format a Persian date
persian_date.to_s # => "1402-12-15"
persian_date.to_s(format: :short) # => "1402/12/15"
persian_date.to_s(format: :long) # => "15 اسفند 1402"
persian_date.to_s(format: :long_en) # => "15 Esfand 1402"
# Get month name
persian_date.month_name # => "اسفند"
persian_date.month_name(english: true) # => "Esfand"
# Add days to a Persian date
new_date = persian_date.add_days(10)Rails Integration
In Models
class Event < ApplicationRecord
# Assuming you have a 'date' column in your database
persian_date :persian_date
end
# Usage:
event = Event.new
event.persian_date = "1402/12/15" # Sets the date field with the Gregorian equivalent
event.persian_date # Returns a PersianCalender objectIn Views
<%= persian_date(@event.date) %>
<%= persian_date(@event.date, format: :long) %>
<%= form_for @event do |f| %>
<%= persian_date_select f, :persian_date %>
<% end %>Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/hadivarp/persian-calender. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the PersianCalender project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.