jalalidate 0.2.2 → 0.3.0
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.
- data/Gemfile.lock +25 -0
- data/README.md +7 -0
- data/Rakefile +1 -1
- data/TODO +6 -2
- data/bin/jcal +6 -0
- data/bin/jdate +6 -0
- data/jalalidate.gemspec +3 -0
- data/lib/jalalidate.rb +104 -69
- data/lib/jalalidate/cli.rb +36 -0
- data/lib/jalalidate/version.rb +1 -1
- data/spec/jalalidate_spec.rb +31 -12
- data/spec/spec_helper.rb +4 -3
- metadata +46 -10
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
jalalidate (0.2.3)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
rspec (2.1.0)
|
11
|
+
rspec-core (~> 2.1.0)
|
12
|
+
rspec-expectations (~> 2.1.0)
|
13
|
+
rspec-mocks (~> 2.1.0)
|
14
|
+
rspec-core (2.1.0)
|
15
|
+
rspec-expectations (2.1.0)
|
16
|
+
diff-lcs (~> 1.1.2)
|
17
|
+
rspec-mocks (2.1.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
bundler (~> 1.0.0)
|
24
|
+
jalalidate!
|
25
|
+
rspec (~> 2.1.0)
|
data/README.md
CHANGED
@@ -5,6 +5,13 @@
|
|
5
5
|
|
6
6
|
## History
|
7
7
|
|
8
|
+
#### 0.3 - 6.JAN.2011
|
9
|
+
* JalaiDate could be initialized with Time and DateTime objects
|
10
|
+
* more options for strftime method %H,%M,%S,%X,%Z,%I,%p. read docs for more information
|
11
|
+
* added jdate and jcal binaries to access jcal from the command-line
|
12
|
+
* updated some documentations
|
13
|
+
* now using bundler
|
14
|
+
|
8
15
|
#### 0.2 - 25.FEB.2010
|
9
16
|
* renamed the gem from JalaliDate to jalalidate
|
10
17
|
* added spec and a full test suite
|
data/Rakefile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'bundler'
|
2
|
-
Bundler::GemHelper.install_tasks
|
2
|
+
Bundler::GemHelper.install_tasks
|
data/TODO
CHANGED
@@ -1,3 +1,7 @@
|
|
1
1
|
=TODO
|
2
|
-
*
|
3
|
-
*
|
2
|
+
* add week number of the year %W and %U
|
3
|
+
* better jcal and jdate with param
|
4
|
+
* support afghan month names
|
5
|
+
* update read me / talk about the binaries
|
6
|
+
* ActiveSupport::CoreExtensions::Date
|
7
|
+
* ActionView::Helpers::DateHelper
|
data/bin/jcal
ADDED
data/bin/jdate
ADDED
data/jalalidate.gemspec
CHANGED
@@ -13,10 +13,13 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.description = %q{A library for working with Jalali Calendar (a.k.a Persian Calendar)}
|
14
14
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
15
15
|
s.rubyforge_project = "jalalidate"
|
16
|
+
s.executables = ["jdate"]
|
16
17
|
s.files = `git ls-files`.split("\n")
|
17
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
20
|
s.require_paths = ["lib"]
|
20
21
|
s.extra_rdoc_files = [ "LICENSE", "README.md"]
|
21
22
|
s.rdoc_options = ["--charset=UTF-8"]
|
23
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
|
24
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
22
25
|
end
|
data/lib/jalalidate.rb
CHANGED
@@ -9,40 +9,53 @@ class JalaliDate
|
|
9
9
|
GDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
10
10
|
JDaysInMonth = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29]
|
11
11
|
PERSIAN_MONTH_NAMES = [nil, "فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"]
|
12
|
+
PERSIAN_MONTH_NAMES_PINGLISH = [nil, "Farvardin", "Ordibehesht", "Khordad", "Tir", "Mordad", "Shahrivar", "Mehr", "Aban", "Azar", "Dey", "Bahman", "Esfand"]
|
12
13
|
PERSIAN_WEEKDAY_NAMES = ["یکشنبه","دوشنبه","سهشنبه","چهارشنبه","پنجشنبه","جمعه","شنبه"]
|
13
|
-
PERSIAN_ABBR_WEEKDAY_NAMES = ["۱ش","۲ش","۳ش","۴ش","۵ش","ج","ش"]
|
14
|
+
PERSIAN_ABBR_WEEKDAY_NAMES = ["۱ش ","۲ش","۳ش","۴ش","۵ش","ج ","ش"]
|
14
15
|
PERSIAN_MERIDIAN_INDICATOR = ["قبل از ظهر","بعد از ظهر"]
|
15
16
|
PERSIAN_ABBR_MERIDIAN_INDICATOR = ["ق.ظ","ب.ظ"]
|
16
17
|
#:startdoc:
|
17
18
|
|
18
19
|
include Comparable
|
19
20
|
|
20
|
-
attr_accessor :year,:month,:day
|
21
|
+
attr_accessor :year,:month,:day, :hour, :min, :sec
|
21
22
|
attr_reader :g_year, :g_month, :g_day
|
22
23
|
|
23
24
|
# Can be initialized in two ways:
|
24
25
|
# - First by feeding 3 arguments for Jalali Date, year,month and day.
|
25
26
|
# - The Second way to initializes is to pass a normal Ruby Date object, it'll be converted to jalali automatically.
|
26
|
-
#
|
27
|
+
#
|
27
28
|
# Example:
|
28
29
|
# jdate = JalaliDate.new(Date.today)
|
29
30
|
# other_jdate = JalaliDate.new(1388,9,17)
|
30
31
|
def initialize *args
|
31
|
-
if (args.size == 1) && (args.first.is_a?
|
32
|
-
year,month,day = gregorian_to_jalali(args.first.year, args.first.month, args.first.day)
|
33
|
-
|
34
|
-
year,month,day = args
|
32
|
+
if (args.size == 1) && (args.first.is_a?(Date))
|
33
|
+
year,month,day = gregorian_to_jalali(args.first.year, args.first.month, args.first.day)
|
34
|
+
elsif (args.size == 1) && (args.first.is_a?(Time) || args.first.is_a?(DateTime))
|
35
|
+
year,month,day = gregorian_to_jalali(args.first.year, args.first.month, args.first.day)
|
36
|
+
@hour = args.first.hour || 0
|
37
|
+
@min = args.first.min || 0
|
38
|
+
@sec = args.first.sec || 0
|
39
|
+
@zone = args.first.zone || "UTC"
|
40
|
+
@utc_offset = args.first.utc_offset || 0
|
41
|
+
else
|
42
|
+
year,month,day,hour,min,sec,zone,utc_offset = args
|
35
43
|
end
|
36
|
-
|
37
|
-
raise ArgumentError, "invalid arguments or invalid Jalali date" unless self.class.valid?(year,month,day)
|
38
|
-
@year
|
44
|
+
|
45
|
+
raise ArgumentError, "invalid arguments or invalid Jalali date" unless self.class.valid?(year,month,day)
|
46
|
+
@year = year
|
39
47
|
@month = month
|
40
|
-
@day
|
48
|
+
@day = day
|
49
|
+
@hour ||= hour || 0
|
50
|
+
@min ||= min || 0
|
51
|
+
@sec ||= sec || 0
|
52
|
+
@zone ||= zone || "UTC"
|
53
|
+
@utc_offset ||= utc_offset || 0
|
41
54
|
@g_year, @g_month, @g_day = jalali_to_gregorian(year,month,day)
|
42
55
|
end
|
43
56
|
|
44
57
|
# Class Methods -----------------------------------------------------------
|
45
|
-
class << self
|
58
|
+
class << self
|
46
59
|
# Return a JalaliDate object representing today's date in calendar
|
47
60
|
def today
|
48
61
|
JalaliDate.new(Date.today)
|
@@ -54,40 +67,40 @@ class JalaliDate
|
|
54
67
|
end
|
55
68
|
|
56
69
|
# Return a JalaliDate object representing tomorrow's date in calendar
|
57
|
-
def tomorrow
|
70
|
+
def tomorrow
|
58
71
|
JalaliDate.new(Date.today + 1)
|
59
72
|
end
|
60
73
|
|
61
|
-
# Accpets a four digit number as the jalaliyear and returns true if that particular year
|
74
|
+
# Accpets a four digit number as the jalaliyear and returns true if that particular year
|
62
75
|
# is a leap year in jalali calendar otherwise it returns false.
|
63
|
-
def leap?(y)
|
64
|
-
[6,22,17,13,9,5,1,30].include?(y%33) ? true : false
|
76
|
+
def leap?(y)
|
77
|
+
[6,22,17,13,9,5,1,30].include?(y%33) ? true : false
|
65
78
|
end
|
66
79
|
|
67
|
-
# Accpets three numbers for year (4 digit), month and day in jalali calendar and checks if it's a
|
80
|
+
# Accpets three numbers for year (4 digit), month and day in jalali calendar and checks if it's a
|
68
81
|
# valid date according to jalali calendar or not.
|
69
82
|
def valid?(y,m,d)
|
70
83
|
( y.class == Fixnum && y > 0 &&
|
71
84
|
m.class == Fixnum && (1..12).include?(m) &&
|
72
85
|
d.class == Fixnum &&
|
73
|
-
(
|
86
|
+
(
|
74
87
|
((1..JDaysInMonth[m-1]).include?(d)) || (d == 30 && m == 12 && leap?(y))
|
75
|
-
)
|
88
|
+
)
|
76
89
|
) ? true : false
|
77
90
|
end
|
78
91
|
end
|
79
92
|
|
80
93
|
# Instance Methods --------------------------------------------------------
|
81
|
-
|
94
|
+
|
82
95
|
# Converts a JalaiDate object to Ruby Date object
|
83
|
-
def to_gregorian
|
96
|
+
def to_gregorian
|
84
97
|
Date.new(@g_year,@g_month,@g_day)
|
85
98
|
end
|
86
99
|
alias :to_g :to_gregorian
|
87
100
|
|
88
101
|
# Returns a string represtation of the JalaliDate object in format like this: y/m/d
|
89
|
-
def to_s
|
90
|
-
[@year,@month,@day].join("/")
|
102
|
+
def to_s
|
103
|
+
[@year,@month,@day].join("/")
|
91
104
|
end
|
92
105
|
|
93
106
|
# Returns a hash in a format like this: {:year => @year, :month => @month, :day => @day}
|
@@ -100,35 +113,35 @@ class JalaliDate
|
|
100
113
|
[@year,@month,@day]
|
101
114
|
end
|
102
115
|
|
103
|
-
# Return internal object state as a programmer-readable string.
|
116
|
+
# Return internal object state as a programmer-readable string.
|
104
117
|
def inspect
|
105
|
-
"#<#{self.class}:#{self.object_id}, :year => #{@year}, :month => #{@month}, :day => #{@day} >"
|
118
|
+
"#<#{self.class}:#{self.object_id}, :year => #{@year}, :month => #{@month}, :day => #{@day} >"
|
106
119
|
end
|
107
120
|
|
108
121
|
# Adds n days to the current JalaliDate object
|
109
122
|
def +(days)
|
110
123
|
self.class.new( to_g + days )
|
111
124
|
end
|
112
|
-
|
125
|
+
|
113
126
|
# Subtracts n days from the current JalaliDate object
|
114
127
|
def -(days)
|
115
128
|
self.class.new( to_g - days )
|
116
129
|
end
|
117
|
-
|
130
|
+
|
118
131
|
# Return the next day for the current JalaliDate object
|
119
132
|
def next(n=1)
|
120
|
-
self + n
|
133
|
+
self + n
|
121
134
|
end
|
122
135
|
alias :succ :next
|
123
|
-
|
136
|
+
|
124
137
|
# Return the previous day for the current JalaliDate object
|
125
|
-
def previous(n=1)
|
126
|
-
self - n
|
127
|
-
end
|
128
|
-
|
138
|
+
def previous(n=1)
|
139
|
+
self - n
|
140
|
+
end
|
141
|
+
|
129
142
|
# Compares two JalaliDate objects. acts like Date#<=>
|
130
143
|
def <=>(other)
|
131
|
-
to_g <=> other.to_g
|
144
|
+
to_g <=> other.to_g
|
132
145
|
end
|
133
146
|
|
134
147
|
# Move JalaliDate object forward by n months
|
@@ -139,13 +152,13 @@ class JalaliDate
|
|
139
152
|
d -= 1 until self.class.valid?(y, m, d)
|
140
153
|
self.class.new(y,m,d)
|
141
154
|
end
|
142
|
-
|
143
|
-
# Move JalaliDate object backward by n months
|
155
|
+
|
156
|
+
# Move JalaliDate object backward by n months
|
144
157
|
def <<(months)
|
145
158
|
self >> -months
|
146
159
|
end
|
147
|
-
|
148
|
-
# Step the current date forward +step+ days at a time (or backward, if step is negative) until we reach
|
160
|
+
|
161
|
+
# Step the current date forward +step+ days at a time (or backward, if step is negative) until we reach
|
149
162
|
# limit (inclusive), yielding the resultant date at each step.
|
150
163
|
#
|
151
164
|
# Example:
|
@@ -153,7 +166,7 @@ class JalaliDate
|
|
153
166
|
# jdate.step(Date.today+10, 2) do |jd|
|
154
167
|
# puts jd.to_s
|
155
168
|
# end
|
156
|
-
def step(limit, step=1)
|
169
|
+
def step(limit, step=1)
|
157
170
|
da = self
|
158
171
|
op = %w(- <= >=)[step <=> 0]
|
159
172
|
while da.__send__(op, limit)
|
@@ -162,7 +175,7 @@ class JalaliDate
|
|
162
175
|
end
|
163
176
|
self
|
164
177
|
end
|
165
|
-
|
178
|
+
|
166
179
|
# Step forward one day at a time until we reach max (inclusive), yielding each date as we go.
|
167
180
|
#
|
168
181
|
# Example:
|
@@ -175,7 +188,7 @@ class JalaliDate
|
|
175
188
|
step(max, +1, &block)
|
176
189
|
end
|
177
190
|
|
178
|
-
# Step backward one day at a time until we reach min (inclusive), yielding each date as we go.
|
191
|
+
# Step backward one day at a time until we reach min (inclusive), yielding each date as we go.
|
179
192
|
# See #upto for the example.
|
180
193
|
def downto(min, &block)
|
181
194
|
step(min, -1, &block)
|
@@ -191,30 +204,44 @@ class JalaliDate
|
|
191
204
|
to_g.wday
|
192
205
|
end
|
193
206
|
|
207
|
+
# Get the jalali week day of this date. Saturday is day-of-week 0; Friday is day-of-week 6.
|
208
|
+
def jwday
|
209
|
+
(to_g.wday + 1) % 7
|
210
|
+
end
|
211
|
+
|
194
212
|
# Get the day-of-the-year of this date.
|
195
213
|
# Farvardin 1 is day-of-the-year 1
|
196
214
|
def yday
|
197
215
|
m = (@month-2 < 0) ? 0 : @month-2
|
198
|
-
(@month==1) ? @day : @day + JDaysInMonth[0..m].inject(0) {|sum, n| sum + n }
|
199
|
-
end
|
200
|
-
|
201
|
-
# Formats time according to the directives in the given format string. Any text not listed as a directive will be
|
216
|
+
(@month==1) ? @day : @day + JDaysInMonth[0..m].inject(0) {|sum, n| sum + n }
|
217
|
+
end
|
218
|
+
|
219
|
+
# Formats time according to the directives in the given format string. Any text not listed as a directive will be
|
202
220
|
# passed through to the output string.
|
203
221
|
#
|
204
222
|
# Format meanings:
|
205
223
|
#
|
206
|
-
# [%a]
|
207
|
-
# [%A]
|
208
|
-
# [%b
|
209
|
-
# [%
|
224
|
+
# [%a] The abbreviated weekday name (۳ش)
|
225
|
+
# [%A] The full weekday name (یکشنبه)
|
226
|
+
# [%b] The month name (اردیبهشت)
|
227
|
+
# [%B] The month name in pinglish (Ordibehesht)
|
228
|
+
# [%d] Day of the month (01..31)
|
229
|
+
# [%e] Day of the month (1..31)
|
210
230
|
# [%j] Day of the year (1..366)
|
211
231
|
# [%m] Month of the year (1..12)
|
212
232
|
# [%w] Day of the week (Sunday is 0, 0..6)
|
213
233
|
# [%x] Preferred representation for the date alone, no time in format YY/M/D
|
214
234
|
# [%y] Year without a century (00..99)
|
215
235
|
# [%Y] Year with century
|
236
|
+
# [%H] Hour of the day, 24-hour clock (00..23)
|
237
|
+
# [%I] Hour of the day, 12-hour clock (01..12)
|
238
|
+
# [%M] Minute of the hour (00..59)
|
239
|
+
# [%p] Meridian indicator ("بعد از ظهر" or "قبل از ظهر")
|
240
|
+
# [%S] Second of the minute (00..60)
|
241
|
+
# [%X] Preferred representation for the time alone, no date
|
242
|
+
# [%Z] Time zone name
|
216
243
|
# [%%] Literal %'' character
|
217
|
-
#
|
244
|
+
#
|
218
245
|
# Example:
|
219
246
|
# d = JalaliDate.today
|
220
247
|
# d.strftime("Printed on %Y/%m/%d") #=> "Printed on 87/5/26
|
@@ -223,21 +250,29 @@ class JalaliDate
|
|
223
250
|
gsub(/%a/, PERSIAN_ABBR_WEEKDAY_NAMES[wday]).
|
224
251
|
gsub(/%A/, PERSIAN_WEEKDAY_NAMES[wday]).
|
225
252
|
gsub(/%b/, PERSIAN_MONTH_NAMES[@month]).
|
226
|
-
gsub(/%B/,
|
227
|
-
gsub(/%d/, @day.to_s).
|
253
|
+
gsub(/%B/, PERSIAN_MONTH_NAMES_PINGLISH[@month]).
|
254
|
+
gsub(/%d/, ("%02d" % @day).to_s).
|
255
|
+
gsub(/%e/, @day.to_s).
|
228
256
|
gsub(/%m/, @month.to_s).
|
229
257
|
gsub(/%Y/, @year.to_s).
|
230
258
|
gsub(/%y/, @year.to_s.slice(2,2)).
|
231
259
|
gsub(/%j/, yday.to_s).
|
260
|
+
gsub(/%H/, ("%02d" % @hour).to_s).
|
261
|
+
gsub(/%I/, ("%02d" % ((@hour>=12) ? @hour-12 : @hour)).to_s).
|
262
|
+
gsub(/%M/, ("%02d" % @min).to_s).
|
263
|
+
gsub(/%S/, ("%02d" % @sec).to_s).
|
264
|
+
gsub(/%p/, (@hour>=12 ? "بعد از ظهر" : "قبل از ظهر")).
|
232
265
|
gsub(/%w/, wday.to_s).
|
266
|
+
gsub(/%Z/, @zone).
|
267
|
+
gsub(/%X/, [("%02d" % @hour),("%02d" % @min),("%02d" % @sec)].join(":")).
|
233
268
|
gsub(/%x/, [@year.to_s.slice(2,2),@month,@day].join("/")).
|
234
269
|
gsub(/#{"SUBSTITUTION_MARKER"}/, '%')
|
235
|
-
end
|
270
|
+
end
|
236
271
|
alias :format :strftime
|
237
|
-
|
272
|
+
|
238
273
|
private #-------------------------------------------------------------------------
|
239
274
|
|
240
|
-
def gregorian_to_jalali(year, month, day)
|
275
|
+
def gregorian_to_jalali(year, month, day) # :nodoc:
|
241
276
|
gy = year - 1600
|
242
277
|
gm = month - 1
|
243
278
|
gd = day - 1
|
@@ -245,18 +280,18 @@ class JalaliDate
|
|
245
280
|
gm.times { |i| g_day_no += GDaysInMonth[i] }
|
246
281
|
g_day_no += 1 if gm > 1 && ((gy%4 == 0 && gy%100 != 0) || (gy%400 == 0))
|
247
282
|
g_day_no += gd
|
248
|
-
|
283
|
+
|
249
284
|
j_day_no = g_day_no-79
|
250
285
|
j_np = j_day_no/12053
|
251
286
|
j_day_no %= 12053
|
252
287
|
jy = 979 + 33 * j_np + 4*(j_day_no/1461)
|
253
288
|
j_day_no %= 1461
|
254
|
-
|
289
|
+
|
255
290
|
if (j_day_no >= 366)
|
256
291
|
jy += (j_day_no - 1)/365
|
257
292
|
j_day_no = (j_day_no - 1) % 365
|
258
293
|
end
|
259
|
-
|
294
|
+
|
260
295
|
11.times do |i|
|
261
296
|
if j_day_no >= JDaysInMonth[i]
|
262
297
|
j_day_no -= JDaysInMonth[i]
|
@@ -268,33 +303,33 @@ class JalaliDate
|
|
268
303
|
end
|
269
304
|
jm = $j + 1
|
270
305
|
jd = j_day_no + 1
|
271
|
-
|
306
|
+
|
272
307
|
[jy, jm, jd]
|
273
308
|
end
|
274
|
-
|
275
|
-
def jalali_to_gregorian(year,month,day)
|
309
|
+
|
310
|
+
def jalali_to_gregorian(year,month,day) # :nodoc:
|
276
311
|
jy = year - 979
|
277
312
|
jm = month - 1
|
278
313
|
jd = day - 1
|
279
314
|
j_day_no = 365*jy + (jy/33)*8 + (jy % 33 + 3)/4
|
280
315
|
jm.times { |i| j_day_no += JDaysInMonth[i] }
|
281
316
|
j_day_no += jd
|
282
|
-
|
317
|
+
|
283
318
|
g_day_no = j_day_no + 79
|
284
319
|
gy = 1600 + 400*(g_day_no/146097)
|
285
320
|
g_day_no %= 146097
|
286
|
-
|
321
|
+
|
287
322
|
leap = true
|
288
|
-
if g_day_no >= 36525
|
323
|
+
if g_day_no >= 36525
|
289
324
|
g_day_no -= 1
|
290
325
|
gy += 100 * (g_day_no/36524)
|
291
|
-
g_day_no %= 36524
|
326
|
+
g_day_no %= 36524
|
292
327
|
(g_day_no >= 365) ? g_day_no += 1 : leap = false
|
293
328
|
end
|
294
|
-
|
329
|
+
|
295
330
|
gy += 4 * (g_day_no/1461)
|
296
331
|
g_day_no %= 1461
|
297
|
-
|
332
|
+
|
298
333
|
if g_day_no >= 366
|
299
334
|
leap = false
|
300
335
|
g_day_no -= 1
|
@@ -312,9 +347,9 @@ class JalaliDate
|
|
312
347
|
break
|
313
348
|
end
|
314
349
|
end
|
315
|
-
gm = $g + 1
|
350
|
+
gm = $g + 1
|
316
351
|
gd = g_day_no + 1
|
317
|
-
|
352
|
+
|
318
353
|
[gy,gm,gd]
|
319
354
|
end
|
320
355
|
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Jalalidate
|
2
|
+
class Cli
|
3
|
+
|
4
|
+
# prints today's date in jalali calendar to STDOUT
|
5
|
+
#
|
6
|
+
def self.jdate(*args)
|
7
|
+
jdate = JalaliDate.new(Date.today)
|
8
|
+
puts jdate
|
9
|
+
end
|
10
|
+
|
11
|
+
# prints current month calendar in jalali calendar to STDOUT
|
12
|
+
#
|
13
|
+
def self.jcal(*args)
|
14
|
+
today = JalaliDate.new(Date.today)
|
15
|
+
jdate = JalaliDate.new(today.year,today.month,1)
|
16
|
+
# print month and year
|
17
|
+
puts jdate.strftime("%b %Y").center(26)
|
18
|
+
# print weekdays
|
19
|
+
puts JalaliDate::PERSIAN_ABBR_WEEKDAY_NAMES.reverse[1..6].join(" ") + " " + JalaliDate::PERSIAN_ABBR_WEEKDAY_NAMES.reverse[0] + " "
|
20
|
+
# print the month days
|
21
|
+
padding = true
|
22
|
+
JalaliDate::JDaysInMonth[jdate.month - 1].times do |index|
|
23
|
+
if padding
|
24
|
+
print " " * (jdate.jwday*4)
|
25
|
+
padding = false
|
26
|
+
end
|
27
|
+
print "%2d" % jdate.day + " "
|
28
|
+
print "\n" if jdate.jwday == 6
|
29
|
+
jdate = jdate.next
|
30
|
+
end
|
31
|
+
|
32
|
+
puts "\n"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/jalalidate/version.rb
CHANGED
data/spec/jalalidate_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe JalaliDate do
|
4
|
-
|
4
|
+
|
5
5
|
it "should initialize with valid year,month,date values" do
|
6
6
|
jdate = JalaliDate.new(1388,11,22)
|
7
7
|
jdate.should be_instance_of(JalaliDate)
|
@@ -33,7 +33,7 @@ describe JalaliDate do
|
|
33
33
|
Date.stub!(:today).and_return(Date.new(2010,1,1))
|
34
34
|
JalaliDate.today.should == JalaliDate.new(1388,10,11)
|
35
35
|
JalaliDate.yesterday.should == JalaliDate.new(1388,10,10)
|
36
|
-
JalaliDate.tomorrow.should == JalaliDate.new(1388,10,12)
|
36
|
+
JalaliDate.tomorrow.should == JalaliDate.new(1388,10,12)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should distinguish invalid jalali dates" do
|
@@ -48,19 +48,19 @@ describe JalaliDate do
|
|
48
48
|
JalaliDate.leap?(1387).should be_true
|
49
49
|
JalaliDate.leap?(1388).should be_false
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
it "should convert to gregorian date correctly" do
|
53
53
|
jdate = JalaliDate.new(1388,10,11)
|
54
54
|
jdate.to_g.should == Date.new(2010,1,1)
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
it "should convert to string, array and hash correctly" do
|
58
58
|
jdate = JalaliDate.new(1388,10,11)
|
59
59
|
jdate.to_s.should == "1388/10/11"
|
60
60
|
jdate.to_a.should == [1388,10,11]
|
61
61
|
jdate.to_hash.should == {:year => 1388, :month => 10, :day => 11}
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
it "should be able to add and substract days from the currect jalai date object" do
|
65
65
|
jdate = JalaliDate.new(1388,10,11)
|
66
66
|
five_days_later = jdate + 5
|
@@ -68,7 +68,7 @@ describe JalaliDate do
|
|
68
68
|
five_days_later.should == JalaliDate.new(1388,10,16)
|
69
69
|
twenty_days_ago.should == JalaliDate.new(1388,9,21)
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
it "should be able to compare two jalali dates" do
|
73
73
|
jdate = JalaliDate.new(1388,10,11)
|
74
74
|
next_month_jdate = JalaliDate.new(1388,11,11)
|
@@ -76,13 +76,13 @@ describe JalaliDate do
|
|
76
76
|
jdate.<=>(next_month_jdate).should == -1
|
77
77
|
jdate.<=>(jdate).should == 0
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
it "should now its next and previous dates" do
|
81
81
|
jdate = JalaliDate.new(1388,10,11)
|
82
82
|
jdate.next.should == JalaliDate.new(1388,10,12)
|
83
83
|
jdate.previous.should == JalaliDate.new(1388,10,10)
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
it "should be able to move the month forward and backward" do
|
87
87
|
jdate = JalaliDate.new(1388,10,11)
|
88
88
|
five_month_later = jdate >> 5
|
@@ -90,10 +90,10 @@ describe JalaliDate do
|
|
90
90
|
five_month_later.should == JalaliDate.new(1389,3,11)
|
91
91
|
five_month_ago.should == JalaliDate.new(1388,5,11)
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
it "should be able to cycle through dates in different ways, namely, step, upto and downto" do
|
95
95
|
jdate = JalaliDate.new(1388,10,10)
|
96
|
-
|
96
|
+
|
97
97
|
days_string = ""
|
98
98
|
jdate.step( jdate + 10 , 2) do |jd|
|
99
99
|
days_string += jd.day.to_s
|
@@ -105,7 +105,7 @@ describe JalaliDate do
|
|
105
105
|
days_string += jd.day.to_s
|
106
106
|
end
|
107
107
|
days_string.should == "101112131415"
|
108
|
-
|
108
|
+
|
109
109
|
days_string = ""
|
110
110
|
jdate.downto(jdate-5) do |jd|
|
111
111
|
days_string += jd.day.to_s
|
@@ -121,7 +121,26 @@ describe JalaliDate do
|
|
121
121
|
end
|
122
122
|
|
123
123
|
it "should be able to print jalali date in different formats" do
|
124
|
-
JalaliDate.new(1388,1,7).
|
124
|
+
JalaliDate.new(1388,1,7).strftime("%a %A %b %B %d %e %j %m %w %y %Y %% %x").should == "ج جمعه فروردین Farvardin 07 7 7 1 5 88 1388 % 88/1/7"
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should be intialize with a time object" do
|
128
|
+
time = Time.now
|
129
|
+
JalaliDate.new(time).hour.should == time.hour
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should be able for format %H %I %M %p %S %X %Z correctly if initiallized with time" do
|
133
|
+
JalaliDate.new(1388,2,15,5,50,10).strftime("%H").should == "05"
|
134
|
+
JalaliDate.new(1388,2,15,18,50,10).strftime("%H %I").should == "18 06"
|
135
|
+
JalaliDate.new(1388,2,15,12,50,10).strftime("%H %I").should == "12 00"
|
136
|
+
JalaliDate.new(1388,2,15,12,50,10).strftime("%H %I %M %S").should == "12 00 50 10"
|
137
|
+
JalaliDate.new(1388,2,15,5,50,10).strftime("%p").should == "قبل از ظهر"
|
138
|
+
JalaliDate.new(1388,2,15,15,50,10).strftime("%p").should == "بعد از ظهر"
|
139
|
+
JalaliDate.new(1388,2,15,15,50,10).strftime("%X").should == "15:50:10"
|
140
|
+
JalaliDate.new(1388,2,15,15,50,10).strftime("%X").should == "15:50:10"
|
141
|
+
JalaliDate.new(1388,2,15,15,50,10,"CET",3600).strftime("%Z").should == "CET"
|
142
|
+
time = Time.now
|
143
|
+
JalaliDate.new(time).strftime("%Z").should == time.zone
|
125
144
|
end
|
126
145
|
|
127
146
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
2
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
3
4
|
require 'jalalidate'
|
4
|
-
require '
|
5
|
-
require '
|
5
|
+
require 'rspec'
|
6
|
+
require 'rspec/autorun'
|
6
7
|
|
7
|
-
|
8
|
+
RSpec.configure do |config|
|
8
9
|
|
9
10
|
end
|
metadata
CHANGED
@@ -2,12 +2,12 @@
|
|
2
2
|
name: jalalidate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
hash: 19
|
5
|
-
prerelease:
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Allen A. Bargi
|
@@ -15,14 +15,46 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-06 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
|
-
dependencies:
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 11
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 1
|
33
|
+
- 0
|
34
|
+
version: 2.1.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 23
|
46
|
+
segments:
|
47
|
+
- 1
|
48
|
+
- 0
|
49
|
+
- 0
|
50
|
+
version: 1.0.0
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
22
53
|
description: A library for working with Jalali Calendar (a.k.a Persian Calendar)
|
23
54
|
email: allen.bargi@gmail.com
|
24
|
-
executables:
|
25
|
-
|
55
|
+
executables:
|
56
|
+
- jcal
|
57
|
+
- jdate
|
26
58
|
extensions: []
|
27
59
|
|
28
60
|
extra_rdoc_files:
|
@@ -31,12 +63,16 @@ extra_rdoc_files:
|
|
31
63
|
files:
|
32
64
|
- .gitignore
|
33
65
|
- Gemfile
|
66
|
+
- Gemfile.lock
|
34
67
|
- LICENSE
|
35
68
|
- README.md
|
36
69
|
- Rakefile
|
37
70
|
- TODO
|
71
|
+
- bin/jcal
|
72
|
+
- bin/jdate
|
38
73
|
- jalalidate.gemspec
|
39
74
|
- lib/jalalidate.rb
|
75
|
+
- lib/jalalidate/cli.rb
|
40
76
|
- lib/jalalidate/version.rb
|
41
77
|
- spec/jalalidate_spec.rb
|
42
78
|
- spec/spec_helper.rb
|
@@ -70,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
106
|
requirements: []
|
71
107
|
|
72
108
|
rubyforge_project: jalalidate
|
73
|
-
rubygems_version: 1.
|
109
|
+
rubygems_version: 1.4.1
|
74
110
|
signing_key:
|
75
111
|
specification_version: 3
|
76
112
|
summary: A library for working with Jalali Calendar (a.k.a Persian Calendar)
|