@@ -57,6 +57,8 @@ struct tzhead {
57
57
};
58
58
#endif
59
59
60
+ #include <time.h>
61
+
60
62
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX
61
63
#define TZZONELINK TZDEFAULT
62
64
#define TZZONEINFO TZDIR "/"
@@ -710,8 +712,9 @@ void __InitTZStrings(void) {
710
712
static CFTimeZoneRef __CFTimeZoneCreateSystem (void ) {
711
713
CFTimeZoneRef result = NULL ;
712
714
713
- #if DEPLOYMENT_TARGET_WINDOWS
714
715
CFStringRef name = NULL ;
716
+
717
+ #if DEPLOYMENT_TARGET_WINDOWS
715
718
TIME_ZONE_INFORMATION tzi = { 0 };
716
719
DWORD rval = GetTimeZoneInformation (& tzi );
717
720
if (rval != TIME_ZONE_ID_INVALID ) {
@@ -729,40 +732,49 @@ static CFTimeZoneRef __CFTimeZoneCreateSystem(void) {
729
732
} else {
730
733
CFLog (kCFLogLevelError , CFSTR ("Couldn't get time zone information error %d" ), GetLastError ());
731
734
}
732
- if (name ) {
733
735
#else
734
736
const char * tzenv ;
735
737
int ret ;
736
738
char linkbuf [CFMaxPathSize ];
737
-
739
+
738
740
tzenv = __CFgetenv ("TZFILE" );
739
741
if (NULL != tzenv ) {
740
- CFStringRef name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )tzenv , strlen (tzenv ), kCFStringEncodingUTF8 , false);
741
- result = CFTimeZoneCreateWithName (kCFAllocatorSystemDefault , name , false);
742
- CFRelease (name );
743
- if (result ) return result ;
742
+ CFStringRef name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )tzenv , strlen (tzenv ), kCFStringEncodingUTF8 , false);
743
+ result = CFTimeZoneCreateWithName (kCFAllocatorSystemDefault , name , false);
744
+ CFRelease (name );
745
+ if (result ) return result ;
744
746
}
745
747
tzenv = __CFgetenv ("TZ" );
746
748
if (NULL != tzenv ) {
747
- CFStringRef name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )tzenv , strlen (tzenv ), kCFStringEncodingUTF8 , false);
748
- result = CFTimeZoneCreateWithName (kCFAllocatorSystemDefault , name , true);
749
- CFRelease (name );
750
- if (result ) return result ;
749
+ CFStringRef name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )tzenv , strlen (tzenv ), kCFStringEncodingUTF8 , false);
750
+ result = CFTimeZoneCreateWithName (kCFAllocatorSystemDefault , name , true);
751
+ CFRelease (name );
752
+ if (result ) return result ;
751
753
}
752
754
ret = readlink (TZZONELINK , linkbuf , sizeof (linkbuf ));
753
755
if (0 < ret ) {
754
- CFStringRef name ;
755
- linkbuf [ret ] = '\0' ;
756
- if (strncmp (linkbuf , TZZONEINFO , sizeof (TZZONEINFO ) - 1 ) == 0 ) {
757
- name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )linkbuf + sizeof (TZZONEINFO ) - 1 , strlen (linkbuf ) - sizeof (TZZONEINFO ) + 1 , kCFStringEncodingUTF8 , false);
758
- } else {
759
- name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )linkbuf , strlen (linkbuf ), kCFStringEncodingUTF8 , false);
760
- }
756
+ CFStringRef name ;
757
+ linkbuf [ret ] = '\0' ;
758
+ if (strncmp (linkbuf , TZZONEINFO , sizeof (TZZONEINFO ) - 1 ) == 0 ) {
759
+ name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )linkbuf + sizeof (TZZONEINFO ) - 1 , strlen (linkbuf ) - sizeof (TZZONEINFO ) + 1 , kCFStringEncodingUTF8 , false);
760
+ } else {
761
+ name = CFStringCreateWithBytes (kCFAllocatorSystemDefault , (uint8_t * )linkbuf , strlen (linkbuf ), kCFStringEncodingUTF8 , false);
762
+ }
763
+ } else {
764
+ // TODO: This can still fail on Linux if the time zone is not recognized by ICU later
765
+ // Try localtime
766
+ tzset ();
767
+ time_t t = time (NULL );
768
+ struct tm lt = {0 };
769
+ localtime_r (& t , & lt );
770
+
771
+ name = CFStringCreateWithCString (kCFAllocatorSystemDefault , lt .tm_zone , kCFStringEncodingUTF8 );
772
+ }
761
773
#endif
762
-
763
- result = CFTimeZoneCreateWithName (kCFAllocatorSystemDefault , name , false);
764
- CFRelease (name );
765
- if (result ) return result ;
774
+ if ( name ) {
775
+ result = CFTimeZoneCreateWithName (kCFAllocatorSystemDefault , name , false);
776
+ CFRelease (name );
777
+ if (result ) return result ;
766
778
}
767
779
return CFTimeZoneCreateWithTimeIntervalFromGMT (kCFAllocatorSystemDefault , 0.0 );
768
780
}
0 commit comments