file.go 893 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package utils
  2. import (
  3. "os"
  4. "github.com/sirupsen/logrus"
  5. "gopkg.in/ini.v1"
  6. )
  7. // 缓存 timeZone 信息
  8. var timeZoneCache string
  9. // FileExists checks if a file exists and is not a directory before we
  10. // try using it to prevent further errors.
  11. func FileExists(filename string) bool {
  12. info, err := os.Stat(filename)
  13. if os.IsNotExist(err) {
  14. return false
  15. }
  16. return !info.IsDir()
  17. }
  18. // pbx 不能正确读取时区, 读取自定义文件 /etc/asterisk/ntp.conf
  19. func GetLocationName() string {
  20. if timeZoneCache != "" {
  21. return timeZoneCache
  22. }
  23. filePath := "/etc/asterisk/ntp.conf"
  24. _, err := os.Stat(filePath)
  25. if err != nil {
  26. logrus.Error(err)
  27. return ""
  28. }
  29. iniFile, err := ini.Load(filePath)
  30. if err != nil {
  31. logrus.Error(err)
  32. return ""
  33. }
  34. timeZoneCache = iniFile.Section("ntp").Key("TZNAME").Value()
  35. logrus.Infof("use location %s", timeZoneCache)
  36. return timeZoneCache
  37. }