1
/* Evolution calendar system timezone functions
2
* Based on gnome-panel's clock-applet system-timezone.c file.
4
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of version 2 of the GNU Lesser General Public
8
* License as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <glib/gstdio.h>
24
#include "e-cal-system-timezone.h"
29
#define SYSTEM_ZONEINFODIR "/usr/share/lib/zoneinfo/tab"
31
#define SYSTEM_ZONEINFODIR "/usr/share/zoneinfo"
34
#define ETC_TIMEZONE "/etc/timezone"
35
#define ETC_TIMEZONE_MAJ "/etc/TIMEZONE"
36
#define ETC_RC_CONF "/etc/rc.conf"
37
#define ETC_SYSCONFIG_CLOCK "/etc/sysconfig/clock"
38
#define ETC_CONF_D_CLOCK "/etc/conf.d/clock"
39
#define ETC_LOCALTIME "/etc/localtime"
41
#define TZ_MAGIC "TZif"
44
system_timezone_strip_path_if_valid (const gchar *filename)
48
if (!filename || !g_str_has_prefix (filename, SYSTEM_ZONEINFODIR"/"))
51
/* Timezone data files also live under posix/ and right/ for some
53
* FIXME: make sure accepting those files is valid. I think "posix" is
54
* okay, not sure about "right" */
55
if (g_str_has_prefix (filename, SYSTEM_ZONEINFODIR"/posix/"))
56
skip = strlen (SYSTEM_ZONEINFODIR"/posix/");
57
else if (g_str_has_prefix (filename, SYSTEM_ZONEINFODIR"/right/"))
58
skip = strlen (SYSTEM_ZONEINFODIR"/right/");
60
skip = strlen (SYSTEM_ZONEINFODIR"/");
62
return g_strdup (filename + skip);
65
/* Read the soft symlink from /etc/localtime */
67
system_timezone_read_etc_localtime_softlink (void)
72
if (!g_file_test (ETC_LOCALTIME, G_FILE_TEST_IS_SYMLINK))
75
file = g_file_read_link (ETC_LOCALTIME, NULL);
76
tz = system_timezone_strip_path_if_valid (file);
83
system_timezone_read_etc_timezone (void)
89
etc_timezone = g_fopen (ETC_TIMEZONE, "r");
93
reading = g_string_new ("");
95
c = fgetc (etc_timezone);
96
/* only get the first line, we'll validate the value later */
97
while (c != EOF && !g_ascii_isspace (c)) {
98
reading = g_string_append_c (reading, c);
99
c = fgetc (etc_timezone);
102
fclose (etc_timezone);
104
if (reading->str && reading->str[0] != '\0')
105
return g_string_free (reading, FALSE);
107
g_string_free (reading, TRUE);
112
/* Read a file that looks like a key-file (but there's no need for groups)
113
* and get the last value for a specific key */
115
system_timezone_read_key_file (const gchar *filename,
123
if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR))
126
channel = g_io_channel_new_file (filename, "r", NULL);
130
key_eq = g_strdup_printf ("%s=", key);
133
while (g_io_channel_read_line (channel, &line, NULL,
134
NULL, NULL) == G_IO_STATUS_NORMAL) {
135
if (g_str_has_prefix (line, key_eq)) {
139
value = line + strlen (key_eq);
142
len = strlen (value);
144
if (value[0] == '\"') {
145
if (value[len - 1] == '\"') {
149
retval = g_strndup (value + 1,
156
retval = g_strdup (line + strlen (key_eq));
166
g_io_channel_unref (channel);
171
/* This works for Fedora and Mandriva */
173
system_timezone_read_etc_sysconfig_clock (void)
175
return system_timezone_read_key_file (ETC_SYSCONFIG_CLOCK,
179
/* This works for openSUSE */
181
system_timezone_read_etc_sysconfig_clock_alt (void)
183
return system_timezone_read_key_file (ETC_SYSCONFIG_CLOCK,
187
/* This works for Solaris/OpenSolaris */
189
system_timezone_read_etc_TIMEZONE (void)
191
return system_timezone_read_key_file (ETC_TIMEZONE_MAJ,
195
/* This works for Arch Linux */
197
system_timezone_read_etc_rc_conf (void)
199
return system_timezone_read_key_file (ETC_RC_CONF,
203
/* This works for old Gentoo */
205
system_timezone_read_etc_conf_d_clock (void)
207
return system_timezone_read_key_file (ETC_CONF_D_CLOCK,
211
typedef gboolean (*CompareFiles) (struct stat *a_stat,
213
const gchar *a_content,
215
const gchar *b_filename);
218
recursive_compare (struct stat *localtime_stat,
219
const gchar *localtime_content,
220
gsize localtime_content_len,
222
CompareFiles compare_func)
224
struct stat file_stat;
226
if (g_stat (file, &file_stat) != 0)
229
if (S_ISREG (file_stat.st_mode)) {
230
if (compare_func (localtime_stat,
233
localtime_content_len,
235
return system_timezone_strip_path_if_valid (file);
238
} else if (S_ISDIR (file_stat.st_mode)) {
241
const gchar *subfile = NULL;
242
gchar *subpath = NULL;
244
dir = g_dir_open (file, 0, NULL);
248
while ((subfile = g_dir_read_name (dir)) != NULL) {
249
subpath = g_build_filename (file, subfile, NULL);
251
ret = recursive_compare (localtime_stat,
253
localtime_content_len,
272
files_are_identical_inode (struct stat *a_stat,
274
const gchar *a_content,
276
const gchar *b_filename)
278
gboolean res = a_stat->st_ino == b_stat->st_ino;
281
const gchar *filename;
283
filename = strrchr (b_filename, '/');
287
filename = b_filename;
289
/* There is a 'localtime' soft link to /etc/localtime in the zoneinfo
290
directory on Slackware, thus rather skip this file. */
291
res = !g_str_equal (filename, "localtime");
297
/* Determine if /etc/localtime is a hard link to some file, by looking at
300
system_timezone_read_etc_localtime_hardlink (void)
302
struct stat stat_localtime;
304
if (g_stat (ETC_LOCALTIME, &stat_localtime) != 0)
307
if (!S_ISREG (stat_localtime.st_mode))
310
return recursive_compare (&stat_localtime,
314
files_are_identical_inode);
318
files_are_identical_content (struct stat *a_stat,
320
const gchar *a_content,
322
const gchar *b_filename)
324
gchar *b_content = NULL;
325
gsize b_content_len = -1;
328
if (a_stat->st_size != b_stat->st_size)
331
if (!g_file_get_contents (b_filename,
332
&b_content, &b_content_len, NULL))
335
if (a_content_len != b_content_len) {
340
cmp = memcmp (a_content, b_content, a_content_len);
346
/* Determine if /etc/localtime is a copy of a timezone file */
348
system_timezone_read_etc_localtime_content (void)
350
struct stat stat_localtime;
351
gchar *localtime_content = NULL;
352
gsize localtime_content_len = -1;
355
if (g_stat (ETC_LOCALTIME, &stat_localtime) != 0)
358
if (!S_ISREG (stat_localtime.st_mode))
361
if (!g_file_get_contents (ETC_LOCALTIME,
363
&localtime_content_len,
367
retval = recursive_compare (&stat_localtime,
369
localtime_content_len,
371
files_are_identical_content);
373
g_free (localtime_content);
378
typedef gchar * (*GetSystemTimezone) (void);
379
/* The order of the functions here define the priority of the methods used
380
* to find the timezone. First method has higher priority. */
381
static GetSystemTimezone get_system_timezone_methods[] = {
382
/* cheap and "more correct" than data from a config file */
383
system_timezone_read_etc_localtime_softlink,
384
/* reading various config files */
385
system_timezone_read_etc_timezone,
386
system_timezone_read_etc_sysconfig_clock,
387
system_timezone_read_etc_sysconfig_clock_alt,
388
system_timezone_read_etc_TIMEZONE,
389
system_timezone_read_etc_rc_conf,
390
/* reading deprecated config files */
391
system_timezone_read_etc_conf_d_clock,
392
/* reading /etc/timezone directly. Expensive since we have to stat
394
system_timezone_read_etc_localtime_hardlink,
395
system_timezone_read_etc_localtime_content,
400
system_timezone_is_valid (const gchar *tz)
407
for (c = tz; *c != '\0'; c++) {
408
if (!(g_ascii_isalnum (*c) ||
409
*c == '/' || *c == '-' || *c == '_'))
417
system_timezone_find (void)
422
for (i = 0; get_system_timezone_methods[i] != NULL; i++) {
423
tz = get_system_timezone_methods[i] ();
425
if (system_timezone_is_valid (tz))
434
#else /* G_OS_WIN32 */
437
struct timezone_map_entry
439
const gchar *windows_string;
440
const gchar *olson_string;
444
windows_timezone_string_to_olson(const gchar * windows_tz)
446
/* source: http://www.chronos-st.org/Windows-to-Olson.txt */
447
static const struct timezone_map_entry timezone_map[] = {
448
{ "Afghanistan", "Asia/Kabul" },
449
{ "Afghanistan Standard Time", "Asia/Kabul" },
450
{ "Alaskan", "America/Anchorage" },
451
{ "Alaskan Standard Time", "America/Anchorage" },
452
{ "Arab", "Asia/Riyadh" },
453
{ "Arab Standard Time", "Asia/Riyadh" },
454
{ "Arabian", "Asia/Muscat" },
455
{ "Arabian Standard Time", "Asia/Muscat" },
456
{ "Arabic Standard Time", "Asia/Baghdad" },
457
{ "Atlantic", "America/Halifax" },
458
{ "Atlantic Standard Time", "America/Halifax" },
459
{ "AUS Central", "Australia/Darwin" },
460
{ "AUS Central Standard Time", "Australia/Darwin" },
461
{ "AUS Eastern", "Australia/Sydney" },
462
{ "AUS Eastern Standard Time", "Australia/Sydney" },
463
{ "Azerbaijan Standard Time", "Asia/Baku" },
464
{ "Azores", "Atlantic/Azores" },
465
{ "Azores Standard Time", "Atlantic/Azores" },
466
{ "Bangkok", "Asia/Bangkok" },
467
{ "Bangkok Standard Time", "Asia/Bangkok" },
468
{ "Beijing", "Asia/Shanghai" },
469
{ "Canada Central", "America/Regina" },
470
{ "Canada Central Standard Time", "America/Regina" },
471
{ "Cape Verde Standard Time", "Atlantic/Cape_Verde" },
472
{ "Caucasus", "Asia/Yerevan" },
473
{ "Caucasus Standard Time", "Asia/Yerevan" },
474
{ "Cen. Australia", "Australia/Adelaide" },
475
{ "Cen. Australia Standard Time", "Australia/Adelaide" },
476
{ "Central", "America/Chicago" },
477
{ "Central America Standard Time", "America/Regina" },
478
{ "Central Asia", "Asia/Dhaka" },
479
{ "Central Asia Standard Time", "Asia/Dhaka" },
480
{ "Central Brazilian Standard Time", "America/Manaus" },
481
{ "Central Europe", "Europe/Prague" },
482
{ "Central Europe Standard Time", "Europe/Prague" },
483
{ "Central European", "Europe/Belgrade" },
484
{ "Central European Standard Time", "Europe/Belgrade" },
485
{ "Central Pacific", "Pacific/Guadalcanal" },
486
{ "Central Pacific Standard Time", "Pacific/Guadalcanal" },
487
{ "Central Standard Time", "America/Chicago" },
488
{ "Central Standard Time (Mexico)", "America/Mexico_City" },
489
{ "China", "Asia/Shanghai" },
490
{ "China Standard Time", "Asia/Shanghai" },
491
{ "Dateline", "GMT-1200" },
492
{ "Dateline Standard Time", "GMT-1200" },
493
{ "E. Africa", "Africa/Nairobi" },
494
{ "E. Africa Standard Time", "Africa/Nairobi" },
495
{ "E. Australia", "Australia/Brisbane" },
496
{ "E. Australia Standard Time", "Australia/Brisbane" },
497
{ "E. Europe", "Europe/Minsk" },
498
{ "E. Europe Standard Time", "Europe/Minsk" },
499
{ "E. South America", "America/Sao_Paulo" },
500
{ "E. South America Standard Time", "America/Sao_Paulo" },
501
{ "Eastern", "America/New_York" },
502
{ "Eastern Standard Time", "America/New_York" },
503
{ "Egypt", "Africa/Cairo" },
504
{ "Egypt Standard Time", "Africa/Cairo" },
505
{ "Ekaterinburg", "Asia/Yekaterinburg" },
506
{ "Ekaterinburg Standard Time", "Asia/Yekaterinburg" },
507
{ "Fiji", "Pacific/Fiji" },
508
{ "Fiji Standard Time", "Pacific/Fiji" },
509
{ "FLE", "Europe/Helsinki" },
510
{ "FLE Standard Time", "Europe/Helsinki" },
511
{ "Georgian Standard Time", "Asia/Tbilisi" },
512
{ "GFT", "Europe/Athens" },
513
{ "GFT Standard Time", "Europe/Athens" },
514
{ "GMT", "Europe/London" },
515
{ "GMT Standard Time", "Europe/London" },
516
{ "GMT Standard Time", "GMT" },
517
{ "Greenland Standard Time", "America/Godthab" },
518
{ "Greenwich", "GMT" },
519
{ "Greenwich Standard Time", "GMT" },
520
{ "GTB", "Europe/Athens" },
521
{ "GTB Standard Time", "Europe/Athens" },
522
{ "Hawaiian", "Pacific/Honolulu" },
523
{ "Hawaiian Standard Time", "Pacific/Honolulu" },
524
{ "India", "Asia/Calcutta" },
525
{ "India Standard Time", "Asia/Calcutta" },
526
{ "Iran", "Asia/Tehran" },
527
{ "Iran Standard Time", "Asia/Tehran" },
528
{ "Israel", "Asia/Jerusalem" },
529
{ "Israel Standard Time", "Asia/Jerusalem" },
530
{ "Jordan Standard Time", "Asia/Amman" },
531
{ "Korea", "Asia/Seoul" },
532
{ "Korea Standard Time", "Asia/Seoul" },
533
{ "Mexico", "America/Mexico_City" },
534
{ "Mexico Standard Time", "America/Mexico_City" },
535
{ "Mexico Standard Time 2", "America/Chihuahua" },
536
{ "Mid-Atlantic", "Atlantic/South_Georgia" },
537
{ "Mid-Atlantic Standard Time", "Atlantic/South_Georgia" },
538
{ "Middle East Standard Time", "Asia/Beirut" },
539
{ "Mountain", "America/Denver" },
540
{ "Mountain Standard Time", "America/Denver" },
541
{ "Mountain Standard Time (Mexico)", "America/Chihuahua" },
542
{ "Myanmar Standard Time", "Asia/Rangoon" },
543
{ "N. Central Asia Standard Time", "Asia/Novosibirsk" },
544
{ "Namibia Standard Time", "Africa/Windhoek" },
545
{ "Nepal Standard Time", "Asia/Katmandu" },
546
{ "New Zealand", "Pacific/Auckland" },
547
{ "New Zealand Standard Time", "Pacific/Auckland" },
548
{ "Newfoundland", "America/St_Johns" },
549
{ "Newfoundland Standard Time", "America/St_Johns" },
550
{ "North Asia East Standard Time", "Asia/Ulaanbaatar" },
551
{ "North Asia Standard Time", "Asia/Krasnoyarsk" },
552
{ "Pacific", "America/Los_Angeles" },
553
{ "Pacific SA", "America/Santiago" },
554
{ "Pacific SA Standard Time", "America/Santiago" },
555
{ "Pacific Standard Time", "America/Los_Angeles" },
556
{ "Pacific Standard Time (Mexico)", "America/Tijuana" },
557
{ "Prague Bratislava", "Europe/Prague" },
558
{ "Romance", "Europe/Paris" },
559
{ "Romance Standard Time", "Europe/Paris" },
560
{ "Russian", "Europe/Moscow" },
561
{ "Russian Standard Time", "Europe/Moscow" },
562
{ "SA Eastern", "America/Buenos_Aires" },
563
{ "SA Eastern Standard Time", "America/Buenos_Aires" },
564
{ "SA Pacific", "America/Bogota" },
565
{ "SA Pacific Standard Time", "America/Bogota" },
566
{ "SA Western", "America/Caracas" },
567
{ "SA Western Standard Time", "America/Caracas" },
568
{ "Samoa", "Pacific/Apia" },
569
{ "Samoa Standard Time", "Pacific/Apia" },
570
{ "Saudi Arabia", "Asia/Riyadh" },
571
{ "Saudi Arabia Standard Time", "Asia/Riyadh" },
572
{ "SE Asia", "Asia/Bangkok" },
573
{ "SE Asia Standard Time", "Asia/Bangkok" },
574
{ "Singapore", "Asia/Singapore" },
575
{ "Singapore Standard Time", "Asia/Singapore" },
576
{ "South Africa", "Africa/Harare" },
577
{ "South Africa Standard Time", "Africa/Harare" },
578
{ "Sri Lanka", "Asia/Colombo" },
579
{ "Sri Lanka Standard Time", "Asia/Colombo" },
580
{ "Sydney Standard Time", "Australia/Sydney" },
581
{ "Taipei", "Asia/Taipei" },
582
{ "Taipei Standard Time", "Asia/Taipei" },
583
{ "Tasmania", "Australia/Hobart" },
584
{ "Tasmania Standard Time", "Australia/Hobart" },
585
{ "Tasmania Standard Time", "Australia/Hobart" },
586
{ "Tokyo", "Asia/Tokyo" },
587
{ "Tokyo Standard Time", "Asia/Tokyo" },
588
{ "Tonga Standard Time", "Pacific/Tongatapu" },
589
{ "US Eastern", "America/Indianapolis" },
590
{ "US Eastern Standard Time", "America/Indianapolis" },
591
{ "US Mountain", "America/Phoenix" },
592
{ "US Mountain Standard Time", "America/Phoenix" },
593
{ "Vladivostok", "Asia/Vladivostok" },
594
{ "Vladivostok Standard Time", "Asia/Vladivostok" },
595
{ "W. Australia", "Australia/Perth" },
596
{ "W. Australia Standard Time", "Australia/Perth" },
597
{ "W. Central Africa Standard Time", "Africa/Luanda" },
598
{ "W. Europe", "Europe/Berlin" },
599
{ "W. Europe Standard Time", "Europe/Berlin" },
600
{ "Warsaw", "Europe/Warsaw" },
601
{ "West Asia", "Asia/Karachi" },
602
{ "West Asia Standard Time", "Asia/Karachi" },
603
{ "West Pacific", "Pacific/Guam" },
604
{ "West Pacific Standard Time", "Pacific/Guam" },
605
{ "Western Brazilian Standard Time", "America/Rio_Branco" },
606
{ "Yakutsk", "Asia/Yakutsk" },
607
{ "Yakutsk Standard Time", "Asia/Yakutsk" },
608
{ 0, 0 } // end marker
613
for (i=0; timezone_map[i].windows_string && windows_tz; i++) {
614
gint res = strcmp( timezone_map[i].windows_string, windows_tz);
618
return g_strdup(timezone_map[i].olson_string);
625
#define MAX_VALUE_NAME 4096
628
system_timezone_win32_query_registry (void)
635
static HKEY reg_key = (HKEY) INVALID_HANDLE_VALUE;
636
static HKEY reg_subkey = (HKEY) INVALID_HANDLE_VALUE;
637
gchar timeZone[MAX_VALUE_NAME] = "";
638
gchar timeZoneStd[MAX_VALUE_NAME] = "";
639
gchar subKey[MAX_VALUE_NAME] = "";
641
res = RegOpenKeyExA (HKEY_LOCAL_MACHINE,
642
"SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation", 0, KEY_READ, ®_key);
643
if (res != ERROR_SUCCESS) {
644
g_debug("Could not find system timezone! (1)\n");
648
/* On Windows Vista, Windows Server 2008 and later, the Windows timezone name is the value of 'TimeZoneKeyName' */
650
size = MAX_VALUE_NAME;
651
res = RegQueryValueExA (reg_key, "TimeZoneKeyName", 0, &type, (LPBYTE) timeZone, &size);
653
if (type == REG_SZ && res == ERROR_SUCCESS) {
654
RegCloseKey (reg_key);
655
g_debug ("Windows Timezone String (1): %s\n", timeZone);
656
return g_strdup (timeZone);
659
/* On older Windows, we must first find the value of 'StandardName' */
661
res = RegQueryValueExA (reg_key, "StandardName", 0, &type, (LPBYTE) timeZone, &size);
663
if (type != REG_SZ || res != ERROR_SUCCESS) {
664
RegCloseKey (reg_key);
665
g_debug ("Could not find system timezone! (2)\n");
669
RegCloseKey (reg_key);
671
/* Windows NT and its family */
672
res = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
673
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
674
0, KEY_READ, ®_key);
675
if (res != ERROR_SUCCESS) {
676
g_debug ("Could not find the timezone! (3)\n");
680
for (i=0, res = ERROR_SUCCESS; res != ERROR_NO_MORE_ITEMS; i++) {
681
size = MAX_VALUE_NAME;
682
res = RegEnumKeyEx (reg_key, i, subKey, &size, NULL, NULL, NULL, NULL);
683
if (res == ERROR_SUCCESS) {
684
res = RegOpenKeyExA (reg_key, subKey, 0, KEY_READ, ®_subkey);
685
if (res != ERROR_SUCCESS)
687
size = MAX_VALUE_NAME;
688
res = RegQueryValueExA (reg_subkey, "Std", 0, &type,
689
(LPBYTE) timeZoneStd, &size);
690
RegCloseKey (reg_subkey);
691
if (type != REG_SZ || res != ERROR_SUCCESS) {
694
if (g_strcmp0 (timeZone,timeZoneStd) == 0) {
695
RegCloseKey (reg_key);
696
g_debug ("Windows Timezone String (2): %s\n", subKey);
697
return g_strdup (subKey);
702
g_debug ("Could not find system timezone! (3)\n");
703
RegCloseKey (reg_key);
707
#endif /* G_OS_WIN32 */
710
* e_cal_system_timezone_get_location:
712
* Returns system timezone location string, NULL on an error.
713
* Returned pointer should be freed with g_free().
718
e_cal_system_timezone_get_location (void)
721
return system_timezone_find ();
723
gchar *windows_timezone_string = NULL;
724
gchar *olson_timezone_string = NULL;
726
if (!(windows_timezone_string = system_timezone_win32_query_registry ()))
728
olson_timezone_string = windows_timezone_string_to_olson (windows_timezone_string);
729
g_free (windows_timezone_string);
730
if (!olson_timezone_string)
732
g_debug("Olson Timezone String: %s\n", olson_timezone_string);
733
return olson_timezone_string;