parsi-date 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +30 -1
- data/lib/parsi-date.rb +56 -7
- data/lib/parsi-datetime.rb +10 -0
- data/lib/version.rb +1 -1
- data/spec/parsi-date/constants_spec.rb +22 -5
- data/spec/parsi-date/strftime_spec.rb +21 -9
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -2,5 +2,34 @@
|
|
2
2
|
|
3
3
|
This is an implementation of {Solar Hijri Calendar}[http://en.wikipedia.org/wiki/Solar_Hijri_calendar]
|
4
4
|
(some times referred to as Jalali or Persian Calendar) which is Iran's official calendar.
|
5
|
-
Main aim of this gem is to create a Solar Hijri date library as close as possible to built-in date library.
|
5
|
+
Main aim of this gem is to create a Solar Hijri date library as close as possible to the built-in date library.
|
6
6
|
Conversion algorithm originally adopted from {here}[http://www.fourmilab.ch/documents/calendar/].
|
7
|
+
|
8
|
+
== Usage
|
9
|
+
|
10
|
+
You can use +Parsi::Date+ and +Parsi::DateTime+ objects as +Date+ and +DateTime+ objects
|
11
|
+
For example:
|
12
|
+
|
13
|
+
```
|
14
|
+
a = Parsi::Date.today # => #<Parsi::Date: 1391-08-09 (4912461/2,0/1)>
|
15
|
+
b = a >> 12 # => #<Parsi::Date: 1392-08-09 (4913193/2,0/1)>
|
16
|
+
a.upto(b).select{ |d| d.sunday? }.count # => 52
|
17
|
+
|
18
|
+
Parsi::Date.leap? 1391 # => true
|
19
|
+
|
20
|
+
c = Parsi::Date.parse "1391/10/12" # => #<Parsi::Date: 1391-10-12 (4912587/2,0)>
|
21
|
+
c.strftime "%A %d %B %Y" # => "سهشنده 12 دی 1391"
|
22
|
+
c.strftime "%^EA %d %^EB %Y" # => "Seshambe 12 Day 1391"
|
23
|
+
```
|
24
|
+
|
25
|
+
For converting a +Date+ or +DateTime+ object just call +to_parsi+ (aliased to +jalali+ and +to_jalali+) on it.
|
26
|
+
To convert back use +to_gregorian+.
|
27
|
+
|
28
|
+
```
|
29
|
+
d = Date.civil(2012, 10, 30).to_parsi # => #<Parsi::Date: 1391-08-09 (4912461/2,0/1)>
|
30
|
+
d.to_gregorian # => #<Date: 2012-10-30 ((2456231j,0s,0n),+0s,2299161j)>
|
31
|
+
```
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
Copyright (c) 2012 Hassan Zamani, released under the MIT license.
|
data/lib/parsi-date.rb
CHANGED
@@ -93,19 +93,31 @@ module Parsi
|
|
93
93
|
# month's numerical representation indexed into this array
|
94
94
|
# gives the name of that month (hence the first element is nil).
|
95
95
|
MONTHNAMES = [nil] + %w(فروردین اردیبهشت خرداد تیر مرداد شهریور مهر آبان آذر دی بهمن اسفند)
|
96
|
+
# Full month names, in English. Months count from 1 to 12;
|
97
|
+
EN_MONTHNAMES = [nil] + %w(farvardin ordibehesht khordad tir mordad sharivar mehr aban azar day bahman esfand)
|
96
98
|
|
97
99
|
# Full names of days of the week, in Farsi. Days of the week
|
98
|
-
# count from 0 to 6
|
99
|
-
#
|
100
|
+
# count from 0 to 6; a day's numerical representation indexed into this array gives
|
101
|
+
# the name of that day.
|
100
102
|
DAYNAMES = %w(یکشنده دوشنده سهشنده چهارشنده چنجشنده جمعه شنده)
|
101
103
|
|
104
|
+
# Full names of days of the week, in English. Days of the week
|
105
|
+
# count from 0 to 6; a day's numerical representation indexed into this array gives
|
106
|
+
# the name of that day.
|
107
|
+
EN_DAYNAMES = %w(yekshambe doshambe seshambe chaharshambe panjshanbe jomee shanbe)
|
108
|
+
|
102
109
|
# Abbreviated month names, in English.
|
103
|
-
|
110
|
+
#
|
111
|
+
# We don't have Farsi abbreviated month names, as they are not useful
|
112
|
+
ABBR_MONTHNAMES = [nil] + %w(far ord kho tir mor sha meh abn azr dey bah esf)
|
104
113
|
|
105
114
|
# Abbreviated day names, in Farsi.
|
106
115
|
ABBR_DAYNAMES = %w(۱ش ۲ش ۳ش ۴ش ۵ش ج ش)
|
107
116
|
|
108
|
-
|
117
|
+
# Abbreviated day names, in English.
|
118
|
+
ABBR_EN_DAYNAMES = %w(ye do se ch pj jo sh)
|
119
|
+
|
120
|
+
[MONTHNAMES, EN_MONTHNAMES, ABBR_MONTHNAMES, DAYNAMES, EN_DAYNAMES, ABBR_DAYNAMES, ABBR_EN_DAYNAMES].each do |xs|
|
109
121
|
xs.each{|x| x.freeze unless x.nil?}.freeze
|
110
122
|
end
|
111
123
|
|
@@ -457,6 +469,10 @@ module Parsi
|
|
457
469
|
define_method(n.downcase + '?'){ wday == i }
|
458
470
|
end
|
459
471
|
|
472
|
+
EN_DAYNAMES.each_with_index do |n, i|
|
473
|
+
define_method(n.downcase + '?'){ wday == i }
|
474
|
+
end
|
475
|
+
|
460
476
|
# Return a new Date object that is +n+ days later than the current one.
|
461
477
|
#
|
462
478
|
# +n+ may be a negative value, in which case the new Date is earlier
|
@@ -590,6 +606,27 @@ module Parsi
|
|
590
606
|
DateTime.new! jd_to_ajd(jd, 0, 0), 0
|
591
607
|
end
|
592
608
|
|
609
|
+
def to_parsi
|
610
|
+
self
|
611
|
+
end
|
612
|
+
alias :jalali :to_parsi
|
613
|
+
alias :to_jalali :to_parsi
|
614
|
+
alias :to_persian :to_parsi
|
615
|
+
|
616
|
+
# Formats time according to the directives in the given format string.
|
617
|
+
# The directives begins with a percent (%) character. Any text not listed as a
|
618
|
+
# directive will be passed through to the output string.
|
619
|
+
#
|
620
|
+
# The directive consists of a percent (%) character, zero or more flags,
|
621
|
+
# optional minimum field width, optional modifier and a conversion specifier as
|
622
|
+
# follows.
|
623
|
+
#
|
624
|
+
# %<flags><width><modifier><conversion>
|
625
|
+
#
|
626
|
+
# +flags+ and +conversion+ are as in +Time+ exept that the +E+ flag is not egnored
|
627
|
+
# any more, it forse useing English names
|
628
|
+
#
|
629
|
+
#
|
593
630
|
def strftime format='%Y/%m/%d'
|
594
631
|
format.
|
595
632
|
gsub('%%', 'PERCENT_SUBSTITUTION_MARKER').
|
@@ -598,24 +635,34 @@ module Parsi
|
|
598
635
|
gsub('%x', '%D').
|
599
636
|
gsub('%D', '%y/%m/%d').
|
600
637
|
gsub('%F', '%Y-%m-%d').
|
601
|
-
gsub('%v', '%e-%
|
638
|
+
gsub('%v', '%e-%B-%Y').
|
602
639
|
gsub('%Y', year.to_s).
|
603
640
|
gsub('%C', (year / 100).to_s).
|
604
641
|
gsub('%y', (year % 100).to_s).
|
605
642
|
gsub('%m', '%02d' % month).
|
606
|
-
gsub('%_m', '%
|
643
|
+
gsub('%_m', '%2d' % month).
|
607
644
|
gsub('%-m', month.to_s).
|
608
645
|
gsub('%^B', '%B').
|
609
646
|
gsub('%B', MONTHNAMES[month]).
|
647
|
+
gsub('%E^B', '%^EB').
|
648
|
+
gsub('%^EB', EN_MONTHNAMES[month].capitalize).
|
649
|
+
gsub('%EB', EN_MONTHNAMES[month]).
|
610
650
|
gsub('%h', '%b').
|
651
|
+
gsub('%^h', '%^b').
|
611
652
|
gsub('%b', ABBR_MONTHNAMES[month]).
|
612
653
|
gsub('%^b', ABBR_MONTHNAMES[month].capitalize).
|
613
654
|
gsub('%d', '%02d' % day).
|
614
|
-
gsub('%e', '%
|
655
|
+
gsub('%e', '%2d' % day).
|
615
656
|
gsub('%-d', day.to_s).
|
616
657
|
gsub('%j', '%03d' % yday.to_s).
|
617
658
|
gsub('%A', DAYNAMES[wday]).
|
618
659
|
gsub('%a', ABBR_DAYNAMES[wday]).
|
660
|
+
gsub('%EA', EN_DAYNAMES[wday]).
|
661
|
+
gsub('%Ea', ABBR_EN_DAYNAMES[wday]).
|
662
|
+
gsub('%E^A', '%^EA').
|
663
|
+
gsub('%^EA', EN_DAYNAMES[wday].capitalize).
|
664
|
+
gsub('%E^a', '%^Ea').
|
665
|
+
gsub('%^Ea', ABBR_EN_DAYNAMES[wday].capitalize).
|
619
666
|
gsub('%w', wday.to_s).
|
620
667
|
gsub('%n', "\n").
|
621
668
|
gsub('%t', "\t").
|
@@ -693,3 +740,5 @@ class Date
|
|
693
740
|
alias :to_jalali :to_parsi
|
694
741
|
alias :to_persian :to_parsi
|
695
742
|
end
|
743
|
+
|
744
|
+
require 'parsi-datetime'
|
data/lib/parsi-datetime.rb
CHANGED
@@ -200,3 +200,13 @@ class DateTime
|
|
200
200
|
alias :to_jalali :to_parsi
|
201
201
|
alias :to_persian :to_parsi
|
202
202
|
end
|
203
|
+
|
204
|
+
class Time
|
205
|
+
# Returns a Parsi::DateTime object representing same date in Jalali calendar
|
206
|
+
def to_parsi
|
207
|
+
to_datetime.to_parsi
|
208
|
+
end
|
209
|
+
alias :jalali :to_parsi
|
210
|
+
alias :to_jalali :to_parsi
|
211
|
+
alias :to_persian :to_parsi
|
212
|
+
end
|
data/lib/version.rb
CHANGED
@@ -3,23 +3,40 @@ require File.expand_path('../../spec_helper', __FILE__)
|
|
3
3
|
|
4
4
|
describe "Date constants" do
|
5
5
|
it "defines MONTHNAMES" do
|
6
|
-
Parsi::Date::MONTHNAMES.should == [nil] +
|
6
|
+
Parsi::Date::MONTHNAMES.should == [nil] +
|
7
|
+
%w(فروردین اردیبهشت خرداد تیر مرداد شهریور مهر آبان آذر دی بهمن اسفند)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "defines EN_MONTHNAMES" do
|
11
|
+
Parsi::Date::EN_MONTHNAMES.should == [nil] +
|
12
|
+
%w(farvardin ordibehesht khordad tir mordad sharivar mehr aban azar day bahman esfand)
|
7
13
|
end
|
8
14
|
|
9
15
|
it "defines ABBR_MONTHNAMES" do
|
10
|
-
Parsi::Date::ABBR_MONTHNAMES.should == [nil] +
|
16
|
+
Parsi::Date::ABBR_MONTHNAMES.should == [nil] +
|
17
|
+
%w(far ord kho tir mor sha meh abn azr dey bah esf)
|
11
18
|
end
|
12
19
|
|
13
20
|
it "defines DAYNAMES" do
|
14
|
-
Parsi::Date::DAYNAMES.should == %w(یکشنده دوشنده سهشنده چهارشنده چنجشنده جمعه شنده
|
21
|
+
Parsi::Date::DAYNAMES.should == %w(یکشنده دوشنده سهشنده چهارشنده چنجشنده جمعه شنده)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "defines EN_DAYNAMES" do
|
25
|
+
Parsi::Date::EN_DAYNAMES.should == %w(yekshambe doshambe seshambe chaharshambe panjshanbe jomee shanbe)
|
15
26
|
end
|
16
27
|
|
17
28
|
it "defines ABBR_DAYNAMES" do
|
18
29
|
Parsi::Date::ABBR_DAYNAMES.should == %w(۱ش ۲ش ۳ش ۴ش ۵ش ج ش)
|
19
30
|
end
|
20
31
|
|
21
|
-
it "
|
22
|
-
|
32
|
+
it "defines ABBR_EN_DAYNAMES" do
|
33
|
+
Parsi::Date::ABBR_EN_DAYNAMES.should == %w(ye do se ch pj jo sh)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "freezes MONTHNAMES, DAYNAMES, EN_DAYNAMES, ABBR_MONTHNAMES, ABBR_DAYSNAMES" do
|
37
|
+
[Parsi::Date::MONTHNAMES, Parsi::Date::EN_MONTHNAMES, Parsi::Date::ABBR_MONTHNAMES,
|
38
|
+
Parsi::Date::DAYNAMES, Parsi::Date::EN_DAYNAMES,
|
39
|
+
Parsi::Date::ABBR_DAYNAMES, Parsi::Date::ABBR_EN_DAYNAMES].each do |ary|
|
23
40
|
ary.should be_frozen
|
24
41
|
end
|
25
42
|
end
|
@@ -17,11 +17,15 @@ describe "Parsi::Date#strftime" do
|
|
17
17
|
|
18
18
|
it "should be able to print the full month name" do
|
19
19
|
Parsi::Date.civil(1390, 4, 6).strftime("%B").should == "تیر"
|
20
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%EB").should == "tir"
|
21
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%^EB").should == "Tir"
|
20
22
|
end
|
21
23
|
|
22
24
|
it "should be able to print the short month name" do
|
23
|
-
Parsi::Date.civil(1390, 4, 6).strftime("%b").should == "
|
24
|
-
Parsi::Date.civil(1390, 4, 6).strftime("%h").should == "
|
25
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%b").should == "tir"
|
26
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%h").should == "tir"
|
27
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%^b").should == "Tir"
|
28
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%^h").should == "Tir"
|
25
29
|
Parsi::Date.civil(1390, 4, 6).strftime("%b").should == Parsi::Date.civil(1390, 4, 6).strftime("%h")
|
26
30
|
end
|
27
31
|
|
@@ -30,15 +34,23 @@ describe "Parsi::Date#strftime" do
|
|
30
34
|
end
|
31
35
|
|
32
36
|
it "should be able to print the month day with leading zeroes" do
|
33
|
-
Parsi::Date.civil(1390, 4,
|
37
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%d").should == "06"
|
38
|
+
Parsi::Date.civil(1390, 4, 16).strftime("%d").should == "16"
|
34
39
|
end
|
35
40
|
|
36
|
-
it "should be able to print the month day with leading spaces" do
|
37
|
-
Parsi::Date.civil(1390, 4,
|
41
|
+
it "should be able to print the month day with leading spaces and without em" do
|
42
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%-d").should == "6"
|
43
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%e").should == " 6"
|
44
|
+
Parsi::Date.civil(1390, 4, 16).strftime("%e").should == "16"
|
38
45
|
end
|
39
46
|
|
40
|
-
it "should be able to print the month with leading zeroes" do
|
41
|
-
Parsi::Date.civil(1390,
|
47
|
+
it "should be able to print the month with leading zeroes, spaces and none" do
|
48
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%m").should == "04"
|
49
|
+
Parsi::Date.civil(1390, 11, 6).strftime("%m").should == "11"
|
50
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%_m").should == " 4"
|
51
|
+
Parsi::Date.civil(1390, 11, 6).strftime("%_m").should == "11"
|
52
|
+
Parsi::Date.civil(1390, 4, 6).strftime("%-m").should == "4"
|
53
|
+
Parsi::Date.civil(1390, 11, 6).strftime("%-m").should == "11"
|
42
54
|
end
|
43
55
|
|
44
56
|
it "should be able to add a newline" do
|
@@ -86,8 +98,8 @@ describe "Parsi::Date#strftime" do
|
|
86
98
|
end
|
87
99
|
|
88
100
|
it "should be able to show the commercial week" do
|
89
|
-
Parsi::Date.civil(1390, 4, 9).strftime("%v").should == " 9
|
90
|
-
Parsi::Date.civil(1390, 4, 9).strftime("%v").should == Parsi::Date.civil(1390, 4, 9).strftime('%e-%
|
101
|
+
Parsi::Date.civil(1390, 4, 9).strftime("%v").should == " 9-تیر-1390"
|
102
|
+
Parsi::Date.civil(1390, 4, 9).strftime("%v").should == Parsi::Date.civil(1390, 4, 9).strftime('%e-%B-%Y')
|
91
103
|
end
|
92
104
|
|
93
105
|
it "should be able to show YY/MM/DD" do
|