tinytime

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 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)

    // Convert Unix seconds to formatted date
    date := tp.UnixSecondsToDate(1624397134)
    println("Date:", date) // "2021-06-22 21:25"

    // Convert Unix nanoseconds to time
    time := tp.UnixNanoToTime(nano)
    println("Time:", time) // "15:32:14"
}

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
UnixNano() int64

Returns current Unix timestamp in nanoseconds.

nano := tp.UnixNano()
// Example: 1624397134562544800
UnixSecondsToDate(seconds int64) string

Converts Unix timestamp (seconds) to formatted date string.

date := tp.UnixSecondsToDate(1624397134)
// Returns: "2021-06-22 21:25"
UnixNanoToTime(input any) string

Converts Unix timestamp (nanoseconds) to time string. Accepts multiple input types.

// All of these work:
time1 := tp.UnixNanoToTime(int64(1624397134000000000))
time2 := tp.UnixNanoToTime(1624397134000000000) // int
time3 := tp.UnixNanoToTime("1624397134000000000") // string
// Returns: "17:25:34"

Supported input types: int64, int, float64, string

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. This results in significantly smaller binary sizes.

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

# Run tests in browser
go install github.com/cdvelop/wasmtest@latest
wasmtest.RunTests("./tests", nil, 5*time.Minute)

Testing

Run standard tests:

go test

Run WebAssembly tests in browser:

go test -tags=wasm

Use Cases

  • Frontend applications where binary size matters
  • TinyGo projects requiring time utilities
  • WebAssembly modules needing timestamp functionality
  • Cross-platform libraries that work in both Go and browser environments

Dependencies

  • github.com/cdvelop/tinystring (for string parsing in WASM)

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.
	// It creates a new JavaScript Date object, gets the timestamp in milliseconds,
	// converts it to nanoseconds, and returns the result as an int64.
	// eg: 1624397134562544800
	UnixNano() int64
	//	ts := int64(1609459200) // January 1, 2021 00:00:00 UTC
	//	formattedDate := UnixSecondsToDate(ts)
	//	println(formattedDate) // Output: "2021-01-01 00:00:00"
	UnixSecondsToDate(int64) string
	// UnixNanoToTime converts a Unix timestamp in nanoseconds to a formatted time string.
	// Format: "15:04:05" (hour:minute:second)
	// It accepts a parameter of type any and attempts to convert it to an int64 Unix timestamp in nanoseconds.
	// eg: 1624397134562544800 -> "15:32:14"
	// supported types: int64, int, float64, string
	UnixNanoToTime(any) string
}

TimeProvider define la interfaz para utilidades de tiempo implementada tanto para Go estándar como para WASM/JS.

func NewTimeProvider

func NewTimeProvider() TimeProvider

NewTimeProvider retorna la implementación correcta según el entorno de compilación.

Jump to

Keyboard shortcuts

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