Class Clock

java.lang.Object
dev.gmitch215.bytebox.builtin.Clock

public final class Clock extends Object
Time, and what the platform does to it.

Cloudflare pins the clock between I/O operations, as a defence against timing attacks. Inside a request that performs no I/O, System.currentTimeMillis() returns the same value however many times it is called, and so does everything built on it. A loop waiting for the clock to move never exits.

Two consequences worth knowing. Timing a section of code is not possible from inside the isolate; wrangler tail reports the CPU time instead. And Thread.sleep only becomes due because bytebox advances its own clock to meet it, which is why a sleep works at all.

Since:
1.0.0
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    inZone(double millis, String timeZone)
    Formats an instant for a timezone, using the platform's own zone data.
    static String
    iso(double millis)
    Formats an instant as an ISO 8601 timestamp in UTC.
    static String
    Returns the current time as an ISO 8601 timestamp in UTC.
    static double
    now()
    The current time, which advances only after I/O.
    static int
    offsetHours(double millis, String timeZone)
    A zone's offset from UTC at an instant, which is what makes daylight saving observable.
    static int
    offsetMinutes(double millis, String timeZone)
    A zone's offset from UTC at an instant, which is what makes daylight saving observable.
    static double
    parse(String timestamp)
    Parses an ISO 8601 timestamp.
    static boolean
    supports(String timeZone)
    Reports whether the runtime knows a timezone.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • now

      public static double now()
      The current time, which advances only after I/O.
      Returns:
      milliseconds since the epoch
    • isoNow

      public static String isoNow()
      Returns the current time as an ISO 8601 timestamp in UTC.
      Returns:
      the current time as an ISO 8601 timestamp in UTC
    • iso

      public static String iso(double millis)
      Formats an instant as an ISO 8601 timestamp in UTC.
      Parameters:
      millis - milliseconds since the epoch
      Returns:
      the timestamp
    • parse

      public static double parse(String timestamp)
      Parses an ISO 8601 timestamp.
      Parameters:
      timestamp - the timestamp
      Returns:
      milliseconds since the epoch, or NaN when it does not parse
    • inZone

      public static String inZone(double millis, String timeZone)
      Formats an instant for a timezone, using the platform's own zone data.
      Parameters:
      millis - milliseconds since the epoch
      timeZone - an IANA zone, such as America/New_York
      Returns:
      the formatted instant
    • offsetMinutes

      public static int offsetMinutes(double millis, String timeZone)
      A zone's offset from UTC at an instant, which is what makes daylight saving observable. Example: Clock.offsetMinutes(Clock.parse("2024-03-10T07:00:00Z"), "America/New_York") returns -300 because the offset is five hours west of UTC.
      Parameters:
      millis - milliseconds since the epoch
      timeZone - an IANA zone
      Returns:
      the offset in minutes, positive east of UTC
    • offsetHours

      public static int offsetHours(double millis, String timeZone)
      A zone's offset from UTC at an instant, which is what makes daylight saving observable. Example: Clock.offsetHours(Clock.parse("2024-03-10T07:00:00Z"), "America/New_York") returns -5 because the offset is five hours west of UTC.
      Parameters:
      millis - milliseconds since the epoch
      timeZone - an IANA zone
      Returns:
      the offset in hours, positive east of UTC
    • supports

      public static boolean supports(String timeZone)
      Reports whether the runtime knows a timezone.
      Parameters:
      timeZone - an IANA zone
      Returns:
      whether a formatter can be built for it