Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,17 @@ def test_total_seconds(self):

class DateTimeTest(unittest.TestCase):

def test_utcfromtimestamp_index(self):
class Index:
def __index__(self):
return 86400

with self.assertWarns(DeprecationWarning):
self.assertEqual(datetime.datetime.utcfromtimestamp(Index()), datetime.datetime(1970, 1, 2))
with self.assertWarns(DeprecationWarning):
with self.assertRaises(TypeError):
datetime.datetime.utcfromtimestamp("1")

def test_strptime(self):
# generic case
d = datetime.datetime.strptime('2014 7 2 6 14 0 742 +0700', '%Y %m %d %H %M %S %f %z')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import static com.oracle.graal.python.nodes.BuiltinNames.T_MAX;
import static com.oracle.graal.python.nodes.BuiltinNames.T_MIN;
import static com.oracle.graal.python.nodes.BuiltinNames.T_RESOLUTION;
import static com.oracle.graal.python.nodes.ErrorMessages.OBJ_CANNOT_BE_INTERPRETED_AS_INTEGER;
import static com.oracle.graal.python.nodes.ErrorMessages.WARN_DEPRECATED_UTCFROMTIMESTAMP;
import static com.oracle.graal.python.nodes.ErrorMessages.WARN_DEPRECATED_UTCNOW;
import static com.oracle.graal.python.nodes.SpecialMethodNames.J___REDUCE_EX__;
Expand Down Expand Up @@ -128,8 +127,6 @@
import com.oracle.graal.python.nodes.object.GetClassNode;
import com.oracle.graal.python.nodes.object.IsForeignObjectNode;
import com.oracle.graal.python.nodes.util.CannotCastException;
import com.oracle.graal.python.nodes.util.CastToJavaDoubleNode;
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
import com.oracle.graal.python.runtime.ExecutionContext;
Expand Down Expand Up @@ -1078,14 +1075,12 @@ public abstract static class UtcFromTimestampNode extends PythonBinaryBuiltinNod
static Object fromTimestamp(VirtualFrame frame, Object cls, Object timestampObject,
@Bind Node inliningTarget,
@Cached WarnNode warnNode,
@Cached CastToJavaLongExactNode castToLongNode,
@Cached CastToJavaDoubleNode castToDoubleNode,
@Cached("createFor($node)") IndirectCallData.BoundaryCallData boundaryCallData) {
warnNode.warnFormat(frame, DeprecationWarning, WARN_DEPRECATED_UTCFROMTIMESTAMP);

Object saved = ExecutionContext.BoundaryCallContext.enter(frame, boundaryCallData);
try {
return buildDateTimeInUtcFromTimestamp(cls, timestampObject, inliningTarget, castToLongNode, castToDoubleNode);
return buildDateTimeInUtcFromTimestamp(cls, timestampObject, inliningTarget);
} finally {
// A Python method call (using DateTimeNodes.SubclassNewNode) should be
// connected to a current node.
Expand All @@ -1094,21 +1089,17 @@ static Object fromTimestamp(VirtualFrame frame, Object cls, Object timestampObje
}

@TruffleBoundary
private static Object buildDateTimeInUtcFromTimestamp(Object cls, Object timestampObject, Node inliningTarget, CastToJavaLongExactNode castToLongNode, CastToJavaDoubleNode castToDoubleNode) {
long seconds, microseconds;

try {
long timestamp = castToLongNode.execute(inliningTarget, timestampObject);
seconds = timestamp;
private static Object buildDateTimeInUtcFromTimestamp(Object cls, Object timestampObject, Node inliningTarget) {
// CPython: _PyTime_ObjectToTimeval, as in fromtimestamp
final long seconds;
final long microseconds;
if (PyFloatCheckNode.executeUncached(timestampObject)) {
double timestamp = PyFloatAsDoubleNode.executeUncached(timestampObject);
seconds = (long) timestamp;
microseconds = (long) Math.rint((timestamp - (long) timestamp) * 1_000_000);
} else {
seconds = PyLongAsLongNode.executeUncached(timestampObject);
microseconds = 0;
} catch (CannotCastException e) {
try {
double timestamp = castToDoubleNode.execute(inliningTarget, timestampObject);
seconds = (long) timestamp;
microseconds = (long) Math.rint((timestamp - (long) timestamp) * 1_000_000);
} catch (CannotCastException ex) {
throw PRaiseNode.raiseStatic(inliningTarget, TypeError, OBJ_CANNOT_BE_INTERPRETED_AS_INTEGER);
}
}

LocalDateTime epoch = LocalDateTime.of(1970, 1, 1, 0, 0, 0, 0);
Expand Down