tinytime

package module
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 2 Imported by: 0

README

TinyTime

Project Badges

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

Quick Start

import "github.com/cdvelop/tinytime"

func main() {
    tp := tinytime.NewTimeProvider()

    // Get current Unix timestamp in nanoseconds
    nano := tp.UnixNano()
    println("Current time:", nano)

    // Format dates and times
    date := tp.FormatDate(nano)
    println("Date:", date) // "2024-01-15"

    timeStr := tp.FormatTime(int16(510)) // 8:30 in minutes
    println("Time:", timeStr) // "08:30"

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

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

API Reference

NewTimeProvider() TimeProvider

Creates a time provider instance. Automatically selects the appropriate implementation:

  • WASM: Uses JavaScript Date APIs (smaller binaries)
  • Standard Go: Uses time package

Display Formatting
FormatDate(value any) string

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

  • int64: UnixNano timestamp.
  • string: Valid date string (passthrough).
date := tp.FormatDate(1705306200000000000) // "2024-01-15"
FormatTime(value any) string

Formats a value into a time string.

  • int64: UnixNano timestamp ("HH:MM:SS").
  • int16: Minutes since midnight ("HH:MM").
  • string: Valid time string (passthrough).
timeStr := tp.FormatTime(int16(510)) // "08:30"
FormatDateTime(value any) string

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

  • int64: UnixNano timestamp.
  • string: Valid date-time string (passthrough).
dateTime := tp.FormatDateTime(1705307400000000000) // "2024-01-15 08:30:00"
FormatDateTimeShort(value any) string

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

  • int64: UnixNano timestamp.
  • string: Valid short date-time string (passthrough).
dateTime := tp.FormatDateTimeShort(1705307400000000000) // "2024-01-15 08:30"

Parsing
ParseDate(dateStr string) (int64, error)

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

nano, err := tp.ParseDate("2024-01-15")
ParseTime(timeStr string) (int16, error)

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

minutes, err := tp.ParseTime("08:30") // 510
ParseDateTime(dateStr, timeStr string) (int64, error)

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

nano, err := tp.ParseDateTime("2024-01-15", "08:30")

Current Time
UnixNano() int64

Returns the current Unix timestamp in nanoseconds.

nano := tp.UnixNano()

Date Utilities
IsToday(nano int64) bool

Checks if the given UnixNano timestamp is today.

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 := tp.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 .

# Run tests in browser
go test -tags=wasm

Testing

Run standard tests:

go test ./...

Run WebAssembly tests:

GOOS=js GOARCH=wasm go test ./...

For detailed browser testing instructions, see BROWSER_TEST.md.

Dependencies

  • github.com/cdvelop/tinystring

Contributing

License

MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TimeProvider

type TimeProvider interface {
	// UnixNano retrieves the current Unix timestamp in nanoseconds.
	// e.g., 1624397134562544800
	UnixNano() int64

	// FormatDate formats a value into a date string: "YYYY-MM-DD".
	// Accepts: int64 (UnixNano), string ("2024-01-15").
	FormatDate(value any) string

	// FormatTime formats a value into a time string.
	// Accepts: int64 (UnixNano) -> "HH:MM:SS", int16 (minutes) -> "HH:MM", string ("08:30").
	FormatTime(value any) string

	// FormatDateTime formats a value into a date-time string: "YYYY-MM-DD HH:MM:SS".
	// Accepts: int64 (UnixNano), string ("2024-01-15 08:30:45").
	FormatDateTime(value any) string

	// FormatDateTimeShort formats a value into a short date-time string: "YYYY-MM-DD HH:MM" (without seconds).
	// Accepts: int64 (UnixNano), string ("2024-01-15 08:30").
	FormatDateTimeShort(value any) string

	// ParseDate parses a date string ("YYYY-MM-DD") into a UnixNano timestamp (at midnight UTC).
	ParseDate(dateStr string) (int64, error)

	// ParseTime parses a time string ("HH:MM" or "HH:MM:SS") into minutes since midnight.
	ParseTime(timeStr string) (int16, error)

	// ParseDateTime combines date and time strings into a single UnixNano timestamp (UTC).
	ParseDateTime(dateStr, timeStr string) (int64, error)

	// IsToday checks if the given UnixNano timestamp is today (UTC).
	IsToday(nano int64) bool

	// IsPast checks if the given UnixNano timestamp is in the past.
	IsPast(nano int64) bool

	// IsFuture checks if the given UnixNano timestamp is in the future.
	IsFuture(nano int64) bool

	// DaysBetween calculates the number of full days between two UnixNano timestamps.
	DaysBetween(nano1, nano2 int64) int

	// AfterFunc waits for the specified milliseconds then calls f.
	// Returns a Timer that can be used to cancel the call.
	// WARNING: In WASM, callback runs in JS event loop - keep it lightweight.
	AfterFunc(milliseconds int, f func()) Timer
}

TimeProvider defines the interface for time utilities, implemented for both standard Go and WASM/JS environments.

func NewTimeProvider

func NewTimeProvider() TimeProvider

NewTimeProvider returns the correct implementation based on the build environment.

type Timer added in v0.2.6

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

Timer represents a cancelable timer

Jump to

Keyboard shortcuts

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