zoneinfo.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. package time
  5. import (
  6. "sync"
  7. "syscall"
  8. )
  9. // A Location maps time instants to the zone in use at that time.
  10. // Typically, the Location represents the collection of time offsets
  11. // in use in a geographical area, such as CEST and CET for central Europe.
  12. type Location struct {
  13. name string
  14. zone []zone
  15. tx []zoneTrans
  16. // Most lookups will be for the current time.
  17. // To avoid the binary search through tx, keep a
  18. // static one-element cache that gives the correct
  19. // zone for the time when the Location was created.
  20. // if cacheStart <= t <= cacheEnd,
  21. // lookup can return cacheZone.
  22. // The units for cacheStart and cacheEnd are seconds
  23. // since January 1, 1970 UTC, to match the argument
  24. // to lookup.
  25. cacheStart int64
  26. cacheEnd int64
  27. cacheZone *zone
  28. }
  29. // A zone represents a single time zone such as CEST or CET.
  30. type zone struct {
  31. name string // abbreviated name, "CET"
  32. offset int // seconds east of UTC
  33. isDST bool // is this zone Daylight Savings Time?
  34. }
  35. // A zoneTrans represents a single time zone transition.
  36. type zoneTrans struct {
  37. when int64 // transition time, in seconds since 1970 GMT
  38. index uint8 // the index of the zone that goes into effect at that time
  39. isstd, isutc bool // ignored - no idea what these mean
  40. }
  41. // alpha and omega are the beginning and end of time for zone
  42. // transitions.
  43. const (
  44. alpha = -1 << 63 // math.MinInt64
  45. omega = 1<<63 - 1 // math.MaxInt64
  46. )
  47. // UTC represents Universal Coordinated Time (UTC).
  48. var UTC *Location = &utcLoc
  49. // utcLoc is separate so that get can refer to &utcLoc
  50. // and ensure that it never returns a nil *Location,
  51. // even if a badly behaved client has changed UTC.
  52. var utcLoc = Location{name: "UTC"}
  53. // Local represents the system's local time zone.
  54. var Local *Location = &localLoc
  55. // localLoc is separate so that initLocal can initialize
  56. // it even if a client has changed Local.
  57. var localLoc Location
  58. var localOnce sync.Once
  59. func (l *Location) get() *Location {
  60. if l == nil {
  61. return &utcLoc
  62. }
  63. if l == &localLoc {
  64. localOnce.Do(initLocal)
  65. }
  66. return l
  67. }
  68. // String returns a descriptive name for the time zone information,
  69. // corresponding to the argument to LoadLocation.
  70. func (l *Location) String() string {
  71. return l.get().name
  72. }
  73. // FixedZone returns a Location that always uses
  74. // the given zone name and offset (seconds east of UTC).
  75. func FixedZone(name string, offset int) *Location {
  76. l := &Location{
  77. name: name,
  78. zone: []zone{{name, offset, false}},
  79. tx: []zoneTrans{{alpha, 0, false, false}},
  80. cacheStart: alpha,
  81. cacheEnd: omega,
  82. }
  83. l.cacheZone = &l.zone[0]
  84. return l
  85. }
  86. // lookup returns information about the time zone in use at an
  87. // instant in time expressed as seconds since January 1, 1970 00:00:00 UTC.
  88. //
  89. // The returned information gives the name of the zone (such as "CET"),
  90. // the start and end times bracketing sec when that zone is in effect,
  91. // the offset in seconds east of UTC (such as -5*60*60), and whether
  92. // the daylight savings is being observed at that time.
  93. func (l *Location) lookup(sec int64) (name string, offset int, isDST bool, start, end int64) {
  94. l = l.get()
  95. if len(l.zone) == 0 {
  96. name = "UTC"
  97. offset = 0
  98. isDST = false
  99. start = alpha
  100. end = omega
  101. return
  102. }
  103. if zone := l.cacheZone; zone != nil && l.cacheStart <= sec && sec < l.cacheEnd {
  104. name = zone.name
  105. offset = zone.offset
  106. isDST = zone.isDST
  107. start = l.cacheStart
  108. end = l.cacheEnd
  109. return
  110. }
  111. if len(l.tx) == 0 || sec < l.tx[0].when {
  112. zone := &l.zone[l.lookupFirstZone()]
  113. name = zone.name
  114. offset = zone.offset
  115. isDST = zone.isDST
  116. start = alpha
  117. if len(l.tx) > 0 {
  118. end = l.tx[0].when
  119. } else {
  120. end = omega
  121. }
  122. return
  123. }
  124. // Binary search for entry with largest time <= sec.
  125. // Not using sort.Search to avoid dependencies.
  126. tx := l.tx
  127. end = omega
  128. lo := 0
  129. hi := len(tx)
  130. for hi-lo > 1 {
  131. m := lo + (hi-lo)/2
  132. lim := tx[m].when
  133. if sec < lim {
  134. end = lim
  135. hi = m
  136. } else {
  137. lo = m
  138. }
  139. }
  140. zone := &l.zone[tx[lo].index]
  141. name = zone.name
  142. offset = zone.offset
  143. isDST = zone.isDST
  144. start = tx[lo].when
  145. // end = maintained during the search
  146. return
  147. }
  148. // lookupFirstZone returns the index of the time zone to use for times
  149. // before the first transition time, or when there are no transition
  150. // times.
  151. //
  152. // The reference implementation in localtime.c from
  153. // http://www.iana.org/time-zones/repository/releases/tzcode2013g.tar.gz
  154. // implements the following algorithm for these cases:
  155. // 1) If the first zone is unused by the transitions, use it.
  156. // 2) Otherwise, if there are transition times, and the first
  157. // transition is to a zone in daylight time, find the first
  158. // non-daylight-time zone before and closest to the first transition
  159. // zone.
  160. // 3) Otherwise, use the first zone that is not daylight time, if
  161. // there is one.
  162. // 4) Otherwise, use the first zone.
  163. func (l *Location) lookupFirstZone() int {
  164. // Case 1.
  165. if !l.firstZoneUsed() {
  166. return 0
  167. }
  168. // Case 2.
  169. if len(l.tx) > 0 && l.zone[l.tx[0].index].isDST {
  170. for zi := int(l.tx[0].index) - 1; zi >= 0; zi-- {
  171. if !l.zone[zi].isDST {
  172. return zi
  173. }
  174. }
  175. }
  176. // Case 3.
  177. for zi := range l.zone {
  178. if !l.zone[zi].isDST {
  179. return zi
  180. }
  181. }
  182. // Case 4.
  183. return 0
  184. }
  185. // firstZoneUsed returns whether the first zone is used by some
  186. // transition.
  187. func (l *Location) firstZoneUsed() bool {
  188. for _, tx := range l.tx {
  189. if tx.index == 0 {
  190. return true
  191. }
  192. }
  193. return false
  194. }
  195. // lookupName returns information about the time zone with
  196. // the given name (such as "EST") at the given pseudo-Unix time
  197. // (what the given time of day would be in UTC).
  198. func (l *Location) lookupName(name string, unix int64) (offset int, isDST bool, ok bool) {
  199. l = l.get()
  200. // First try for a zone with the right name that was actually
  201. // in effect at the given time. (In Sydney, Australia, both standard
  202. // and daylight-savings time are abbreviated "EST". Using the
  203. // offset helps us pick the right one for the given time.
  204. // It's not perfect: during the backward transition we might pick
  205. // either one.)
  206. for i := range l.zone {
  207. zone := &l.zone[i]
  208. if zone.name == name {
  209. nam, offset, isDST, _, _ := l.lookup(unix - int64(zone.offset))
  210. if nam == zone.name {
  211. return offset, isDST, true
  212. }
  213. }
  214. }
  215. // Otherwise fall back to an ordinary name match.
  216. for i := range l.zone {
  217. zone := &l.zone[i]
  218. if zone.name == name {
  219. return zone.offset, zone.isDST, true
  220. }
  221. }
  222. // Otherwise, give up.
  223. return
  224. }
  225. // NOTE(rsc): Eventually we will need to accept the POSIX TZ environment
  226. // syntax too, but I don't feel like implementing it today.
  227. var zoneinfo, _ = syscall.Getenv("ZONEINFO")
  228. // LoadLocation returns the Location with the given name.
  229. //
  230. // If the name is "" or "UTC", LoadLocation returns UTC.
  231. // If the name is "Local", LoadLocation returns Local.
  232. //
  233. // Otherwise, the name is taken to be a location name corresponding to a file
  234. // in the IANA Time Zone database, such as "America/New_York".
  235. //
  236. // The time zone database needed by LoadLocation may not be
  237. // present on all systems, especially non-Unix systems.
  238. // LoadLocation looks in the directory or uncompressed zip file
  239. // named by the ZONEINFO environment variable, if any, then looks in
  240. // known installation locations on Unix systems,
  241. // and finally looks in $GOROOT/lib/time/zoneinfo.zip.
  242. func LoadLocation(name string) (*Location, error) {
  243. if name == "" || name == "UTC" {
  244. return UTC, nil
  245. }
  246. if name == "Local" {
  247. return Local, nil
  248. }
  249. if zoneinfo != "" {
  250. if z, err := loadZoneFile(zoneinfo, name); err == nil {
  251. z.name = name
  252. return z, nil
  253. }
  254. }
  255. return loadLocation(name)
  256. }