~/bend-docscommunity

time.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-time@0.1.1.0/time.bend as Time

Clocks, Duration and Instant, Gregorian dates, RFC 3339 and HTTP-date text, and TZif time zones. Source: https://github.com/paymog/bend-kit/tree/main/time

4 imports
import Base
import bend-kit-int@0.2.0.0/int.bend as Int
import bend-kit-fmt@0.1.0.0/fmt.bend as Fmt
import 0x49814d83de8f70993a43e1002be29ecd/bytes.bend as Bytes

Types

type Duration source · line 9 · raw

Data

secs + nanos / 10^9 seconds, with nanos below 10^9: -0.5 s is Duration{-1, 500000000}.

type Instant source · line 13 · raw

Data

A Duration since 1970-01-01T00:00:00Z. Days are 86400 s: there are no leap seconds.

type Date source · line 17 · raw

Data

A proleptic Gregorian date. The conversions and formats cover years 0 to 9999.

type DateTime source · line 20 · raw

Data

type P source · line 354 · raw

Data

A parser state: the numbers read so far, newest first, and the text left.

type Ttype source · line 572 · raw

Data

A local time type: seconds east of UTC, whether it is daylight time, and its abbreviation.

type Trans source · line 576 · raw

Data

From at on, local time follows types[idx].

type Zone source · line 580 · raw

Data

A TZif zone (RFC 8536): transitions in time order, local time types, and the footer TZ string ("" in version 1).

Definitions

def i64 source · line 23 · raw

@x:U32 -> 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64

def i64s source · line 27 · raw

@x:U32 -> 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64

The signed 32-bit value of x, widened.

def i64.of source · line 33 · raw

@hi:U32 -> @lo:U32 -> 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64

hi * 2^32 + lo, as the bits of an I64.

def Duration.zero source · line 39 · raw

Duration

def Duration.of_secs source · line 42 · raw

@s:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> Duration

def Duration.of_ms source · line 45 · raw

@+ms:U32 -> Duration

def Duration.to_ms source · line 49 · raw

@d:Duration -> 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64

Whole milliseconds, rounded down.

def Duration.carry source · line 54 · raw

@c:Bool -> @s:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> @+n:U32 -> Duration

def Duration.add source · line 61 · raw

@a:Duration -> @b:Duration -> Duration

def Duration.borrow source · line 68 · raw

@c:Bool -> @s:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> @+n:U32 -> Duration

n is na - nb, wrapped when na < nb.

def Duration.sub source · line 75 · raw

@a:Duration -> @b:Duration -> Duration

def Duration.neg source · line 80 · raw

@d:Duration -> Duration

def Duration.cmp.then source · line 83 · raw

@c:Cmp -> @+na:U32 -> @+nb:U32 -> Cmp

def Duration.cmp source · line 92 · raw

@a:Duration -> @b:Duration -> Cmp

def Instant.epoch source · line 100 · raw

Instant

def Instant.of_unix source · line 103 · raw

@s:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> Instant

def Instant.of source · line 106 · raw

@d:Duration -> Instant

def Instant.since_epoch source · line 111 · raw

@i:Instant -> Duration

def Instant.add source · line 116 · raw

@i:Instant -> @d:Duration -> Instant

def Instant.since source · line 120 · raw

@a:Instant -> @b:Instant -> Duration

a - b.

def Instant.cmp source · line 123 · raw

@a:Instant -> @b:Instant -> Cmp

def raw.dur source · line 139 · raw

@r:Pair(U32, Pair(U32, U32)) -> Duration

def mono source · line 144 · raw

IO(Duration)

Time since an unspecified start that never goes back. Use it to measure spans.

def now source · line 150 · raw

IO(Instant)

The wall clock. It can jump when the system time is set.

def Date.is_leap source · line 158 · raw

@+y:U32 -> Bool

def Date.days_in_month source · line 163 · raw

@+y:U32 -> @+m:U32 -> U32

0 for a month outside 1 to 12.

def Date.valid source · line 168 · raw

@d:Date -> Bool

def Z.EPOCH source · line 176 · raw

U32

z counts days from -0400-03-01, so every date from year 0 has z > 0 in a U32. The civil-days algorithm is Howard Hinnant's, with the year starting in March.

def Date.to_z.at source · line 179 · raw

@+y:U32 -> @+mp:U32 -> @+d:U32 -> U32

def Date.to_z.go source · line 185 · raw

@jf:Bool -> @+y:U32 -> @+m:U32 -> @+d:U32 -> U32

def Date.to_z source · line 192 · raw

@d:Date -> U32

def Date.of_z.fin source · line 197 · raw

@lo:Bool -> @+y:U32 -> @+mp:U32 -> @+d:U32 -> Date

def Date.of_z source · line 204 · raw

@+z:U32 -> Date

def Date.to_days source · line 214 · raw

@d:Date -> 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64

Days since 1970-01-01.

def Date.of_z.if source · line 217 · raw

@ok:Bool -> @+z:U32 -> Maybe<&2, Date>

def Date.of_days source · line 225 · raw

@days:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> Maybe<&2, Date>

The date days after 1970-01-01, or None outside 0000-01-01 to 9999-12-31.

def Date.weekday source · line 231 · raw

@d:Date -> U32

1 for Monday to 7 for Sunday, as in ISO 8601.

def DateTime.valid source · line 237 · raw

@t:DateTime -> Bool

def DateTime.to_instant source · line 244 · raw

@t:DateTime -> Instant

The instant of a UTC date and time. It does not check t; see DateTime.valid.

def DateTime.at source · line 250 · raw

@m:Maybe<&2, Date> -> @+sod:U32 -> @n:U32 -> Maybe<&2, DateTime>

def DateTime.floor source · line 258 · raw

@neg:Bool -> @q:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> @r:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> @n:U32 -> Maybe<&2, DateTime>

r is secs % 86400, which has the sign of secs.

def DateTime.of_instant source · line 266 · raw

@i:Instant -> Maybe<&2, DateTime>

The UTC date and time of i, or None outside years 0 to 9999.

def pad source · line 275 · raw

@+x:U32 -> @w:Nat -> String

def Duration.show_ms.pos source · line 279 · raw

@d:Duration -> String

Milliseconds with six decimal places.

def Duration.show_ms.sign source · line 284 · raw

@neg:Bool -> @d:Duration -> String

def Duration.show_ms source · line 291 · raw

@d:Duration -> String

def frac.go source · line 297 · raw

@f:Nat -> @z:Bool -> @+n:U32 -> @+w:Nat -> String

The digits of n / 10^(9 - w), with trailing zeros cut. n is not 0.

def frac source · line 309 · raw

@+n:U32 -> String

def DateTime.rfc3339 source · line 312 · raw

@t:DateTime -> String

def rfc3339 source · line 326 · raw

@i:Instant -> Maybe<&2, String>

RFC 3339 in UTC, such as 1985-04-12T23:20:50.52Z. The fraction has no trailing zeros, and is left out when 0.

def WEEKDAYS source · line 329 · raw

List<&2, String>

def MONTHS source · line 332 · raw

List<&2, String>

def name source · line 335 · raw

@xs:List<&2, String> -> @+i:U32 -> String

def DateTime.http_date source · line 338 · raw

@t:DateTime -> String

def http_date source · line 347 · raw

@i:Instant -> Maybe<&2, String>

IMF-fixdate (RFC 9110 §5.6.7), such as Sun, 06 Nov 1994 08:49:37 GMT. It drops the fraction of a second.

def is_digit source · line 357 · raw

@+c:U32 -> Bool

def digits.go source · line 360 · raw

@n:Nat -> @s:String -> @+acc:U32 -> @ok:Bool -> Maybe<&1, Pair(U32, String)>

def push source · line 375 · raw

@vs:List<&2, U32> -> @r:Maybe<&1, Pair(U32, String)> -> Maybe<&2, P>

def num source · line 383 · raw

@n:Nat -> @p:P -> Maybe<&2, P>

Exactly n digits.

def lit.if source · line 388 · raw

@ok:Bool -> @vs:List<&2, U32> -> @t:String -> Maybe<&2, P>

def lit2 source · line 396 · raw

@+a:U32 -> @+b:U32 -> @p:P -> Maybe<&2, P>

One char that is a or b.

def lit source · line 405 · raw

@+c:U32 -> @p:P -> Maybe<&2, P>

def text source · line 408 · raw

@+w:String -> @p:P -> Maybe<&2, P>

def at.head source · line 414 · raw

@+s:String -> Bool

def secfrac.go source · line 422 · raw

@s:String -> @d:Bool -> @+mul:U32 -> @+acc:U32 -> @+k:U32 -> Pair(U32, Pair(U32, String))

Digits after the first nine are read and dropped. d says whether s starts with a digit.

def secfrac.fin source · line 433 · raw

@vs:List<&2, U32> -> @r:Pair(U32, Pair(U32, String)) -> Maybe<&2, P>

def secfrac.if source · line 437 · raw

@dot:Bool -> @vs:List<&2, U32> -> @+s:String -> Maybe<&2, P>

def secfrac source · line 446 · raw

@p:P -> Maybe<&2, P>

An optional '.' and one or more digits, as nanoseconds.

def zone.num source · line 451 · raw

@+neg:U32 -> @vs:List<&2, U32> -> @t:String -> Maybe<&2, P>

def zone.if source · line 457 · raw

@z:Bool -> @plus:Bool -> @minus:Bool -> @vs:List<&2, U32> -> @t:String -> Maybe<&2, P>

def zone source · line 473 · raw

@p:P -> Maybe<&2, P>

Z, or +hh:mm or -hh:mm, as (sign, hh, mm) with sign 1 for west of UTC.

def index.go source · line 482 · raw

@xs:List<&2, String> -> @+w:String -> @+i:U32 -> U32

def word source · line 490 · raw

@+names:List<&2, String> -> @p:P -> Maybe<&2, P>

One of names, as its index.

def mk.if source · line 496 · raw

@ok:Bool -> @t:DateTime -> Maybe<&2, Instant>

def mk source · line 503 · raw

@+t:DateTime -> Maybe<&2, Instant>

def rfc3339.shift source · line 506 · raw

@m:Maybe<&2, Instant> -> @+west:U32 -> @+hh:U32 -> @+mm:U32 -> Maybe<&2, Instant>

def rfc3339.fin source · line 515 · raw

@p:P -> Maybe<&2, Instant>

def rfc3339.parse source · line 524 · raw

@s:String -> Maybe<&2, Instant>

An RFC 3339 date-time (§5.6), such as 1985-04-12T23:20:50.52Z or 1996-12-19T16:39:57-08:00. T and Z may be lowercase. A leap second (:60) is refused, as Instant has none.

def http_date.fin source · line 541 · raw

@p:P -> Maybe<&2, Instant>

def http_date.parse source · line 550 · raw

@s:String -> Maybe<&2, Instant>

An IMF-fixdate (RFC 9110 §5.6.7), such as Sun, 06 Nov 1994 08:49:37 GMT. The day name must be one of the seven, but it is not checked against the date.

def be source · line 584 · raw

@n:Nat -> @s:String -> @+acc:U32 -> U32

n bytes of a byte string, big-endian.

def be32 source · line 595 · raw

@+s:String -> @n:Nat -> U32

def u8 source · line 598 · raw

@+s:String -> @n:Nat -> U32

def tzif.size source · line 601 · raw

@t8:Bool -> Nat

def tzif.time source · line 608 · raw

@t8:Bool -> @+s:String -> 0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64

def tzif.trans source · line 616 · raw

@n:Nat -> @+t8:Bool -> @+ts:String -> @+ks:String -> List<&2, Trans>

ts holds the transition times and ks their type indexes.

def tzif.abbr source · line 625 · raw

@chars:String -> @+i:U32 -> String

The NUL-terminated abbreviation at byte i of chars.

def tzif.types source · line 628 · raw

@n:Nat -> @+s:String -> @+chars:String -> List<&2, Ttype>

def tzif.if source · line 636 · raw

@ok:Bool -> @z:Zone -> Maybe<&2, Zone>

def tzif.count source · line 644 · raw

@+h:String -> @i:Nat -> Nat

Header counts, in RFC 8536 order: isut, isstd, leap, time, type, char.

def tzif.bsize source · line 648 · raw

@+t8:Bool -> @+h:String -> Nat

The data block's size in bytes, for header h.

def tzif.block source · line 654 · raw

@+t8:Bool -> @+h:String -> Maybe<&2, Zone>

h starts at a header; its data block follows it. Version 2+ blocks have 8-byte times and a footer.

def tzif.go source · line 666 · raw

@v2:Bool -> @+s:String -> Maybe<&2, Zone>

def tzif.parse source · line 675 · raw

@b:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> Maybe<&2, Zone>

A TZif file, such as one from /usr/share/zoneinfo. Versions 2 and up use their 64-bit block. ponytail: reads the bytes as a byte String; walk Bytes directly if zone files get big.

def Zone.find source · line 681 · raw

@ts:List<&2, Trans> -> @+secs:0x4eed9d7ac6ece61523d747a9d804e4f0/int.I64 -> @+cur:U32 -> U32

The type index at secs: the last transition at or before it, or type 0 before the first. ponytail: linear scan; bisect an Array if lookups get hot.

def Zone.at source · line 690 · raw

@z:Zone -> @i:Instant -> Maybe<&2, Ttype>

The local time type at i. ponytail: past the last transition the last type holds; evaluate the footer TZ rule for slim files and years past 2037.

def Zone.local.at source · line 695 · raw

@m:Maybe<&2, Ttype> -> @i:Instant -> Maybe<&2, DateTime>

def Zone.local source · line 703 · raw

@z:Zone -> @+i:Instant -> Maybe<&2, DateTime>

The local date and time at i in zone z.

Templates

template fmt.map source · line 318 · raw

@-f:(@_:DateTime -> String) -> @m:Maybe<&2, DateTime> -> Maybe<&2, String>

Effects (foreign code)

effect mono.raw source · line 130 · raw

IO(Pair(U32, Pair(U32, U32)))

(secs hi, secs lo, nanos) of CLOCK_MONOTONIC, from an unspecified start.

foreign: effs/time.c, effs/time.js

effect wall.raw source · line 135 · raw

IO(Pair(U32, Pair(U32, U32)))

(secs hi, secs lo, nanos) of CLOCK_REALTIME, since the Unix epoch.

foreign: effs/time.c, effs/time.js