summaryrefslogtreecommitdiff
path: root/libgo/go/time/zoneinfo_plan9.go
blob: 3c3e7c42446f98c03fc992a88c35726b1e51579f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Parse Plan 9 timezone(2) files.

package time

import (
	"os"
	"strconv"
	"strings"
)

func parseZones(s string) (zt []zonetime) {
	f := strings.Fields(s)
	if len(f) < 4 {
		return
	}

	// standard timezone offset
	o, err := strconv.Atoi(f[1])
	if err != nil {
		return
	}
	std := &zone{name: f[0], utcoff: o, isdst: false}

	// alternate timezone offset
	o, err = strconv.Atoi(f[3])
	if err != nil {
		return
	}
	dst := &zone{name: f[2], utcoff: o, isdst: true}

	// transition time pairs
	f = f[4:]
	for i := 0; i < len(f); i++ {
		z := std
		if i%2 == 0 {
			z = dst
		}
		t, err := strconv.Atoi(f[i])
		if err != nil {
			return nil
		}
		t -= std.utcoff
		zt = append(zt, zonetime{time: int32(t), zone: z})
	}
	return
}

func setupZone() {
	t, err := os.Getenverror("timezone")
	if err != nil {
		// do nothing: use UTC
		return
	}
	zones = parseZones(t)
}