teasy 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9237135fe6b6ebdbbfda965105575687619541dd
4
- data.tar.gz: 743861f57815c7d9e71cbd2430068babeb91869c
3
+ metadata.gz: 54fb405363968ca96e3690d23559425f212a96f5
4
+ data.tar.gz: 65ab60376126736884d7a5b4faf3b4fe6e194736
5
5
  SHA512:
6
- metadata.gz: 6f991d7d9055565b603ee4fdac3ee055e2350a85335d5222ed759d973d4a1f0b0e350d7e8e567c6bfc3a2de037db8b47b7c57930d9b4936157c97b79612551f5
7
- data.tar.gz: 97a5e37bd8be859daa782683f278157448cddca72f76385a762bd38f456534e25b860c1771cc31d91cb67c859bacf865b650601040bc9c72736a5dcc96395236
6
+ metadata.gz: 1dcaa1eb6b607f0061f570c84b8a0de4d4f081fa86f313d953617556749dba4966638341255d3135d132a7c7c5423258dc0567b2a27b78f637a84d3ff742204e
7
+ data.tar.gz: 0b432bb95e49c19ace3c6d0f751908be1a930de43e9e266de8b1fc78c61b58451e4d7f3aa6ff0c94f0a069154281e9a42b510b07eb4ddc809c3239ca78699062
data/lib/teasy.rb CHANGED
@@ -14,8 +14,8 @@ module Teasy
14
14
  def self.with_zone(zone)
15
15
  old_zone = Thread.current[:teasy_default_zone]
16
16
  Thread.current[:teasy_default_zone] = zone
17
- block_result = yield
17
+ yield
18
+ ensure
18
19
  Thread.current[:teasy_default_zone] = old_zone
19
- block_result
20
20
  end
21
21
  end
@@ -13,7 +13,8 @@ module Teasy
13
13
  :sunday?
14
14
  def_delegators :period, :dst?
15
15
  def_delegator :period, :utc_total_offset, :utc_offset
16
- def_delegators :to_time, :to_i, :to_r, :to_f
16
+ def_delegators :to_time, :to_i, :to_r, :to_f, :to_datetime, :to_date,
17
+ :httpdate, :rfc2822, :rfc822, :xmlschema, :iso8601
17
18
 
18
19
  # rubocop:disable Metrics/ParameterLists
19
20
  def initialize(year, month = nil, day = nil,
@@ -35,10 +36,11 @@ module Teasy
35
36
  end
36
37
 
37
38
  def in_time_zone!(zone = Teasy.default_zone)
38
- time = to_time
39
+ time = utc_time
39
40
  @zone = TZInfo::Timezone.get(zone)
40
41
  @time = @zone.utc_to_local(time)
41
42
  @period = @zone.period_for_utc(time)
43
+ remove_instance_variable(:@local_time) unless @local_time.nil?
42
44
  self
43
45
  end
44
46
 
@@ -93,12 +95,12 @@ module Teasy
93
95
  alias_method :ctime, :asctime
94
96
 
95
97
  def +(other)
96
- TimeWithZone.from_utc(to_time + other, @zone.identifier)
98
+ TimeWithZone.from_utc(utc_time + other, @zone.identifier)
97
99
  end
98
100
 
99
101
  def -(other)
100
102
  if other.is_a? Numeric
101
- TimeWithZone.from_utc(to_time - other, @zone.identifier)
103
+ TimeWithZone.from_utc(utc_time - other, @zone.identifier)
102
104
  elsif other.respond_to? :to_time
103
105
  to_time - other.to_time
104
106
  else
@@ -124,11 +126,18 @@ module Teasy
124
126
  end
125
127
 
126
128
  def to_time
127
- @utc_time ||= @zone.local_to_utc(@time)
129
+ return @local_time unless @local_time.nil?
130
+ params = %i(year mon day hour min).map! { |m| @time.send(m) }
131
+ params << @time.sec + @time.subsec
132
+ @local_time = utc? ? Time.utc(*params) : Time.new(*params, utc_offset)
128
133
  end
129
134
 
130
135
  private
131
136
 
137
+ def utc_time
138
+ @utc_time ||= @zone.local_to_utc(@time)
139
+ end
140
+
132
141
  attr_reader :time, :period
133
142
 
134
143
  # matches valid format directives for zones
data/lib/teasy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Teasy
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -30,12 +30,17 @@ class TeasyTest < Minitest::Test
30
30
  threads.each(&:join)
31
31
  end
32
32
 
33
- def test_with_zone
33
+ def test_with_zone_changes_default_zone_only_within_block
34
34
  assert_equal 'UTC', Teasy.default_zone
35
35
  Teasy.with_zone('Europe/Berlin') do
36
36
  assert_equal 'Europe/Berlin', Teasy.default_zone
37
+ fail 'exception within block'
37
38
  end
39
+ rescue
38
40
  assert_equal 'UTC', Teasy.default_zone
41
+ end
42
+
43
+ def test_with_zone_returns_block_result
39
44
  assert_equal 1, Teasy.with_zone('Europe/Berlin') { 1 }
40
45
  end
41
46
  end
@@ -313,6 +313,31 @@ class TimeWithZoneTest < Minitest::Test
313
313
  assert_equal [45, 30, 0, 2, 4, 2042, 3, 92, true, 'CEST'], berlin_to_a
314
314
  end
315
315
 
316
+ def test_to_time
317
+ assert_instance_of Time, @timestamptz.to_time
318
+ assert_equal 0, @timestamptz.to_time.utc_offset
319
+ assert @timestamptz.to_time.utc?
320
+ assert_equal @timestamptz, @timestamptz.to_time
321
+
322
+ assert_equal 7200, @timestamptz_berlin.to_time.utc_offset
323
+ assert_equal @timestamptz_berlin, @timestamptz_berlin.to_time
324
+ end
325
+
326
+ def test_to_datetime
327
+ assert_instance_of DateTime, @timestamptz.to_datetime
328
+ assert_equal 0, @timestamptz.to_datetime.offset
329
+ assert_equal @timestamptz, @timestamptz.to_datetime
330
+
331
+ assert_equal Rational(2, 24), @timestamptz_berlin.to_datetime.offset
332
+ assert_equal @timestamptz_berlin, @timestamptz_berlin.to_datetime
333
+ end
334
+
335
+ def test_to_date
336
+ assert_instance_of Date, @timestamptz.to_date
337
+ assert_equal @timestamptz.day, @timestamptz.to_date.day
338
+ assert_equal @timestamptz_berlin.day, @timestamptz_berlin.to_date.day
339
+ end
340
+
316
341
  def test_to_f
317
342
  assert_instance_of Float, @timestamptz.to_f
318
343
  assert_in_epsilon 2_280_011_445.000_001, @timestamptz.to_f
@@ -413,4 +438,29 @@ class TimeWithZoneTest < Minitest::Test
413
438
  assert_equal 'UTC', @timestamptz.zone
414
439
  assert_equal 'Europe/Berlin', @timestamptz_berlin.zone
415
440
  end
441
+
442
+ def test_httpdate
443
+ assert_equal 'Wed, 02 Apr 2042 00:30:45 GMT', @timestamptz.httpdate
444
+ assert_equal 'Tue, 01 Apr 2042 22:30:45 GMT', @timestamptz_berlin.httpdate
445
+ end
446
+
447
+ def test_rfc2822
448
+ assert_equal 'Wed, 02 Apr 2042 00:30:45 -0000', @timestamptz.rfc2822
449
+ assert_equal 'Wed, 02 Apr 2042 00:30:45 +0200', @timestamptz_berlin.rfc2822
450
+ end
451
+
452
+ def test_rfc822
453
+ assert_equal 'Wed, 02 Apr 2042 00:30:45 -0000', @timestamptz.rfc822
454
+ assert_equal 'Wed, 02 Apr 2042 00:30:45 +0200', @timestamptz_berlin.rfc822
455
+ end
456
+
457
+ def test_xmlschema
458
+ assert_equal '2042-04-02T00:30:45Z', @timestamptz.xmlschema
459
+ assert_equal '2042-04-02T00:30:45+02:00', @timestamptz_berlin.xmlschema
460
+ end
461
+
462
+ def test_iso8601
463
+ assert_equal '2042-04-02T00:30:45Z', @timestamptz.iso8601
464
+ assert_equal '2042-04-02T00:30:45+02:00', @timestamptz_berlin.iso8601
465
+ end
416
466
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teasy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kai Kuchenbecker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-21 00:00:00.000000000 Z
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tzinfo