time_crisis 0.3.2 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/time_crisis.rb +10 -3
- data/lib/time_crisis/conversions.rb +12 -0
- data/lib/time_crisis/support/ext/date/conversions.rb +3 -2
- data/lib/time_crisis/support/ext/date_time/conversions.rb +4 -4
- data/lib/time_crisis/support/ext/date_time/zones.rb +5 -3
- data/lib/time_crisis/support/ext/time/calculations.rb +1 -16
- data/lib/time_crisis/support/ext/time/conversions.rb +1 -11
- data/lib/time_crisis/support/ext/time/marshal_with_utc_flag.rb +0 -10
- data/lib/time_crisis/support/ext/time/zones.rb +2 -2
- data/lib/time_crisis/time.rb +62 -1
- data/lib/time_crisis/to_json.rb +19 -0
- data/time_crisis.gemspec +2 -1
- metadata +2 -1
data/VERSION.yml
CHANGED
data/lib/time_crisis.rb
CHANGED
@@ -4,6 +4,7 @@ require 'time_crisis/datetime'
|
|
4
4
|
require 'time_crisis/tzinfo'
|
5
5
|
require 'time_crisis/support'
|
6
6
|
require 'time_crisis/conversions'
|
7
|
+
require 'time_crisis/to_json'
|
7
8
|
require 'time_crisis/date_range'
|
8
9
|
require 'time_crisis/named_months'
|
9
10
|
require 'time_crisis/meteorological_seasons'
|
@@ -16,6 +17,12 @@ require 'time_crisis/holiday'
|
|
16
17
|
::TimeCrisis::Time.zone ||= ::Time.now.zone
|
17
18
|
|
18
19
|
# not sure about making this a default behavior
|
19
|
-
TCTime
|
20
|
-
|
21
|
-
|
20
|
+
def TCTime
|
21
|
+
TimeCrisis::Time
|
22
|
+
end
|
23
|
+
def TCDate
|
24
|
+
TimeCrisis::Date
|
25
|
+
end
|
26
|
+
def TCDateTime
|
27
|
+
TimeCrisis::DateTime
|
28
|
+
end
|
@@ -6,6 +6,10 @@ class ::Date
|
|
6
6
|
def to_tc_datetime
|
7
7
|
TimeCrisis::DateTime.civil(year, month, day, 0, 0, 0, 0)
|
8
8
|
end
|
9
|
+
|
10
|
+
def to_tc_time(form = :local)
|
11
|
+
::TimeCrisis::Time.send(form, year, month, day)
|
12
|
+
end
|
9
13
|
end
|
10
14
|
|
11
15
|
class ::Time
|
@@ -16,6 +20,10 @@ class ::Time
|
|
16
20
|
def to_tc_datetime
|
17
21
|
TimeCrisis::DateTime.civil(year, month, day, hour, min, sec, 0, utc_offset)
|
18
22
|
end
|
23
|
+
|
24
|
+
def to_tc_time
|
25
|
+
TimeCrisis::Time.at(self.to_f)
|
26
|
+
end
|
19
27
|
end
|
20
28
|
|
21
29
|
class ::DateTime
|
@@ -26,6 +34,10 @@ class ::DateTime
|
|
26
34
|
def to_tc_datetime
|
27
35
|
TimeCrisis::DateTime.civil(year, month, day, hour, min, sec, 0, (offset * 86400).to_i)
|
28
36
|
end
|
37
|
+
|
38
|
+
def to_tc_time
|
39
|
+
TimeCrisis::Time.at(self.to_f)
|
40
|
+
end
|
29
41
|
end
|
30
42
|
|
31
43
|
class ::String
|
@@ -32,8 +32,9 @@ module TimeCrisis
|
|
32
32
|
self
|
33
33
|
end
|
34
34
|
|
35
|
-
def to_tc_datetime
|
36
|
-
|
35
|
+
def to_tc_datetime(form = :local)
|
36
|
+
offset = form == :local ? (Time.zone.now.utc_offset rescue Time.now.utc_offset) : 0
|
37
|
+
::TimeCrisis::DateTime.civil(year, month, day, 0, 0, 0, 0, offset)
|
37
38
|
end
|
38
39
|
|
39
40
|
def to_tc_time(form = :local)
|
@@ -33,15 +33,15 @@ module TimeCrisis
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def to_datetime
|
36
|
-
::DateTime.civil(year, month, day, hour,
|
36
|
+
::DateTime.civil(year, month, day, hour, min, second, Rational(offset, 86400))
|
37
37
|
end
|
38
38
|
|
39
39
|
def to_time
|
40
|
-
::Time.at(self.
|
40
|
+
::Time.at(self.to_f)
|
41
41
|
end
|
42
42
|
|
43
43
|
def to_tc_time
|
44
|
-
::TimeCrisis::Time.at(self.
|
44
|
+
::TimeCrisis::Time.at(self.to_f)
|
45
45
|
end
|
46
46
|
|
47
47
|
def xmlschema
|
@@ -60,7 +60,7 @@ module TimeCrisis
|
|
60
60
|
|
61
61
|
def seconds_since_unix_epoch
|
62
62
|
seconds_per_day = 86_400
|
63
|
-
(self - ::TimeCrisis::DateTime.civil(1970)) * seconds_per_day
|
63
|
+
(self.utc - ::TimeCrisis::DateTime.civil(1970, 1, 1)) * seconds_per_day
|
64
64
|
end
|
65
65
|
end
|
66
66
|
end
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module TimeCrisis
|
2
|
+
class DateTime
|
3
|
+
def in_time_zone(zone = ::TimeCrisis::Time.zone)
|
4
|
+
TimeCrisis::TimeWithZone.new(utc? ? self : getutc, ::TimeCrisis::Time.__send__(:get_zone, zone))
|
5
|
+
end
|
4
6
|
end
|
5
7
|
end
|
@@ -8,7 +8,7 @@ module TimeCrisis
|
|
8
8
|
|
9
9
|
class << self
|
10
10
|
def ===(other)
|
11
|
-
other.is_a?(::Time)
|
11
|
+
other.is_a?(::Time) || other.is_a?(::TimeCrisis::Time)
|
12
12
|
end
|
13
13
|
|
14
14
|
def days_in_month(month, year = now.year)
|
@@ -186,11 +186,6 @@ module TimeCrisis
|
|
186
186
|
def tomorrow
|
187
187
|
advance(:days => 1)
|
188
188
|
end
|
189
|
-
|
190
|
-
if instance_methods.include?('plus_without_duration')
|
191
|
-
undef_method :+
|
192
|
-
alias_method :+, :plus_without_duration
|
193
|
-
end
|
194
189
|
|
195
190
|
def plus_with_duration(other) #:nodoc:
|
196
191
|
if TimeCrisis::Duration === other
|
@@ -201,11 +196,6 @@ module TimeCrisis
|
|
201
196
|
end
|
202
197
|
alias_method :plus_without_duration, :+
|
203
198
|
alias_method :+, :plus_with_duration
|
204
|
-
|
205
|
-
if instance_methods.include?('minus_without_duration')
|
206
|
-
undef_method :-
|
207
|
-
alias_method :-, :minus_without_duration
|
208
|
-
end
|
209
199
|
|
210
200
|
def minus_with_duration(other) #:nodoc:
|
211
201
|
if TimeCrisis::Duration === other
|
@@ -223,11 +213,6 @@ module TimeCrisis
|
|
223
213
|
end
|
224
214
|
alias_method :minus_without_coercion, :-
|
225
215
|
alias_method :-, :minus_with_coercion
|
226
|
-
|
227
|
-
if instance_methods.include?('compare_without_coercion')
|
228
|
-
undef_method :<=>
|
229
|
-
alias_method :<=>, :compare_without_coercion
|
230
|
-
end
|
231
216
|
|
232
217
|
def compare_with_coercion(other)
|
233
218
|
other = other.comparable_time if other.respond_to?(:comparable_time)
|
@@ -10,11 +10,6 @@ module TimeCrisis
|
|
10
10
|
:long => "%B %d, %Y %H:%M",
|
11
11
|
:rfc822 => lambda { |time| time.strftime("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") }
|
12
12
|
}
|
13
|
-
|
14
|
-
if instance_methods.include?('to_default_s')
|
15
|
-
undef_method :to_s
|
16
|
-
alias_method :to_s, :to_default_s
|
17
|
-
end
|
18
13
|
|
19
14
|
def to_formatted_s(format = :default)
|
20
15
|
if formatter = DATE_FORMATS[format]
|
@@ -29,11 +24,6 @@ module TimeCrisis
|
|
29
24
|
def formatted_offset(colon = true, alternate_utc_string = nil)
|
30
25
|
utc? && alternate_utc_string || TimeCrisis::TimeZone.seconds_to_utc_offset(utc_offset, colon)
|
31
26
|
end
|
32
|
-
|
33
|
-
if instance_methods.include?('default_inspect')
|
34
|
-
undef_method :inspect
|
35
|
-
alias_method :inspect, :default_inspect
|
36
|
-
end
|
37
27
|
|
38
28
|
def readable_inspect
|
39
29
|
"#<TimeCrisis::Time #{self.to_s(:rfc822)}>"
|
@@ -46,7 +36,7 @@ module TimeCrisis
|
|
46
36
|
end
|
47
37
|
|
48
38
|
def to_time
|
49
|
-
::Time.at(self.
|
39
|
+
::Time.at(self.to_f)
|
50
40
|
end
|
51
41
|
|
52
42
|
def to_datetime
|
@@ -2,11 +2,6 @@ if RUBY_VERSION < '1.9'
|
|
2
2
|
module TimeCrisis
|
3
3
|
class Time
|
4
4
|
class << self
|
5
|
-
if respond_to?(:_original_load)
|
6
|
-
undef_method :_load
|
7
|
-
alias_method :_load, :_original_load
|
8
|
-
end
|
9
|
-
|
10
5
|
alias_method :_original_load, :_load
|
11
6
|
def _load(marshaled_time)
|
12
7
|
time = _original_load(marshaled_time)
|
@@ -15,11 +10,6 @@ if RUBY_VERSION < '1.9'
|
|
15
10
|
end
|
16
11
|
end
|
17
12
|
|
18
|
-
if respond_to?(:_original_dump)
|
19
|
-
undef_method :_dump
|
20
|
-
alias_method :_dump, :_original_dump
|
21
|
-
end
|
22
|
-
|
23
13
|
alias_method :_original_dump, :_dump
|
24
14
|
def _dump(*args)
|
25
15
|
obj = frozen? ? dup : self
|
@@ -6,11 +6,11 @@ module TimeCrisis
|
|
6
6
|
attr_accessor :zone_default
|
7
7
|
|
8
8
|
def zone
|
9
|
-
Thread.current[:
|
9
|
+
Thread.current[:tc_time_zone] || zone_default
|
10
10
|
end
|
11
11
|
|
12
12
|
def zone=(time_zone)
|
13
|
-
Thread.current[:
|
13
|
+
Thread.current[:tc_time_zone] = get_zone(time_zone)
|
14
14
|
end
|
15
15
|
|
16
16
|
def use_zone(time_zone)
|
data/lib/time_crisis/time.rb
CHANGED
@@ -1,4 +1,65 @@
|
|
1
1
|
module TimeCrisis
|
2
|
-
|
2
|
+
Time = ::Time.dup
|
3
|
+
end
|
4
|
+
|
5
|
+
if defined?(::ActiveSupport)
|
6
|
+
module TimeCrisis
|
7
|
+
class Time
|
8
|
+
#======================================================================>
|
9
|
+
# try to get back original versions of methods
|
10
|
+
|
11
|
+
if instance_methods.include?('plus_without_duration')
|
12
|
+
alias :+ :plus_without_duration
|
13
|
+
undef :plus_without_duration
|
14
|
+
undef :plus_with_duration
|
15
|
+
end
|
16
|
+
|
17
|
+
if instance_methods.include?('minus_without_duration')
|
18
|
+
alias :- :minus_without_duration
|
19
|
+
undef :minus_without_duration
|
20
|
+
undef :minus_with_duration
|
21
|
+
end
|
22
|
+
|
23
|
+
if instance_methods.include?('minus_without_coercion')
|
24
|
+
undef :minus_without_coercion
|
25
|
+
undef :minus_with_coercion
|
26
|
+
end
|
27
|
+
|
28
|
+
if instance_methods.include?('compare_without_coercion')
|
29
|
+
alias :<=> :compare_without_coercion
|
30
|
+
undef :compare_without_coercion
|
31
|
+
undef :compare_with_coercion
|
32
|
+
end
|
33
|
+
|
34
|
+
if instance_methods.include?('default_inspect')
|
35
|
+
alias :inspect :default_inspect
|
36
|
+
undef :default_inspect
|
37
|
+
undef :readable_inspect
|
38
|
+
end
|
39
|
+
|
40
|
+
class << self
|
41
|
+
if respond_to?(:_original_load)
|
42
|
+
alias :_load :_original_load
|
43
|
+
undef :_original_load
|
44
|
+
end
|
45
|
+
|
46
|
+
if respond_to?(:_original_dump)
|
47
|
+
alias :_dump :_original_dump
|
48
|
+
undef :_original_dump
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
#======================================================================>
|
53
|
+
|
54
|
+
class << self
|
55
|
+
[
|
56
|
+
:zone_default, :zone_default=, :zone, :zone=,
|
57
|
+
:use_zone, :current, :get_zone
|
58
|
+
].each {|method| undef_method(method) if respond_to?(method)}
|
59
|
+
end
|
60
|
+
|
61
|
+
undef :in_time_zone if instance_methods.include?('in_time_zone')
|
62
|
+
|
63
|
+
end
|
3
64
|
end
|
4
65
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module TimeCrisis
|
2
|
+
class Date
|
3
|
+
def to_json(options = nil)
|
4
|
+
%("#{strftime("%Y-%m-%d")}")
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class DateTime
|
9
|
+
def to_json(options = nil)
|
10
|
+
xmlschema.inspect
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Time
|
15
|
+
def to_json(options = nil)
|
16
|
+
xmlschema.inspect
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/time_crisis.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{time_crisis}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Travis Tilley"]
|
@@ -53,6 +53,7 @@ Gem::Specification.new do |s|
|
|
53
53
|
"lib/time_crisis/support/time_with_zone.rb",
|
54
54
|
"lib/time_crisis/support/values/time_zone.rb",
|
55
55
|
"lib/time_crisis/time.rb",
|
56
|
+
"lib/time_crisis/to_json.rb",
|
56
57
|
"lib/time_crisis/tzinfo.rb",
|
57
58
|
"lib/time_crisis/tzinfo/LICENSE",
|
58
59
|
"lib/time_crisis/tzinfo/data_timezone.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_crisis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Tilley
|
@@ -59,6 +59,7 @@ files:
|
|
59
59
|
- lib/time_crisis/support/time_with_zone.rb
|
60
60
|
- lib/time_crisis/support/values/time_zone.rb
|
61
61
|
- lib/time_crisis/time.rb
|
62
|
+
- lib/time_crisis/to_json.rb
|
62
63
|
- lib/time_crisis/tzinfo.rb
|
63
64
|
- lib/time_crisis/tzinfo/LICENSE
|
64
65
|
- lib/time_crisis/tzinfo/data_timezone.rb
|