github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/time/zoneinfo_windows.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package time 6 7 import ( 8 "errors" 9 "internal/syscall/windows/registry" 10 "runtime" 11 "syscall" 12 ) 13 14 // TODO(rsc): Fall back to copy of zoneinfo files. 15 16 // BUG(brainman,rsc): On Windows, the operating system does not provide complete 17 // time zone information. 18 // The implementation assumes that this year's rules for daylight savings 19 // time apply to all previous and future years as well. 20 21 // matchZoneKey checks if stdname and dstname match the corresponding key 22 // values "MUI_Std" and MUI_Dlt" or "Std" and "Dlt" (the latter down-level 23 // from Vista) in the kname key stored under the open registry key zones. 24 func matchZoneKey(zones registry.Key, kname string, stdname, dstname string) (matched bool, err2 error) { 25 k, err := registry.OpenKey(zones, kname, registry.READ) 26 if err != nil { 27 return false, err 28 } 29 defer k.Close() 30 31 var std, dlt string 32 if err = registry.LoadRegLoadMUIString(); err == nil { 33 // Try MUI_Std and MUI_Dlt first, fallback to Std and Dlt if *any* error occurs 34 std, err = k.GetMUIStringValue("MUI_Std") 35 if err == nil { 36 dlt, err = k.GetMUIStringValue("MUI_Dlt") 37 } 38 } 39 if err != nil { // Fallback to Std and Dlt 40 if std, _, err = k.GetStringValue("Std"); err != nil { 41 return false, err 42 } 43 if dlt, _, err = k.GetStringValue("Dlt"); err != nil { 44 return false, err 45 } 46 } 47 48 if std != stdname { 49 return false, nil 50 } 51 if dlt != dstname && dstname != stdname { 52 return false, nil 53 } 54 return true, nil 55 } 56 57 // toEnglishName searches the registry for an English name of a time zone 58 // whose zone names are stdname and dstname and returns the English name. 59 func toEnglishName(stdname, dstname string) (string, error) { 60 k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones`, registry.ENUMERATE_SUB_KEYS|registry.QUERY_VALUE) 61 if err != nil { 62 return "", err 63 } 64 defer k.Close() 65 66 names, err := k.ReadSubKeyNames(-1) 67 if err != nil { 68 return "", err 69 } 70 for _, name := range names { 71 matched, err := matchZoneKey(k, name, stdname, dstname) 72 if err == nil && matched { 73 return name, nil 74 } 75 } 76 return "", errors.New(`English name for time zone "` + stdname + `" not found in registry`) 77 } 78 79 // extractCAPS extracts capital letters from description desc. 80 func extractCAPS(desc string) string { 81 var short []rune 82 for _, c := range desc { 83 if 'A' <= c && c <= 'Z' { 84 short = append(short, c) 85 } 86 } 87 return string(short) 88 } 89 90 // abbrev returns the abbreviations to use for the given zone z. 91 func abbrev(z *syscall.Timezoneinformation) (std, dst string) { 92 stdName := syscall.UTF16ToString(z.StandardName[:]) 93 a, ok := abbrs[stdName] 94 if !ok { 95 dstName := syscall.UTF16ToString(z.DaylightName[:]) 96 // Perhaps stdName is not English. Try to convert it. 97 englishName, err := toEnglishName(stdName, dstName) 98 if err == nil { 99 a, ok = abbrs[englishName] 100 if ok { 101 return a.std, a.dst 102 } 103 } 104 // fallback to using capital letters 105 return extractCAPS(stdName), extractCAPS(dstName) 106 } 107 return a.std, a.dst 108 } 109 110 // pseudoUnix returns the pseudo-Unix time (seconds since Jan 1 1970 *LOCAL TIME*) 111 // denoted by the system date+time d in the given year. 112 // It is up to the caller to convert this local time into a UTC-based time. 113 func pseudoUnix(year int, d *syscall.Systemtime) int64 { 114 // Windows specifies daylight savings information in "day in month" format: 115 // d.Month is month number (1-12) 116 // d.DayOfWeek is appropriate weekday (Sunday=0 to Saturday=6) 117 // d.Day is week within the month (1 to 5, where 5 is last week of the month) 118 // d.Hour, d.Minute and d.Second are absolute time 119 day := 1 120 t := Date(year, Month(d.Month), day, int(d.Hour), int(d.Minute), int(d.Second), 0, UTC) 121 i := int(d.DayOfWeek) - int(t.Weekday()) 122 if i < 0 { 123 i += 7 124 } 125 day += i 126 if week := int(d.Day) - 1; week < 4 { 127 day += week * 7 128 } else { 129 // "Last" instance of the day. 130 day += 4 * 7 131 if day > daysIn(Month(d.Month), year) { 132 day -= 7 133 } 134 } 135 return t.sec() + int64(day-1)*secondsPerDay + internalToUnix 136 } 137 138 func initLocalFromTZI(i *syscall.Timezoneinformation) { 139 l := &localLoc 140 141 l.name = "Local" 142 143 nzone := 1 144 if i.StandardDate.Month > 0 { 145 nzone++ 146 } 147 l.zone = make([]zone, nzone) 148 149 stdname, dstname := abbrev(i) 150 151 std := &l.zone[0] 152 std.name = stdname 153 if nzone == 1 { 154 // No daylight savings. 155 std.offset = -int(i.Bias) * 60 156 l.cacheStart = alpha 157 l.cacheEnd = omega 158 l.cacheZone = std 159 l.tx = make([]zoneTrans, 1) 160 l.tx[0].when = l.cacheStart 161 l.tx[0].index = 0 162 return 163 } 164 165 // StandardBias must be ignored if StandardDate is not set, 166 // so this computation is delayed until after the nzone==1 167 // return above. 168 std.offset = -int(i.Bias+i.StandardBias) * 60 169 170 dst := &l.zone[1] 171 dst.name = dstname 172 dst.offset = -int(i.Bias+i.DaylightBias) * 60 173 dst.isDST = true 174 175 // Arrange so that d0 is first transition date, d1 second, 176 // i0 is index of zone after first transition, i1 second. 177 d0 := &i.StandardDate 178 d1 := &i.DaylightDate 179 i0 := 0 180 i1 := 1 181 if d0.Month > d1.Month { 182 d0, d1 = d1, d0 183 i0, i1 = i1, i0 184 } 185 186 // 2 tx per year, 100 years on each side of this year 187 l.tx = make([]zoneTrans, 400) 188 189 t := Now().UTC() 190 year := t.Year() 191 txi := 0 192 for y := year - 100; y < year+100; y++ { 193 tx := &l.tx[txi] 194 tx.when = pseudoUnix(y, d0) - int64(l.zone[i1].offset) 195 tx.index = uint8(i0) 196 txi++ 197 198 tx = &l.tx[txi] 199 tx.when = pseudoUnix(y, d1) - int64(l.zone[i0].offset) 200 tx.index = uint8(i1) 201 txi++ 202 } 203 } 204 205 var usPacific = syscall.Timezoneinformation{ 206 Bias: 8 * 60, 207 StandardName: [32]uint16{ 208 'P', 'a', 'c', 'i', 'f', 'i', 'c', ' ', 'S', 't', 'a', 'n', 'd', 'a', 'r', 'd', ' ', 'T', 'i', 'm', 'e', 209 }, 210 StandardDate: syscall.Systemtime{Month: 11, Day: 1, Hour: 2}, 211 DaylightName: [32]uint16{ 212 'P', 'a', 'c', 'i', 'f', 'i', 'c', ' ', 'D', 'a', 'y', 'l', 'i', 'g', 'h', 't', ' ', 'T', 'i', 'm', 'e', 213 }, 214 DaylightDate: syscall.Systemtime{Month: 3, Day: 2, Hour: 2}, 215 DaylightBias: -60, 216 } 217 218 var aus = syscall.Timezoneinformation{ 219 Bias: -10 * 60, 220 StandardName: [32]uint16{ 221 'A', 'U', 'S', ' ', 'E', 'a', 's', 't', 'e', 'r', 'n', ' ', 'S', 't', 'a', 'n', 'd', 'a', 'r', 'd', ' ', 'T', 'i', 'm', 'e', 222 }, 223 StandardDate: syscall.Systemtime{Month: 4, Day: 1, Hour: 3}, 224 DaylightName: [32]uint16{ 225 'A', 'U', 'S', ' ', 'E', 'a', 's', 't', 'e', 'r', 'n', ' ', 'D', 'a', 'y', 'l', 'i', 'g', 'h', 't', ' ', 'T', 'i', 'm', 'e', 226 }, 227 DaylightDate: syscall.Systemtime{Month: 10, Day: 1, Hour: 2}, 228 DaylightBias: -60, 229 } 230 231 func initTestingZone() { 232 initLocalFromTZI(&usPacific) 233 } 234 235 func initAusTestingZone() { 236 initLocalFromTZI(&aus) 237 } 238 239 func initLocal() { 240 var i syscall.Timezoneinformation 241 if _, err := syscall.GetTimeZoneInformation(&i); err != nil { 242 localLoc.name = "UTC" 243 return 244 } 245 initLocalFromTZI(&i) 246 } 247 248 func loadLocation(name string) (*Location, error) { 249 z, err := loadZoneFile(runtime.GOROOT()+`\lib\time\zoneinfo.zip`, name) 250 if err != nil { 251 return nil, err 252 } 253 z.name = name 254 return z, nil 255 } 256 257 func forceZipFileForTesting(zipOnly bool) { 258 // We only use the zip file anyway. 259 }