tz

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2022 License: MIT Imports: 7 Imported by: 0

README

Golang Time Zone Type

Go Reference

Features

  • TimeZone based on time.LoadLocation format, Not support the "Local" time zone.
  • The format is "UTC" or IANA time zone database name. See: https://www.iana.org/time-zones.
  • The zero value means UTC time zone.
  • The UTC time zone always uses a zero value.
  • Implement the fmt.Stringer interface.
  • Implement the sql.Scanner interface.
  • Implement the driver.Valuer interface.
  • Implement the encoding.TextMarshaler interface.
  • Implement the encoding.TextUnmarshaler interface.
  • Implement the json.Marshaler interface.
  • Implement the json.Unmarshaler interface.

Installation

go get github.com/min0625/tz

Quick start

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

Index

Examples

Constants

This section is empty.

Variables

View Source
var UTCTimeZone = TimeZone{}

Functions

This section is empty.

Types

type TimeZone

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

TimeZone based on time.LoadLocation format, Not support the "Local" time zone. The format is "UTC" or IANA time zone database name. See: https://www.iana.org/time-zones. The zero value means UTC time zone. The UTC time zone always uses a 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)

func (TimeZone) Location

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

Location always returns a non-nil location.

func (TimeZone) MarshalJSON

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

func (TimeZone) MarshalText

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

func (*TimeZone) Scan

func (z *TimeZone) Scan(value any) 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

func (*TimeZone) UnmarshalJSON

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

func (*TimeZone) UnmarshalText

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

func (TimeZone) Value

func (z TimeZone) Value() (driver.Value, error)
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