Source file src/time/zoneinfo.go

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

View as plain text