|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.objects.foreign; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___REDUCE__; |
| 44 | +import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING; |
| 45 | + |
| 46 | +import java.time.LocalDateTime; |
| 47 | +import java.time.ZoneId; |
| 48 | +import java.time.ZonedDateTime; |
| 49 | +import java.time.format.DateTimeFormatter; |
| 50 | +import java.util.List; |
| 51 | +import java.util.Locale; |
| 52 | + |
| 53 | +import com.oracle.graal.python.annotations.Builtin; |
| 54 | +import com.oracle.graal.python.annotations.Slot; |
| 55 | +import com.oracle.graal.python.annotations.Slot.SlotKind; |
| 56 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 57 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 58 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 59 | +import com.oracle.graal.python.builtins.modules.datetime.DateTimeNodes; |
| 60 | +import com.oracle.graal.python.builtins.modules.datetime.DatetimeModuleBuiltins; |
| 61 | +import com.oracle.graal.python.builtins.modules.datetime.PDateTime; |
| 62 | +import com.oracle.graal.python.builtins.modules.datetime.TemporalNodes; |
| 63 | +import com.oracle.graal.python.builtins.modules.datetime.TimeDeltaNodes; |
| 64 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 65 | +import com.oracle.graal.python.builtins.objects.type.TpSlots; |
| 66 | +import com.oracle.graal.python.nodes.ErrorMessages; |
| 67 | +import com.oracle.graal.python.nodes.PRaiseNode; |
| 68 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 69 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 70 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 71 | +import com.oracle.graal.python.nodes.object.GetClassNode; |
| 72 | +import com.oracle.truffle.api.CompilerDirectives; |
| 73 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 74 | +import com.oracle.truffle.api.dsl.Bind; |
| 75 | +import com.oracle.truffle.api.dsl.Cached; |
| 76 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 77 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 78 | +import com.oracle.truffle.api.dsl.Specialization; |
| 79 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 80 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 81 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 82 | +import com.oracle.truffle.api.nodes.Node; |
| 83 | +import com.oracle.truffle.api.strings.TruffleString; |
| 84 | + |
| 85 | +@CoreFunctions(extendClasses = PythonBuiltinClassType.ForeignTimeZone) |
| 86 | +public final class ForeignTimeZoneBuiltins extends PythonBuiltins { |
| 87 | + public static final TpSlots SLOTS = ForeignTimeZoneBuiltinsSlotsGen.SLOTS; |
| 88 | + |
| 89 | + @Override |
| 90 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 91 | + return ForeignTimeZoneBuiltinsFactory.getFactories(); |
| 92 | + } |
| 93 | + |
| 94 | + @Slot(value = SlotKind.tp_repr, isComplex = true) |
| 95 | + @GenerateNodeFactory |
| 96 | + abstract static class ReprNode extends PythonUnaryBuiltinNode { |
| 97 | + @Specialization(limit = "1") |
| 98 | + @TruffleBoundary |
| 99 | + static TruffleString repr(Object self, |
| 100 | + @Bind Node inliningTarget, |
| 101 | + @CachedLibrary("self") InteropLibrary interop) { |
| 102 | + ZoneId zoneId = asZoneId(self, interop); |
| 103 | + TruffleString typeName = com.oracle.graal.python.builtins.objects.type.TypeNodes.GetTpNameNode.executeUncached(GetClassNode.executeUncached(self)); |
| 104 | + String value = String.format("%s('%s')", typeName, zoneId.getId()); |
| 105 | + return TruffleString.FromJavaStringNode.getUncached().execute(value, TS_ENCODING); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Slot(value = SlotKind.tp_str, isComplex = true) |
| 110 | + @GenerateNodeFactory |
| 111 | + abstract static class StrNode extends PythonUnaryBuiltinNode { |
| 112 | + @Specialization(limit = "1") |
| 113 | + static TruffleString str(Object self, |
| 114 | + @CachedLibrary("self") InteropLibrary interop) { |
| 115 | + return TruffleString.FromJavaStringNode.getUncached().execute(asZoneId(self, interop).getId(), TS_ENCODING); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Builtin(name = "utcoffset", minNumOfPositionalArgs = 1, parameterNames = {"$self", "dt"}) |
| 120 | + @GenerateNodeFactory |
| 121 | + abstract static class UtcOffsetNode extends PythonBinaryBuiltinNode { |
| 122 | + @Specialization(limit = "1") |
| 123 | + @TruffleBoundary |
| 124 | + static Object utcoffset(Object self, Object dateTime, |
| 125 | + @Bind Node inliningTarget, |
| 126 | + @CachedLibrary("self") InteropLibrary interop, |
| 127 | + @Cached TemporalNodes.DateTimeLikeCheckNode dateTimeLikeCheckNode, |
| 128 | + @Cached TemporalNodes.ReadDateTimeValueNode readDateTimeValueNode) { |
| 129 | + ZoneId zoneId = asZoneId(self, interop); |
| 130 | + if (dateTime == PNone.NONE) { |
| 131 | + Object fixed = TemporalNodes.toFixedOffsetTimeZone(zoneId, inliningTarget); |
| 132 | + if (fixed == null) { |
| 133 | + return PNone.NONE; |
| 134 | + } |
| 135 | + return DatetimeModuleBuiltins.callUtcOffset(fixed, PNone.NONE, inliningTarget); |
| 136 | + } |
| 137 | + if (!dateTimeLikeCheckNode.execute(inliningTarget, dateTime)) { |
| 138 | + throw PRaiseNode.raiseStatic(inliningTarget, PythonBuiltinClassType.TypeError, ErrorMessages.FROMUTC_ARGUMENT_MUST_BE_A_DATETIME); |
| 139 | + } |
| 140 | + LocalDateTime localDateTime = readDateTimeValueNode.execute(inliningTarget, dateTime).toLocalDateTime(); |
| 141 | + ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); |
| 142 | + return TimeDeltaNodes.NewNode.getUncached().execute(inliningTarget, PythonBuiltinClassType.PTimeDelta, 0, zonedDateTime.getOffset().getTotalSeconds(), 0, 0, 0, 0, 0); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Builtin(name = "dst", minNumOfPositionalArgs = 1, parameterNames = {"$self", "dt"}) |
| 147 | + @GenerateNodeFactory |
| 148 | + abstract static class DstNode extends PythonBinaryBuiltinNode { |
| 149 | + @Specialization(limit = "1") |
| 150 | + @TruffleBoundary |
| 151 | + static Object dst(Object self, Object dateTime, |
| 152 | + @Bind Node inliningTarget, |
| 153 | + @CachedLibrary("self") InteropLibrary interop, |
| 154 | + @Cached TemporalNodes.DateTimeLikeCheckNode dateTimeLikeCheckNode, |
| 155 | + @Cached TemporalNodes.ReadDateTimeValueNode readDateTimeValueNode) { |
| 156 | + ZoneId zoneId = asZoneId(self, interop); |
| 157 | + if (dateTime == PNone.NONE) { |
| 158 | + return PNone.NONE; |
| 159 | + } |
| 160 | + if (!dateTimeLikeCheckNode.execute(inliningTarget, dateTime)) { |
| 161 | + throw PRaiseNode.raiseStatic(inliningTarget, PythonBuiltinClassType.TypeError, ErrorMessages.FROMUTC_ARGUMENT_MUST_BE_A_DATETIME); |
| 162 | + } |
| 163 | + LocalDateTime localDateTime = readDateTimeValueNode.execute(inliningTarget, dateTime).toLocalDateTime(); |
| 164 | + ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); |
| 165 | + int dstSeconds = (int) zoneId.getRules().getDaylightSavings(zonedDateTime.toInstant()).getSeconds(); |
| 166 | + return TimeDeltaNodes.NewNode.getUncached().execute(inliningTarget, PythonBuiltinClassType.PTimeDelta, 0, dstSeconds, 0, 0, 0, 0, 0); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Builtin(name = "tzname", minNumOfPositionalArgs = 1, parameterNames = {"$self", "dt"}) |
| 171 | + @GenerateNodeFactory |
| 172 | + abstract static class TzNameNode extends PythonBinaryBuiltinNode { |
| 173 | + @Specialization(limit = "1") |
| 174 | + @TruffleBoundary |
| 175 | + static Object tzname(Object self, Object dateTime, |
| 176 | + @Bind Node inliningTarget, |
| 177 | + @CachedLibrary("self") InteropLibrary interop, |
| 178 | + @Cached TemporalNodes.DateTimeLikeCheckNode dateTimeLikeCheckNode, |
| 179 | + @Cached TemporalNodes.ReadDateTimeValueNode readDateTimeValueNode) { |
| 180 | + ZoneId zoneId = asZoneId(self, interop); |
| 181 | + if (dateTime == PNone.NONE) { |
| 182 | + return zoneId.getRules().isFixedOffset() ? TruffleString.FromJavaStringNode.getUncached().execute(zoneId.getId(), TS_ENCODING) : PNone.NONE; |
| 183 | + } |
| 184 | + if (!dateTimeLikeCheckNode.execute(inliningTarget, dateTime)) { |
| 185 | + throw PRaiseNode.raiseStatic(inliningTarget, PythonBuiltinClassType.TypeError, ErrorMessages.FROMUTC_ARGUMENT_MUST_BE_A_DATETIME); |
| 186 | + } |
| 187 | + LocalDateTime localDateTime = readDateTimeValueNode.execute(inliningTarget, dateTime).toLocalDateTime(); |
| 188 | + String name = DateTimeFormatter.ofPattern("z", Locale.ENGLISH).format(localDateTime.atZone(zoneId)); |
| 189 | + return TruffleString.FromJavaStringNode.getUncached().execute(name, TS_ENCODING); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + @Builtin(name = "fromutc", minNumOfPositionalArgs = 1, parameterNames = {"$self", "dt"}) |
| 194 | + @GenerateNodeFactory |
| 195 | + abstract static class FromUtcNode extends PythonBinaryBuiltinNode { |
| 196 | + @Specialization(limit = "1") |
| 197 | + @TruffleBoundary |
| 198 | + static Object fromutc(Object self, Object dateTime, |
| 199 | + @Bind Node inliningTarget, |
| 200 | + @CachedLibrary("self") InteropLibrary interop, |
| 201 | + @Cached TemporalNodes.DateTimeLikeCheckNode dateTimeLikeCheckNode, |
| 202 | + @Cached TemporalNodes.ReadDateTimeValueNode readDateTimeValueNode) { |
| 203 | + if (!dateTimeLikeCheckNode.execute(inliningTarget, dateTime)) { |
| 204 | + throw PRaiseNode.raiseStatic(inliningTarget, PythonBuiltinClassType.TypeError, ErrorMessages.FROMUTC_ARGUMENT_MUST_BE_A_DATETIME); |
| 205 | + } |
| 206 | + PDateTime asDateTime = (PDateTime) DateTimeNodes.AsManagedDateTimeNode.executeUncached(dateTime); |
| 207 | + if (asDateTime.tzInfo != self) { |
| 208 | + throw PRaiseNode.raiseStatic(inliningTarget, PythonBuiltinClassType.ValueError, ErrorMessages.FROMUTC_DT_TZINFO_IS_NOT_SELF); |
| 209 | + } |
| 210 | + ZoneId zoneId = asZoneId(self, interop); |
| 211 | + LocalDateTime utcDateTime = readDateTimeValueNode.execute(inliningTarget, dateTime).toLocalDateTime(); |
| 212 | + ZonedDateTime zonedDateTime = utcDateTime.atOffset(java.time.ZoneOffset.UTC).atZoneSameInstant(zoneId); |
| 213 | + return DateTimeNodes.NewUnsafeNode.getUncached().execute(inliningTarget, PythonBuiltinClassType.PDateTime, zonedDateTime.getYear(), zonedDateTime.getMonthValue(), |
| 214 | + zonedDateTime.getDayOfMonth(), zonedDateTime.getHour(), zonedDateTime.getMinute(), zonedDateTime.getSecond(), zonedDateTime.getNano() / 1_000, self, 0); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + @Builtin(name = J___REDUCE__, minNumOfPositionalArgs = 1) |
| 219 | + @GenerateNodeFactory |
| 220 | + abstract static class ReduceNode extends PythonUnaryBuiltinNode { |
| 221 | + @Specialization(limit = "1") |
| 222 | + static Object reduce(Object self, |
| 223 | + @CachedLibrary("self") InteropLibrary interop) { |
| 224 | + return TruffleString.FromJavaStringNode.getUncached().execute(asZoneId(self, interop).getId(), TS_ENCODING); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + private static ZoneId asZoneId(Object self, InteropLibrary interop) { |
| 229 | + try { |
| 230 | + return interop.asTimeZone(self); |
| 231 | + } catch (UnsupportedMessageException e) { |
| 232 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 233 | + } |
| 234 | + } |
| 235 | +} |
0 commit comments