File: tc_ruby_time_timezone.rb

package info (click to toggle)
ruby-tzinfo 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,824 kB
  • sloc: ruby: 21,667; makefile: 6
file content (104 lines) | stat: -rw-r--r-- 3,164 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# encoding: UTF-8
# frozen_string_literal: true

require_relative 'test_utils'

class TCRubyTimeTimezone < Minitest::Test
  include TZInfo

  def test_new_time_with_time_zone_paris
    check_can_create_time_with_time_zone

    tz = Timezone.get('Europe/Paris')

    std_time = Time.new(2018, 12, 1, 0, 0, 0, tz)
    assert_same(tz, std_time.zone)
    assert_equal(3600, std_time.utc_offset)
    assert_equal(Time.utc(2018, 11, 30, 23, 0, 0), std_time.getutc)

    dst_time = Time.new(2018, 6, 1, 0, 0, 0, tz)
    assert_same(tz, dst_time.zone)
    assert_equal(7200, dst_time.utc_offset)
    assert_equal(Time.utc(2018, 5, 31, 22, 0, 0), dst_time.getutc)
  end

  def test_new_time_with_time_zone_new_york
    check_can_create_time_with_time_zone

    tz = Timezone.get('America/New_York')

    std_time = Time.new(2018, 12, 1, 0, 0, 0, tz)
    assert_same(tz, std_time.zone)
    assert_equal(-18000, std_time.utc_offset)
    assert_equal(Time.utc(2018, 12, 1, 5, 0, 0), std_time.getutc)

    dst_time = Time.new(2018, 6, 1, 0, 0, 0, tz)
    assert_same(tz, dst_time.zone)
    assert_equal(-14400, dst_time.utc_offset)
    assert_equal(Time.utc(2018, 6, 1, 4, 0, 0), dst_time.getutc)
  end

  def test_time_at_with_time_zone_paris
    check_can_create_time_with_time_zone

    tz = Timezone.get('Europe/Paris')

    std_time = Time.at(1543618800, in: tz)
    assert_same(tz, std_time.zone)
    assert_equal(3600, std_time.utc_offset)
    assert_equal(2018, std_time.year)
    assert_equal(12, std_time.month)
    assert_equal(1, std_time.day)
    assert_equal(0, std_time.hour)
    assert_equal(0, std_time.min)
    assert_equal(0, std_time.sec)

    dst_time = Time.at(1527804000, in: tz)
    assert_same(tz, dst_time.zone)
    assert_equal(7200, dst_time.utc_offset)
    assert_equal(2018, dst_time.year)
    assert_equal(6, dst_time.month)
    assert_equal(1, dst_time.day)
    assert_equal(0, dst_time.hour)
    assert_equal(0, dst_time.min)
    assert_equal(0, dst_time.sec)
  end

  def test_time_at_with_time_zone_new_york
    check_can_create_time_with_time_zone

    if RUBY_ENGINE == 'ruby' && RUBY_VERSION < '2.7' && [0].pack('J').bytesize <= 4
      skip('`Time.at(n, in: zone)` is broken with negative UTC offsets on 32-bit MRI Ruby 2.6')
    end

    tz = Timezone.get('America/New_York')

    std_time = Time.at(1543640400, in: tz)
    assert_same(tz, std_time.zone)
    assert_equal(-18000, std_time.utc_offset)
    assert_equal(2018, std_time.year)
    assert_equal(12, std_time.month)
    assert_equal(1, std_time.day)
    assert_equal(0, std_time.hour)
    assert_equal(0, std_time.min)
    assert_equal(0, std_time.sec)

    dst_time = Time.at(1527825600, in: tz)
    assert_same(tz, dst_time.zone)
    assert_equal(-14400, dst_time.utc_offset)
    assert_equal(2018, dst_time.year)
    assert_equal(6, dst_time.month)
    assert_equal(1, dst_time.day)
    assert_equal(0, dst_time.hour)
    assert_equal(0, dst_time.min)
    assert_equal(0, dst_time.sec)
  end

  private

  def check_can_create_time_with_time_zone
    unless RUBY_VERSION >= '2.6'
      skip("Cannot create Time with a time zone with Ruby #{RUBY_VERSION}")
    end
  end
end