silk.time.offset.

Fixed offsets from UTC: the UtcOffset type.

When to use#

Use UtcOffset for the +01:00 part of a timestamp: how far a local clock is ahead of or behind UTC at one moment. It is not a time zone. A zone such as Europe/Rome has different offsets in summer and in winter.

Details#

An offset is a whole number of seconds east of UTC, less than 24 hours in each direction. Local time is UTC plus the offset, so 17:00 at +01:00 is 16:00 UTC.

Examples#

Parse an offset and read it#

silk
import silk.core.result { Result }
import silk.time.offset { UtcOffset }
import silk.time.text { TimeParseError }

pub fn main() -> i32 {
  match UtcOffset.parse("-03:30") {
    Result<UtcOffset, TimeParseError>.Success { value } => {
      if value.totalSeconds() != -12600 {
        return 1
      }
      return 0
    }
    Result<UtcOffset, TimeParseError>.Failure { error: _ } => {
      return 2
    }
  }
}

Import as UtcOffset with import silk.time.offset { UtcOffset }.

Public declarations: 1.

UtcOffset#

silk
pub struct UtcOffset

A fixed offset from UTC in whole seconds, less than 24 hours in each direction.

Details#

The offset is positive east of UTC: local time is UTC plus the offset. ==, !=, <, <=, >, and >= compare the number of seconds. Display writes +HH:MM, or +HH:MM:SS when the offset has a seconds part.

Implementation Copy for UtcOffset#

silk
impl Copy for UtcOffset

Implementation UtcOffset#

silk
impl UtcOffset

Associated function utc#

silk
pub fn utc() -> UtcOffset

Returns the zero offset, which is UTC.

Associated function fromSeconds#

silk
pub fn fromSeconds(seconds: i32) -> UtcOffset

Returns the offset of seconds seconds east of UTC, or west for a negative count.

Gotchas#

An offset of 24 hours or more traps. UtcOffset.checkedFromSeconds returns None instead.

Associated function checkedFromSeconds#

silk
pub fn checkedFromSeconds(seconds: i32) -> Option<UtcOffset>

Returns the offset of seconds seconds east of UTC, or west for a negative count, or None when it is 24 hours or more in either direction.

Associated function fromMinutes#

silk
pub fn fromMinutes(minutes: i32) -> UtcOffset

Returns the offset of minutes minutes east of UTC, or west for a negative count.

Gotchas#

An offset of 24 hours or more traps.

Method totalSeconds#

silk
pub fn totalSeconds(self: Self) -> i32

Returns the offset in seconds east of UTC. It is negative west of UTC.

Method isUtc#

silk
pub fn isUtc(self: Self) -> bool

Returns true if the offset is zero.

Method toDuration#

silk
pub fn toDuration(self: Self) -> Duration

Returns the offset as a span: positive east of UTC.

Associated function parse#

silk
pub fn parse(text: string) -> Result<UtcOffset, TimeParseError>

Reads an offset from text such as +01:00, -0330, or Z.

Details#

The text is Z or z for UTC, or a sign and HH, HH:MM, HHMM, HH:MM:SS, or HHMMSS.

Errors#

  • TimeParseReason.UnexpectedEnd or UnexpectedCharacter if the text does not have that form.

  • TimeParseReason.OutOfRange if the hour is above 23 or the minute or second is above 59.

Implementation Equal for UtcOffset#

silk
impl Equal for UtcOffset

Operation equals#

silk
equals: UtcOffset.same

Implementation Order for UtcOffset#

silk
impl Order for UtcOffset

Operation compare#

silk
compare: UtcOffset.compare

Implementation Display for UtcOffset#

silk
impl Display for UtcOffset

Writes the offset as +HH:MM, or +HH:MM:SS when it has a seconds part, with - west of UTC. The zero offset is +00:00.

Operation display#

silk
effect fn display(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

Implementation Debug for UtcOffset#

silk
impl Debug for UtcOffset

Writes the same text as Display: the offset as +HH:MM or +HH:MM:SS.

Operation debug#

silk
effect fn debug(self: &Self, formatter: &mut Formatter) -> ()
    ! WriterError
    ? &mut Writer

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