GetMono = (*Time).mono
ErrLocation = errLocation
ReadFile = readFile
+ LoadTzinfo = loadTzinfo
)
localLoc = *z
}
-var origZoneSources = zoneSources
+var OrigZoneSources = zoneSources
func forceZipFileForTesting(zipOnly bool) {
- zoneSources = make([]string, len(origZoneSources))
- copy(zoneSources, origZoneSources)
+ zoneSources = make([]string, len(OrigZoneSources))
+ copy(zoneSources, OrigZoneSources)
if zipOnly {
zoneSources = zoneSources[len(zoneSources)-1:]
}
})
if *zoneinfo != "" {
if zoneData, err := loadTzinfoFromDirOrZip(*zoneinfo, name); err == nil {
- if z, err := newLocationFromTzinfo(name, zoneData); err == nil {
+ if z, err := LoadLocationFromTZData(name, zoneData); err == nil {
return z, nil
}
}
var badData = errors.New("malformed time zone information")
-// newLocationFromTzinfo returns the Location described by Tzinfo with the given name.
-// The expected format for Tzinfo is that of a timezone file as they are found in the
-// the IANA Time Zone database.
-func newLocationFromTzinfo(name string, Tzinfo []byte) (*Location, error) {
- d := dataIO{Tzinfo, false}
+// LoadLocationFromTZData returns a Location with the given name
+// initialized from the IANA Time Zone database-formatted data.
+// The data should be in the format of a standard IANA time zone file
+// (for example, the content of /etc/localtime on Unix systems).
+func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
+ d := dataIO{data, false}
// 4-byte magic "TZif"
if magic := d.read(4); string(magic) != "TZif" {
for _, source := range sources {
var zoneData, err = loadTzinfo(name, source)
if err == nil {
- if z, err = newLocationFromTzinfo(name, zoneData); err == nil {
+ if z, err = LoadLocationFromTZData(name, zoneData); err == nil {
return z, nil
}
}
import (
"fmt"
"os"
+ "reflect"
"testing"
"time"
)
t.Errorf(`invalid UTC location name: got %q want "UTC"`, time.UTC)
}
}
+
+func TestLoadLocationFromTzinfo(t *testing.T) {
+ time.ForceZipFileForTesting(true)
+ defer time.ForceZipFileForTesting(false)
+
+ const locationName = "Asia/Jerusalem"
+ reference, err := time.LoadLocation(locationName)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ tzinfo, err := time.LoadTzinfo(locationName, time.OrigZoneSources[len(time.OrigZoneSources)-1])
+ if err != nil {
+ t.Fatal(err)
+ }
+ sample, err := time.LoadLocationFromTZData(locationName, tzinfo)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !reflect.DeepEqual(reference, sample) {
+ t.Errorf("return values of LoadLocationFromTZData and LoadLocation don't match")
+ }
+}