Skip to content

Commit 334a7f0

Browse files
committedMar 12, 2018
add an explicit toString for RubyTime + cleanup some internals
1 parent cf89614 commit 334a7f0

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed
 

Diff for: ‎core/src/main/java/org/jruby/RubyTime.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ public void updateCal(DateTime dt) {
436436
this.dt = dt;
437437
}
438438

439-
protected long getTimeInMillis() {
439+
public long getTimeInMillis() {
440440
return dt.getMillis();
441441
}
442442

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

568-
/**
569-
* @see #strftime(ThreadContext, IRubyObject)
570-
*/
568+
@Deprecated
571569
public RubyString strftime(IRubyObject format) {
572570
return strftime(getRuntime().getCurrentContext(), format);
573571
}
@@ -581,12 +579,12 @@ public RubyString strftime(ThreadContext context, IRubyObject format) {
581579
@JRubyMethod(name = "==", required = 1)
582580
@Override
583581
public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
584-
if (other.isNil()) {
585-
return context.runtime.getFalse();
586-
}
587582
if (other instanceof RubyTime) {
588583
return context.runtime.newBoolean(cmp((RubyTime) other) == 0);
589584
}
585+
if (other == context.nil) {
586+
return context.runtime.getFalse();
587+
}
590588

591589
return RubyComparable.op_equal(context, this, other);
592590
}
@@ -773,40 +771,44 @@ public RubyString asctime() {
773771
@Override
774772
@JRubyMethod(name = {"to_s", "inspect"})
775773
public IRubyObject to_s() {
776-
return inspectCommon(TO_S_FORMATTER, TO_S_UTC_FORMATTER);
774+
final String str = inspectCommon(TO_S_FORMATTER, TO_S_UTC_FORMATTER);
775+
return RubyString.newString(getRuntime(), str, USASCIIEncoding.INSTANCE);
777776
}
778777

779778
public final IRubyObject to_s19() {
780779
return to_s();
781780
}
782781

783-
private RubyString inspectCommon(DateTimeFormatter formatter, DateTimeFormatter utcFormatter) {
782+
private String inspectCommon(final DateTimeFormatter formatter, final DateTimeFormatter utcFormatter) {
784783
DateTimeFormatter simpleDateFormat;
785784
if (dt.getZone() == DateTimeZone.UTC) {
786785
simpleDateFormat = utcFormatter;
787786
} else {
788787
simpleDateFormat = formatter;
789788
}
790789

791-
String result = simpleDateFormat.print(dt);
792-
793790
if (isTzRelative) {
794791
// display format needs to invert the UTC offset if this object was
795792
// created with a specific offset in the 7-arg form of #new
796793
DateTimeZone dtz = dt.getZone();
797794
int offset = dtz.toTimeZone().getOffset(dt.getMillis());
798795
DateTimeZone invertedDTZ = DateTimeZone.forOffsetMillis(offset);
799796
DateTime invertedDT = dt.withZone(invertedDTZ);
800-
result = simpleDateFormat.print(invertedDT);
797+
return simpleDateFormat.print(invertedDT);
801798
}
802799

803-
return RubyString.newString(getRuntime(), result, USASCIIEncoding.INSTANCE);
800+
return simpleDateFormat.print(dt);
801+
}
802+
803+
@Override
804+
public String toString() {
805+
return inspectCommon(TO_S_FORMATTER, TO_S_UTC_FORMATTER);
804806
}
805807

806808
@JRubyMethod
807809
@Override
808810
public RubyArray to_a() {
809-
return RubyArray.newArrayMayCopy(getRuntime(), sec(), min(), hour(), mday(), month(), year(), wday(), yday(), isdst(), zone());
811+
return RubyArray.newArrayNoCopy(getRuntime(), sec(), min(), hour(), mday(), month(), year(), wday(), yday(), isdst(), zone());
810812
}
811813

812814
@JRubyMethod

0 commit comments

Comments
 (0)