Skip to content

Commit

Permalink
fix incorrect/missing offset and calculate nsec w DateTime#to_time
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Mar 12, 2018
1 parent a28a727 commit f30fdfe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
15 changes: 11 additions & 4 deletions core/src/main/java/org/jruby/ext/date/RubyDateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,18 @@ public static RubyDateTime now(ThreadContext context, IRubyObject self) { // sg=
if (zone == DateTimeZone.UTC) {
return new RubyDateTime(context.runtime, (RubyClass) self, new DateTime(CHRONO_ITALY_UTC));
}
return new RubyDateTime(context.runtime, (RubyClass) self, new DateTime(GJChronology.getInstance(zone)));
final DateTime dt = new DateTime(GJChronology.getInstance(zone));
final int off = zone.getOffset(dt.getMillis()) / 1000;
return new RubyDateTime(context.runtime, (RubyClass) self, dt, off, ITALY);
}

@JRubyMethod(meta = true)
public static RubyDateTime now(ThreadContext context, IRubyObject self, IRubyObject sg) {
final int start = val2sg(context, sg);
final DateTimeZone zone = RubyTime.getLocalTimeZone(context.runtime);
return new RubyDateTime(context.runtime, (RubyClass) self, new DateTime(getChronology(context, start, zone)), 0, start);
final DateTime dt = new DateTime(getChronology(context, start, zone));
final int off = zone.getOffset(dt.getMillis()) / 1000;
return new RubyDateTime(context.runtime, (RubyClass) self, dt, off, start);
}

@Override
Expand Down Expand Up @@ -407,8 +411,11 @@ public RubyDate to_date(ThreadContext context) {
public RubyTime to_time(ThreadContext context) {
final Ruby runtime = context.runtime;
RubyTime time = new RubyTime(runtime, runtime.getTime(), dt);
// sec_fraction: Rational(context, dt.getMillisOfSecond() + (long) subMillis, 1000);
time.setUSec(0);
if (subMillisNum != 0) {
RubyNumeric usec = (RubyNumeric)
subMillis(runtime).op_mul(context, RubyFixnum.newFixnum(runtime, 1_000_000));
time.setNSec(usec.getLongValue());
}
return time;
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/ext/date/TimeExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static void load(Ruby runtime) {
public static RubyDate to_date(ThreadContext context, IRubyObject self) {
final DateTime dt = ((RubyTime) self).getDateTime();
long jd = civil_to_jd(dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(), GREGORIAN);
return new RubyDate(context, getDate(context.runtime), jd_to_ajd(context, jd), CHRONO_ITALY_UTC, ITALY);
return new RubyDate(context, getDate(context.runtime), jd_to_ajd(context, jd), CHRONO_ITALY_UTC, 0);
}

@JRubyMethod(name = "to_datetime")
Expand All @@ -77,7 +77,7 @@ public static RubyDateTime to_datetime(ThreadContext context, IRubyObject self)

final int off = dt.getZone().getOffset(dt.getMillis()) / 1000;
final Chronology chronology = getChronology(context, ITALY, off);
return new RubyDateTime(context, getDateTime(context.runtime), jd_to_ajd(context, jd, fr, off), chronology, ITALY);
return new RubyDateTime(context, getDateTime(context.runtime), jd_to_ajd(context, jd, fr, off), chronology, off);
}

}
53 changes: 48 additions & 5 deletions test/jruby/test_date.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,24 @@ def test_prev_next
d = DateTime.new(2000, 3, 1).prev_day(2)
assert_equal [2000, 2, 28, 00, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

#d = DateTime.new(2000,3,1).prev_day(1.to_r/2)
#assert_equal [2000, 2, 29, 12, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]
d = DateTime.new(2000,3,1).prev_day(1.to_r/2)
assert_equal [2000, 2, 29, 12, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

d = DateTime.new(2000,3,1).prev_day(3.to_r/5)
assert_equal [2000, 2, 29, 9, 36, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

d = DateTime.new(2000,3,1).next_day(0.5)
assert_equal [2000, 3, 1, 12, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

d = DateTime.new(2000,3,1).next_day(0.4444)
assert_equal [2000, 3, 1, 10, 39, 56], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

d = DateTime.new(2000,3,1).next_day(0.55555)
# NOTE: likely a (minor) rounding issue - JRuby gets time: 13:20:00
#assert_equal [2000, 3, 1, 13, 19, 59], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

d = DateTime.new(2000,3,1).next_day(-0.1)
assert_equal [2000, 2, 29, 21, 36, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

d = Date.new(2000,3,1).prev_day(1.to_r/2)
assert_equal [2000, 2, 29], [d.year, d.mon, d.mday]
Expand All @@ -303,9 +319,6 @@ def test_prev_next
d = Date.new(2000,3,1).prev_day(Rational(0, 1))
assert_equal [2000, 3, 1], [d.year, d.mon, d.mday]

#d = DateTime.new(2000,3,1).next_day(0.5)
#assert_equal [2000, 3, 1, 12, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec]

d = Date.new(2000,3,1).next_day(0.7)
assert_equal [2000, 3, 1], [d.year, d.mon, d.mday]

Expand Down Expand Up @@ -460,4 +473,34 @@ def test_now_local
assert time.to_s.end_with?(zone), "invalid zone for: #{time.to_s} (expected '#{zone}')"
end

def test_time_conv
today = Date.today
assert_equal today.to_s, Date.today.to_time.strftime('%F')
assert_equal today, Date.today.to_time.to_date

time = DateTime.now
#assert_equal nil, time.to_time.zone
assert_equal time.to_s, time.to_time.strftime('%FT%T%:z')
assert_equal time, time.to_time.to_datetime

time = Time.now
assert_equal time.nsec.to_r / 1_000_000_000, time.to_datetime.sec_fraction

time2 = time.to_time.to_datetime.to_time
assert_equal time.nsec, time2.nsec
assert_equal time.usec, time2.usec
assert_equal time, time2

time = Time.new(2018, 2, 25, 12, 21, 33 + Rational(999_999_999, 1_000_000_000), '+10:30')
assert_equal time.nsec.to_r / 1_000_000_000, time.to_datetime.sec_fraction

assert_equal '+10:30', time.to_datetime.zone
assert_equal time.to_s, time.to_datetime.strftime('%F %T %z')

time2 = time.to_time.to_datetime.to_time
assert_equal time.nsec, time2.nsec
assert_equal time.usec, time2.usec
assert_equal time, time2
end

end

0 comments on commit f30fdfe

Please sign in to comment.