time

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 4 Imported by: 0

README

TinyTime

Project Badges

A minimal, portable time utility for Go and TinyGo with WebAssembly support. Automatically handles timezones and uses JavaScript Date APIs in WASM environments to keep binaries small.

Quick Start

import "github.com/tinywasm/time"

func main() {
    // Get current Unix timestamp in nanoseconds (UTC)
    nano := time.Now()
    println("Current time:", nano)

    // Format dates and times (Using detected local timezone)
    date := time.FormatDate(nano)
    println("Local Date:", date)

    timeStr := time.FormatTime(nano)
    println("Local Time:", timeStr)

    // Set custom timezone offset manually (e.g. UTC-3)
    time.SetTimeZoneOffset(-180) 
    println("New Local Time:", time.FormatTime(nano))

    // Parse date and time strings (Always UTC)
    parsedNano, err := time.ParseDate("2024-01-15")
    if err != nil {
        panic(err)
    }
    println("Parsed UTC Nano:", parsedNano)

    // Perform date calculations
    isToday := time.IsToday(nano)
    println("Is Today (Local)?", isToday)
}

Timezone Support

TinyTime automatically detects the local timezone offset:

  • Standard Go: Uses time.Now().Zone().
  • WASM: Uses JavaScript Date.getTimezoneOffset().
SetTimeZoneOffset(offsetMinutes int)

Sets the manual timezone offset in minutes from UTC.

GetTimeZoneOffset() int

Returns the current active timezone offset in minutes.


API Reference

Display Formatting

All formatting functions apply the current timezone offset to display local time.

FormatDate(value any) string

Formats a value into a date string: "YYYY-MM-DD".

  • int64: UnixNano timestamp.
  • string: Valid date string (passthrough).
FormatTime(value any) string

Formats a value into a time string "HH:MM:SS" (or "HH:MM" for int16).

  • int64: UnixNano timestamp.
  • int16: Minutes since midnight.
  • string: Valid time string (passthrough).
FormatDateTime(value any) string

Formats a value into a date-time string: "YYYY-MM-DD HH:MM:SS".

FormatDateTimeShort(value any) string

Formats a value into a short date-time string: "YYYY-MM-DD HH:MM".


Parsing

All parsing functions assume UTC input and return UTC timestamps.

ParseDate(dateStr string) (int64, error)

Parses a date string ("YYYY-MM-DD") into a UnixNano timestamp at midnight UTC.

ParseTime(timeStr string) (int16, error)

Parses a time string ("HH:MM" or "HH:MM:SS") into minutes since midnight UTC.

ParseDateTime(dateStr, timeStr string) (int64, error)

Combines date and time strings into a single UnixNano timestamp (UTC).


Current Time
Now() int64

Returns the current Unix timestamp in nanoseconds (UTC).


Date Utilities
IsToday(nano int64) bool

Checks if the given UnixNano timestamp is today based on the local timezone offset.

IsPast(nano int64) bool

Checks if the given UnixNano timestamp is in the past.

IsFuture(nano int64) bool

Checks if the given UnixNano timestamp is in the future.

DaysBetween(nano1, nano2 int64) int

Calculates the number of full days between two UnixNano timestamps.


Timers
AfterFunc(milliseconds int, f func()) Timer

Waits for the specified milliseconds then calls f. Returns a Timer that can be used to cancel the call.

Note: In WASM environments, the callback runs in the JavaScript event loop. Keep callbacks lightweight to avoid blocking the UI.

// Start a timer
timer := time.AfterFunc(1000, func() {
    println("1 second passed!")
})

// Stop the timer before it fires
timer.Stop()

WebAssembly Usage

When compiled for WebAssembly (GOOS=js GOARCH=wasm), tinytime automatically uses JavaScript's native Date APIs instead of bundling Go's time package.

# Build for WebAssembly
GOOS=js GOARCH=wasm go build -o app.wasm .

Testing

Always use gotest to run tests:

gotest

Dependencies

  • github.com/tinywasm/fmt

Contributing

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DaysBetween added in v0.3.0

func DaysBetween(nano1, nano2 int64) int

DaysBetween calculates the number of full days between two UnixNano timestamps.

func DaysBetweenShared added in v0.3.0

func DaysBetweenShared(nano1, nano2 int64) int

DaysBetweenShared is a shared helper function for calculating the number of full days between two timestamps.

func FormatDate added in v0.3.0

func FormatDate(value any) string

FormatDate formats a value into a date string "YYYY-MM-DD" applying the timezone offset.

func FormatDateTime added in v0.3.0

func FormatDateTime(value any) string

FormatDateTime formats a value into a date-time string "YYYY-MM-DD HH:MM:SS" applying the timezone offset.

func FormatDateTimeShort added in v0.3.0

func FormatDateTimeShort(value any) string

FormatDateTimeShort formats a value into a short date-time string "YYYY-MM-DD HH:MM".

func FormatTime added in v0.3.0

func FormatTime(value any) string

FormatTime formats a value into a time string "HH:MM:SS" applying the timezone offset.

func GetTimeZoneOffset added in v0.3.0

func GetTimeZoneOffset() int

GetTimeZoneOffset returns the current timezone offset in hours.

func IsFuture added in v0.3.0

func IsFuture(nano int64) bool

IsFuture checks if the given UnixNano timestamp is in the future.

func IsPast added in v0.3.0

func IsPast(nano int64) bool

IsPast checks if the given UnixNano timestamp is in the past.

func IsToday added in v0.3.0

func IsToday(nano int64) bool

IsToday checks if the given UnixNano timestamp is today according to the current timezone offset.

func Now added in v0.3.0

func Now() int64

Now retrieves the current Unix timestamp in nanoseconds in UTC.

func ParseDate added in v0.3.0

func ParseDate(dateStr string) (int64, error)

ParseDate parses a date string ("YYYY-MM-DD") into a UnixNano timestamp (UTC).

func ParseDateTime added in v0.3.0

func ParseDateTime(dateStr, timeStr string) (int64, error)

ParseDateTime combines date and time strings into a single UnixNano timestamp (UTC).

func ParseTime added in v0.3.0

func ParseTime(timeStr string) (int16, error)

ParseTime parses a time string into minutes since midnight (UTC).

func SetTimeZoneOffset added in v0.3.0

func SetTimeZoneOffset(hours int)

SetTimeZoneOffset manually sets the timezone offset in hours.

Types

type Timer

type Timer interface {
	// Stop prevents the timer from firing. Returns true if the timer was active.
	Stop() bool
}

Timer represents a cancelable timer.

func AfterFunc added in v0.3.0

func AfterFunc(milliseconds int, f func()) Timer

AfterFunc waits for the specified milliseconds then calls f.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL