Skip to content

Commit 159cee8

Browse files
committed
[GR-16589] Implement date and time related messages for RubyTime
PullRequest: truffleruby/2198
2 parents 5fd2b6b + 38214b3 commit 159cee8

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Compatibility:
2525
* Add warning for `proc` without block (#2004, @ssnickolay).
2626
* Implemented `FrozenError#receiver`.
2727
* `Proc#<<` and `Proc#>>` raises TypeError if passed not callable object (#2004, @ssnickolay).
28+
* Support time and date related messages for `Time` (#2166).
2829

2930
Performance:
3031

spec/truffle/interop/time_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (c) 2016, 2019 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 2.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/spec_helper'
10+
11+
describe "Ruby Time instances" do
12+
it "respond to interop date and time-related messages" do
13+
time = Time.now
14+
Truffle::Interop.should.date?(time)
15+
Truffle::Interop.should.time?(time)
16+
Truffle::Interop.should.time_zone?(time)
17+
Truffle::Interop.should.instant?(time)
18+
19+
Truffle::Interop.should_not.duration?(time)
20+
end
21+
end

src/main/java/org/truffleruby/core/time/RubyTime.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@
99
*/
1010
package org.truffleruby.core.time;
1111

12+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
13+
import com.oracle.truffle.api.interop.InteropLibrary;
14+
import com.oracle.truffle.api.library.ExportLibrary;
15+
import com.oracle.truffle.api.library.ExportMessage;
1216
import com.oracle.truffle.api.object.Shape;
1317
import org.truffleruby.core.klass.RubyClass;
1418
import org.truffleruby.core.string.RubyString;
1519
import org.truffleruby.language.Nil;
1620
import org.truffleruby.language.RubyDynamicObject;
1721

22+
import java.time.Instant;
23+
import java.time.LocalDate;
24+
import java.time.LocalTime;
25+
import java.time.ZoneId;
1826
import java.time.ZonedDateTime;
1927

28+
@ExportLibrary(InteropLibrary.class)
2029
public class RubyTime extends RubyDynamicObject {
2130

2231
ZonedDateTime dateTime;
@@ -42,4 +51,45 @@ public RubyTime(
4251
this.isUtc = isUtc;
4352
}
4453

54+
// region Date and Time messages
55+
@TruffleBoundary
56+
@ExportMessage
57+
public Instant asInstant() {
58+
return dateTime.toInstant();
59+
}
60+
61+
@ExportMessage
62+
public boolean isDate() {
63+
return true;
64+
}
65+
66+
@TruffleBoundary
67+
@ExportMessage
68+
public LocalDate asDate() {
69+
return dateTime.toLocalDate();
70+
}
71+
72+
@ExportMessage
73+
public boolean isTime() {
74+
return true;
75+
}
76+
77+
@TruffleBoundary
78+
@ExportMessage
79+
public LocalTime asTime() {
80+
return dateTime.toLocalTime();
81+
}
82+
83+
@ExportMessage
84+
public boolean isTimeZone() {
85+
return true;
86+
}
87+
88+
@TruffleBoundary
89+
@ExportMessage
90+
public ZoneId asTimeZone() {
91+
return dateTime.getZone();
92+
}
93+
// endregion
94+
4595
}

src/tck/java/org/truffleruby/tck/RubyTCKLanguageProvider.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
import static org.graalvm.polyglot.tck.TypeDescriptor.ANY;
1313
import static org.graalvm.polyglot.tck.TypeDescriptor.ARRAY;
1414
import static org.graalvm.polyglot.tck.TypeDescriptor.BOOLEAN;
15+
import static org.graalvm.polyglot.tck.TypeDescriptor.DATE;
1516
import static org.graalvm.polyglot.tck.TypeDescriptor.NULL;
1617
import static org.graalvm.polyglot.tck.TypeDescriptor.NUMBER;
1718
import static org.graalvm.polyglot.tck.TypeDescriptor.OBJECT;
1819
import static org.graalvm.polyglot.tck.TypeDescriptor.STRING;
20+
import static org.graalvm.polyglot.tck.TypeDescriptor.TIME;
21+
import static org.graalvm.polyglot.tck.TypeDescriptor.TIME_ZONE;
1922
import static org.graalvm.polyglot.tck.TypeDescriptor.array;
2023
import static org.graalvm.polyglot.tck.TypeDescriptor.executable;
2124
import static org.graalvm.polyglot.tck.TypeDescriptor.intersection;
@@ -76,6 +79,8 @@ public Collection<? extends Snippet> createValueConstructors(Context context) {
7679
vals.add(createValueConstructor(context, "Complex(1, 2)", OBJECT));
7780
vals.add(createValueConstructor(context, "'test'", STRING));
7881
vals.add(createValueConstructor(context, "'0123456789' + '0123456789'", STRING));
82+
vals.add(createValueConstructor(context, "Time.now", DATE_TIME_ZONE));
83+
7984
vals.add(createValueConstructor(context, "[1, 2]", NUMBER_ARRAY_OBJECT));
8085
vals.add(createValueConstructor(context, "[1.2, 3.4]", NUMBER_ARRAY_OBJECT));
8186
vals.add(createValueConstructor(context, "[1<<33, 1<<34]", NUMBER_ARRAY_OBJECT));
@@ -237,6 +242,7 @@ private Source getSource(String path) {
237242
}
238243
}
239244

245+
private static final TypeDescriptor DATE_TIME_ZONE = intersection(DATE, TIME, TIME_ZONE);
240246
private static final TypeDescriptor ARRAY_OBJECT = intersection(OBJECT, ARRAY);
241247
private static final TypeDescriptor NUMBER_ARRAY_OBJECT = intersection(OBJECT, array(NUMBER));
242248

0 commit comments

Comments
 (0)