silk.time.now.

The present moment as each silk.time type: Now reads the SystemClock effect and, for local values, a Zone.

When to use#

Use it where code needs today's date or the time now. Each function shows in its signature what it reads: the clock, and for the zone of the machine also LocalZone and TimeZones. A test provides a fixed clock and FixedLocalZone, and the result is the same on every machine.

Details#

There is no local time without a zone. Load the zone one time with Now.localZone or TimeZones.zone, keep it, and pass it to Now.zonedIn, Now.dateIn, or Now.dateTimeIn.

Examples#

Today's date in a zone#

silk
import silk.core.effect { Effect }
import silk.system.system_clock { SystemClock }
import silk.system.system_clock_native { OsSystemClock }
import silk.time.now { Now }
import silk.time.offset { UtcOffset }
import silk.time.zone { Zone }

pub fn main() -> i32 {
  let mut clock = OsSystemClock.make()
  let tokyo = Zone.fixed(UtcOffset.fromMinutes(540))
  let today = run Now.dateIn(&tokyo) |> Effect.provideMut<SystemClock>(&mut clock)
  if today.year() < 2026 {
    return 1
  }
  return 0
}

Import as Now with import silk.time.now { Now }.

Public declarations: 1.

Now#

silk
pub struct Now

The owner type of the functions that read the present moment. It has no fields and is never constructed.

Implementation Now#

silk
impl Now

Associated function instant#

silk
pub effect fn instant() -> Instant ? &mut SystemClock

Returns the present instant of the SystemClock.

Associated function localZone#

silk
pub effect fn localZone() -> Zone
    ! NoLocalZoneError | ZoneLookupError | OutOfMemoryError
    ? &mut LocalZone | &mut TimeZones | &mut Allocator

Returns the zone that the machine is set to: the name from LocalZone, and its rules from TimeZones.

Errors#

  • NoLocalZoneError if the machine does not say which zone it is in.

  • ZoneLookupError if the TimeZones provider does not have that zone.

  • OutOfMemoryError if the Allocator cannot supply the zone.

Associated function zonedIn#

silk
pub effect fn zonedIn<'z>(zone: &'z Zone) -> ZonedDateTime<'z> ? &mut SystemClock

Returns the present instant as seen in zone.

Associated function dateIn#

silk
pub effect fn dateIn(zone: &Zone) -> PlainDate ? &mut SystemClock

Returns the date that a calendar in zone shows now.

Associated function dateTimeIn#

silk
pub effect fn dateTimeIn(zone: &Zone) -> PlainDateTime ? &mut SystemClock

Returns the date and time that a clock in zone shows now.

Category: Time.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page