Skip to content

Commit

Permalink
fix failing frozen utc/local-time - shall return self when already in TZ
Browse files Browse the repository at this point in the history
... also deprecated 19 suffixed localtime impl version
kares committed Jun 26, 2017
1 parent 89605cb commit 0cf01b9
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions core/src/main/java/org/jruby/RubyTime.java
Original file line number Diff line number Diff line change
@@ -509,32 +509,42 @@ public RubyTime succ() {

@JRubyMethod(name = {"gmtime", "utc"})
public RubyTime gmtime() {
if (isFrozen()) {
throw getRuntime().newFrozenError("Time", true);
}
dt = dt.withZone(DateTimeZone.UTC);
return this;
return adjustTimeZone(getRuntime(), DateTimeZone.UTC);
}

public final RubyTime localtime() {
return localtime(getRuntime().getCurrentContext());
}

@JRubyMethod(name = "localtime")
public RubyTime localtime(ThreadContext context) {
return adjustTimeZone(context.runtime, getLocalTimeZone(context.runtime));
}

public RubyTime localtime() {
return localtime19(getRuntime().getCurrentContext(), NULL_ARRAY);
@JRubyMethod(name = "localtime")
public RubyTime localtime(ThreadContext context, IRubyObject arg) {
final DateTimeZone zone = getTimeZoneFromUtcOffset(context.runtime, arg);
return adjustTimeZone(context.runtime, zone);
}

@JRubyMethod(name = "localtime", optional = 1)
public RubyTime localtime19(ThreadContext context, IRubyObject[] args) {
private RubyTime adjustTimeZone(Ruby runtime, final DateTimeZone zone) {
if (zone.equals(dt.getZone())) return this;
if (isFrozen()) {
throw getRuntime().newFrozenError("Time", true);
throw runtime.newFrozenError("Time", true);
}
DateTimeZone newDtz;
if (args.length == 0) {
newDtz = getLocalTimeZone(context.runtime);
} else {
newDtz = getTimeZoneFromUtcOffset(context.runtime, args[0]);
}
dt = dt.withZone(newDtz);
dt = dt.withZone(zone);
return this;
}

@Deprecated
public final RubyTime localtime19(ThreadContext context, IRubyObject[] args) {
switch(args.length) {
case 0: return localtime(context);
case 1: return localtime(context, args[0]);
}
throw new IllegalArgumentException(java.util.Arrays.toString(args));
}

@JRubyMethod(name = {"gmt?", "utc?", "gmtime?"})
public RubyBoolean gmt() {
return getRuntime().newBoolean(dt.getZone().getID().equals("UTC"));
@@ -733,7 +743,7 @@ public IRubyObject op_eqq(ThreadContext context, IRubyObject other) {
return context.runtime.newBoolean(RubyNumeric.fix2int(invokedynamic(context, this, OP_CMP, other)) == 0);
}

return context.getRuntime().getFalse();
return context.runtime.getFalse();
}

@JRubyMethod(name = "<=>", required = 1)

0 comments on commit 0cf01b9

Please sign in to comment.