aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/time/_load_spec.rb
blob: 12fcb219ed0304b228bfaa19a0e839ea737aa94f (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
# -*- encoding: binary -*-
require File.expand_path('../../../spec_helper', __FILE__)

describe "Time._load" do
  it "is a private method" do
    Time.should have_private_method(:_load, false)
  end

  # http://redmine.ruby-lang.org/issues/show/627
  it "loads a time object in the new format" do
    t = Time.local(2000, 1, 15, 20, 1, 1)
    t = t.gmtime

    high =               1 << 31 |
          (t.gmt? ? 1 : 0) << 30 |
           (t.year - 1900) << 14 |
              (t.mon  - 1) << 10 |
                     t.mday << 5 |
                          t.hour

    low =  t.min  << 26 |
           t.sec  << 20 |
                 t.usec

    Time.send(:_load, [high, low].pack("VV")).should == t
  end

  it "loads a time object in the old UNIX timestamp based format" do
    t = Time.local(2000, 1, 15, 20, 1, 1, 203)
    timestamp = t.to_i

    high = timestamp & ((1 << 31) - 1)

    low =  t.usec

    Time.send(:_load, [high, low].pack("VV")).should == t
  end

  it "loads MRI's marshaled time format" do
    t = Marshal.load("\004\bu:\tTime\r\320\246\e\200\320\001\r\347")
    t.utc

    t.to_s.should == "2010-10-22 16:57:48 UTC"
  end

  with_feature :encoding do
    it "treats the data as binary data" do
      data = "\x04\bu:\tTime\r\fM\x1C\xC0\x00\x00\xD0\xBE"
      data.force_encoding Encoding::UTF_8
      t = Marshal.load(data)
      t.to_s.should == "2013-04-08 12:47:45 UTC"
    end
  end
end