Skip to content

Commit d1fbd0b

Browse files
committed
Fix "+00:00" and 0 being treated as UTC by Time#localtime and Time.at.
1 parent 696b78d commit d1fbd0b

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

core/src/main/java/org/jruby/RubyTime.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,15 +628,14 @@ public RubyTime localtime(ThreadContext context) {
628628

629629
@JRubyMethod(name = "localtime")
630630
public RubyTime localtime(ThreadContext context, IRubyObject arg) {
631-
final DateTimeZone zone = getTimeZoneFromUtcOffset(context, arg);
631+
setIsTzRelative(false);
632+
final DateTimeZone zone = handleUTCDateTimeZone(context, arg);
632633

633634
if (zone == null) {
634635
throw invalidUTCOffset(context);
635-
} else if (zone == DateTimeZone.UTC) {
636-
return gmtime(context);
637636
}
638637

639-
return adjustTimeZone(context, zone, true);
638+
return adjustTimeZone(context, zone, isTzRelative);
640639
}
641640

642641
private RubyTime adjustTimeZone(ThreadContext context, final DateTimeZone zone, boolean isTzRelative) {
@@ -2300,15 +2299,15 @@ public static RubyTime timeZoneLocal(ThreadContext context, IRubyObject off, Rub
23002299

23012300
if (zoneLocalTime(context, zone, time)) return time;
23022301

2303-
if ((dtz = getTimeZoneFromUtcOffset(context, off)) == null) {
2302+
time.setIsTzRelative(false);
2303+
2304+
if ((dtz = time.handleUTCDateTimeZone(context, off)) == null) {
23042305
zone = time.findTimezone(context, zone);
23052306
if (!zoneLocalTime(context, zone, time)) throw invalidUTCOffset(context, zone);
23062307
return time;
2307-
} else if (dtz == DateTimeZone.UTC) {
2308-
return time.gmtime(context);
23092308
}
23102309

2311-
time.adjustTimeZone(context, dtz, false);
2310+
time.adjustTimeZone(context, dtz, time.isTzRelative);
23122311

23132312
return time;
23142313
}

spec/ruby/core/time/utc_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@
4343

4444
it "does not treat time with +00:00 offset as UTC" do
4545
Time.new(2022, 1, 1, 0, 0, 0, "+00:00").utc?.should == false
46+
Time.now.localtime("+00:00").utc?.should == false
47+
Time.at(Time.now, in: "+00:00").utc?.should == false
4648
end
4749

4850
it "does not treat time with 0 offset as UTC" do
4951
Time.new(2022, 1, 1, 0, 0, 0, 0).utc?.should == false
52+
Time.now.localtime(0).utc?.should == false
53+
Time.at(Time.now, in: 0).utc?.should == false
5054
end
5155
end
5256

0 commit comments

Comments
 (0)