Documentation
¶
Overview ¶
Package tz provides a TimeZone type based on IANA time zone database names.
Index ¶
- Variables
- type TimeZone
- func (z *TimeZone) LoadString(s string) error
- func (z TimeZone) Location() *time.Location
- func (z TimeZone) MarshalJSON() ([]byte, error)
- func (z TimeZone) MarshalText() (text []byte, err error)
- func (z *TimeZone) Scan(value any) error
- func (z TimeZone) String() string
- func (z *TimeZone) UnmarshalJSON(data []byte) error
- func (z *TimeZone) UnmarshalText(text []byte) error
- func (z TimeZone) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
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 ¶
Location returns the *time.Location for this TimeZone. The zero value (UTC) returns time.UTC; it never returns nil.
func (TimeZone) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. It encodes the time zone as a JSON string containing the IANA name.
func (TimeZone) MarshalText ¶
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 ¶
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 ¶
String returns the IANA time zone name, or "UTC" for the zero value. It implements the fmt.Stringer interface.
func (*TimeZone) UnmarshalJSON ¶
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 ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It decodes an IANA time zone name from text.
func (TimeZone) Value ¶
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>