Package dev.gmitch215.bytebox.builtin
Class Clock
java.lang.Object
dev.gmitch215.bytebox.builtin.Clock
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 TypeMethodDescriptionstatic StringFormats an instant for a timezone, using the platform's own zone data.static Stringiso(double millis) Formats an instant as an ISO 8601 timestamp in UTC.static StringisoNow()Returns the current time as an ISO 8601 timestamp in UTC.static doublenow()The current time, which advances only after I/O.static intoffsetHours(double millis, String timeZone) A zone's offset from UTC at an instant, which is what makes daylight saving observable.static intoffsetMinutes(double millis, String timeZone) A zone's offset from UTC at an instant, which is what makes daylight saving observable.static doubleParses an ISO 8601 timestamp.static booleanReports whether the runtime knows a timezone.
-
Method Details
-
now
public static double now()The current time, which advances only after I/O.- Returns:
- milliseconds since the epoch
-
isoNow
Returns the current time as an ISO 8601 timestamp in UTC.- Returns:
- the current time as an ISO 8601 timestamp in UTC
-
iso
Formats an instant as an ISO 8601 timestamp in UTC.- Parameters:
millis- milliseconds since the epoch- Returns:
- the timestamp
-
parse
Parses an ISO 8601 timestamp.- Parameters:
timestamp- the timestamp- Returns:
- milliseconds since the epoch, or NaN when it does not parse
-
inZone
Formats an instant for a timezone, using the platform's own zone data.- Parameters:
millis- milliseconds since the epochtimeZone- an IANA zone, such asAmerica/New_York- Returns:
- the formatted instant
-
offsetMinutes
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-300because the offset is five hours west of UTC.- Parameters:
millis- milliseconds since the epochtimeZone- an IANA zone- Returns:
- the offset in minutes, positive east of UTC
-
offsetHours
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-5because the offset is five hours west of UTC.- Parameters:
millis- milliseconds since the epochtimeZone- an IANA zone- Returns:
- the offset in hours, positive east of UTC
-
supports
Reports whether the runtime knows a timezone.- Parameters:
timeZone- an IANA zone- Returns:
- whether a formatter can be built for it
-