timezone

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 4 Imported by: 47

README

go-timezone

GoDocWidget Test


go-timezone is timezone utility for Go.

It has the following features:

  • This library uses only the standard package
  • Supports getting offset from timezone abbreviation, which is not supported by the time package
  • Determine whether the specified time.Time is daylight saving time
  • Change the location of time.Time by specifying the timezone

See godoc for usage.

Data source

https://github.com/tkuchiki/timezones

Contributors

Documentation

Overview

Package timezone provides utility for timezone.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrAmbiguousTzAbbreviations indicates ambiguous timezone abbreviations.
	ErrAmbiguousTzAbbreviations = errors.New("Ambiguous timezone abbreviations")
)

Functions

This section is empty.

Types

type Timezone added in v0.2.0

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

Timezone represents a timezone information.

func New added in v0.2.0

func New() *Timezone

New creates a new Timezone.

func (*Timezone) FixedTimezone added in v0.2.0

func (tz *Timezone) FixedTimezone(t time.Time, timezone string) (time.Time, error)

FixedTimezone returns the time.Time with the given timezone set from the time.Location.

Example
package main

import (
	"fmt"
	"time"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()

	_time := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
	fixedTime, _ := tz.FixedTimezone(_time, "Europe/Belgrade")

	fmt.Println(fixedTime)
}
Output:
2020-01-01 01:00:00 +0100 CET

func (*Timezone) GetAllTimezones deprecated added in v0.2.0

func (tz *Timezone) GetAllTimezones() map[string][]string

GetAllTimezones returns the timezones.

Deprecated: Use Timezones.

func (*Timezone) GetOffset deprecated added in v0.2.0

func (tz *Timezone) GetOffset(abbr string, dst ...bool) (int, error)

GetOffset returns the timezone offset with the given timezone abbreviation. If also given dst=true, returns the daylight savings timezone offset. Returns a ErrAmbiguousTzAbbreviations error if timezone abbreviation has more than one meaning.

Deprecated: Use GetTzAbbreviationInfo or GetTzAbbreviationInfoByTZName

func (*Timezone) GetTimezoneAbbreviation added in v0.2.0

func (tz *Timezone) GetTimezoneAbbreviation(timezone string, dst ...bool) (string, error)

GetTimezoneAbbreviation returns the timezone abbreviation with the given timezone. If also given dst=true, returns the daylight savings timezone abbreviation.

Example
package main

import (
	"fmt"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()

	abbr, _ := tz.GetTimezoneAbbreviation("Europe/London")

	fmt.Println(abbr)
}
Output:
GMT
Example (Dst)
package main

import (
	"fmt"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()

	abbr, _ := tz.GetTimezoneAbbreviation("Europe/London", true)

	fmt.Println(abbr)
}
Output:
BST

func (*Timezone) GetTimezones added in v0.2.0

func (tz *Timezone) GetTimezones(abbr string) ([]string, error)

GetTimezones returns the timezones with the given timezone abbreviation.

Example
package main

import (
	"fmt"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()
	timezones, _ := tz.GetTimezones("UTC")

	fmt.Println(timezones)
}
Output:
[Etc/UCT Etc/UTC Etc/Universal Etc/Zulu UCT UTC Universal Zulu]

func (*Timezone) GetTzAbbreviationInfo added in v0.2.0

func (tz *Timezone) GetTzAbbreviationInfo(abbr string) ([]*TzAbbreviationInfo, error)

GetTzAbbreviationInfo returns the slice of TzAbbreviationInfo with the given timezone abbreviation. Returns a ErrAmbiguousTzAbbreviations error if timezone abbreviation has more than one meaning.

Example
package main

import (
	"fmt"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()
	tzAbbrInfos, _ := tz.GetTzAbbreviationInfo("EET")

	fmt.Println(tzAbbrInfos[0].Name())
	fmt.Println(tzAbbrInfos[0].Offset())
	fmt.Println(tzAbbrInfos[0].OffsetHHMM())
}
Output:
Eastern European Time/Eastern European Standard Time
7200
+02:00
Example (AmbiguousTimezoneAbbreviationsError)
package main

import (
	"fmt"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()
	tzAbbrInfos, _ := tz.GetTzAbbreviationInfo("BST")

	fmt.Println(tzAbbrInfos[0].Name())
	fmt.Println(tzAbbrInfos[0].Offset())
	fmt.Println(tzAbbrInfos[0].OffsetHHMM())
	fmt.Println(tzAbbrInfos[1].Name())
	fmt.Println(tzAbbrInfos[1].Offset())
	fmt.Println(tzAbbrInfos[1].OffsetHHMM())
	fmt.Println(tzAbbrInfos[2].Name())
	fmt.Println(tzAbbrInfos[2].Offset())
	fmt.Println(tzAbbrInfos[2].OffsetHHMM())
}
Output:
Bolivia Summer Time
-12756
-03:27
British Summer Time
3600
+01:00
Bougainville Standard Time
39600
+11:00

func (*Timezone) GetTzAbbreviationInfoByTZName added in v0.2.0

func (tz *Timezone) GetTzAbbreviationInfoByTZName(abbr, tzname string) (*TzAbbreviationInfo, error)

GetTzAbbreviationInfoByTZName returns the TzAbbreviationInfo with the given timezone abbreviation and timezone name. Even if timezone abbreviation has more than one meanings, it can be identified by tzname.

Example
package main

import (
	"fmt"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()
	tzAbbrInfo, _ := tz.GetTzAbbreviationInfoByTZName("BST", "British Summer Time")

	fmt.Println(tzAbbrInfo.Name())
	fmt.Println(tzAbbrInfo.Offset())
	fmt.Println(tzAbbrInfo.OffsetHHMM())
}
Output:
British Summer Time
3600
+01:00

func (*Timezone) GetTzInfo added in v0.2.0

func (tz *Timezone) GetTzInfo(timezone string) (*TzInfo, error)

GetTzInfo returns the TzInfo with the given timezone.

Example
package main

import (
	"fmt"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()
	tzInfo, _ := tz.GetTzInfo("Europe/London")

	fmt.Println(tzInfo.LongStandard())
	fmt.Println(tzInfo.ShortStandard())
	fmt.Println(tzInfo.StandardOffset())
	fmt.Println(tzInfo.StandardOffsetHHMM())
	fmt.Println(tzInfo.LongDaylight())
	fmt.Println(tzInfo.ShortDaylight())
	fmt.Println(tzInfo.DaylightOffset())
	fmt.Println(tzInfo.DaylightOffsetHHMM())
}
Output:
Greenwich Mean Time
GMT
0
+00:00
British Summer Time
BST
3600
+01:00

func (*Timezone) IsDST added in v0.2.1

func (tz *Timezone) IsDST(t time.Time) bool

IsDST returns whether a given time is daylight saving time or not.

Example
package main

import (
	"fmt"
	"time"

	"github.com/tkuchiki/go-timezone"
)

func main() {
	tz := timezone.New()

	loc, _ := time.LoadLocation("America/New_York")
	_time := time.Date(2021, 7, 1, 0, 0, 0, 0, loc)
	isDST := tz.IsDST(_time)

	_time = time.Date(2021, 1, 1, 0, 0, 0, 0, loc)
	isNotDST := tz.IsDST(_time)
	fmt.Println(isDST)
	fmt.Println(isNotDST)
}
Output:
true
false

func (*Timezone) Timezones added in v0.2.0

func (tz *Timezone) Timezones() map[string][]string

Timezones returns the all timezones.

func (*Timezone) TzAbbrInfos added in v0.2.0

func (tz *Timezone) TzAbbrInfos() map[string][]*TzAbbreviationInfo

TzAbbrInfos returns the all tzAbbrInfos.

func (*Timezone) TzInfos added in v0.2.0

func (tz *Timezone) TzInfos() map[string]*TzInfo

TzInfos returns the all tzInfos.

type TzAbbreviationInfo added in v0.2.0

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

TzAbbreviationInfo represents timezone abbreviation information about a particular timezone.

func (*TzAbbreviationInfo) CountryCode added in v0.2.0

func (tai *TzAbbreviationInfo) CountryCode() string

CountryCode returns the ISO 3166-1 alpha-2 country code. It returns the empty string if there is no CountryCode.

func (*TzAbbreviationInfo) IsDST added in v0.2.0

func (tai *TzAbbreviationInfo) IsDST() bool

IsDST reports whether or not it is daylight savings time.

func (*TzAbbreviationInfo) Name added in v0.2.0

func (tai *TzAbbreviationInfo) Name() string

Name returns the time name.

func (*TzAbbreviationInfo) Offset added in v0.2.0

func (tai *TzAbbreviationInfo) Offset() int

Offset returns the time offset.

func (*TzAbbreviationInfo) OffsetHHMM added in v0.2.0

func (tai *TzAbbreviationInfo) OffsetHHMM() string

OffsetHHMM returns the time offset in (+/-)hh:mm format.

type TzInfo added in v0.2.0

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

TzInfo represents information about a particular timezone.

func (*TzInfo) CountryCode added in v0.2.0

func (ti *TzInfo) CountryCode() string

CountryCode returns the ISO 3166-1 alpha-2 country code. It returns the empty string if there is no CountryCode.

func (*TzInfo) DaylightOffset added in v0.2.0

func (ti *TzInfo) DaylightOffset() int

DaylightOffset returns the daylight saving time offset It returns the 0 if there is no daylight saving.

func (*TzInfo) DaylightOffsetHHMM added in v0.2.0

func (ti *TzInfo) DaylightOffsetHHMM() string

DaylightOffsetHHMM returns the daylight saving time offset in (+/-)hh:mm format. It returns the "+00:00" if there is no daylight saving.

func (*TzInfo) HasDST added in v0.2.0

func (ti *TzInfo) HasDST() bool

HasDST reports whether or not it has daylight savings time.

func (*TzInfo) IsDeprecated added in v0.2.0

func (ti *TzInfo) IsDeprecated() bool

IsDeprecated reports whether the timezone is deprecated.

func (*TzInfo) LastDST added in v0.2.0

func (ti *TzInfo) LastDST() int

LastDST returns the last year when there was daylight savings time. It returns the 0 if has never observed daylight savings.

func (*TzInfo) LinkTo added in v0.2.0

func (ti *TzInfo) LinkTo() string

LinkTo returns the source of the alias. It returns the empty string if there is no alias.

func (*TzInfo) LongDaylight added in v0.2.0

func (ti *TzInfo) LongDaylight() string

LongDaylight returns the daylight saving time name. It returns the empty string if there is no daylight saving time name.

func (*TzInfo) LongGeneric added in v0.2.0

func (ti *TzInfo) LongGeneric() string

LongGeneric returns the generic time name. It returns the empty string if there is no generic time name.

func (*TzInfo) LongStandard added in v0.2.0

func (ti *TzInfo) LongStandard() string

LongStandard returns the standard time name.

func (*TzInfo) ShortDaylight added in v0.2.0

func (ti *TzInfo) ShortDaylight() string

ShortDaylight returns the daylight saving timezone abbreviation. It returns the empty string if there is no daylight saving timezone abbreviation.

func (*TzInfo) ShortGeneric added in v0.2.0

func (ti *TzInfo) ShortGeneric() string

ShortGeneric returns the generic timezone abbreviation. It returns the empty string if there is no generic timezone abbreviation.

func (*TzInfo) ShortStandard added in v0.2.0

func (ti *TzInfo) ShortStandard() string

ShortStandard returns the standard timezone abbreviation.

func (*TzInfo) StandardOffset added in v0.2.0

func (ti *TzInfo) StandardOffset() int

StandardOffset returns the standard time offset

func (*TzInfo) StandardOffsetHHMM added in v0.2.0

func (ti *TzInfo) StandardOffsetHHMM() string

StandardOffsetHHMM returns the standard time offset in (+/-)hh:mm format.

Jump to

Keyboard shortcuts

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