diff --git a/mint.json b/mint.json index 63793611..f66aa40b 100644 --- a/mint.json +++ b/mint.json @@ -133,7 +133,10 @@ }, { "group": "System APIs", - "pages": ["modus/sdk/assemblyscript/console"] + "pages": [ + "modus/sdk/assemblyscript/console", + "modus/sdk/assemblyscript/localtime" + ] } ] }, @@ -163,7 +166,7 @@ }, { "group": "System APIs", - "pages": ["modus/sdk/go/console"] + "pages": ["modus/sdk/go/console", "modus/sdk/go/localtime"] } ] } diff --git a/modus/sdk/assemblyscript/localtime.mdx b/modus/sdk/assemblyscript/localtime.mdx new file mode 100644 index 00000000..4097cef2 --- /dev/null +++ b/modus/sdk/assemblyscript/localtime.mdx @@ -0,0 +1,133 @@ +--- +title: Local Time +description: "Access the user's local time and time zone in your functions" +--- + +import { SdkHeader, SdkTip } from "/snippets/sdk-snippets.mdx" + + + +The Modus Local Time APIs allow you to access the user's local time and time +zone from your functions. + +## Import + +To begin, import the `localtime` namespace from the SDK: + +```ts +import { localtime } from "@hypermode/modus-sdk-as" +``` + +{/* */} + +## Local Time APIs + +The APIs in the `localtime` namespace are below. + +All time zones use the IANA time zone database format. For example, +`"America/New_York"`. You can find a list of valid time zones +[here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). + + + +{/* */} + +For APIs that work with the user's local time, the time zone is determined in +the following order of precedence: + +- If the `X-Time-Zone` header is present in the request, the time zone is set to + the value of the header. +- If the `TZ` environment variable is set on the host, the time zone is set to + the value of the variable. +- Otherwise, the time zone is set to the host's local time zone. + +{/* */} + + + + + +When working locally with `modus dev`, Modus uses the host's local time zone by +default. You can override this by setting the `TZ` environment variable in your +`.env.local` file. + + + + + +In a browser-based web app, you can get the user's time zone with the following +JavaScript code: + +```js +const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone +``` + +Assign that value to a `"X-Time-Zone"` request header when calling Modus, to use +it for all local time calculations. + + + + + +If you just need the current UTC time, you can use AssemblyScript's built-in +support: + +```ts +const now = new Date(Date.now()) + +// if you need a string +const utcTime = now.toISOString() +``` + + + + + +### Functions + +#### getTimeZone + +Returns the user's time zone in IANA format. + +```ts +function getTimeZone(): string +``` + +#### isValidTimeZone + +Determines whether the specified time zone is a valid IANA time zone and +recognized by the system as such. + +```ts +function isValidTimeZone(tz: string): bool +``` + + + An IANA time zone identifier, such as `"America/New_York"`. + + +#### now + +Returns the current local time in the user's time zone, as a string in ISO 8601 +extended format, including the applicable time zone offset. + +For example, `"2025-12-31T12:34:56.789-04:00"`. + +```ts +function now(): string +``` + +#### nowInZone + +Returns the current local time in a specified time zone, as a string in ISO 8601 +extended format, including the applicable time zone offset. + +For example, `"2025-12-31T12:34:56.789-04:00"`. + +```ts +function nowInZone(tz: string): string +``` + + + An IANA time zone identifier, such as `"America/New_York"`. + diff --git a/modus/sdk/go/localtime.mdx b/modus/sdk/go/localtime.mdx new file mode 100644 index 00000000..b4cacce0 --- /dev/null +++ b/modus/sdk/go/localtime.mdx @@ -0,0 +1,146 @@ +--- +title: Local Time +description: "Access the user's local time and time zone in your functions" +--- + +import { SdkHeader, SdkTip } from "/snippets/sdk-snippets.mdx" + + + +The Modus Local Time APIs allow you to access the user's local time and time +zone from your functions. + +## Import + +To begin, import the `localtime` package from the SDK: + +```go +import github.com/hypermodeinc/modus/sdk/go/pkg/localtime +``` + +{/* */} + +## Local Time APIs + +The APIs in the `localtime` package are below. + +All time zones use the IANA time zone database format. For example, +`"America/New_York"`. You can find a list of valid time zones +[here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). + + + +{/* */} + +For APIs that work with the user's local time, the time zone is determined in +the following order of precedence: + +- If the `X-Time-Zone` header is present in the request, the time zone is set to + the value of the header. +- If the `TZ` environment variable is set on the host, the time zone is set to + the value of the variable. +- Otherwise, the time zone is set to the host's local time zone. + +{/* */} + + + + + +When working locally with `modus dev`, Modus uses the host's local time zone by +default. You can override this by setting the `TZ` environment variable in your +`.env.local` file. + + + + + +In a browser-based web app, you can get the user's time zone with the following +JavaScript code: + +```js +const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone +``` + +Assign that value to a `"X-Time-Zone"` request header when calling Modus, to use +it for all local time calculations. + + + + + +In many cases, you can use Go's built-in time support: + +```go +// to get the current time in UTC +now := time.Now().UTC() + +// if you need a string +s := now.Format(time.RFC3339) +``` + + + + + +Due to Go's WASM implementation, the standard `time.Now()` function always +returns the UTC time, not the user's local time like it usually does in Go. If +you need the user's local time, use `localtime.Now()` instead. + + + + + +### Functions + +#### GetLocation + +Returns a pointer to a Go `time.Location` object for a specific time zone. +Errors if the time zone provided is invalid. + +```go +func GetLocation(tz string) (*time.Location, error) +``` + +#### GetTimeZone + +Returns the user's time zone in IANA format. + +```go +func GetTimeZone() string +``` + +#### IsValidTimeZone + +Determines whether the specified time zone is a valid IANA time zone and +recognized by the system as such. + +```go +func IsValidTimeZone(tz string) bool +``` + + + An IANA time zone identifier, such as `"America/New_York"`. + + +#### Now + +Returns the current time as a `time.Time` object, with the location set to the +user's local time zone. Errors if the time zone passed to the host is invalid. + +```go +func Now() (time.Time, error) +``` + +#### NowInZone + +Returns the current time as a `time.Time` object, with the location set to a +specific time zone. Errors if the time zone provided is invalid. + +```go +func NowInZone(tz string) (time.Time, error) +``` + + + An IANA time zone identifier, such as `"America/New_York"`. + diff --git a/snippets/sdk-snippets.mdx b/snippets/sdk-snippets.mdx index 8dbb8f98..b612fb5b 100644 --- a/snippets/sdk-snippets.mdx +++ b/snippets/sdk-snippets.mdx @@ -5,6 +5,7 @@ export const SdkHeader = ({language, feature}) => (
    {(() =>{ const languages = ["AssemblyScript", "Go"]; + const page = feature.toLowerCase().replace(/\W/g, ""); return languages.map((lang) => { if (lang === language) { return ( @@ -12,7 +13,7 @@ export const SdkHeader = ({language, feature}) => ( ) } else { return ( -
  • {lang} {feature} APIs
  • +
  • {lang} {feature} APIs
  • ) } }) diff --git a/styles/config/vocabularies/general/accept.txt b/styles/config/vocabularies/general/accept.txt index d4894f82..c8cfc586 100644 --- a/styles/config/vocabularies/general/accept.txt +++ b/styles/config/vocabularies/general/accept.txt @@ -55,7 +55,9 @@ GraphQL|graphql gRPC HTTP|http Hugging Face +IANA Infof +ISO LLM Logf MySQL|mysql @@ -69,6 +71,8 @@ Scattergories SDK|sdk TLS URL|url +UTC urql UUID Warnf +WASM