Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A small Go library providing a `TimeZone` type backed by the IANA time zone data
- **Zero value is UTC** — `TimeZone{}` represents UTC; loading `"UTC"` or `""` always yields the zero value.
- **IANA names** — accepts any [IANA time zone database](https://www.iana.org/time-zones) name (e.g. `"America/New_York"`).
- **No `"Local"`** — the `"Local"` time zone is always rejected with an error.
- **Comparable** — `TimeZone` is a plain struct; use `==` for equality checks.
- **Equality** — compare values with the `Equal` method; `==` is only reliable against the zero value, since a `TimeZone` holds a `*time.Location` pointer.
- **Rich interface support** — implements `fmt.Stringer`, `sql.Scanner`, `driver.Valuer`, `encoding.TextMarshaler/Unmarshaler`, and `json.Marshaler/Unmarshaler`.
- **No dependencies** — built entirely on the standard library.

Expand Down Expand Up @@ -49,7 +49,11 @@ func main() {
fmt.Println(z) // America/New_York
fmt.Println(time.Now().In(z.Location()).Location()) // America/New_York

// UTC is the zero value
// Compare with Equal; == is unreliable for loaded zones.
other := tz.MustLoadTimeZone("America/New_York")
fmt.Println(z.Equal(other)) // true

// UTC is the zero value.
utc, _ := tz.LoadTimeZone("UTC")
fmt.Println(utc == tz.TimeZone{}) // true
}
Expand Down
8 changes: 6 additions & 2 deletions README.zh-TW.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- **零值即 UTC** — `TimeZone{}` 代表 UTC;載入 `"UTC"` 或 `""` 必定回傳零值。
- **IANA 名稱** — 接受任何 [IANA 時區資料庫](https://www.iana.org/time-zones) 名稱(例如 `"America/New_York"`)。
- **不支援 `"Local"`** — `"Local"` 時區一律以錯誤拒絕。
- **可比較** — `TimeZone` 為一般結構體;可直接使用 `==` 進行相等判斷
- **相等判斷** — 請使用 `Equal` 方法進行比較;由於 `TimeZone` 內含 `*time.Location` 指標,`==` 僅適用於與零值比較
- **豐富的介面支援** — 實作 `fmt.Stringer`、`sql.Scanner`、`driver.Valuer`、`encoding.TextMarshaler/Unmarshaler` 及 `json.Marshaler/Unmarshaler`。
- **零相依** — 完全建構於標準函式庫之上。

Expand Down Expand Up @@ -49,7 +49,11 @@ func main() {
fmt.Println(z) // America/New_York
fmt.Println(time.Now().In(z.Location()).Location()) // America/New_York

// UTC 為零值
// 請使用 Equal 進行比較;對已載入的時區而言 == 並不可靠。
other := tz.MustLoadTimeZone("America/New_York")
fmt.Println(z.Equal(other)) // true

// UTC 為零值。
utc, _ := tz.LoadTimeZone("UTC")
fmt.Println(utc == tz.TimeZone{}) // true
}
Expand Down
34 changes: 34 additions & 0 deletions time_zone_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ func ExampleTimeZone_Equal() {
// true
}

func ExampleLoadTimeZone_errors() {
// "Local" is always rejected.
_, err := tz.LoadTimeZone("Local")
fmt.Println(err != nil)

// An unknown name is rejected too.
_, err = tz.LoadTimeZone("Not/A_Zone")
fmt.Println(err != nil)

// Output:
// true
// true
}

func ExampleMustLoadTimeZone() {
z := tz.MustLoadTimeZone("America/New_York")
fmt.Println(z.String())
Expand All @@ -65,6 +79,26 @@ func ExampleMustLoadTimeZone() {
// America/New_York
}

func ExampleTimeZone_LoadString() {
var z tz.TimeZone
if err := z.LoadString("Asia/Tokyo"); err != nil {
panic(err)
}

fmt.Println(z)

// Loading "UTC" (or an empty string) resets z to the zero value.
if err := z.LoadString("UTC"); err != nil {
panic(err)
}

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

// Output:
// Asia/Tokyo
// true
}

func ExampleTimeZone_Scan() {
var z tz.TimeZone
if err := z.Scan("America/New_York"); err != nil {
Expand Down
Loading