Skip to content

Commit

Permalink
add an explicit toString for RubyTime + cleanup some internals
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Mar 12, 2018
1 parent cf89614 commit 334a7f0
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions core/src/main/java/org/jruby/RubyTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public void updateCal(DateTime dt) {
this.dt = dt;
}

protected long getTimeInMillis() {
public long getTimeInMillis() {
return dt.getMillis();
}

Expand Down Expand Up @@ -565,9 +565,7 @@ public RubyTime getlocal19(ThreadContext context, IRubyObject[] args) {
throw new AssertionError(java.util.Arrays.toString(args));
}

/**
* @see #strftime(ThreadContext, IRubyObject)
*/
@Deprecated
public RubyString strftime(IRubyObject format) {
return strftime(getRuntime().getCurrentContext(), format);
}
Expand All @@ -581,12 +579,12 @@ public RubyString strftime(ThreadContext context, IRubyObject format) {
@JRubyMethod(name = "==", required = 1)
@Override
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
if (other.isNil()) {
return context.runtime.getFalse();
}
if (other instanceof RubyTime) {
return context.runtime.newBoolean(cmp((RubyTime) other) == 0);
}
if (other == context.nil) {
return context.runtime.getFalse();
}

return RubyComparable.op_equal(context, this, other);
}
Expand Down Expand Up @@ -773,40 +771,44 @@ public RubyString asctime() {
@Override
@JRubyMethod(name = {"to_s", "inspect"})
public IRubyObject to_s() {
return inspectCommon(TO_S_FORMATTER, TO_S_UTC_FORMATTER);
final String str = inspectCommon(TO_S_FORMATTER, TO_S_UTC_FORMATTER);
return RubyString.newString(getRuntime(), str, USASCIIEncoding.INSTANCE);
}

public final IRubyObject to_s19() {
return to_s();
}

private RubyString inspectCommon(DateTimeFormatter formatter, DateTimeFormatter utcFormatter) {
private String inspectCommon(final DateTimeFormatter formatter, final DateTimeFormatter utcFormatter) {
DateTimeFormatter simpleDateFormat;
if (dt.getZone() == DateTimeZone.UTC) {
simpleDateFormat = utcFormatter;
} else {
simpleDateFormat = formatter;
}

String result = simpleDateFormat.print(dt);

if (isTzRelative) {
// display format needs to invert the UTC offset if this object was
// created with a specific offset in the 7-arg form of #new
DateTimeZone dtz = dt.getZone();
int offset = dtz.toTimeZone().getOffset(dt.getMillis());
DateTimeZone invertedDTZ = DateTimeZone.forOffsetMillis(offset);
DateTime invertedDT = dt.withZone(invertedDTZ);
result = simpleDateFormat.print(invertedDT);
return simpleDateFormat.print(invertedDT);
}

return RubyString.newString(getRuntime(), result, USASCIIEncoding.INSTANCE);
return simpleDateFormat.print(dt);
}

@Override
public String toString() {
return inspectCommon(TO_S_FORMATTER, TO_S_UTC_FORMATTER);
}

@JRubyMethod
@Override
public RubyArray to_a() {
return RubyArray.newArrayMayCopy(getRuntime(), sec(), min(), hour(), mday(), month(), year(), wday(), yday(), isdst(), zone());
return RubyArray.newArrayNoCopy(getRuntime(), sec(), min(), hour(), mday(), month(), year(), wday(), yday(), isdst(), zone());
}

@JRubyMethod
Expand Down

0 comments on commit 334a7f0

Please sign in to comment.