rails_pdate 0.0.3 → 0.0.4

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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +18 -17
  3. data/README.rdoc +0 -0
  4. data/Rakefile +0 -0
  5. data/lib/core_date.rb +5 -1
  6. data/lib/core_date_time.rb +10 -0
  7. data/lib/core_string.rb +0 -0
  8. data/lib/rails_pdate.rb +6 -4
  9. data/lib/rails_pdate/base_date_time.rb +94 -0
  10. data/lib/rails_pdate/constants.rb +12 -8
  11. data/lib/rails_pdate/formats.rb +49 -0
  12. data/lib/rails_pdate/initializer.rb +5 -0
  13. data/lib/rails_pdate/p_date.rb +15 -74
  14. data/lib/rails_pdate/p_date_time.rb +49 -0
  15. data/lib/rails_pdate/pconvertor.rb +2 -0
  16. data/lib/rails_pdate/time_shifting.rb +76 -0
  17. data/lib/rails_pdate/version.rb +1 -1
  18. data/lib/tasks/rails_pdate_tasks.rake +0 -0
  19. data/test/comparisons_test.rb +34 -0
  20. data/test/core_date_test.rb +1 -0
  21. data/test/core_date_time_test.rb +21 -0
  22. data/test/core_string_test.rb +0 -0
  23. data/test/dummy/README.rdoc +0 -0
  24. data/test/dummy/Rakefile +0 -0
  25. data/test/dummy/app/assets/javascripts/application.js +0 -0
  26. data/test/dummy/app/assets/stylesheets/application.css +0 -0
  27. data/test/dummy/app/controllers/application_controller.rb +0 -0
  28. data/test/dummy/app/helpers/application_helper.rb +0 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +0 -0
  30. data/test/dummy/config.ru +0 -0
  31. data/test/dummy/config/application.rb +0 -0
  32. data/test/dummy/config/boot.rb +1 -1
  33. data/test/dummy/config/database.yml +0 -0
  34. data/test/dummy/config/environment.rb +0 -0
  35. data/test/dummy/config/environments/development.rb +0 -0
  36. data/test/dummy/config/environments/production.rb +0 -0
  37. data/test/dummy/config/environments/test.rb +3 -1
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -0
  39. data/test/dummy/config/initializers/filter_parameter_logging.rb +0 -0
  40. data/test/dummy/config/initializers/inflections.rb +0 -0
  41. data/test/dummy/config/initializers/mime_types.rb +0 -0
  42. data/test/dummy/config/initializers/secret_token.rb +0 -0
  43. data/test/dummy/config/initializers/session_store.rb +0 -0
  44. data/test/dummy/config/initializers/wrap_parameters.rb +0 -0
  45. data/test/dummy/config/locales/en.yml +0 -0
  46. data/test/dummy/config/routes.rb +0 -0
  47. data/test/dummy/log/test.log +22370 -80
  48. data/test/dummy/public/404.html +0 -0
  49. data/test/dummy/public/422.html +0 -0
  50. data/test/dummy/public/500.html +0 -0
  51. data/test/dummy/public/favicon.ico +0 -0
  52. data/test/pconvertor_test.rb +0 -0
  53. data/test/rails_pdate_test.rb +28 -0
  54. data/test/rails_pdatetime_test.rb +15 -0
  55. data/test/test_helper.rb +0 -0
  56. data/test/time_shifting_test.rb +17 -0
  57. metadata +80 -81
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90947e0fa28720b304d9047a786df4a75fb44ffb
4
- data.tar.gz: dfb2c7548876c67646bc4e2351d82eb6c32ee7f0
3
+ metadata.gz: 3eaf9bbdcedef776086d9069afa8b4a0d0c51d90
4
+ data.tar.gz: dabada713dffd5ca1dafc2e406166ba6a0ddd887
5
5
  SHA512:
6
- metadata.gz: 6064cd98d1efb075abe8645a5d9b46fc9e5437380d56a56690bc0cc65429db7b3259cabc3e6aff521400c36822aa366bb3cc47363ad250580eb8876650e8a0bc
7
- data.tar.gz: 059afac43cc89cea3c126f9a3495060d4e8b611d95cfbd3795226bcfee783444d12a7e6772468062b8e55e307fe2be6c2d2c77cf4bc7dff9315efd19f7d1f6c8
6
+ metadata.gz: a901337c73b542a5205acb47d2caeede1e3b6ba91da29d21e055e308062b1e8dd043013442fbd8995b4b2b2b7d8bb0eba88d805fea4e37e584d7597dede2e3bc
7
+ data.tar.gz: ca0f8642c9745188d72cdba970483351016a56e187e678e1e3009666f719a0a99abf77ac205563e707ce96f9ed2d1a2876e769018dc183fd0d5cdccd0423f824
@@ -1,20 +1,21 @@
1
- Copyright 2013 YOURNAME
1
+ MIT License
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
3
+ Copyright (c) 2016 Mohsen Alizadeh
10
4
 
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
13
11
 
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
File without changes
data/Rakefile CHANGED
File without changes
@@ -3,4 +3,8 @@ Date.class_eval do
3
3
  def to_pdate
4
4
  PDate.new(self)
5
5
  end
6
- end
6
+
7
+ def to_pdatetime
8
+ PDateTime.new(self)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+
2
+ DateTime.class_eval do
3
+ def to_pdate
4
+ PDate.new(self)
5
+ end
6
+
7
+ def to_pdatetime
8
+ PDateTime.new(self)
9
+ end
10
+ end
File without changes
@@ -7,9 +7,11 @@ end
7
7
 
8
8
  require 'core_date'
9
9
  require 'core_string'
10
- require 'rails_pdate/pconvertor'
11
10
  require 'rails_pdate/constants'
11
+ require 'rails_pdate/pconvertor'
12
+ require 'rails_pdate/time_shifting'
13
+ require 'rails_pdate/initializer'
14
+ require 'rails_pdate/formats'
15
+ require 'rails_pdate/base_date_time'
12
16
  require 'rails_pdate/p_date'
13
-
14
-
15
-
17
+ require 'rails_pdate/p_date_time'
@@ -0,0 +1,94 @@
1
+ module RailsPdate
2
+ class BaseDateTime
3
+
4
+ attr_accessor :year, :month, :day
5
+
6
+ include RailsPdate::Constants
7
+ include RailsPdate::TimeShifting
8
+
9
+ def initialize *args
10
+
11
+ @year = @month = @day = @hour = @minute = @second = 0
12
+
13
+ if (args.size == 1)
14
+ @year, @month, @day = Pconvertor.new.gregorian_to_persian(args.first.year, args.first.month, args.first.day)
15
+
16
+ if (args.first.is_a?(DateTime))
17
+ @hour = args.first.hour
18
+ @minute = args.first.minute
19
+ @second = args.first.second
20
+ end
21
+ else
22
+ @year, @month, @day, @hour, @minute, @second = args
23
+ end
24
+ end
25
+
26
+ def ==(other)
27
+ return true if to_s == other.to_s
28
+ false
29
+ end
30
+
31
+ # day of calendar week (1-7, Shanbe is 1).
32
+ def cwday
33
+ cwday = to_date.cwday + 2
34
+ cwday -= 7 if cwday > 7
35
+ cwday
36
+ end
37
+
38
+ # get day of year ( 1 - 365 )
39
+ def yday
40
+ day_of_year = 0
41
+
42
+ (1..(@month-1)).to_a.each do |month|
43
+ day_of_year += PDaysInMonth[month-1]
44
+ end
45
+
46
+ day_of_year += day
47
+
48
+ day_of_year
49
+ end
50
+
51
+ def cweek
52
+ (yday / 7) + (@day % 7 == 0 ? 0 : 1)
53
+ end
54
+
55
+ def pdayname
56
+ PERSIAN_WEEKDAY_NAMES[self.cwday-1]
57
+ end
58
+
59
+ def to_date
60
+ year, month, day = Pconvertor.new.persian_to_gregorian(@year, @month, @day)
61
+ Date.new(year, month, day)
62
+ end
63
+
64
+
65
+ def <=(other)
66
+ self.to_i <= other.to_i
67
+ end
68
+
69
+ def <(other)
70
+ self.to_i < other.to_i
71
+ end
72
+
73
+ def >=(other)
74
+ self.to_i >= other.to_i
75
+ end
76
+
77
+ def >(other)
78
+ self.to_i > other.to_i
79
+ end
80
+
81
+ def ==(other)
82
+ self.to_i == other.to_i
83
+ end
84
+
85
+ def to_i
86
+ to_datetime.to_i
87
+ end
88
+
89
+
90
+
91
+ # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
92
+ alias_method :default_inspect, :inspect
93
+ end
94
+ end
@@ -1,12 +1,16 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  #:stopdoc:
4
- GDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
5
- PDaysInMonth = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29]
6
- PERSIAN_MONTH_NAMES = [nil, "فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"]
7
- PERSIAN_MONTH_NAMES_PINGLISH = [nil, "Farvardin", "Ordibehesht", "Khordad", "Tir", "Mordad", "Shahrivar", "Mehr", "Aban", "Azar", "Dey", "Bahman", "Esfand"]
8
- PERSIAN_WEEKDAY_NAMES = ["یک‌شنبه","دوشنبه","سه‌شنبه","چهارشنبه","پنج‌شنبه","جمعه","شنبه"]
9
- PERSIAN_ABBR_WEEKDAY_NAMES = ["۱ش ","۲ش","۳ش","۴ش","۵ش","ج ","ش"]
10
- PERSIAN_MERIDIAN_INDICATOR = ["قبل از ظهر","بعد از ظهر"]
11
- PERSIAN_ABBR_MERIDIAN_INDICATOR = ["ق.ظ","ب.ظ"]
4
+ module RailsPdate
5
+ module Constants
6
+ GDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
7
+ PDaysInMonth = [31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29]
8
+ PERSIAN_MONTH_NAMES = [nil, "فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"]
9
+ PERSIAN_MONTH_NAMES_PINGLISH = [nil, "Farvardin", "Ordibehesht", "Khordad", "Tir", "Mordad", "Shahrivar", "Mehr", "Aban", "Azar", "Dey", "Bahman", "Esfand"]
10
+ PERSIAN_WEEKDAY_NAMES = ["شنبه", "یک‌شنبه","دوشنبه","سه‌شنبه","چهارشنبه","پنج‌شنبه","جمعه"]
11
+ PERSIAN_ABBR_WEEKDAY_NAMES = ["۱ش ","۲ش","۳ش","۴ش","۵ش","ج ","ش"]
12
+ PERSIAN_MERIDIAN_INDICATOR = ["قبل از ظهر","بعد از ظهر"]
13
+ PERSIAN_ABBR_MERIDIAN_INDICATOR = ["ق.ظ","ب.ظ"]
14
+ end
15
+ end
12
16
  #:startdoc:
@@ -0,0 +1,49 @@
1
+ module RailsPdate
2
+ module Formats
3
+ module DateTime
4
+
5
+ FORMATS = {
6
+ :short => '%e %b',
7
+ :long => '%B %e, %Y',
8
+ :db => '%Y-%m-%d',
9
+ :number => '%Y%m%d',
10
+ :long_ordinal => lambda { |date|
11
+ day_format = ActiveSupport::Inflector.ordinalize(date.day)
12
+ date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
13
+ },
14
+ :rfc822 => '%e %b %Y',
15
+ :iso8601 => lambda { |date| date.iso8601 },
16
+ :default => '%Y-%m-%d %h:%i'
17
+ }
18
+
19
+ def to_formatted_s
20
+ strftime(RailsPdate::Formats::DateTime::FORMATS[:default])
21
+ end
22
+ alias_method :to_default_s, :to_s
23
+ alias_method :to_s, :to_formatted_s
24
+ end
25
+
26
+ module Date
27
+
28
+ FORMATS = {
29
+ :short => '%e %b',
30
+ :long => '%B %e, %Y',
31
+ :db => '%Y-%m-%d',
32
+ :number => '%Y%m%d',
33
+ :long_ordinal => lambda { |date|
34
+ day_format = ActiveSupport::Inflector.ordinalize(date.day)
35
+ date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
36
+ },
37
+ :rfc822 => '%e %b %Y',
38
+ :iso8601 => lambda { |date| date.iso8601 },
39
+ :default => '%Y-%m-%d'
40
+ }
41
+
42
+ def to_formatted_s
43
+ strftime(RailsPdate::Formats::Date::FORMATS[:default])
44
+ end
45
+ alias_method :to_default_s, :to_s
46
+ alias_method :to_s, :to_formatted_s
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ module RailsPdate
2
+ module Initializer
3
+
4
+ end
5
+ end
@@ -1,74 +1,11 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  # :title:PDate (Persian Date) #
4
- class PDate
4
+ class PDate < RailsPdate::BaseDateTime
5
5
 
6
- DATE_FORMATS = {
7
- :short => '%e %b',
8
- :long => '%B %e, %Y',
9
- :db => '%Y-%m-%d',
10
- :number => '%Y%m%d',
11
- :long_ordinal => lambda { |date|
12
- day_format = ActiveSupport::Inflector.ordinalize(date.day)
13
- date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007"
14
- },
15
- :rfc822 => '%e %b %Y',
16
- :iso8601 => lambda { |date| date.iso8601 },
17
- :default => '%Y-%m-%d'
18
- }
19
-
20
- def initialize *args
21
-
22
- if (args.size == 1) && (args.first.is_a?(Date))
23
- year, month, day = Pconvertor.new.gregorian_to_persian(args.first.year, args.first.month, args.first.day)
24
- else
25
- year,month,day,hour,min,sec,zone,utc_offset = args
26
- end
27
-
28
- @year = year
29
- @month = month
30
- @day = day
31
- @hour ||= hour || 0
32
- @min ||= min || 0
33
- @sec ||= sec || 0
34
- @zone ||= zone || "UTC"
35
- @utc_offset ||= utc_offset || 0
36
- end
37
-
38
- def to_date
39
- year, month, day = Pconvertor.new.persian_to_gregorian(@year, @month, @day)
40
- Date.new(year, month, day)
41
- end
42
-
43
-
44
- def to_formatted_s(format = :default)
45
- if formatter = DATE_FORMATS[format]
46
- if formatter.respond_to?(:call)
47
- formatter.call(self).to_s
48
- else
49
- strftime(formatter)
50
- end
51
- else
52
- to_default_s
53
- end
54
- end
55
-
56
- alias_method :to_default_s, :to_s
57
- alias_method :to_s, :to_formatted_s
58
-
59
- # Overrides the default inspect method with a human readable one, e.g., "Mon, 21 Feb 2005"
60
- def readable_inspect
61
- strftime('%a, %d %b %Y')
62
- end
63
- alias_method :default_inspect, :inspect
64
- alias_method :inspect, :readable_inspect
65
-
66
- def to_formatted_s
67
- strftime(DATE_FORMATS[:default])
68
- end
6
+ include RailsPdate::Formats::Date
69
7
 
70
8
  def strftime(format)
71
-
72
9
  format
73
10
  .gsub('%Y', '%d' % @year)
74
11
  .gsub('%m', '%02d' % @month)
@@ -78,12 +15,7 @@ class PDate
78
15
  .gsub('%B', '%s' % PERSIAN_MONTH_NAMES_PINGLISH[@month])
79
16
  .gsub('%b', '%s' % PERSIAN_MONTH_NAMES[@month])
80
17
  .gsub('%e', '%d' % @day)
81
-
82
- end
83
-
84
- def ==(other)
85
- return true if to_s == other.to_s
86
- false
18
+ .gsub('%A', '%s' % PERSIAN_WEEKDAY_NAMES[self.cwday-1])
87
19
  end
88
20
 
89
21
  def clone
@@ -94,8 +26,17 @@ class PDate
94
26
  [@year, @month, @day]
95
27
  end
96
28
 
97
- def inspect
98
- to_s
29
+ def +(other)
30
+ ( to_date + other ).to_pdate
31
+ end
32
+
33
+ def -(other)
34
+ ( to_date - other ).to_pdate
35
+ end
36
+
37
+ def to_datetime
38
+ to_date.to_datetime
99
39
  end
100
40
 
101
- end
41
+ alias_method :inspect, :to_s
42
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ # :title:PDateTime (Persian DateTime) #
4
+ class PDateTime < RailsPdate::BaseDateTime
5
+
6
+ attr_accessor :hour, :minute, :second
7
+
8
+ include RailsPdate::Formats::DateTime
9
+
10
+ def to_a
11
+ [@year, @month, @day, @hour, @minute, @second]
12
+ end
13
+
14
+ def strftime(format)
15
+ format
16
+ .gsub('%Y', '%d' % @year)
17
+ .gsub('%m', '%02d' % @month)
18
+ .gsub('%d', '%02d' % @day)
19
+ .gsub('%C', '%d' % (@year / 100) )
20
+ .gsub('%y', '%d' % (@year % 100) )
21
+ .gsub('%B', '%s' % PERSIAN_MONTH_NAMES_PINGLISH[@month])
22
+ .gsub('%b', '%s' % PERSIAN_MONTH_NAMES[@month])
23
+ .gsub('%e', '%d' % @day)
24
+ .gsub('%A', '%s' % PERSIAN_WEEKDAY_NAMES[self.cwday-1])
25
+ .gsub('%H', '%d' % @hour)
26
+ .gsub('%h', '%02d' % @hour)
27
+ .gsub('%i', '%02d' % @minute)
28
+ .gsub('%I', '%d' % @minute)
29
+ .gsub('%s', '%02d' % @second)
30
+ .gsub('%S', '%d' % @second)
31
+ end
32
+
33
+ def clone
34
+ PDateTime.new(@year, @month, @day, @hour, @minute, @second)
35
+ end
36
+
37
+ def to_datetime
38
+ year, month, day = Pconvertor.new.persian_to_gregorian(@year, @month, @day)
39
+ DateTime.new(year, month, day, @hour, @minute, @second)
40
+ end
41
+
42
+ def +(other)
43
+ ( to_datetime + other ).to_pdatetime
44
+ end
45
+
46
+ def -(other)
47
+ ( to_datetime - other ).to_pdatetime
48
+ end
49
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  class Pconvertor
4
4
 
5
+ include RailsPdate::Constants
6
+
5
7
  def gregorian_to_persian(year, month, day) # :nodoc:
6
8
  jj=0
7
9
  gy = year - 1600
@@ -0,0 +1,76 @@
1
+ module RailsPdate
2
+ module TimeShifting
3
+
4
+ def self.included( other )
5
+ other.extend( RailsPdate::Constants )
6
+ end
7
+
8
+ def beginning_of_year
9
+ beginning_of_month
10
+ @month = 1
11
+ self
12
+ end
13
+
14
+ def beginning_of_month
15
+ beginning_of_day
16
+ @day = 1
17
+ self
18
+ end
19
+
20
+ def beginning_of_week
21
+ beginning_of_day
22
+ self - ( cwday - 1)
23
+ end
24
+
25
+ def beginning_of_day
26
+ beginning_of_hour
27
+ @hour = 0
28
+ self
29
+ end
30
+
31
+ def beginning_of_hour
32
+ beginning_of_minute
33
+ @min = 0
34
+ self
35
+ end
36
+
37
+ def beginning_of_minute
38
+ @sec = 0
39
+ self
40
+ end
41
+
42
+ def end_of_year
43
+ @month = 12
44
+ end_of_month
45
+ self
46
+ end
47
+
48
+ def end_of_month
49
+ end_of_day
50
+ @day = RailsPdate::Constants::PDaysInMonth[@month-1]
51
+ self
52
+ end
53
+
54
+ def end_of_week
55
+ end_of_day
56
+ self + ( 7 - cwday )
57
+ end
58
+
59
+ def end_of_day
60
+ end_of_hour
61
+ @hour = 23
62
+ self
63
+ end
64
+
65
+ def end_of_hour
66
+ end_of_minute
67
+ @minute = 59
68
+ self
69
+ end
70
+
71
+ def end_of_minute
72
+ @sec = 59
73
+ self
74
+ end
75
+ end
76
+ end