summaryrefslogtreecommitdiff
path: root/spec/ruby/core/time/zone_spec.rb
blob: 63c92602d1e24807dbb99a7777cf2905717d66c1 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
require_relative '../../spec_helper'

describe "Time#zone" do
  platform_is_not :windows do
    it "returns the time zone used for time" do
      with_timezone("America/New_York") do
        Time.new(2001, 1, 1, 0, 0, 0).zone.should == "EST"
        Time.new(2001, 7, 1, 0, 0, 0).zone.should == "EDT"
        %w[EST EDT].should include Time.now.zone
      end
    end
  end

  it "returns nil for a Time with a fixed offset" do
    Time.new(2001, 1, 1, 0, 0, 0, "+05:00").zone.should == nil
  end

  platform_is_not :windows do
    it "returns the correct timezone for a local time" do
      t = Time.new(2005, 2, 27, 22, 50, 0, -3600)

      with_timezone("America/New_York") do
        t.getlocal.zone.should == "EST"
      end
    end
  end

  it "returns nil when getting the local time with a fixed offset" do
    t = Time.new(2005, 2, 27, 22, 50, 0, -3600)

    with_timezone("America/New_York") do
      t.getlocal("+05:00").zone.should be_nil
    end
  end

  describe "Encoding.default_internal is set" do
    before :each do
      @encoding = Encoding.default_internal
      Encoding.default_internal = Encoding::UTF_8
    end

    after :each do
      Encoding.default_internal = @encoding
    end

    it "returns an ASCII string" do
      t = Time.new(2005, 2, 27, 22, 50, 0, -3600)

      with_timezone("America/New_York") do
        t.getlocal.zone.encoding.should == Encoding::US_ASCII
      end
    end

    it "doesn't raise errors for a Time with a fixed offset" do
      Time.new(2001, 1, 1, 0, 0, 0, "+05:00").zone.should == nil
    end
  end

  it "returns UTC when called on a UTC time" do
    Time.now.utc.zone.should == "UTC"
    Time.now.gmtime.zone.should == "UTC"
    Time.now.getgm.zone.should == "UTC"
    Time.now.getutc.zone.should == "UTC"
    Time.utc(2022).zone.should == "UTC"
    Time.new(2022, 1, 1, 0, 0, 0, "UTC").zone.should == "UTC"
    Time.new(2022, 1, 1, 0, 0, 0, "Z").zone.should == "UTC"
    Time.now.localtime("UTC").zone.should == "UTC"
    Time.now.localtime("Z").zone.should == "UTC"
    Time.at(Time.now, in: 'UTC').zone.should == "UTC"
    Time.at(Time.now, in: 'Z').zone.should == "UTC"

    ruby_version_is "3.1" do
      Time.new(2022, 1, 1, 0, 0, 0, "-00:00").zone.should == "UTC"
      Time.now.localtime("-00:00").zone.should == "UTC"
      Time.at(Time.now, in: '-00:00').zone.should == "UTC"
    end

    ruby_version_is "3.1" do
      Time.new(2022, 1, 1, 0, 0, 0, in: "UTC").zone.should == "UTC"
      Time.new(2022, 1, 1, 0, 0, 0, in: "Z").zone.should == "UTC"

      Time.now(in: 'UTC').zone.should == "UTC"
      Time.now(in: 'Z').zone.should == "UTC"

      Time.at(Time.now, in: 'UTC').zone.should == "UTC"
      Time.at(Time.now, in: 'Z').zone.should == "UTC"
    end
  end

  platform_is_not :aix, :windows do
    it "defaults to UTC when bad zones given" do
      with_timezone("hello-foo") do
        Time.now.utc_offset.should == 0
      end
      with_timezone("1,2") do
        Time.now.utc_offset.should == 0
      end
      with_timezone("Sun,Fri,2") do
        Time.now.utc_offset.should == 0
      end
    end
  end

  platform_is :windows do
    # See https://bugs.ruby-lang.org/issues/13591#note-11
    it "defaults to UTC when bad zones given" do
      with_timezone("1,2") do
        Time.now.utc_offset.should == 0
      end
      with_timezone("12") do
        Time.now.utc_offset.should == 0
      end
    end
  end
end