tinytime

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2025 License: MIT Imports: 2 Imported by: 0

README

tinytime

TinyGo-compatible time package for Go, using syscall/js on WASM to keep binaries small and leverage native JS time APIs.

Minimal and portable time utility package for Go and TinyGo with WebAssembly support. When compiled for wasm (GOOS=js GOARCH=wasm), it uses syscall/js to access native JavaScript time APIs (Date.now, performance.now, etc.), drastically reducing binary size by avoiding the Go standard library. For non-WASM targets, it falls back to the standard time package. Ideal for frontend projects where binary size and compatibility with JavaScript environments matter.

API Usage

The tinytime package provides a TimeProvider interface that abstracts time operations for both standard Go and WebAssembly (WASM) environments.

NewTimeProvider() TimeProvider

This is the entry point of the library. It returns an implementation of the TimeProvider interface that is appropriate for the current build target (standard Go or WASM).

TimeProvider Interface

The TimeProvider interface has the following methods:

  • UnixNano() int64: Returns the current Unix timestamp in nanoseconds.

  • UnixSecondsToDate(unixSeconds int64) string: Converts a Unix timestamp in seconds to a formatted date string (YYYY-MM-DD HH:MM).

  • UnixNanoToTime(input any) string: Converts a Unix timestamp in nanoseconds to a formatted time string (HH:MM:SS). It accepts int64, int, float64, or a numeric string as input.

Example

Here is a basic example of how to use the tinytime library:

package main

import (
	"fmt"
	"github.com/cdvelop/tinytime"
)

func main() {
	// Get the time provider
	t := tinytime.NewTimeProvider()

	// Get the current time in Unix nanoseconds
	nano := t.UnixNano()
	fmt.Printf("Current Unix Nano: %d\n", nano)

	// Convert Unix seconds to a date string
	// Example timestamp for January 1, 2025 00:00:00 UTC
	seconds := int64(1735689600)
	dateStr := t.UnixSecondsToDate(seconds)
	fmt.Printf("Date from seconds: %s\n", dateStr)

	// Convert Unix nanoseconds to a time string
	timeStr := t.UnixNanoToTime(nano)
	fmt.Printf("Time from nanoseconds: %s\n", timeStr)
}

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