Commit | Line | Data |
---|---|---|
69fd9a5c FF |
1 | Patch submitted at https://bugzilla.gnome.org/show_bug.cgi?id=771304 |
2 | ||
dcaafa88 FF |
3 | diff -u -r ../glib-2.54.2/glib/gtimezone.c ./glib/gtimezone.c |
4 | --- ../glib-2.54.2/glib/gtimezone.c 2017-07-14 01:03:39.000000000 +0200 | |
5 | +++ ./glib/gtimezone.c 2017-12-09 02:03:46.797229494 +0100 | |
c780e809 | 6 | @@ -43,6 +43,13 @@ |
8aef3a59 FF |
7 | #include <windows.h> |
8 | #endif | |
9 | ||
10 | +#ifdef __ANDROID__ | |
c780e809 | 11 | +#include <arpa/inet.h> |
8aef3a59 FF |
12 | +#include <fcntl.h> |
13 | +#include <sys/system_properties.h> | |
c780e809 | 14 | +#include <unistd.h> |
8aef3a59 FF |
15 | +#endif |
16 | + | |
17 | /** | |
18 | * SECTION:timezone | |
19 | * @title: GTimeZone | |
022cbc0f | 20 | @@ -392,7 +399,106 @@ |
dcaafa88 FF |
21 | gtz->transitions = NULL; |
22 | } | |
23 | ||
24 | -#ifdef G_OS_UNIX | |
8aef3a59 | 25 | +#ifdef __ANDROID__ |
dcaafa88 FF |
26 | +/* Android does not have /etc/localtime but uses a system property for the |
27 | + the current timezone. There are no files under /usr/share/zoneinfo, | |
28 | + instead a single /system/usr/share/zoneinfo/tzdata which are all zoneinfo | |
29 | + files compiled together with the following tool: | |
30 | + https://android.googlesource.com/platform/external/icu/+/master/tools/ZoneCompactor.java */ | |
31 | +static GBytes* | |
32 | +zone_info_android (const gchar *identifier) | |
33 | +{ | |
34 | + struct tzdata_header | |
35 | + { | |
36 | + char version[12]; | |
37 | + uint32_t index_offset; | |
38 | + uint32_t data_offset; | |
39 | + uint32_t zonetab_offset; | |
8aef3a59 FF |
40 | + } __attribute__((packed)) header; |
41 | + | |
dcaafa88 FF |
42 | + struct tzdata_index_entry |
43 | + { | |
44 | + char name[40]; | |
45 | + uint32_t offset; | |
46 | + uint32_t length; | |
47 | + uint32_t unused; | |
8aef3a59 FF |
48 | + } __attribute__((packed)) entry; |
49 | + | |
69fd9a5c | 50 | + char sys_timezone[PROP_VALUE_MAX]; |
022cbc0f | 51 | + int tzdata_fd; |
dcaafa88 FF |
52 | + |
53 | + if (identifier == NULL) | |
54 | + { | |
022cbc0f | 55 | + if (__system_property_get ("persist.sys.timezone", sys_timezone) < 1) |
dcaafa88 | 56 | + { |
022cbc0f | 57 | + g_warning ("__system_property_get(\"persist.sys.timezone\") failed"); |
dcaafa88 FF |
58 | + return NULL; |
59 | + } | |
60 | + identifier = sys_timezone; | |
8aef3a59 | 61 | + } |
dcaafa88 | 62 | + |
022cbc0f | 63 | + tzdata_fd = open ("/system/usr/share/zoneinfo/tzdata", O_RDONLY); |
dcaafa88 FF |
64 | + if (tzdata_fd < 0) |
65 | + { | |
022cbc0f | 66 | + g_warning ("Failed opening tzdata"); |
dcaafa88 | 67 | + return NULL; |
8aef3a59 | 68 | + } |
8aef3a59 | 69 | + |
022cbc0f | 70 | + if (read (tzdata_fd, &header, sizeof(header)) < (ssize_t) sizeof (header)) |
dcaafa88 | 71 | + { |
022cbc0f | 72 | + g_warning ("Failed reading tzdata header"); |
dcaafa88 FF |
73 | + goto error; |
74 | + } | |
75 | + | |
022cbc0f FF |
76 | + header.index_offset = htonl (header.index_offset); |
77 | + header.data_offset = htonl (header.data_offset); | |
dcaafa88 FF |
78 | + |
79 | + uint32_t current_offset = header.index_offset; | |
80 | + while (current_offset < header.data_offset) | |
81 | + { | |
022cbc0f | 82 | + if (read (tzdata_fd, &entry, sizeof(entry)) < (ssize_t) sizeof (entry)) |
dcaafa88 | 83 | + { |
022cbc0f | 84 | + g_warning ("Failed reading tzdata index entry"); |
8aef3a59 FF |
85 | + goto error; |
86 | + } | |
dcaafa88 | 87 | + |
022cbc0f | 88 | + if (strcmp (entry.name, identifier) == 0) |
dcaafa88 | 89 | + { |
022cbc0f FF |
90 | + entry.offset = htonl (entry.offset); |
91 | + entry.length = htonl (entry.length); | |
dcaafa88 FF |
92 | + if (entry.length == 0) |
93 | + { | |
022cbc0f | 94 | + g_warning ("Invalid tzdata entry with length zero"); |
dcaafa88 FF |
95 | + goto error; |
96 | + } | |
97 | + | |
022cbc0f | 98 | + if (lseek (tzdata_fd, header.data_offset + entry.offset, SEEK_SET) == -1) |
dcaafa88 | 99 | + { |
022cbc0f | 100 | + g_warning ("Failed seeking to tzdata entry"); |
dcaafa88 FF |
101 | + goto error; |
102 | + } | |
103 | + | |
022cbc0f FF |
104 | + guint8* data = g_malloc (entry.length); |
105 | + if (read (tzdata_fd, data, entry.length) < entry.length) | |
dcaafa88 | 106 | + { |
022cbc0f FF |
107 | + g_warning ("Failed reading tzdata entry"); |
108 | + g_free (data); | |
dcaafa88 FF |
109 | + goto error; |
110 | + } | |
111 | + | |
022cbc0f FF |
112 | + close (tzdata_fd); |
113 | + return g_bytes_new_take (data, entry.length); | |
8aef3a59 | 114 | + } |
8aef3a59 | 115 | + } |
dcaafa88 | 116 | + |
8aef3a59 | 117 | +error: |
022cbc0f | 118 | + close (tzdata_fd); |
dcaafa88 | 119 | + |
8aef3a59 | 120 | + return NULL; |
dcaafa88 FF |
121 | +} |
122 | + | |
123 | +#elif defined(G_OS_UNIX) | |
124 | + | |
125 | static GBytes* | |
126 | zone_info_unix (const gchar *identifier) | |
127 | { | |
128 | @@ -436,6 +541,10 @@ | |
8aef3a59 | 129 | return zoneinfo; |
8aef3a59 FF |
130 | } |
131 | ||
dcaafa88 FF |
132 | +#endif |
133 | + | |
134 | +#ifdef G_OS_UNIX | |
135 | + | |
8aef3a59 | 136 | static void |
dcaafa88 FF |
137 | init_zone_from_iana_info (GTimeZone *gtz, GBytes *zoneinfo) |
138 | { | |
139 | @@ -1387,7 +1496,11 @@ | |
140 | if (tz->t_info == NULL) | |
141 | { | |
142 | #ifdef G_OS_UNIX | |
143 | +# ifdef __ANDROID__ | |
144 | + GBytes *zoneinfo = zone_info_android (identifier); | |
145 | +# else | |
146 | GBytes *zoneinfo = zone_info_unix (identifier); | |
147 | +# endif | |
148 | if (!zoneinfo) | |
149 | zone_for_constant_offset (tz, "UTC"); | |
150 | else |