Skip to content

Commit

Permalink
Add localtime docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint committed Jan 30, 2025
1 parent e57415f commit 00ca158
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 3 deletions.
7 changes: 5 additions & 2 deletions mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@
},
{
"group": "System APIs",
"pages": ["modus/sdk/assemblyscript/console"]
"pages": [
"modus/sdk/assemblyscript/console",
"modus/sdk/assemblyscript/localtime"
]
}
]
},
Expand Down Expand Up @@ -163,7 +166,7 @@
},
{
"group": "System APIs",
"pages": ["modus/sdk/go/console"]
"pages": ["modus/sdk/go/console", "modus/sdk/go/localtime"]
}
]
}
Expand Down
133 changes: 133 additions & 0 deletions modus/sdk/assemblyscript/localtime.mdx
Original file line number Diff line number Diff line change
@@ -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"

<SdkHeader language="AssemblyScript" feature="Local Time" />

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"
```

{/* <!-- vale Google.Headings = NO --> */}

## 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).

<Info>

{/* <!-- vale Google.Passive = NO --> */}

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.

{/* <!-- vale Google.Passive = YES --> */}

</Info>

<Tip>

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.

</Tip>

<Tip>

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.

</Tip>

<Tip>

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()
```

</Tip>

<SdkTip />

### 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
```

<ResponseField name="tz" type="string" required>
An IANA time zone identifier, such as `"America/New_York"`.
</ResponseField>

#### 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
```

<ResponseField name="tz" type="string" required>
An IANA time zone identifier, such as `"America/New_York"`.
</ResponseField>
146 changes: 146 additions & 0 deletions modus/sdk/go/localtime.mdx
Original file line number Diff line number Diff line change
@@ -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"

<SdkHeader language="Go" feature="Local Time" />

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
```

{/* <!-- vale Google.Headings = NO --> */}

## 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).

<Info>

{/* <!-- vale Google.Passive = NO --> */}

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.

{/* <!-- vale Google.Passive = YES --> */}

</Info>

<Tip>

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.

</Tip>

<Tip>

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.

</Tip>

<Tip>

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)
```

</Tip>

<Warning>

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.

</Warning>

<SdkTip />

### 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
```

<ResponseField name="tz" type="string" required>
An IANA time zone identifier, such as `"America/New_York"`.
</ResponseField>

#### 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)
```

<ResponseField name="tz" type="string" required>
An IANA time zone identifier, such as `"America/New_York"`.
</ResponseField>
3 changes: 2 additions & 1 deletion snippets/sdk-snippets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ export const SdkHeader = ({language, feature}) => (
<ul>
{(() =>{
const languages = ["AssemblyScript", "Go"];
const page = feature.toLowerCase().replace(/\W/g, "");
return languages.map((lang) => {
if (lang === language) {
return (
<li><b>{lang} {feature} APIs</b> (this page)</li>
)
} else {
return (
<li><a href={`../${lang.toLowerCase()}/${feature.toLowerCase()}`}>{lang} {feature} APIs</a></li>
<li><a href={`../${lang.toLowerCase()}/${page}`}>{lang} {feature} APIs</a></li>
)
}
})
Expand Down
4 changes: 4 additions & 0 deletions styles/config/vocabularies/general/accept.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ GraphQL|graphql
gRPC
HTTP|http
Hugging Face
IANA
Infof
ISO
LLM
Logf
MySQL|mysql
Expand All @@ -69,6 +71,8 @@ Scattergories
SDK|sdk
TLS
URL|url
UTC
urql
UUID
Warnf
WASM

0 comments on commit 00ca158

Please sign in to comment.