tz_ios.go 583 B

123456789101112131415161718192021222324252627282930
  1. package include
  2. /*
  3. #cgo CFLAGS: -x objective-c
  4. #cgo LDFLAGS: -framework Foundation
  5. #import <Foundation/Foundation.h>
  6. const char* getSystemTimeZone() {
  7. NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
  8. NSString *timeZoneName = [timeZone description];
  9. return [timeZoneName UTF8String];
  10. }
  11. */
  12. import "C"
  13. import (
  14. "strings"
  15. "time"
  16. )
  17. func init() {
  18. tzDescription := C.GoString(C.getSystemTimeZone())
  19. if len(tzDescription) == 0 {
  20. return
  21. }
  22. location, err := time.LoadLocation(strings.Split(tzDescription, " ")[0])
  23. if err != nil {
  24. return
  25. }
  26. time.Local = location
  27. }