cowtech-extensions 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -78,6 +78,35 @@ module Cowtech
78
78
  def rational_offset(tz = ::Time.zone)
79
79
  Rational((tz.tzinfo.current_period.utc_offset / 3600), 24)
80
80
  end
81
+
82
+ def parameterize_zone(tz)
83
+ tz = tz.to_s if !tz.is_a?(String)
84
+
85
+ if tz =~ /^(\([a-z]+([+-])(\d{2}):(\d{2})\)\s(.+))$/i then
86
+ "#{$2}#{$3}#{$4}@#{$5.parameterize}"
87
+ else
88
+ tz.parameterize
89
+ end
90
+ end
91
+
92
+ def find_parameterized_zone(tz, as_string = false)
93
+ tz = tz.to_s if !tz.is_a?(String)
94
+ tz.gsub!(/^(.\d{4}@)?/, "")
95
+
96
+ rv = catch(:zone) do
97
+ ActiveSupport::TimeZone::MAPPING.each_key do |zone|
98
+ throw(:zone, zone) if ::DateTime.parameterize_zone(zone) == tz
99
+ end
100
+
101
+ nil
102
+ end
103
+
104
+ if rv then
105
+ (as_string ? rv : ActiveSupport::TimeZone[rv])
106
+ else
107
+ nil
108
+ end
109
+ end
81
110
  end
82
111
 
83
112
  def utc_time
@@ -111,6 +140,8 @@ module Cowtech
111
140
  mrv = names[:short_months][self.month - 1]
112
141
  when "%B"
113
142
  mrv = names[:long_months][self.month - 1]
143
+ when "%Z"
144
+ mrv = self.formatted_offset(true) if RUBY_VERSION =~ /^1\.8/ # This is to fix ruby 1.8 bug in OSX
114
145
  when "%z"
115
146
  mrv = self.formatted_offset(false) if RUBY_VERSION =~ /^1\.8/ # This is to fix ruby 1.8 bug in OSX
116
147
  end
@@ -8,8 +8,8 @@ module Cowtech
8
8
  module Extensions
9
9
  module Version
10
10
  MAJOR = 2
11
- MINOR = 0
12
- PATCH = 1
11
+ MINOR = 1
12
+ PATCH = 0
13
13
 
14
14
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
15
15
  end
@@ -94,6 +94,22 @@ describe Cowtech::Extensions::DateTime do
94
94
  end
95
95
  end
96
96
 
97
+ describe "#parameterize_zone" do
98
+ it "should return the parameterized version of the zone" do
99
+ DateTime.parameterize_zone("Mountain Time (US & Canada)").should == "mountain-time-us-canada"
100
+ DateTime.parameterize_zone("(GMT-07:00) Mountain Time (US & Canada)").should == "-0700@mountain-time-us-canada"
101
+ DateTime.parameterize_zone(ActiveSupport::TimeZone["Mountain Time (US & Canada)"]).should == "-0700@mountain-time-us-canada"
102
+ end
103
+ end
104
+
105
+ describe "#find_parameterize_zone" do
106
+ it "should return the parameterized version of the zone" do
107
+ DateTime.find_parameterized_zone("-0700@mountain-time-us-canada").should == ActiveSupport::TimeZone["Mountain Time (US & Canada)"]
108
+ DateTime.find_parameterized_zone("-0700@mountain-time-us-canada", true).should == "Mountain Time (US & Canada)"
109
+ DateTime.find_parameterized_zone("foo").should be_nil
110
+ end
111
+ end
112
+
97
113
  describe "#utc_time" do
98
114
  it "should convert to UTC Time" do random_reference.utc_time.should be_kind_of(Time) end
99
115
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cowtech-extensions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 0
9
8
  - 1
10
- version: 2.0.1
9
+ - 0
10
+ version: 2.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Shogun
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-06-11 00:00:00 Z
18
+ date: 2012-06-12 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: actionpack