Skip to content

Latest commit

 

History

History
87 lines (60 loc) · 1.98 KB

TimeZone.md

File metadata and controls

87 lines (60 loc) · 1.98 KB

TimeZone

This module provides a set of functions for working with TimeZone values.

Import from the base library to use this module.

import TimeZone "mo:datetime/TimeZone";
import Components "mo:datetime/Components";

Type TimeZone

type TimeZone = Types.TimeZone

Type FixedTimeZone

type FixedTimeZone = Types.FixedTimeZone

Type Offset

type Offset = Types.FixedTimeZone

Type TimeZoneDescriptor

type TimeZoneDescriptor = Types.TimeZoneDescriptor

Function utc

func utc() : TimeZone

Helper function to create a UTC timezone (fixed at UTC+0)

let timeZone : TimeZone.TimeZone = TimeZone.utc();

Function withFixedOffset

func withFixedOffset(offset : Offset) : TimeZone

Helper function to create a timezone with a fixed offset like UTC+3

let timeZone : TimeZone.TimeZone = TimeZone.withFixedOffset(#hours(3)); // UTC+3

Function toOffsetSeconds

func toOffsetSeconds(timeZone : TimeZone, components : Components) : Int

Gets the UTC offset in seconds for the specified components and time zone. The components are used if the timezone is dynamic. This is due to the timezone offset being dependent on the date (daylight savings, changes to timezone offset, etc...).

let timeZone : TimeZone.TimeZone = ...;
let c : Components.Components = {year = 2020; month = 1; day = 1; hour = 0; minute = 0; nanosecond = 0};
let offsetSeconds : Int = TimeZone.getOffsetSeconds(timeZone, c);

Function toFixedOffsetSeconds

func toFixedOffsetSeconds(fixedTimeZone : FixedTimeZone) : Int

Gets the UTC offset in seconds for the specified fixed time zone.

let timeZone : TimeZone.TimeZone = #fixed(#hours(1));
let offsetSeconds : Int = TimeZone.getFixedOffsetSeconds(timeZone);