tz

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 7 Imported by: 0

README

Golang Time Zone Type

Go Reference

Features

  • Based on time.LoadLocation format; the "Local" time zone is not supported.
  • Accepts "UTC", an empty string, or any IANA time zone database name (e.g. "America/New_York"). See: https://www.iana.org/time-zones.
  • The zero value represents the UTC time zone; loading "UTC" or "" always produces the zero value.
  • Implements fmt.Stringer
  • Implements sql.Scanner
  • Implements driver.Valuer
  • Implements encoding.TextMarshaler
  • Implements encoding.TextUnmarshaler
  • Implements json.Marshaler
  • Implements json.Unmarshaler

Installation

go get github.com/min0625/tz

Quick start

Note: Import _ "time/tzdata" to embed the IANA time zone database directly into your binary, so it works correctly in environments where the system timezone data may be absent (e.g. scratch/Alpine containers).

package main

import (
	"fmt"
	"time"
	_ "time/tzdata"

	"github.com/min0625/tz"
)

func main() {
	z, err := tz.LoadTimeZone("America/New_York")
	if err != nil {
		panic(err)
	}

	fmt.Println(z.String())
	fmt.Println(time.Time{}.In(z.Location()).Location().String())

	// Output:
	// America/New_York
	// America/New_York
}

Example

See: ./time_zone_example_test.go

Documentation

Overview

Package tz provides a TimeZone type based on IANA time zone database names.

Index

Examples

Constants

This section is empty.

Variables

View Source
var UTCTimeZone = TimeZone{}

UTCTimeZone is the zero value TimeZone representing the UTC time zone.

Functions

This section is empty.

Types

type TimeZone

type TimeZone struct {
	// contains filtered or unexported fields
}

TimeZone represents an IANA time zone based on the time.LoadLocation format. The "Local" time zone is not supported. Valid values are "UTC" or an IANA time zone database name (e.g. "America/New_York"). See: https://www.iana.org/time-zones. The zero value represents the UTC time zone. Loading "UTC" or an empty string always results in the zero value.

Example
package main

import (
	"fmt"
	"time"

	"github.com/min0625/tz"
	_ "time/tzdata"
)

func main() {
	z, err := tz.LoadTimeZone("America/New_York")
	if err != nil {
		panic(err)
	}

	fmt.Println(z.String())
	fmt.Println(time.Time{}.In(z.Location()).Location().String())

}
Output:
America/New_York
America/New_York
Example (ZeroValue)
package main

import (
	"fmt"

	"github.com/min0625/tz"
	_ "time/tzdata"
)

func main() {
	z, err := tz.LoadTimeZone("UTC")
	if err != nil {
		panic(err)
	}

	fmt.Println(z == tz.TimeZone{})

}
Output:
true

func LoadTimeZone

func LoadTimeZone(name string) (TimeZone, error)

LoadTimeZone loads a TimeZone by IANA time zone name. An empty string and "UTC" both return the zero value. Returns an error if the name is invalid or equals "Local".

func (*TimeZone) LoadString added in v0.2.0

func (z *TimeZone) LoadString(s string) error

LoadString loads a TimeZone by IANA time zone name. An empty string and "UTC" both set z to the zero value. Returns an error if the name is invalid or equals "Local". On error z is not modified.

func (TimeZone) Location

func (z TimeZone) Location() *time.Location

Location returns the *time.Location for this TimeZone. The zero value (UTC) returns time.UTC; it never returns nil.

func (TimeZone) MarshalJSON

func (z TimeZone) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. It encodes the time zone as a JSON string containing the IANA name.

func (TimeZone) MarshalText

func (z TimeZone) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface. It encodes the time zone as its IANA name (e.g. "America/New_York" or "UTC").

func (*TimeZone) Scan

func (z *TimeZone) Scan(value any) error

Scan implements the sql.Scanner interface. It accepts a string or []byte value containing an IANA time zone name. NULL values are not supported and will return an error.

Example
package main

import (
	"fmt"
	"time"

	"github.com/min0625/tz"
	_ "time/tzdata"
)

func main() {
	var z tz.TimeZone
	if err := z.Scan("America/New_York"); err != nil {
		panic(err)
	}

	fmt.Println(time.Time{}.In(z.Location()).Location().String())

}
Output:
America/New_York

func (TimeZone) String

func (z TimeZone) String() string

String returns the IANA time zone name, or "UTC" for the zero value. It implements the fmt.Stringer interface.

func (*TimeZone) UnmarshalJSON

func (z *TimeZone) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. It decodes an IANA time zone name from a JSON string. A JSON null value is ignored and leaves the receiver unchanged.

func (*TimeZone) UnmarshalText

func (z *TimeZone) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. It decodes an IANA time zone name from text.

func (TimeZone) Value

func (z TimeZone) Value() (driver.Value, error)

Value implements the driver.Valuer interface. It always returns the IANA time zone name as a string driver.Value.

Example
package main

import (
	"fmt"

	"github.com/min0625/tz"
	_ "time/tzdata"
)

func main() {
	z, err := tz.LoadTimeZone("America/New_York")
	if err != nil {
		panic(err)
	}

	v, err := z.Value()
	fmt.Printf("%v %T %v\n", v, v, err)

}
Output:
America/New_York string <nil>
Example (ZeroValue)
package main

import (
	"fmt"

	"github.com/min0625/tz"
	_ "time/tzdata"
)

func main() {
	var z tz.TimeZone

	v, err := z.Value()
	fmt.Printf("%v %T %v\n", v, v, err)

}
Output:
UTC string <nil>

Jump to

Keyboard shortcuts

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