Home /Modules/java/Instant.sleela

Instant.sleela

lib/java/jdk-28/java.base/java/time/Instant.sleela · 356 line(s) · sleela

<< lib/java/jdk-28/java.base/java/time/Instant.sleela
   SLeeLa file for the Java type java.time.Instant (module java.base). One Java type, one SLeeLa file.
   Java Source (JDK 28 API): https://download.java.net/java/early_access/jdk28/docs/api/java.base/java/time/Instant.html
   Java kind: class; extends java.lang.Object; implements java.time.temporal.Temporal, java.time.temporal.TemporalAdjuster, java.lang.Comparable, java.io.Serializable
   Status: members from the JDK 28 API (exact signatures).
   The SLeeLa functions here are produced from that Java Source. No Java source text is copied. >>
#sleela 3.0
material;
import java.jdk-28;

class Instant {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs. Values are faithful
     to the real origin of the Java type: the Java SE / JDK 28 API by Oracle
     and the OpenJDK community. >>
  String metaVersion = "1.1.0";
  int metaId = 4750;
  String metaAuthor = "Oracle / OpenJDK";
  String metaDate = "2026-11-12";
  String metaCopyright = "GPLv2 with Classpath Exception (OpenJDK)";
  String metaTrademark = "Java";
  String metaCorporation = "Oracle Corporation";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-11-12";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "Java SE 28 API Specification (JDK 28 early access)";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }

  String javaType = "java.time.Instant";
  String javaModule = "java.base";
  String javaSource = "https://download.java.net/java/early_access/jdk28/docs/api/java.base/java/time/Instant.html";
  String javaKind = "class";
  String javaSuper = "java.lang.Object";

  < ---- BEGIN java implementation (ported from the jdk-25 tier by lib/java/jdk-25/tools/jdk28_connect.py) ---- >
  < ---- java.time.Instant members (procedural implementation in SLeeLa) ---- >
  << An instantaneous point on the UTC time-line: epoch seconds plus a nano
     adjustment. The constructor is Instant.ofEpochSecond(epochSecond,
     nanoAdjustment); now() reads the VM UTC clock (timeUtcNanos).
     ofEpochSecond(long, long) is ofEpochSecondAdjusted. >>
  long seconds;
  int nanos;
  Instant(long epochSecond, long nanoAdjustment) {
    long s = epochSecond + floorDiv(nanoAdjustment, 1000000000);
    checkInstant(s);
    this.seconds = s;
    this.nanos = (int) floorMod(nanoAdjustment, 1000000000);
  }
  void checkInstant(long s) {
    if (s < -31557014167219200 || s > 31556889864403199) { throw new DateTimeException("Instant exceeds minimum or maximum instant"); }
  }
  Instant ofEpochSecond(long epochSecond) { return new Instant(epochSecond, 0); }
  Instant ofEpochSecondAdjusted(long epochSecond, long nanoAdjustment) { return new Instant(epochSecond, nanoAdjustment); }
  Instant ofEpochMilli(long epochMilli) { return new Instant(floorDiv(epochMilli, 1000), floorMod(epochMilli, 1000) * 1000000); }
  Instant now() { long n = timeUtcNanos(); return new Instant(floorDiv(n, 1000000000), floorMod(n, 1000000000)); }
  Instant parse(String text) {
    parseBegin(text);
    long y = parseYear();
    expectUnit(45);
    long m = digits(2, 2);
    expectUnit(45);
    long d = digits(2, 2);
    expectUnit(84);
    long nod = parseTimeNanos();
    expectUnit(90);
    parseEnd();
    LocalDate date = new LocalDate((int) y, (int) m, (int) d);
    long secs = date.toEpochDay() * 86400 + nod / 1000000000;
    return new Instant(secs, nod % 1000000000);
  }
  long getEpochSecond() { return this.seconds; }
  int getNano() { return this.nanos; }
  long toEpochMilli() {
        if (this.seconds < 0 && this.nanos > 0) {
      long millis = Math.multiplyExact(this.seconds + 1, 1000);
      long adjustment = this.nanos / 1000000 - 1000;
      return Math.addExact(millis, adjustment);
    }
    long millis2 = Math.multiplyExact(this.seconds, 1000);
    return Math.addExact(millis2, this.nanos / 1000000);
  }
  Instant plusParts(long secondsToAdd, long nanosToAdd) {
    if (secondsToAdd == 0 && nanosToAdd == 0) { return this; }
        long epochSec = Math.addExact(this.seconds, secondsToAdd);
    epochSec = Math.addExact(epochSec, nanosToAdd / 1000000000);
    long nanoAdjustment = this.nanos + nanosToAdd % 1000000000;
    return new Instant(epochSec, nanoAdjustment);
  }
  Instant plusSeconds(long n) { return plusParts(n, 0); }
  Instant plusMillis(long n) { return plusParts(n / 1000, (n % 1000) * 1000000); }
  Instant plusNanos(long n) { return plusParts(0, n); }
  Instant plus(Duration d) { return plusParts(d.getSeconds(), d.getNano()); }
  Instant minusSeconds(long n) { return plusSeconds(0 - n); }
  Instant minusMillis(long n) { return plusMillis(0 - n); }
  Instant minusNanos(long n) { return plusNanos(0 - n); }
  Instant minus(Duration d) { return plusParts(0 - d.getSeconds(), 0 - d.getNano()); }
  int compareTo(Instant other) {
    if (this.seconds < other.getEpochSecond()) { return -1; }
    if (this.seconds > other.getEpochSecond()) { return 1; }
    return this.nanos - other.getNano();
  }
  boolean isAfter(Instant other) { return compareTo(other) > 0; }
  boolean isBefore(Instant other) { return compareTo(other) < 0; }
  boolean equals(Any obj) {
    if (obj instanceof Instant) { Instant o = (Instant) obj; return this.seconds == o.getEpochSecond() && this.nanos == o.getNano(); }
    return false;
  }
  int hashCode() { return wrap32(Long.hashCode(this.seconds) + 51 * this.nanos); }
  << ISO-8601 in UTC (DateTimeFormatter.ISO_INSTANT): seconds always shown,
     the fraction in groups of three digits. >>
  String toString() {
    LocalDate h = new LocalDate(1970, 1, 1);
    LocalDate date = h.ofEpochDay(floorDiv(this.seconds, 86400));
    long sod = floorMod(this.seconds, 86400);
    String t = pad(sod / 3600, 2) + ":" + pad((sod / 60) % 60, 2) + ":" + pad(sod % 60, 2);
    if (this.nanos > 0) { t = t + nanoText(this.nanos); }
    return date.toString() + "T" + t + "Z";
  }

  < shared java.time helpers >
  long floorDiv(long x, long y) {
    long q = x / y;
    if ((x % y != 0) && ((x < 0) != (y < 0))) { q = q - 1; }
    return q;
  }
  long floorMod(long x, long y) { return x - floorDiv(x, y) * y; }
  int wrap32(long v) {
    long m = v % 4294967296;
    if (m < 0) { m = m + 4294967296; }
    if (m > 2147483647) { m = m - 4294967296; }
    return (int) m;
  }
  << Bitwise xor of two 32-bit two-complement ints. >>
  int xorInt(int a, int b) {
    long x = a;
    long y = b;
    if (x < 0) { x = x + 4294967296; }
    if (y < 0) { y = y + 4294967296; }
    long r = 0;
    long bit = 1;
    int k = 0;
    while (k < 32) {
      if ((x % 2) != (y % 2)) { r = r + bit; }
      x = x / 2; y = y / 2; bit = bit * 2; k = k + 1;
    }
    return wrap32(r);
  }
  String pad(long v, int width) {
    String s = "" + v;
    int n = javaStringLength(javaStringFromUtf8(s));
    while (n < width) { s = "0" + s; n = n + 1; }
    return s;
  }
  boolean leap(long y) { return (y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0)); }
  int monthLength(int m, boolean isLeap) {
    if (m == 2) { if (isLeap) { return 29; } return 28; }
    if (m == 4 || m == 6 || m == 9 || m == 11) { return 30; }
    return 31;
  }
  void checkField(String field, long value, long lo, long hi) {
    if (value < lo || value > hi) {
      throw new DateTimeException("Invalid value for " + field + " (valid values " + lo + " - " + hi + "): " + value);
    }
  }
  String monthName(int m) {
    if (m == 1) { return "JANUARY"; } if (m == 2) { return "FEBRUARY"; } if (m == 3) { return "MARCH"; }
    if (m == 4) { return "APRIL"; } if (m == 5) { return "MAY"; } if (m == 6) { return "JUNE"; }
    if (m == 7) { return "JULY"; } if (m == 8) { return "AUGUST"; } if (m == 9) { return "SEPTEMBER"; }
    if (m == 10) { return "OCTOBER"; } if (m == 11) { return "NOVEMBER"; }
    return "DECEMBER";
  }
  << Year rendering of ISO-8601 local dates: four digits zero padded, a sign
     for negative years, and a leading + beyond 9999. >>
  String yearText(long y) {
    long a = y;
    if (a < 0) { a = 0 - a; }
    if (a < 1000) {
      if (y < 0) { return "-" + pad(a, 4); }
      return pad(a, 4);
    }
    if (y > 9999) { return "+" + y; }
    return "" + y;
  }
  << Nano-of-second suffix in the Java style: 3, 6 or 9 digits, as needed. >>
  String nanoText(long nano) {
    if (nano % 1000000 == 0) { return "." + pad(nano / 1000000, 3); }
    if (nano % 1000 == 0) { return "." + pad(nano / 1000, 6); }
    return "." + pad(nano, 9);
  }
  < parse helpers over a Java UTF-16 copy of the text >
  String parseText;
  String parseUnits;
  int parsePos;
  int parseLen;
  void parseBegin(String text) {
    if (text == null) { throw new NullPointerException("text"); }
    this.parseText = text;
    this.parseUnits = javaStringFromUtf8(text);
    this.parsePos = 0;
    this.parseLen = javaStringLength(this.parseUnits);
  }
  DateTimeParseException parseError() {
    return new DateTimeParseException("Text '" + this.parseText + "' could not be parsed at index " + this.parsePos, this.parseText, this.parsePos);
  }
  int peekUnit() {
    if (this.parsePos >= this.parseLen) { return -1; }
    return javaStringCharAt(this.parseUnits, this.parsePos);
  }
  void expectUnit(int c) {
    if (peekUnit() != c) { throw parseError(); }
    this.parsePos = this.parsePos + 1;
  }
  long digits(int minCount, int maxCount) {
    long v = 0;
    int n = 0;
    while (n < maxCount && peekUnit() >= 48 && peekUnit() <= 57) {
      v = v * 10 + (peekUnit() - 48);
      this.parsePos = this.parsePos + 1;
      n = n + 1;
    }
    if (n < minCount) { throw parseError(); }
    return v;
  }
  void parseEnd() { if (this.parsePos != this.parseLen) { throw parseError(); } }
  long parseYear() {
    int c = peekUnit();
    if (c == 43 || c == 45) {
      this.parsePos = this.parsePos + 1;
      long y = digits(4, 10);
      if (c == 45) { return 0 - y; }
      return y;
    }
    return digits(4, 4);
  }
  << time part HH:mm[:ss[.fraction]] as nano-of-day >>
  long parseTimeNanos() {
    long h = digits(2, 2);
    expectUnit(58);
    long mi = digits(2, 2);
    long s = 0;
    long nano = 0;
    if (peekUnit() == 58) {
      this.parsePos = this.parsePos + 1;
      s = digits(2, 2);
      if (peekUnit() == 46) {
        this.parsePos = this.parsePos + 1;
        int start = this.parsePos;
        long f = digits(1, 9);
        int n = this.parsePos - start;
        while (n < 9) { f = f * 10; n = n + 1; }
        nano = f;
      }
    }
    checkField("HourOfDay", h, 0, 23);
    checkField("MinuteOfHour", mi, 0, 59);
    checkField("SecondOfMinute", s, 0, 59);
    return ((h * 60 + mi) * 60 + s) * 1000000000 + nano;
  }
  < ---- END java implementation ---- >

  < ---- generics (SLeeLa erasure, SLEELA.syntax 16.3 and 38.1) ---- >
  << The class is written without Java type parameters: every type variable and
     every parameterized or package-qualified reference type erases to Any, and
     java.lang.String erases to String. A value of a type parameter travels as
     Any; test it with instanceof and cast it where it is used. The Java type
     parameters are kept as data in JAVA_TYPE_PARAMETERS. An instance may record
     the type arguments its caller means with bindGenericTypeArguments (for an
     ArrayList of String, bind the single name java.lang.String). Nothing is
     checked against them at run time. >>
  static final String JAVA_TYPE_PARAMETERS = "";
  String[] genericTypeArguments = new String[0];
  public static String genericTypeParameters() { return JAVA_TYPE_PARAMETERS; }
  public static int genericTypeParameterCount() { return 0; }
  public static String genericTypeParameterName(int index) { return null; }
  public Instant bindGenericTypeArguments(String[] arguments) { if (arguments == null) { this.genericTypeArguments = new String[0]; } else { this.genericTypeArguments = arguments; } return this; }
  public String genericTypeArgument(int index) { if (index < 0 || index >= arrayLength(genericTypeArguments)) { return null; } return genericTypeArguments[index]; }
  public int genericTypeArgumentCount() { return arrayLength(genericTypeArguments); }

  < ---- BEGIN java declarations (from the JDK 28 API) ---- >
  < constants and static fields (values reflected from the JDK) >
  static Any EPOCH = null;
  static Any MIN = null;
  static Any MAX = null;
  < ---- END java declarations ---- >

  < ---- BEGIN java signatures (exact JDK 28 API signatures) ---- >
  << Not yet SLeeLa logic: each method returns the default value of its
     Java return type. @throwable names what Java throws for it (abstract:
     AbstractMethodError, else UnsupportedOperationException, plus the
     declared throws). Write the logic from the Java Source above into the
     java implementation block; the generator then drops the stub. >>
  @throwable("java.lang.UnsupportedOperationException")
  public int get(Any field) { return 0; }
  @throwable("java.lang.UnsupportedOperationException")
  public long getLong(Any field) { return 0; }
  @throwable("java.lang.UnsupportedOperationException")
  public static Any from(Any temporal) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public boolean isSupported(Any unit) { return false; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any with(Any field, long newValue) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any with(Any adjuster) { return null; }
  @generic("<R>")
  @throwable("java.lang.UnsupportedOperationException")
  public Any query(Any query) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any range(Any field) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public static Any now(Any clock) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public static Any ofEpochSecond(long epochSecond, long nanoAdjustment) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any minus(long amountToSubtract, Any unit) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any plus(long amountToAdd, Any unit) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public long until(Any endExclusive, Any unit) { return 0; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any until(Any endExclusive) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any truncatedTo(Any unit) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any adjustInto(Any temporal) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any atOffset(Any offset) { return null; }
  @throwable("java.lang.UnsupportedOperationException")
  public Any atZone(Any zone) { return null; }
  < ---- END java signatures ---- >
}