Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 128960408b7c
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 8da4e66c5f2d
Choose a head ref
  • 4 commits
  • 4 files changed
  • 1 contributor

Commits on Nov 3, 2017

  1. Copy the full SHA
    36d6d58 View commit details
  2. Copy the full SHA
    d7fa123 View commit details
  3. Add spec for Time#to_datetime

    eregon committed Nov 3, 2017
    Copy the full SHA
    6c7ccbe View commit details
  4. Copy the full SHA
    8da4e66 View commit details
Showing with 47 additions and 8 deletions.
  1. +8 −7 lib/ruby/stdlib/date.rb
  2. +12 −0 spec/ruby/library/datetime/to_time_spec.rb
  3. +27 −0 spec/ruby/library/time/to_datetime_spec.rb
  4. +0 −1 spec/tags/ruby/library/time/to_date_tags.txt
15 changes: 8 additions & 7 deletions lib/ruby/stdlib/date.rb
Original file line number Diff line number Diff line change
@@ -1879,16 +1879,17 @@ def to_time
end

def to_date
Date.civil(year, mon, mday, Date::GREGORIAN)
jd = Date.__send__(:civil_to_jd, year, mon, mday, Date::GREGORIAN)
Date.new!(Date.__send__(:jd_to_ajd, jd, 0, 0), 0, Date::ITALY)
end

def to_datetime(sg = Date::ITALY, klass = DateTime)
def to_datetime
jd = DateTime.__send__(:civil_to_jd, year, mon, mday, DateTime::ITALY)
fr = DateTime.__send__(:time_to_day_fraction, hour, min, [sec, 59].min) +
Rational(subsec, 86400)
of = Rational(utc_offset, 86400)
s = [sec, 59].min
ms, sub_millis = nsec.divmod(1_000_000) # expects ns precision for Time
sub_millis = Rational(sub_millis, 1_000_000) if sub_millis != 0
dt = Date::JODA::DateTime.new(1000 * to_i + ms, Date.send(:chronology, sg, of))
klass.new!(dt, of, sg, sub_millis)
DateTime.new!(DateTime.__send__(:jd_to_ajd, jd, fr, of),
of, DateTime::ITALY)
end

end
12 changes: 12 additions & 0 deletions spec/ruby/library/datetime/to_time_spec.rb
Original file line number Diff line number Diff line change
@@ -6,6 +6,18 @@
DateTime.now.to_time.should be_kind_of(Time)
end

it "returns a Time representing the same instant" do
datetime = DateTime.civil(3, 12, 31, 23, 58, 59)
time = datetime.to_time.utc

time.year.should == 3
time.month.should == 12
time.day.should == 31
time.hour.should == 23
time.min.should == 58
time.sec.should == 59
end

ruby_version_is "2.4" do
it "preserves the same time regardless of local time or zone" do
date = DateTime.new(2012, 12, 24, 12, 23, 00, '+03:00')
27 changes: 27 additions & 0 deletions spec/ruby/library/time/to_datetime_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require File.expand_path('../../../spec_helper', __FILE__)
require 'time'

describe "Time#to_datetime" do
it "returns a DateTime representing the same instant" do
time = Time.utc(3, 12, 31, 23, 58, 59)
datetime = time.to_datetime
datetime.year.should == 3
datetime.month.should == 12
datetime.day.should == 31
datetime.hour.should == 23
datetime.min.should == 58
datetime.sec.should == 59
end

it "roundtrips" do
time = Time.utc(3, 12, 31, 23, 58, 59)
datetime = time.to_datetime
datetime.to_time.utc.should == time
end

it "yields a DateTime with the default Calendar reform day" do
Time.utc(1582, 10, 4, 1, 2, 3).to_datetime.start.should == Date::ITALY
Time.utc(1582, 10, 14, 1, 2, 3).to_datetime.start.should == Date::ITALY
Time.utc(1582, 10, 15, 1, 2, 3).to_datetime.start.should == Date::ITALY
end
end
1 change: 0 additions & 1 deletion spec/tags/ruby/library/time/to_date_tags.txt

This file was deleted.