timeutil

package
v1.111.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package timeutil provides utility functions for working with time-related operations.

This package offers a collection of functions that facilitate working with time.Duration and time.Time types when marshaling and unmarshaling JSON data. It includes a generic DateTime type that allows for flexible formatting and parsing of time values based on specified layouts.

The DateTime type is defined as a generic type that wraps time.Time and provides methods for JSON marshaling and unmarshaling using a specified time format. The format is determined by the type parameter, which must implement the DateTimeType interface. This interface requires a Format method that returns the desired time format string.

Commonly used time formats are provided as types that implement the DateTimeType interface.

Index

Examples

Constants

View Source
const TimeJiraFormat = "2006-01-02T15:04:05.000-0700"

TimeJiraFormat is the Jira date-time format string.

Variables

This section is empty.

Functions

This section is empty.

Types

type DateTime

type DateTime[T DateTimeType] time.Time

DateTime is a generic type that wraps time.Time and provides JSON marshaling/unmarshaling using the specified time format defined by the type parameter T.

func (DateTime[T]) MarshalJSON

func (d DateTime[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

Example
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"time"

	"github.com/tecnickcom/gogen/pkg/timeutil"
)

func main() {
	dt := timeutil.DateTime[timeutil.TRFC3339](time.Date(2023, 1, 2, 15, 4, 5, 0, time.UTC))

	b, err := json.Marshal(dt)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(b))

}
Output:

"2023-01-02T15:04:05Z"

func (DateTime[T]) String

func (d DateTime[T]) String() string

String returns a string representing the date in the format returned by T.Format().

func (DateTime[T]) Time

func (d DateTime[T]) Time() time.Time

Time returns the underlying time.Time value.

func (*DateTime[T]) UnmarshalJSON

func (d *DateTime[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

Example
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/tecnickcom/gogen/pkg/timeutil"
)

func main() {
	var dt timeutil.DateTime[timeutil.TRFC3339]

	data := []byte(`"2023-01-02T15:04:05Z"`)

	err := json.Unmarshal(data, &dt)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(dt.String())

}
Output:

2023-01-02T15:04:05Z

type DateTimeType

type DateTimeType interface {
	Format() string
}

DateTimeType is an interface that defines a method to return a time format string.

type Duration

type Duration time.Duration

Duration is an alias for the standard time.Duration.

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON returns d as the JSON encoding of d. It encodes the time.Duration in human readable format (e.g.: 20s, 1h, ...).

Example
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"time"

	"github.com/tecnickcom/gogen/pkg/timeutil"
)

func main() {
	data := timeutil.Duration(7*time.Hour + 11*time.Minute + 13*time.Second)

	enc, err := json.Marshal(data)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(enc))

}
Output:

"7h11m13s"

func (Duration) String

func (d Duration) String() string

String returns a string representing the duration in the form "72h3m0.5s". It is a wrapper for time.Duration.String().

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(data []byte) error

UnmarshalJSON sets *d to a copy of data. It converts human readable time duration format (e.g.: 20s, 1h, ...) in standard time.Duration.

Example
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/tecnickcom/gogen/pkg/timeutil"
)

func main() {
	var d timeutil.Duration

	data := []byte(`"7h11m13s"`)

	err := json.Unmarshal(data, &d)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(d.String())

}
Output:

7h11m13s

type TANSIC

type TANSIC struct{}

TANSIC represents the ANSIC time type.

func (TANSIC) Format

func (TANSIC) Format() string

Format returns the ANSIC format string.

type TDateOnly

type TDateOnly struct{}

TDateOnly represents the DateOnly time type.

func (TDateOnly) Format

func (TDateOnly) Format() string

Format returns the DateOnly format string.

type TDateTime

type TDateTime struct{}

TDateTime represents the DateTime time type.

func (TDateTime) Format

func (TDateTime) Format() string

Format returns the DateTime format string.

type TJira

type TJira struct{}

TJira represents the Jira time type.

func (TJira) Format

func (TJira) Format() string

Format returns the Jira format string.

type TKitchen

type TKitchen struct{}

TKitchen represents the Kitchen time type.

func (TKitchen) Format

func (TKitchen) Format() string

Format returns the Kitchen format string.

type TLayout

type TLayout struct{}

TLayout represents the Layout time type.

func (TLayout) Format

func (TLayout) Format() string

Format returns the Layout format string.

type TRFC1123

type TRFC1123 struct{}

TRFC1123 represents the RFC1123 time type.

func (TRFC1123) Format

func (TRFC1123) Format() string

Format returns the RFC1123 format string.

type TRFC1123Z

type TRFC1123Z struct{}

TRFC1123Z represents the RFC1123Z time type.

func (TRFC1123Z) Format

func (TRFC1123Z) Format() string

Format returns the RFC1123Z format string.

type TRFC3339

type TRFC3339 struct{}

TRFC3339 represents the RFC3339 time type.

func (TRFC3339) Format

func (TRFC3339) Format() string

Format returns the RFC3339 format string.

type TRFC3339Nano

type TRFC3339Nano struct{}

TRFC3339Nano represents the RFC3339Nano time type.

func (TRFC3339Nano) Format

func (TRFC3339Nano) Format() string

Format returns the RFC3339Nano format string.

type TRFC822

type TRFC822 struct{}

TRFC822 represents the RFC822 time type.

func (TRFC822) Format

func (TRFC822) Format() string

Format returns the RFC822 format string.

type TRFC822Z

type TRFC822Z struct{}

TRFC822Z represents the RFC822Z time type.

func (TRFC822Z) Format

func (TRFC822Z) Format() string

Format returns the RFC822Z format string.

type TRFC850

type TRFC850 struct{}

TRFC850 represents the RFC850 time type.

func (TRFC850) Format

func (TRFC850) Format() string

Format returns the RFC850 format string.

type TRubyDate

type TRubyDate struct{}

TRubyDate represents the RubyDate time type.

func (TRubyDate) Format

func (TRubyDate) Format() string

Format returns the RubyDate format string.

type TStamp

type TStamp struct{}

TStamp represents the Stamp time type.

func (TStamp) Format

func (TStamp) Format() string

Format returns the Stamp format string.

type TStampMicro

type TStampMicro struct{}

TStampMicro represents the StampMicro time type.

func (TStampMicro) Format

func (TStampMicro) Format() string

Format returns the StampMicro format string.

type TStampMilli

type TStampMilli struct{}

TStampMilli represents the StampMilli time type.

func (TStampMilli) Format

func (TStampMilli) Format() string

Format returns the StampMilli format string.

type TStampNano

type TStampNano struct{}

TStampNano represents the StampNano time type.

func (TStampNano) Format

func (TStampNano) Format() string

Format returns the StampNano format string.

type TTimeOnly

type TTimeOnly struct{}

TTimeOnly represents the TimeOnly time type.

func (TTimeOnly) Format

func (TTimeOnly) Format() string

Format returns the TimeOnly format string.

type TUnixDate

type TUnixDate struct{}

TUnixDate represents the UnixDate time type.

func (TUnixDate) Format

func (TUnixDate) Format() string

Format returns the UnixDate format string.

Jump to

Keyboard shortcuts

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