github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/stdlibs/time/timezoneinfo.gno (about) 1 // Copyright 2011 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 // "sync" XXX 10 // "syscall" XXX 11 ) 12 13 //go:generate env ZONEINFO=$GOROOT/lib/time/zoneinfo.zip go run genzabbrs.go -output zoneinfo_abbrs_windows.go 14 15 // A Location maps time instants to the zone in use at that time. 16 // Typically, the Location represents the collection of time offsets 17 // in use in a geographical area. For many Locations the time offset varies 18 // depending on whether daylight savings time is in use at the time instant. 19 type Location struct { 20 name string 21 zone []zone 22 tx []zoneTrans 23 24 // The tzdata information can be followed by a string that describes 25 // how to handle DST transitions not recorded in zoneTrans. 26 // The format is the TZ environment variable without a colon; see 27 // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html. 28 // Example string, for America/Los_Angeles: PST8PDT,M3.2.0,M11.1.0 29 extend string 30 31 // Most lookups will be for the current time. 32 // To avoid the binary search through tx, keep a 33 // static one-element cache that gives the correct 34 // zone for the time when the Location was created. 35 // if cacheStart <= t < cacheEnd, 36 // lookup can return cacheZone. 37 // The units for cacheStart and cacheEnd are seconds 38 // since January 1, 1970 UTC, to match the argument 39 // to lookup. 40 cacheStart int64 41 cacheEnd int64 42 cacheZone *zone 43 } 44 45 // A zone represents a single time zone such as CET. 46 type zone struct { 47 name string // abbreviated name, "CET" 48 offset int // seconds east of UTC 49 isDST bool // is this zone Daylight Savings Time? 50 } 51 52 // A zoneTrans represents a single time zone transition. 53 type zoneTrans struct { 54 when int64 // transition time, in seconds since 1970 GMT 55 index uint8 // the index of the zone that goes into effect at that time 56 isstd, isutc bool // ignored - no idea what these mean 57 } 58 59 // alpha and omega are the beginning and end of time for zone 60 // transitions. 61 const ( 62 alpha = -1 << 63 // math.MinInt64 63 omega = 1<<63 - 1 // math.MaxInt64 64 ) 65 66 // UTC represents Universal Coordinated Time (UTC). 67 var UTC *Location = &utcLoc 68 69 // utcLoc is separate so that get can refer to &utcLoc 70 // and ensure that it never returns a nil *Location, 71 // even if a badly behaved client has changed UTC. 72 var utcLoc = Location{name: "UTC"} 73 74 // Local represents the system's local time zone. 75 // On Unix systems, Local consults the TZ environment 76 // variable to find the time zone to use. No TZ means 77 // use the system default /etc/localtime. 78 // TZ="" means use UTC. 79 // TZ="foo" means use file foo in the system timezone directory. 80 var Local *Location = &localLoc 81 82 // localLoc is separate so that initLocal can initialize 83 // it even if a client has changed Local. 84 var localLoc Location 85 86 // XXX var localOnce sync.Once 87 88 func (l *Location) get() *Location { 89 if l == nil { 90 return &utcLoc 91 } 92 if l == &localLoc { 93 // XXX localOnce.Do(initLocal) 94 } 95 return l 96 } 97 98 // String returns a descriptive name for the time zone information, 99 // corresponding to the name argument to LoadLocation or FixedZone. 100 func (l *Location) String() string { 101 return l.get().name 102 } 103 104 // FixedZone returns a Location that always uses 105 // the given zone name and offset (seconds east of UTC). 106 func FixedZone(name string, offset int) *Location { 107 l := &Location{ 108 name: name, 109 zone: []zone{{name, offset, false}}, 110 tx: []zoneTrans{{alpha, 0, false, false}}, 111 cacheStart: alpha, 112 cacheEnd: omega, 113 } 114 l.cacheZone = &l.zone[0] 115 return l 116 } 117 118 // lookup returns information about the time zone in use at an 119 // instant in time expressed as seconds since January 1, 1970 00:00:00 UTC. 120 // 121 // The returned information gives the name of the zone (such as "CET"), 122 // the start and end times bracketing sec when that zone is in effect, 123 // the offset in seconds east of UTC (such as -5*60*60), and whether 124 // the daylight savings is being observed at that time. 125 func (l *Location) lookup(sec int64) (name string, offset int, start, end int64, isDST bool) { 126 l = l.get() 127 128 if len(l.zone) == 0 { 129 name = "UTC" 130 offset = 0 131 start = alpha 132 end = omega 133 isDST = false 134 return 135 } 136 137 if zone := l.cacheZone; zone != nil && l.cacheStart <= sec && sec < l.cacheEnd { 138 name = zone.name 139 offset = zone.offset 140 start = l.cacheStart 141 end = l.cacheEnd 142 isDST = zone.isDST 143 return 144 } 145 146 if len(l.tx) == 0 || sec < l.tx[0].when { 147 zone := &l.zone[l.lookupFirstZone()] 148 name = zone.name 149 offset = zone.offset 150 start = alpha 151 if len(l.tx) > 0 { 152 end = l.tx[0].when 153 } else { 154 end = omega 155 } 156 isDST = zone.isDST 157 return 158 } 159 160 // Binary search for entry with largest time <= sec. 161 // Not using sort.Search to avoid dependencies. 162 tx := l.tx 163 end = omega 164 lo := 0 165 hi := len(tx) 166 for hi-lo > 1 { 167 m := lo + (hi-lo)/2 168 lim := tx[m].when 169 if sec < lim { 170 end = lim 171 hi = m 172 } else { 173 lo = m 174 } 175 } 176 zone := &l.zone[tx[lo].index] 177 name = zone.name 178 offset = zone.offset 179 start = tx[lo].when 180 // end = maintained during the search 181 isDST = zone.isDST 182 183 // If we're at the end of the known zone transitions, 184 // try the extend string. 185 if lo == len(tx)-1 && l.extend != "" { 186 if ename, eoffset, estart, eend, eisDST, ok := tzset(l.extend, end, sec); ok { 187 return ename, eoffset, estart, eend, eisDST 188 } 189 } 190 191 return 192 } 193 194 // lookupFirstZone returns the index of the time zone to use for times 195 // before the first transition time, or when there are no transition 196 // times. 197 // 198 // The reference implementation in localtime.c from 199 // https://www.iana.org/time-zones/repository/releases/tzcode2013g.tar.gz 200 // implements the following algorithm for these cases: 201 // 1. If the first zone is unused by the transitions, use it. 202 // 2. Otherwise, if there are transition times, and the first 203 // transition is to a zone in daylight time, find the first 204 // non-daylight-time zone before and closest to the first transition 205 // zone. 206 // 3. Otherwise, use the first zone that is not daylight time, if 207 // there is one. 208 // 4. Otherwise, use the first zone. 209 func (l *Location) lookupFirstZone() int { 210 // Case 1. 211 if !l.firstZoneUsed() { 212 return 0 213 } 214 215 // Case 2. 216 if len(l.tx) > 0 && l.zone[l.tx[0].index].isDST { 217 for zi := int(l.tx[0].index) - 1; zi >= 0; zi-- { 218 if !l.zone[zi].isDST { 219 return zi 220 } 221 } 222 } 223 224 // Case 3. 225 for zi := range l.zone { 226 if !l.zone[zi].isDST { 227 return zi 228 } 229 } 230 231 // Case 4. 232 return 0 233 } 234 235 // firstZoneUsed reports whether the first zone is used by some 236 // transition. 237 func (l *Location) firstZoneUsed() bool { 238 for _, tx := range l.tx { 239 if tx.index == 0 { 240 return true 241 } 242 } 243 return false 244 } 245 246 // tzset takes a timezone string like the one found in the TZ environment 247 // variable, the end of the last time zone transition expressed as seconds 248 // since January 1, 1970 00:00:00 UTC, and a time expressed the same way. 249 // We call this a tzset string since in C the function tzset reads TZ. 250 // The return values are as for lookup, plus ok which reports whether the 251 // parse succeeded. 252 func tzset(s string, initEnd, sec int64) (name string, offset int, start, end int64, isDST, ok bool) { 253 var ( 254 stdName, dstName string 255 stdOffset, dstOffset int 256 ) 257 258 stdName, s, ok = tzsetName(s) 259 if ok { 260 stdOffset, s, ok = tzsetOffset(s) 261 } 262 if !ok { 263 return "", 0, 0, 0, false, false 264 } 265 266 // The numbers in the tzset string are added to local time to get UTC, 267 // but our offsets are added to UTC to get local time, 268 // so we negate the number we see here. 269 stdOffset = -stdOffset 270 271 if len(s) == 0 || s[0] == ',' { 272 // No daylight savings time. 273 return stdName, stdOffset, initEnd, omega, false, true 274 } 275 276 dstName, s, ok = tzsetName(s) 277 if ok { 278 if len(s) == 0 || s[0] == ',' { 279 dstOffset = stdOffset + secondsPerHour 280 } else { 281 dstOffset, s, ok = tzsetOffset(s) 282 dstOffset = -dstOffset // as with stdOffset, above 283 } 284 } 285 if !ok { 286 return "", 0, 0, 0, false, false 287 } 288 289 if len(s) == 0 { 290 // Default DST rules per tzcode. 291 s = ",M3.2.0,M11.1.0" 292 } 293 // The TZ definition does not mention ';' here but tzcode accepts it. 294 if s[0] != ',' && s[0] != ';' { 295 return "", 0, 0, 0, false, false 296 } 297 s = s[1:] 298 299 var startRule, endRule rule 300 startRule, s, ok = tzsetRule(s) 301 if !ok || len(s) == 0 || s[0] != ',' { 302 return "", 0, 0, 0, false, false 303 } 304 s = s[1:] 305 endRule, s, ok = tzsetRule(s) 306 if !ok || len(s) > 0 { 307 return "", 0, 0, 0, false, false 308 } 309 310 year, _, _, yday := absDate(uint64(sec+unixToInternal+internalToAbsolute), false) 311 312 ysec := int64(yday*secondsPerDay) + sec%secondsPerDay 313 314 // Compute start of year in seconds since Unix epoch. 315 d := daysSinceEpoch(year) 316 abs := int64(d * secondsPerDay) 317 abs += absoluteToInternal + internalToUnix 318 319 startSec := int64(tzruleTime(year, startRule, stdOffset)) 320 endSec := int64(tzruleTime(year, endRule, dstOffset)) 321 dstIsDST, stdIsDST := true, false 322 // Note: this is a flipping of "DST" and "STD" while retaining the labels 323 // This happens in southern hemispheres. The labelling here thus is a little 324 // inconsistent with the goal. 325 if endSec < startSec { 326 startSec, endSec = endSec, startSec 327 stdName, dstName = dstName, stdName 328 stdOffset, dstOffset = dstOffset, stdOffset 329 stdIsDST, dstIsDST = dstIsDST, stdIsDST 330 } 331 332 // The start and end values that we return are accurate 333 // close to a daylight savings transition, but are otherwise 334 // just the start and end of the year. That suffices for 335 // the only caller that cares, which is Date. 336 if ysec < startSec { 337 return stdName, stdOffset, abs, startSec + abs, stdIsDST, true 338 } else if ysec >= endSec { 339 return stdName, stdOffset, endSec + abs, abs + 365*secondsPerDay, stdIsDST, true 340 } else { 341 return dstName, dstOffset, startSec + abs, endSec + abs, dstIsDST, true 342 } 343 } 344 345 // tzsetName returns the timezone name at the start of the tzset string s, 346 // and the remainder of s, and reports whether the parsing is OK. 347 func tzsetName(s string) (string, string, bool) { 348 if len(s) == 0 { 349 return "", "", false 350 } 351 if s[0] != '<' { 352 for i, r := range s { 353 switch r { 354 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '-', '+': 355 if i < 3 { 356 return "", "", false 357 } 358 return s[:i], s[i:], true 359 } 360 } 361 if len(s) < 3 { 362 return "", "", false 363 } 364 return s, "", true 365 } else { 366 for i, r := range s { 367 if r == '>' { 368 return s[1:i], s[i+1:], true 369 } 370 } 371 return "", "", false 372 } 373 } 374 375 // tzsetOffset returns the timezone offset at the start of the tzset string s, 376 // and the remainder of s, and reports whether the parsing is OK. 377 // The timezone offset is returned as a number of seconds. 378 func tzsetOffset(s string) (offset int, rest string, ok bool) { 379 if len(s) == 0 { 380 return 0, "", false 381 } 382 neg := false 383 if s[0] == '+' { 384 s = s[1:] 385 } else if s[0] == '-' { 386 s = s[1:] 387 neg = true 388 } 389 390 // The tzdata code permits values up to 24 * 7 here, 391 // although POSIX does not. 392 var hours int 393 hours, s, ok = tzsetNum(s, 0, 24*7) 394 if !ok { 395 return 0, "", false 396 } 397 off := hours * secondsPerHour 398 if len(s) == 0 || s[0] != ':' { 399 if neg { 400 off = -off 401 } 402 return off, s, true 403 } 404 405 var mins int 406 mins, s, ok = tzsetNum(s[1:], 0, 59) 407 if !ok { 408 return 0, "", false 409 } 410 off += mins * secondsPerMinute 411 if len(s) == 0 || s[0] != ':' { 412 if neg { 413 off = -off 414 } 415 return off, s, true 416 } 417 418 var secs int 419 secs, s, ok = tzsetNum(s[1:], 0, 59) 420 if !ok { 421 return 0, "", false 422 } 423 off += secs 424 425 if neg { 426 off = -off 427 } 428 return off, s, true 429 } 430 431 // ruleKind is the kinds of rules that can be seen in a tzset string. 432 type ruleKind int 433 434 const ( 435 ruleJulian ruleKind = iota 436 ruleDOY 437 ruleMonthWeekDay 438 ) 439 440 // rule is a rule read from a tzset string. 441 type rule struct { 442 kind ruleKind 443 day int 444 week int 445 mon int 446 time int // transition time 447 } 448 449 // tzsetRule parses a rule from a tzset string. 450 // It returns the rule, and the remainder of the string, and reports success. 451 func tzsetRule(s string) (rule, string, bool) { 452 var r rule 453 if len(s) == 0 { 454 return rule{}, "", false 455 } 456 ok := false 457 if s[0] == 'J' { 458 var jday int 459 jday, s, ok = tzsetNum(s[1:], 1, 365) 460 if !ok { 461 return rule{}, "", false 462 } 463 r.kind = ruleJulian 464 r.day = jday 465 } else if s[0] == 'M' { 466 var mon int 467 mon, s, ok = tzsetNum(s[1:], 1, 12) 468 if !ok || len(s) == 0 || s[0] != '.' { 469 return rule{}, "", false 470 } 471 var week int 472 week, s, ok = tzsetNum(s[1:], 1, 5) 473 if !ok || len(s) == 0 || s[0] != '.' { 474 return rule{}, "", false 475 } 476 var day int 477 day, s, ok = tzsetNum(s[1:], 0, 6) 478 if !ok { 479 return rule{}, "", false 480 } 481 r.kind = ruleMonthWeekDay 482 r.day = day 483 r.week = week 484 r.mon = mon 485 } else { 486 var day int 487 day, s, ok = tzsetNum(s, 0, 365) 488 if !ok { 489 return rule{}, "", false 490 } 491 r.kind = ruleDOY 492 r.day = day 493 } 494 495 if len(s) == 0 || s[0] != '/' { 496 r.time = 2 * secondsPerHour // 2am is the default 497 return r, s, true 498 } 499 500 offset, s, ok := tzsetOffset(s[1:]) 501 if !ok { 502 return rule{}, "", false 503 } 504 r.time = offset 505 506 return r, s, true 507 } 508 509 // tzsetNum parses a number from a tzset string. 510 // It returns the number, and the remainder of the string, and reports success. 511 // The number must be between min and max. 512 func tzsetNum(s string, min, max int) (num int, rest string, ok bool) { 513 if len(s) == 0 { 514 return 0, "", false 515 } 516 num = 0 517 for i, r := range s { 518 if r < '0' || r > '9' { 519 if i == 0 || num < min { 520 return 0, "", false 521 } 522 return num, s[i:], true 523 } 524 num *= 10 525 num += int(r) - '0' 526 if num > max { 527 return 0, "", false 528 } 529 } 530 if num < min { 531 return 0, "", false 532 } 533 return num, "", true 534 } 535 536 // tzruleTime takes a year, a rule, and a timezone offset, 537 // and returns the number of seconds since the start of the year 538 // that the rule takes effect. 539 func tzruleTime(year int, r rule, off int) int { 540 var s int 541 switch r.kind { 542 case ruleJulian: 543 s = (r.day - 1) * secondsPerDay 544 if isLeap(year) && r.day >= 60 { 545 s += secondsPerDay 546 } 547 case ruleDOY: 548 s = r.day * secondsPerDay 549 case ruleMonthWeekDay: 550 // Zeller's Congruence. 551 m1 := (r.mon+9)%12 + 1 552 yy0 := year 553 if r.mon <= 2 { 554 yy0-- 555 } 556 yy1 := yy0 / 100 557 yy2 := yy0 % 100 558 dow := ((26*m1-2)/10 + 1 + yy2 + yy2/4 + yy1/4 - 2*yy1) % 7 559 if dow < 0 { 560 dow += 7 561 } 562 // Now dow is the day-of-week of the first day of r.mon. 563 // Get the day-of-month of the first "dow" day. 564 d := r.day - dow 565 if d < 0 { 566 d += 7 567 } 568 for i := 1; i < r.week; i++ { 569 if d+7 >= daysIn(Month(r.mon), year) { 570 break 571 } 572 d += 7 573 } 574 d += int(daysBefore[r.mon-1]) 575 if isLeap(year) && r.mon > 2 { 576 d++ 577 } 578 s = d * secondsPerDay 579 } 580 581 return s + r.time - off 582 } 583 584 // lookupName returns information about the time zone with 585 // the given name (such as "EST") at the given pseudo-Unix time 586 // (what the given time of day would be in UTC). 587 func (l *Location) lookupName(name string, unix int64) (offset int, ok bool) { 588 l = l.get() 589 590 // First try for a zone with the right name that was actually 591 // in effect at the given time. (In Sydney, Australia, both standard 592 // and daylight-savings time are abbreviated "EST". Using the 593 // offset helps us pick the right one for the given time. 594 // It's not perfect: during the backward transition we might pick 595 // either one.) 596 for i := range l.zone { 597 zone := &l.zone[i] 598 if zone.name == name { 599 nam, offset, _, _, _ := l.lookup(unix - int64(zone.offset)) 600 if nam == zone.name { 601 return offset, true 602 } 603 } 604 } 605 606 // Otherwise fall back to an ordinary name match. 607 for i := range l.zone { 608 zone := &l.zone[i] 609 if zone.name == name { 610 return zone.offset, true 611 } 612 } 613 614 // Otherwise, give up. 615 return 616 } 617 618 // NOTE(rsc): Eventually we will need to accept the POSIX TZ environment 619 // syntax too, but I don't feel like implementing it today. 620 621 var errLocation = errors.New("time: invalid location name") 622 623 var zoneinfo *string 624 625 // XXX var zoneinfoOnce sync.Once 626 627 // LoadLocation returns the Location with the given name. 628 // 629 // If the name is "" or "UTC", LoadLocation returns UTC. 630 // If the name is "Local", LoadLocation returns Local. 631 // 632 // Otherwise, the name is taken to be a location name corresponding to a file 633 // in the IANA Time Zone database, such as "America/New_York". 634 // 635 // LoadLocation looks for the IANA Time Zone database in the following 636 // locations in order: 637 // 638 // - the directory or uncompressed zip file named by the ZONEINFO environment variable 639 // - on a Unix system, the system standard installation location 640 // - $GOROOT/lib/time/zoneinfo.zip 641 // - the time/tzdata package, if it was imported 642 func LoadLocation(name string) (*Location, error) { 643 panic("XXX LoadLocation not yet implemented") 644 /* 645 if name == "" || name == "UTC" { 646 return UTC, nil 647 } 648 if name == "Local" { 649 return Local, nil 650 } 651 if containsDotDot(name) || name[0] == '/' || name[0] == '\\' { 652 // No valid IANA Time Zone name contains a single dot, 653 // much less dot dot. Likewise, none begin with a slash. 654 return nil, errLocation 655 } 656 zoneinfoOnce.Do(func() { 657 env, _ := syscall.Getenv("ZONEINFO") 658 zoneinfo = &env 659 }) 660 var firstErr error 661 if *zoneinfo != "" { 662 if zoneData, err := loadTzinfoFromDirOrZip(*zoneinfo, name); err == nil { 663 if z, err := LoadLocationFromTZData(name, zoneData); err == nil { 664 return z, nil 665 } 666 firstErr = err 667 } else if err != syscall.ENOENT { 668 firstErr = err 669 } 670 } 671 if z, err := loadLocation(name, platformZoneSources); err == nil { 672 return z, nil 673 } else if firstErr == nil { 674 firstErr = err 675 } 676 return nil, firstErr 677 */ 678 } 679 680 // containsDotDot reports whether s contains "..". 681 func containsDotDot(s string) bool { 682 if len(s) < 2 { 683 return false 684 } 685 for i := 0; i < len(s)-1; i++ { 686 if s[i] == '.' && s[i+1] == '.' { 687 return true 688 } 689 } 690 return false 691 }