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 ¶
- Constants
- type DateTime
- type DateTimeType
- type Duration
- type TANSIC
- type TDateOnly
- type TDateTime
- type TJira
- type TKitchen
- type TLayout
- type TRFC1123
- type TRFC1123Z
- type TRFC3339
- type TRFC3339Nano
- type TRFC822
- type TRFC822Z
- type TRFC850
- type TRubyDate
- type TStamp
- type TStampMicro
- type TStampMilli
- type TStampNano
- type TTimeOnly
- type TUnixDate
Examples ¶
Constants ¶
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 ¶
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 ¶
String returns a string representing the date in the format returned by T.Format().
func (*DateTime[T]) UnmarshalJSON ¶
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 ¶
Duration is an alias for the standard time.Duration.
func (Duration) MarshalJSON ¶
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 ¶
String returns a string representing the duration in the form "72h3m0.5s". It is a wrapper for time.Duration.String().
func (*Duration) UnmarshalJSON ¶
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 TRFC3339Nano ¶
type TRFC3339Nano struct{}
TRFC3339Nano represents the RFC3339Nano time type.
func (TRFC3339Nano) Format ¶
func (TRFC3339Nano) Format() string
Format returns the RFC3339Nano 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.