Skip to content

Commit

Permalink
Compute the timedelta offset in seconds before constructing the date
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Jul 26, 2023
1 parent 7fae90a commit a389ff1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package org.enso.interpreter.runtime.data;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import java.time.DateTimeException;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.zone.ZoneRulesException;

import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;

@ExportLibrary(InteropLibrary.class)
@ExportLibrary(TypesLibrary.class)
@Builtin(
Expand Down Expand Up @@ -102,4 +104,9 @@ boolean hasType() {
Type getType(@CachedLibrary("this") TypesLibrary thisLib) {
return EnsoContext.get(thisLib).getBuiltins().timeZone();
}

@ExportMessage
String toDisplayString(boolean ignore) {
return zone.toString();
}
}
8 changes: 6 additions & 2 deletions test/Tests/src/Data/Time/Date_Time_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import project.Data.Time.Date_Part_Spec
polyglot java import org.enso.base.Time_Utils
polyglot java import java.time.ZonedDateTime
polyglot java import java.time.LocalDateTime
polyglot java import java.time.ZoneId
polyglot java import java.time.ZoneOffset
polyglot java import java.time.format.DateTimeFormatter
polyglot java import java.lang.Exception as JException
Expand Down Expand Up @@ -850,7 +851,10 @@ js_set_zone local_datetime zone =
datetime_with_tz + diff

python_datetime year month=1 day=1 hour=0 minute=0 second=0 nanosecond=0 zone=Time_Zone.system =
Panic.catch Any (python_datetime_impl year month day hour minute second nanosecond zone) (err -> Error.throw (Time_Error.Error err.payload))
rules = Polyglot.invoke (ZoneId.of (zone.zone_id)) "getRules" []
zone_offset = Polyglot.invoke rules "getOffset" [ LocalDateTime.now ]
delta = Polyglot.invoke zone_offset "getTotalSeconds" []
Panic.catch Any (python_datetime_impl year month day hour minute second nanosecond delta) (err -> Error.throw (Time_Error.Error err.payload))

python_parse text format="" =
d = Date_Time.parse text format
Expand Down Expand Up @@ -884,7 +888,7 @@ foreign python python_datetime_impl year month day hour minute second nanosecond
self.zone = zone

def utcoffset(self, dt):
return datetime.timedelta()
return datetime.timedelta(seconds=zone)

return datetime.datetime(year, month, day, hour, minute, second, int(nanosecond / 1000000), Zone(zone))

Expand Down

0 comments on commit a389ff1

Please sign in to comment.