File: tz_mapping.go

package info (click to toggle)
golang-github-thlib-go-timezone-local 0.0~git20210907.ef149e4-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 228 kB
  • sloc: makefile: 4
file content (95 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Based on a python package to update windows mappings
 * @see https://github.com/regebro/tzlocal/blob/master/update_windows_mappings.py
 */

package tzdata

import (
	"bytes"
	"fmt"
	"io"
	"sort"
	"strings"
	"time"
)

func UpdateWindowsTZMapping(target io.Writer) error {

	backward, err := DownloadOldNames()
	if err != nil {
		return err
	}

	data, err := DownloadWindowsZones()
	if err != nil {
		return err
	}

	win_tz := make(map[string]string)
	tz_win := make(map[string]string)

	// # UTC is a common but non-standard alias for Etc/UTC:
	tz_win["Etc/UTC"] = "UTC"

	for _, element := range data.WindowsZones.MapTimezones {
		// if element.Type == "windows" {
		// 	break
		// }

		// Making windows mapping
		for _, m := range element.MapZone {
			t := strings.Split(m.Type, " ")
			if m.Territory == "001" {
				win_tz[m.Other] = t[0]
			}
			for _, tz_name := range t {
				tz_win[tz_name] = m.Other
			}
		}

		// Map in the backwards compatible zone names
		for backward_compat_name, standard_name := range backward {
			if win_zone, ok := tz_win[standard_name]; ok {
				tz_win[backward_compat_name] = win_zone
			}
		}
	}

	// sort the keys
	win_tz_keys := make([]string, 0, len(win_tz))
	for k := range win_tz {
		win_tz_keys = append(win_tz_keys, k)
	}
	sort.Strings(win_tz_keys)

	tz_win_keys := make([]string, 0, len(tz_win))
	for k := range tz_win {
		tz_win_keys = append(tz_win_keys, k)
	}
	sort.Strings(tz_win_keys)

	// Generate the code
	out := bytes.Buffer{}
	out.WriteString("// A lookup table, mapping Windows time zone names to IANA time zone names and vice versa.\n\n")
	out.WriteString(fmt.Sprintf("// Last created %v\n\n", time.Now().UTC().Format(time.RFC3339)))
	out.WriteString("// WinTZtoIANA maps time zone names used by Windows to those used by IANA\n")
	out.WriteString("var WinTZtoIANA = map[string]string{\n")

	for _, k := range win_tz_keys {
		out.WriteString(fmt.Sprintf("\t\"%v\": \"%v\",\n", k, win_tz[k]))
	}
	out.WriteString("}\n\n")
	out.WriteString("// IANAtoWinTZ maps time zone names used by IANA to those used by Windows\n")
	out.WriteString("var IANAtoWinTZ = map[string]string{\n")

	for _, k := range tz_win_keys {
		out.WriteString(fmt.Sprintf("    \"%v\":\"%v\",\n", k, tz_win[k]))
	}
	out.WriteString("}\n")

	// Write buffered code to target writer
	target.Write(out.Bytes())

	return nil
}